名称

ST_Intersection — 计算表示几何 A 和 B 的共享部分的几何。

大纲

geometry ST_Intersection( geometry geomA , geometry geomB , float8 gridSize = -1 );

geography ST_Intersection( geography geogA , geography geogB );

描述

返回表示两个几何图形的点集交集的几何图形。 换句话说,几何图形 A 和几何图形 B 在两个几何图形之间共享的部分。

如果几何图形没有共同点(即不相交),则返回适当类型的空原子几何图形。

If the optional gridSize parameter is given (GEOS-3.9.0 or higher required), all result vertices are guaranteed to fall on a snap-rounded grid of the specified size. Note that operations performed on a grid may contain small artifacts produced during grid alignment, see ST_ReducePrecision.

ST_Intersection 与 ST_Intersects 结合使用对于剪切几何图形非常有用,例如在边界框、缓冲区或区域查询中,您只需要感兴趣的国家或地区内的几何图形部分。

[注意]

Spatial predicates such as ST_Intersects and overlay functions such as ST_Intersection are computed by different algorithms. For very close or nearly coincident coordinates, floating-point precision and geometric robustness may make a predicate return true while the computed intersection is empty, or place an intersection point slightly away from the input line. For repeatable overlay results, snap inputs to a suitable grid first, for example with ST_ReducePrecision, or use the gridSize parameter when all input vertices already lie on the grid.

[注意]

对于地理数据,这是对几何数据实现的一个轻量级封装。 它首先确定适合 2 个地理对象边界框的最佳 SRID(如果地理对象位于 UTM 的半个区域内,但不同的 UTM 将选择其中之一)(倾向于 UTM 或兰伯特方位角等积 (LAEA) 北/南 极点,并在最坏的情况下回到墨卡托),然后在最适合的平面空间参考中相交并重新转换回 WGS84 地理。

[警告]

该函数将删除 M 坐标值(如果存在)。

[警告]

If working with 3D geometries, you may want to use SFCGAL based CG_3DIntersection which does a proper 3D intersection for 3D geometries. Although this function works with Z-coordinate, it does an averaging of Z-Coordinate.

它是通过GEOS模块实现的

增强:3.1.0 接受 gridSize 参数

需要 GEOS >= 3.9.0 才能使用 gridSize 参数

更改:3.0.0 不依赖于 SFCGAL。

可用性:1.5 引入了对地理数据类型的支持。

此方法实现了 SQL 1.1 的 OGC 简单功能规范。 s2.1.1.3

该方法实现了SQL/MM规范。 SQL-MM 3: 5.1.18

该函数支持 3d 并且不会丢失 z-index。 但是,结果仅使用 XY 计算。 结果 Z 值被复制、平均或插值。

示例

Code
SELECT ST_Intersection('POINT(0 0)'::geometry, 'LINESTRING ( 2 0,0 2 )'::geometry);
栅格输出
POINT EMPTY
Code
SELECT ST_Intersection('POINT(0 0)'::geometry, 'LINESTRING ( 0 0,0 2 )'::geometry);
栅格输出
POINT(0 0)
Figure
Geometry figure for visual-st-intersection-02

按国家/地区剪辑所有线路(路径)。 这里我们假设国家几何是多边形或多多边形。 注意:我们只保留导致线串或多线串的交叉点部分,因为我们不关心仅共享一个点的路径。 需要转储来将几何集合扩展为单独的 MULT* 部分。 下面的内容相当通用,只需更改 where 子句即可适用于多边形等。

Code
select clipped.gid, clipped.f_name, clipped_geom
from (
         select trails.gid, trails.f_name,
             (ST_Dump(ST_Intersection(country.geom, trails.geom))).geom clipped_geom
         from country
              inner join trails on ST_Intersects(country.geom, trails.geom)
     ) as clipped
where ST_Dimension(clipped.clipped_geom) = 1;

对于聚合物,例如 多边形地标,您可以使用加速技巧,在 0.0 处缓冲不包括多边形的几何,以获得空的几何集合(因此,如果缓冲包含多边形、线串和点的几何集合为 0.0,则仅保留多边形, 它将不再是几何集合。)

Code
select poly.gid,
    ST_Multi(ST_Buffer(ST_Intersection(country.geom, poly.geom),
            0.0
        )
    ) clipped_geom
from country
     inner join poly on ST_Intersects(country.geom, poly.geom)
where not ST_IsEmpty(ST_Buffer(ST_Intersection(country.geom, poly.geom), 0.0));

Note this is not a true 3D intersection. It uses 2.5D geometries (geometries with Z ordinates, but where calculations are performed in 2D). The same input is compared with CG_3DIntersection, which performs a true 3D intersection.

Code
WITH data AS (
  SELECT 'LINESTRING Z (2 2 6,1.5 1.5 7,1 1 8,0.5 0.5 8,0 0 10)'::geometry AS input_linestring,
         'POLYGON Z ((0 0 8,0 1 8,1 1 8,1 0 8,0 0 8))'::geometry AS input_polygon
)
SELECT input_linestring AS input_linestring,
       input_polygon AS input_polygon,
       ST_Intersection(input_linestring, input_polygon) AS intersection_2d,
       CG_3DIntersection(input_linestring, input_polygon) AS intersection_3d
FROM data;
栅格输出
LINESTRING Z (2 2 6,1.5 1.5 7,1 1 8,0.5 0.5 8,0 0 10) | POLYGON Z ((0 0 8,0 1 8,1 1 8,1 0 8,0 0 8)) | LINESTRING Z (1 1 8,0.5 0.5 8,0 0 10) | LINESTRING Z (1 1 8,0.5 0.5 8)
Figure
Geometry figure for visual-st-intersection-03