ST_Distance

ST_Distance takes two geometry columns and returns a double column. The output column represents the planar distance between the two input geometries. For multipoints, lines, and polygons, the distance is calculated from the nearest point between the geometries. The result will be in the same units as the input geometry data. For example, if your input geometries are in a spatial reference that uses meters, the result values will be in meters.

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.

If your input geometries are in a geographic coordinate system, use ST_GeodesicDistance to calculate distance.

FunctionSyntax
Pythondistance(geometry1, geometry2)
SQLST_Distance(geometry1, geometry2)
Scaladistance(geometry1, geometry2)

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

This function implements the OpenGIS Simple Features Implementation Specification for SQL 1.2.1.

Examples

PythonPythonSQLScala
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
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.distance("point1", "point2").alias("distance")).show()
Result
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
+--------+
|distance|
+--------+
|     0.0|
|     5.0|
|     1.0|
+--------+

Version table

ReleaseNotes

1.0.0

Python and SQL functions introduced

1.5.0

Scala function introduced

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