定义
ST_DWithin 将两个几何作为输入,如果几何彼此间距离在指定范围内则返回 true;否则将返回 false。几何的空间参考系统决定指定距离将应用的测量单位。因此,提供给 ST_DWithin 的几何必须使用同样的坐标投影和 SRID。
语法
sde.st_dwithin (st_geometry geometry1, st_geometry geometry2, double_precision distance);
返回类型
布尔型
示例
在下例中,创建了两张表,并向其中插入了要素。接下来,使用 ST_DWithin 函数来确定第一个表中的点与第二个表中面是否相距 100 米以内。
--Create table to store points.
CREATE TABLE dwithin_test_pt (id INT, geom sde.st_geometry);
--Create table to store polygons.
CREATE TABLE dwithin_test_poly (id INT, geom sde.st_geometry);
--Insert features into each table.
INSERT INTO dwithin_test_pt
VALUES
(
1,
sde.st_geometry('point (1 2)', 4326)
)
;
INSERT INTO dwithin_test_pt
VALUES
(
2,
sde.st_geometry('point (10.02 20.01)', 4326)
)
;
INSERT INTO dwithin_test_poly
VALUES
(
1,
sde.st_geometry('polygon ((10.02 20.01, 11.92 35.64, 25.02 4.15, 19.15 33.94, 10.02 20.01))', 4326)
)
;
INSERT INTO dwithin_test_poly
VALUES
(
2,
sde.st_geometry('polygon ((101.02 200.01, 111.92 350.64, 250.02 340.15, 190.15 330.94, 101.02 200.01))', 4326)
)
;
--Determine which features in the point table are within 100 meters of the features in the polygon table. SELECT pt.id, poly.id, st_distance(pt.geom, poly.geom) distance_meters, ST_DWithin(pt.geom, poly.geom, 100) DWithin
FROM dwithin_test_pt pt, dwithin_test_poly poly;
选择语句返回如下内容:
id id distance_meters dwithin 1 1 20.1425048094819 t 1 2 221.837689538996 f 2 1 0 t 2 2 201.69531476958 f
在下例中,ST_DWithin 用来确定彼此相距 300 米以内的各个要素:
--Determine which features in the point table are within 300 meters of the features in the polygon table.
SELECT pt.id, poly.id, st_distance(pt.geom, poly.geom) distance_meters, ST_DWithin(pt.geom, poly.geom, 300) DWithin
FROM dwithin_test_pt pt, dwithin_test_poly poly;
第二个选择语句返回如下内容:
id id distance_meters dwithin 1 1 20.1425048094819 t 1 2 221.837689538996 t 2 1 0 t 2 2 201.69531476958 t