Name

ST_Distance — For geometry type Returns the 2-dimensional cartesian minimum distance (based on spatial ref) between two geometries in projected units. For geography type defaults to return spheroidal minimum distance between two geographies in meters.

Synopsis

float ST_Distance(geometry g1, geometry g2);

float ST_Distance(geography gg1, geography gg2);

float ST_Distance(geography gg1, geography gg2, boolean use_spheroid);

Description

For geometry type returns the 2-dimensional minimum cartesian distance between two geometries in projected units (spatial ref units). For geography type defaults to return the minimum distance around WGS 84 spheroid between two geographies in meters. Pass in false to return answer in sphere instead of spheroid.

This method implements the OpenGIS Simple Features Implementation Specification for SQL 1.1.

This method implements the SQL/MM specification. SQL-MM 3: 5.1.23

This method supports Circular Strings and Curves

This method is also provided by SFCGAL backend.

Availability: 1.5.0 geography support was introduced in 1.5. Speed improvements for planar to better handle large or many vertex geometries

Enhanced: 2.1.0 improved speed for geography. See Making Geography faster for details.

Enhanced: 2.1.0 - support for curved geometries was introduced.

Basic Geometry Examples

--Geometry example - units in planar degrees 4326 is WGS 84 long lat unit=degrees
SELECT ST_Distance(
		ST_GeomFromText('POINT(-72.1235 42.3521)',4326),
		ST_GeomFromText('LINESTRING(-72.1260 42.45, -72.123 42.1546)', 4326)
	);
st_distance
-----------------
0.00150567726382282

-- Geometry example - units in meters (SRID: 26986 Massachusetts state plane meters) (most accurate for Massachusetts)
SELECT ST_Distance(
			ST_Transform(ST_GeomFromText('POINT(-72.1235 42.3521)',4326),26986),
			ST_Transform(ST_GeomFromText('LINESTRING(-72.1260 42.45, -72.123 42.1546)', 4326),26986)
		);
st_distance
-----------------
123.797937878454

-- Geometry example - units in meters (SRID: 2163 US National Atlas Equal area) (least accurate)
SELECT ST_Distance(
			ST_Transform(ST_GeomFromText('POINT(-72.1235 42.3521)',4326),2163),
			ST_Transform(ST_GeomFromText('LINESTRING(-72.1260 42.45, -72.123 42.1546)', 4326),2163)
		);

st_distance
------------------
126.664256056812

Geography Examples

-- same as geometry example but note units in meters - use sphere for slightly faster less accurate
SELECT ST_Distance(gg1, gg2) As spheroid_dist, ST_Distance(gg1, gg2, false) As sphere_dist 
FROM (SELECT
	ST_GeographyFromText('SRID=4326;POINT(-72.1235 42.3521)') As gg1,
	ST_GeographyFromText('SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)') As gg2
	) As foo  ;

  spheroid_dist   |   sphere_dist
------------------+------------------
 123.802076746848 | 123.475736916397

See Also

ST_3DDistance, ST_DWithin, ST_Distance_Sphere, ST_Distance_Spheroid, ST_MaxDistance, ST_Transform