ST_LongestLine

Name

ST_LongestLine — Returns the 2D longest line between two geometries.

Synopsis

geometry ST_LongestLine ( geometry g1 , geometry g2 ) ;

Description

Returns the 2-dimensional longest line between the points of two geometries.

The function returns the first longest line if more than one is found. The line returned starts on g1 and ends on g2. The length of the line is equal to the distance returned by ST_MaxDistance .

Availability: 1.5.0

Examples

Longest line between a point and a line

SELECT ST_AsText(
	ST_LongestLine('POINT(100 100)'::geometry,
		'LINESTRING (20 80, 98 190, 110 180, 50 75 )'::geometry)
	) As lline;


   lline
-----------------
LINESTRING(100 100,98 190)
				

Longest line between two polygons

SELECT ST_AsText(
	ST_LongestLine(
		ST_GeomFromText('POLYGON((175 150, 20 40,
			50 60, 125 100, 175 150))'),
		ST_Buffer(ST_GeomFromText('POINT(110 170)'), 20)
		)
	) As llinewkt;

   lline
-----------------
LINESTRING(20 40,121.111404660392 186.629392246051)
				

Longest straight distance to travel from one part of a city to another. Note that the maximum distance is equal to the length of the line.

SELECT ST_AsText( ST_LongestLine(c.geom, c.geom)) AS llinewkt,
       ST_MaxDistance( c.geom,c.geom) AS max_dist,
       ST_Length( ST_LongestLine(c.geom, c.geom)) AS lenll
FROM (SELECT ST_MakeValid( ST_Collect(geom)) AS geom
      FROM (SELECT ST_Translate( ST_SnapToGrid(
                ST_Buffer(
                    ST_Point(50 ,generate_series(50,190, 50)),
                    40, 'quad_segs=2'),1), x, 0) AS geom
            FROM generate_series(1,100,50) As x) AS foo
      ) AS c;

          llinewkt          |     max_dist     |      lenll
---------------------------+------------------+------------------
 LINESTRING(23 22,129 178) | 188.605408193933 | 188.605408193933