PostGISMeasurement

ST_ClosestPoint

What is ST_ClosestPoint?

ST_ClosestPoint returns the 2D point on geometry g1 that is closest to geometry g2. It is the "foot of the perpendicular" generalised to arbitrary geometries — if g1 is a line and g2 is a point, the result is the point on the line nearest to that point.

SQL
1ST_ClosestPoint(geometry g1, geometry g2)geometry
2ST_ClosestPoint(geography g1, geography g2, boolean use_spheroid = true) → geography

A geography overload was added in PostGIS 3.4. The result is a POINT geometry (or geography) in the same SRID as the inputs.

When would you use ST_ClosestPoint?

Use ST_ClosestPoint whenever you need to snap or project a feature onto another. Typical uses include snapping a GPS fix to the nearest road centreline for map-matching, placing a label at the point on a polygon's boundary closest to its centroid, or computing the entry point where a service line meets a parcel boundary. It also pairs naturally with ST_MakeLine to draw the visible "tether" between two features in a map:

SQL
1SELECT ST_MakeLine(ST_ClosestPoint(road.geom, house.geom), house.geom) AS tether
2FROM houses house
3JOIN roads  road ON ST_DWithin(house.geom, road.geom, 50);

FAQs

Is ST_ClosestPoint symmetric?

No. ST_ClosestPoint(g1, g2) returns a point on g1, while ST_ClosestPoint(g2, g1) returns a point on g2. The two points coincide only when the geometries intersect.

Does it work in 3D?

No. ST_ClosestPoint ignores Z and operates in 2D. For 3D use ST_3DClosestPoint, which computes the closest point in three-dimensional space.

What units does the underlying distance use?

ST_ClosestPoint returns a geometry, not a distance — but internally it minimises the 2D Cartesian distance in the input SRID. For geography input it minimises the spheroidal distance in metres.

How do I get both the closest point and the distance?

Call ST_ClosestPoint and ST_Distance on the same pair, or use ST_ShortestLine to get a two-point LINESTRING whose start and end are the closest points on each geometry, then measure its length.