Definition
ST_DWithin takes two geometries as input and returns true if the geometries are within the specified distance of one another; otherwise, false is returned. The spatial reference system of the geometries determines what unit of measure is applied to the specified distance. Therefore, the geometries you provide to ST_DWithin must both use the same coordinate projection and SRID.
Syntax
sde.st_dwithin (st_geometry geometry1, st_geometry geometry2, double_precision distance);
Return type
Boolean
Example
In the following example, two tables are created and features are inserted to them. Next, the ST_DWithin function is used to determine if a point in the first table is within 100 meters of a polygon in the second table.
--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;
The select statement returns the following:
id id distance_meters dwithin 1 1 20.1425048094819 t 1 2 221.837689538996 f 2 1 0 t 2 2 201.69531476958 f
In the following example, ST_DWithin is used to find which features are within 300 meters of one another:
--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;
This second select statement returns the following:
id id distance_meters dwithin 1 1 20.1425048094819 t 1 2 221.837689538996 t 2 1 0 t 2 2 201.69531476958 t