ST_DWithin — 测试两个几何图形是否在给定距离内
boolean ST_DWithin(
geometry g1, geometry g2, double precision distance_of_srid)
;
boolean ST_DWithin(
geography gg1, geography gg2, double precision distance_meters, boolean use_spheroid = true)
;
如果几何图形在给定距离内,则返回 true
对于几何图形:距离以几何图形的空间参考系统定义的单位指定。 为了使此函数有意义,源几何图形必须位于同一坐标系中(具有相同的 SRID)。
对于 地理:单位为米,距离测量默认使用 use_spheroid = true
(使用椭球体)。为了更快的计算,可以使用 use_spheroid = false
,在球面上进行测量。
将 ST_3DDWithin用于 3D 几何图形。 |
此函数调用包括利用几何上可用的任何索引的边界框比较。 |
此方法实现了 SQL 1.1 的 OGC 简单功能规范。
可用性:1.5.0 引入了对地理的支持
增强:2.1.0 提高了地理速度。 有关详细信息,请参阅使地理更快。
增强:2.1.0 引入了对弯曲几何形状的支持。
在版本1.3之前,通常会将ST_Expand与 && 和ST_Distance一起使用来测试距离,而在1.3.4之前的版本中,该函数使用了这种逻辑。从1.3.4版本开始,ST_DWithin使用更快速的短路距离函数。
-- Find the nearest hospital to each school -- that is within 3000 units of the school. -- We do an ST_DWithin search to utilize indexes to limit our search list -- that the non-indexable ST_Distance needs to process -- If the units of the spatial reference is meters then units would be meters SELECT DISTINCT ON (s.gid) s.gid, s.school_name, s.geom, h.hospital_name FROM schools s LEFT JOIN hospitals h ON ST_DWithin(s.geom, h.geom, 3000) ORDER BY s.gid, ST_Distance(s.geom, h.geom); -- The schools with no close hospitals -- Find all schools with no hospital within 3000 units -- away from the school. Units is in units of spatial ref (e.g. meters, feet, degrees) SELECT s.gid, s.school_name FROM schools s LEFT JOIN hospitals h ON ST_DWithin(s.geom, h.geom, 3000) WHERE h.gid IS NULL; -- Find broadcasting towers that receiver with limited range can receive. -- Data is geometry in Spherical Mercator (SRID=3857), ranges are approximate. -- Create geometry index that will check proximity limit of user to tower CREATE INDEX ON broadcasting_towers using gist (geom); -- Create geometry index that will check proximity limit of tower to user CREATE INDEX ON broadcasting_towers using gist (ST_Expand(geom, sending_range)); -- Query towers that 4-kilometer receiver in Minsk Hackerspace can get -- Note: two conditions, because shorter LEAST(b.sending_range, 4000) will not use index. SELECT b.tower_id, b.geom FROM broadcasting_towers b WHERE ST_DWithin(b.geom, 'SRID=3857;POINT(3072163.4 7159374.1)', 4000) AND ST_DWithin(b.geom, 'SRID=3857;POINT(3072163.4 7159374.1)', b.sending_range);