Skip to content

ST_DWithin

ST_DWithin takes two geometry columns and a numeric distance value and returns a boolean column. The function returns True if the two geometries are spatially within the given distance; otherwise, it returns False. For multipoints, lines, and polygons, the distance is calculated from the nearest point between the geometries. The distance can be specified with or without a unit.

When specified with a unit, the distance can be defined using ST_CreateDistance or with a tuple containing a number and a unit string (e.g., (10, "kilometers")).

You can optionally provide a boolean value that determines if geodesic distances will be used by the function. Planar distances will be used by default. When geodesic distance is used, and distance is specified without a unit, the function assumes the distance value is in meters. When planar distance is used, and distance is specified without a unit, the function assumes the distance value is in the same units as the input geometry.

If the two geometry columns are in different spatial references, the function will automatically transform the second geometry into the spatial reference of the first.

When using geodesic distances, the first geometry column must have a spatial reference set. To learn more see Coordinate systems and transformations.

This function may result in an optimized spatial join.

FunctionSyntax
Pythondwithin(geometry1, geometry2, distance, geodesic=False)
SQLST_DWithin(geometry1, geometry2, distance, geodesic)
Scaladwithin(geometry1, geometry2, distance, geodesic)

For more details, go to the GeoAnalytics Engine API reference for dwithin.

Examples

PythonPythonSQLScala
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12

from geoanalytics.sql import functions as ST, Point

data = [
    (Point(-176, -15), Point(-176, -15)),
    (Point(-176, -15), Point(-176, -20)),
    (Point(-176, -15), Point(-175, -15))
]

df = spark.createDataFrame(data, ["point1", "point2"])

df.select(ST.dwithin("point1", "point2", 3.0).alias("dwithin")).show()
Result
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
+-------+
|dwithin|
+-------+
|   true|
|  false|
|   true|
+-------+

Version table

ReleaseNotes

1.0.0

Python and SQL functions introduced

1.5.0

Scala function introduced

1.6.0

distance parameter accepts values specified with units

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.