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 34.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, sde.st_distance(pt.geom, poly.geom) distance_meters, sde.st_dwithin(pt.geom, poly.geom, 100) DWithin
FROM dwithin_test_pt pt, dwithin_test_poly poly;
The select statement returns the following when run on data in PostgreSQL:
id id distance_meters dwithin 1 1 20.1425048094819 t 1 2 221.837689538996 f 2 1 0 t 2 2 201.69531476958 f
The statement returns the following when run on data in Oracle:
ID ID DISTANCE_METERS DWITHIN 1 1 20.1425048 1 1 2 221.83769 0 2 1 0 1 2 2 201.695315 0
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, sde.st_distance(pt.geom, poly.geom) distance_meters, sde.st_dwithin(pt.geom, poly.geom, 300) DWithin
FROM dwithin_test_pt pt, dwithin_test_poly poly;
This second select statement returns the following when run on data in PostgreSQL:
id id distance_meters dwithin 1 1 20.1425048094819 t 1 2 221.837689538996 t 2 1 0 t 2 2 201.69531476958 t
The second select statement returns the following when run on data in Oracle:
ID ID DISTANCE_METERS DWITHIN 1 1 20.1425048 1 1 2 221.83769 1 2 1 0 1 2 2 201.695315 1