ST_LongestLine
What is ST_LongestLine?
ST_LongestLine returns the 2D line connecting the two points — one on each input geometry — that are farthest apart. It is the complement of ST_ShortestLine and returns a two-point LINESTRING whose length equals ST_MaxDistance(g1, g2).
ST_LongestLine(geometry g1, geometry g2) → geometryIf called with the same geometry for both arguments, it returns the internal longest line — the "diameter" of the geometry.
When would you use ST_LongestLine?
Use ST_LongestLine to visualise maximum extent or worst-case separation between features: the longest span across a lake or island, the maximum separation between two polygon boundaries, or the diameter of a complex polygon for sizing labels, legends, or symbols. It is also useful for quickly drawing the "major axis" of irregular shapes, and in QA workflows to flag geometries whose longest internal line exceeds a threshold.
1-- Diameter of each lake polygon
2SELECT id, ST_Length(ST_LongestLine(geom, geom)) AS diameter_m
3FROM lakes;FAQs
Is it the same as Hausdorff distance?
No. Hausdorff distance is the greatest of minimum distances, measuring shape dissimilarity. ST_LongestLine returns the maximum possible distance between any two points of the inputs — usually much larger.
Does it work in 3D?
No. ST_LongestLine is 2D and ignores Z. For 3D, use ST_3DLongestLine.
What does it return for identical inputs?
The internal longest line of the geometry — the two points within the geometry that are farthest apart. Its length is the geometry's diameter.
How is the result structured?
A two-point LINESTRING whose start and end are the farthest pair across g1 and g2. Extract its length with ST_Length, or its endpoints with ST_StartPoint and ST_EndPoint.