This topic describes the ST_ClosestPoint function. This function compares two geometry objects and returns the 2D point that belongs to one geometry object and is the closest to the other geometry object.

Syntax

geometry  ST_ClosestPoint(geometry  g1 , geometry  g2);

Parameters

Parameter Description
g1 The first geometry object that you want to specify.
g2 The second geometry object that you want to specify.

Description

The point that is returned is the end point of the shortest line between the geometry objects that you specify.

Examples

SELECT ST_AsText(ST_ClosestPoint('LINESTRING(0 -1,0 1)'::geometry,'POINT(1 0)'::geometry));
 st_astext
------------
 POINT(0 0)
(1 row)