St_disjoint(geom A,geom B)返回A是否不在B中
St_union(geom A,geom B)返回A+B两个几何的合并
ST_Collect(geom A,geom B)返回A+B两个几何的聚合
St_intersects(geom A,geom B)返回A是否和B有接触
St_intersection(geom A,geom B)返回A和B的交集
St_within(geom A,geom B)返回A是否处于B中
St_difference(geom A,geom B)返回A与B不相交的部分几何
A B 图作为测试图形,用来验证这些函数的使用
-- A 图
select ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[120.52757263183594,31.495432803134843],[120.59211730957031,31.31199502365151],[120.89012145996094,31.35950051982242],[120.74729919466666,31.483245492650792],[120.52757263183594,31.495432803134843]]]}')
-- B 图
select ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[120.74386596679688,31.487235582017444],[120.84548950195312,31.269747790889888],[121.01783752441405,31.378261512889125],[120.91827392578125,31.487821121636433],[120.74386596679688,31.487235582017444]]]}')
A图:
B图:
=========================== St_union和ST_Collect对比 start ==============================
St_union和ST_Collect对比:
ST_Collect和ST_Union看起来很相似,但实际上却有很大不同
-- ST_Collect将几何形状聚合到一个集合中,而无需进行任何更改。
select ST_Collect (
ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[120.52757263183594,31.495432803134843],[120.59211730957031,31.31199502365151],[120.89012145996094,31.35950051982242],[120.74729919466666,31.483245492650792],[120.52757263183594,31.495432803134843]]]}'),
ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[120.74386596679688,31.487235582017444],[120.84548950195312,31.269747790889888],[121.01783752441405,31.378261512889125],[120.91827392578125,31.487821121636433],[120.74386596679688,31.487235582017444]]]}')
)
-- ST_Union在几何形状重叠的地方对其进行几何合并,并在相交处拆分线串。溶解边界时,它可能会返回单个几何。
select ST_Union (
ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[120.52757263183594,31.495432803134843],[120.59211730957031,31.31199502365151],[120.89012145996094,31.35950051982242],[120.52757263183594,31.495432803134843]]]}'),
ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[120.74386596679688,31.487235582017444],[120.84548950195312,31.269747790889888],[121.01783752441405,31.378261512889125],[120.91827392578125,31.487821121636433],[120.74386596679688,31.487235582017444]]]}')
)
ST_Union合并后的结果:
=========================== St_union和ST_Collect对比 end ==============================
=================== St_intersects、St_intersection、St_within对比 start ========================
St_within 和 St_intersection 查询逻辑是一样的,但是:
St_within :如果几何体A完全在几何体B内,则返回TRUE,为了使此函数有意义,源几何体必须都具有相同的坐标投影,并且具有相同的SRID。
假设如果ST_Within(A,B) 为true并且ST_Within(B,A) 为true,则认为这两个几何在空间上相等。
St_intersection :如果几何或地理共享空间的任何部分,则它们相交.对于地理区域-公差为0.00001米(因此,任何靠近的点都视为相交)
ST_Overlaps,ST_Touches,ST_Within都暗示空间相交.如果上述任何一个返回true,则几何形状也在空间上相交.不相交意味着空间相交为假.
-- ST_Intersects是一个函数,它接受两个几何,如果两个几何之间的任何一部分在2个之间共享,则返回true
SELECT a.somfield, b.somefield2
FROM a INNER JOIN b ON ST_Intersects(a.the_geom, b.the_geom)
--
select st_intersects (
ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[120.52757263183594,31.495432803134843],[120.59211730957031,31.31199502365151],[120.89012145996094,31.35950051982242],[120.74729919466666,31.483245492650792],[120.52757263183594,31.495432803134843]]]}'),
ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[120.74386596679688,31.487235582017444],[120.84548950195312,31.269747790889888],[121.01783752441405,31.378261512889125],[120.91827392578125,31.487821121636433],[120.74386596679688,31.487235582017444]]]}')
)
-- st_intersects 我一般用这个空间函数去匹配空间内的门店点数, 比如:我希望得到地图上某个区域A内所有的水果店数据。
-- 在我们知道区域A的边界以及一张门店B表中的门店的经纬度时,sql如下:
select * from B b
inner join A a on
ST_intersects (
-- 数据库内 经度store_lng 和 纬度store_lat 是 varcher类型的,所以在使用的时候转换成 NUMERIC
ST_SetSRID ( ST_Point ( b.store_lng :: NUMERIC, b.store_lat :: NUMERIC ), 4326 ),
a.gd_geom
)
where a.id = #{区域A的边界id}
-- 函数ST_Intersection和Intersection是ST_Intersects的补充。它们返回的是两个几何之间共享的几何A和几何B部分。如果两个几何不相交,则返回一个空的GEOMETRYCOLLECTION对象。
SELECT St_intersection(
ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[120.52757263183594,31.495432803134843],[120.59211730957031,31.31199502365151],[120.89012145996094,31.35950051982242],[120.74729919466666,31.483245492650792],[120.52757263183594,31.495432803134843]]]}'),
ST_GeomFromGeoJSON('{"type":"Polygon","coordinates":[[[120.74386596679688,31.487235582017444],[120.84548950195312,31.269747790889888],[121.01783752441405,31.378261512889125],[120.91827392578125,31.487821121636433],[120.74386596679688,31.487235582017444]]]}')
)
结果:
=================== St_intersects、St_intersection、St_within对比 end========================
============================== St_difference 使用================================
St_difference :
geometry ST_Difference(geometry geomA, geometry geomB);
返回一个表示不与几何B相交的几何A的那部分的几何.可以将其视为GeometryA-ST_Intersection(A,B)
如果A完全包含在B中,则返回一个空的几何集合.
**注:顺序很重要。B-A将始终返回B的一部分**