How the ST_Distance Calculator works, and what it assumes

ST_Distance(A, B) is the minimum distance between any point of A and any point of B: for two polygons, the gap between their nearest edges; for a point and a line, the perpendicular distance to the line or to its nearest end. The tool also returns the two nearest points and the shortest line joining them, which PostGIS gives as ST_ClosestPoint and ST_ShortestLine. If the geometries touch or overlap, the distance is 0; if either is empty, there is no distance, and PostGIS returns NULL.

With Coordinate units the answer is exactly what ST_Distance returns for the geometry type: a planar distance in the coordinates' own unit. For longitude and latitude that unit is the degree, which is not a length, and the page warns. With Metres the geometries are projected into an azimuthal equidistant projection on WGS 84 centred between them, the nearest points are found there, and the distance between those two points is measured on the ellipsoid with Vincenty's formula, which is the question PostGIS's geography type answers.

The distance reported in metres is always a true ellipsoidal distance between two points of the geometries. What the projection can affect is which two points are chosen, and only for geometries spread over thousands of kilometres; the page states the possible error whenever it is more than 0.1 %.

Where this comes up in GIS work

  • Measuring how far a site is from the nearest boundary, river or road rather than from its centre.
  • Checking a nearest-neighbour result from a database query.
  • Getting the shortest connection between two networks or two parcels.
  • Seeing why ST_Distance on EPSG:4326 returned a tiny number: it was degrees.

A worked example

A point and a line, in coordinate units:

ST_Distance('POINT (0 0)', 'LINESTRING (3 4, 3 10)') = 5
Nearest point on B: 3 4

The nearest point is the line's end, 5 units away (a 3-4-5 triangle). Extend the line down to LINESTRING (3 -4, 3 10) and the distance becomes 3, to the point 3 0 perpendicular to the origin. The page's example, a building footprint near Charing Cross and a point by Buckingham Palace, is 1,196.2 m apart in metres; in coordinate units the same pair is 0.0151 degrees, which is the number ST_Distance would give on EPSG:4326.

Using the ST_Distance Calculator

  1. Paste the first geometry into Geometry A, or select Load example.
  2. Paste the second geometry into Geometry B.
  3. Choose Metres for longitude and latitude, or Coordinate units for projected data.
  4. Select Calculate distance, or press Ctrl + Enter (Cmd + Enter on a Mac).
  5. Read the distance, the nearest points and the shortest line, drawn on the plot.

When the output looks wrong

The distance is a small number like 0.015
That is degrees: on longitude and latitude, ST_Distance for the geometry type measures in the coordinates' own unit. Choose Metres to measure on the ground; the page warns when degrees are in use.
The distance is 0 but the geometries look apart
They touch or overlap somewhere, perhaps at a single vertex. Check ST_Intersects, and read the nearest points: when they coincide, that is where the geometries meet.
There is no distance: one of the geometries is empty
An empty geometry has no points to measure from, so PostGIS returns NULL rather than zero, and so does this tool.
The choice of nearest points may be off by up to a few percent
The geometries are spread over thousands of kilometres, where one local projection cannot hold every distance exactly. The distance reported is still a true ellipsoidal distance between the two points found; for precise work over that span, measure smaller pieces.

Questions about the ST_Distance Calculator

How is the distance in metres calculated?

The nearest points are found in an azimuthal equidistant projection on WGS 84 centred between the two geometries, then the distance between those two points is measured on the ellipsoid with Vincenty's formula. For geometries a few hundred kilometres apart or closer, with edges up to tens of kilometres long, it differs from PostGIS geography by far less than a metre.

Is this the distance between the centres?

No. It is the minimum distance between any point of A and any point of B, which for two polygons is the gap between their nearest edges. The Distance Calculator on this site measures between two single points.

What is the shortest line?

The straight line joining the two nearest points, which is what PostGIS's ST_ShortestLine returns. It is drawn on the plot and given as WKT.

Can I use projected coordinates?

Yes: choose Coordinate units, and the distance is in the projection's own unit, which is metres for UTM, British National Grid and most national grids.

Is my data uploaded?

No. The distance is computed in your browser.