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 |
|
|
|
|
|
|
|
该函数将删除 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 值被复制、平均或插值。
SELECT ST_Intersection('POINT(0 0)'::geometry, 'LINESTRING ( 2 0,0 2 )'::geometry);
POINT EMPTY
SELECT ST_Intersection('POINT(0 0)'::geometry, 'LINESTRING ( 0 0,0 2 )'::geometry);
POINT(0 0)
按国家/地区剪辑所有线路(路径)。 这里我们假设国家几何是多边形或多多边形。 注意:我们只保留导致线串或多线串的交叉点部分,因为我们不关心仅共享一个点的路径。 需要转储来将几何集合扩展为单独的 MULT* 部分。 下面的内容相当通用,只需更改 where 子句即可适用于多边形等。
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,则仅保留多边形, 它将不再是几何集合。)
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.
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)