Name

ST_ClosestPoint — Returns the 2-dimensional point on g1 that is closest to g2. This is the first point of the shortest line.

Synopsis

geometry ST_ClosestPoint(geometry g1, geometry g2);

Description

Returns the 2-dimensional point on g1 that is closest to g2. This is the first point of the shortest line.

Availability: 1.5.0

Examples

Closest between point and linestring is the point itself, but closest point between a linestring and point is the point on line string that is closest.

SELECT ST_AsText(ST_ClosestPoint(pt,line)) AS cp_pt_line, 
	ST_AsText(ST_ClosestPoint(line,pt)) As cp_line_pt
FROM (SELECT 'POINT(100 100)'::geometry As pt, 
		'LINESTRING (20 80, 98 190, 110 180, 50 75 )'::geometry As line
	) As foo;

	
   cp_pt_line   |                cp_line_pt
----------------+------------------------------------------
 POINT(100 100) | POINT(73.0769230769231 115.384615384615)
				

closest point on polygon A to polygon B

SELECT ST_AsText(
		ST_ClosestPoint(
			ST_GeomFromText('POLYGON((175 150, 20 40, 50 60, 125 100, 175 150))'),
			ST_Buffer(ST_GeomFromText('POINT(110 170)'), 20)
			) 
		) As ptwkt;
		
                  ptwkt
------------------------------------------
 POINT(140.752120669087 125.695053378061)
				

See Also

ST_Distance, ST_LongestLine, ST_ShortestLine, ST_MaxDistance