Compute the planar Euclidean distance between two geometries in the units of their coordinate system.
ST_DISTANCE(point1, point2)
## Overview Returns the minimum planar Euclidean distance between two geometries in the units of their coordinate reference system. For two POINTs this is the straight-line distance; for a POINT and a POLYGON it is the distance to the nearest edge (or 0 if the point lies inside); for two POLYGONs it is the shortest distance between any pair of points on their boundaries, or 0 if they overlap. ST_DISTANCE assumes a flat, cartesian plane. It does not apply Earth curvature or convert between coordinate systems. That makes it the right choice for data already projected into a metric CRS, and the wrong choice for raw longitude/latitude data where you need meters. Use ST_DISTANCE_HAVERSINE or ST_DISTANCE_VINCENTY for geodesic distance on WGS 84 inputs. ## When to use ST_DISTANCE vs. alternatives - **ST_DISTANCE** (this function): planar distance in CRS units. Best for pre-projected data (UTM, national grids, map-tile coordinates) and for small-scale local analysis. - **ST_DISTANCE_HAVERSINE**: great-circle distance on a sphere, in meters, from raw lat/lng. Preferred default for geographic data. - **ST_DISTANCE_SPHERE**: alternative spherical formula using the law of cosines. Similar accuracy to Haversine; prefer Haversine for general use. - **ST_DISTANCE_VINCENTY**: ellipsoidal (WGS 84) distance in meters. Survey-grade accuracy but slower and can fail to converge at near-antipodes. ## Behavior - Returns a DOUBLE in the units of the CRS (meters if the CRS uses meters, degrees if the CRS is WGS 84 lat/lng). - Returns 0 if the two geometries intersect or share any point. - Accepts any geometry types on both sides; the result is the shortest distance between the two geometry footprints. - Returns NULL if either argument is NULL. - Does not apply Earth curvature. Planar distance on raw WGS 84 data is in degrees and is not a physical distance. - Does not track or validate SRID. The caller is responsible for ensuring both inputs share the same CRS. ## Accuracy and performance - Distance is mathematically exact for planar inputs up to floating-point rounding. - Performance depends on the complexity of the input geometries; for polygons with many vertices, consider pre-filtering with a bounding-box test before calling ST_DISTANCE. - For large-scale geographic distance workloads, project to a metric CRS once (ingest-time) rather than converting inside the query. ## Compatibility - Matches the OGC Simple Features definition of planar distance. - Compatible with other SQL geospatial engines that compute planar distance on GEOMETRY inputs.
| Name | Type | Description |
|---|---|---|
point1 | Specifies the first geometry. Accepts any geometry type; distance is computed between the closest points of the two geometries. | |
point2 | Specifies the second geometry. Accepts any geometry type; distance is computed between the closest points of the two geometries. |
-- Planar distance in coordinate-system units. Expect 5.0 for a 3-4-5 triangle.
SELECT ST_DISTANCE(ST_MAKE_POINT(0, 0), ST_MAKE_POINT(3, 4)) AS dist;
-- Zero if the point is inside the polygon, otherwise the distance to the nearest edge.
SELECT ST_DISTANCE(
ST_MAKE_POINT(5, 5),
ST_GEOM_FROM_TEXT('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))')
) AS dist;
-- Assumes parcel_geom is in a metric CRS so dist is in meters.
SELECT parcel_id,
ST_DISTANCE(parcel_geom, ST_MAKE_POINT(500000.0, 180000.0)) AS dist_m
FROM gis.catalog.parcels_projected
ORDER BY dist_m
LIMIT 10;
-- Keep features within 500 CRS units of a reference point.
SELECT feature_id
FROM gis.catalog.features_projected
WHERE ST_DISTANCE(geom, ST_MAKE_POINT(1000.0, 2000.0)) <= 500.0;
-- Shows how planar distance on raw WGS 84 coordinates produces degrees, not meters.
SELECT
ST_DISTANCE(ST_MAKE_POINT(-0.1278, 51.5074), ST_MAKE_POINT(2.3522, 48.8566)) AS planar_deg,
ST_DISTANCE_HAVERSINE(51.5074, -0.1278, 48.8566, 2.3522) AS haversine_m;
-- Either argument being NULL yields NULL.
SELECT ST_DISTANCE(CAST(NULL AS GEOMETRY), ST_MAKE_POINT(0, 0)) AS dist;