Name

ST_MaxDistance — Returns the 2D largest distance between two geometries in projected units.

Synopsis

float ST_MaxDistance(geometry g);

float ST_MaxDistance(geometry g1, geometry g2);

Description

Returns the 2-dimensional maximum distance between two geometries, in projected units. The maximum distance always occurs between two vertices. This is the length of the line returned by ST_LongestLine.

If only one geometry is provided, or g1 and g2 are the same geometry, returns the distance between the two vertices farthest apart in that geometry.

Availability: 1.5.0

Enhanced: 3.7.0 - support for a single geometry input.

Examples

Maximum distance between a point and lines.

Code
SELECT ST_MaxDistance('POINT(0 0)'::geometry, 'LINESTRING ( 2 0,0 2 )'::geometry);
Output
2
Figure
Geometry figure for visual-st-maxdistance-01
Code
SELECT ST_MaxDistance('POINT(0 0)'::geometry, 'LINESTRING ( 2 2,2 2 )'::geometry);
Output
2.82842712474619
Figure
Geometry figure for visual-st-maxdistance-02

Maximum distance between vertices of a single geometry.

Code
SELECT ST_MaxDistance('POLYGON ((10 10,10 0,0 0,10 10))'::geometry);
Output
 14.142135623730951
Figure
Geometry figure for visual-st-maxdistance-03