Name

ST_ClosestPoint — 返回G1上最接近G2的二维点。这是从一个几何图形到另一个几何图形的最短直线的第一个点。

Synopsis

geometry ST_ClosestPoint(geometry geom1, geometry geom2);

描述

返回上的二维点 geom1 最接近于 geom2 。这是几何之间最短直线的第一个点(计算方式为 ST_ShortestLine )。

[Note]

如果您有3D几何,您可能更喜欢使用 ST_3DClosestPoint

可用性:1.5.0

示例

点和线串的最近点是点本身。直线串和点的最近点是直线上的点。

SELECT ST_AsText( ST_ClosestPoint(pt,line)) AS cp_pt_line,
       ST_AsText( ST_ClosestPoint(line,pt)) AS cp_line_pt
    FROM (SELECT 'POINT (160 40)'::geometry AS pt,
                 'LINESTRING (10 30, 50 50, 30 110, 70 90, 180 140, 130 190)'::geometry AS line ) AS t;

   cp_pt_line   |                cp_line_pt
----------------+------------------------------------------
 POINT(160 40)  | POINT(125.75342465753425 115.34246575342466)

多边形A上与多边形B最近的点

SELECT ST_AsText( ST_ClosestPoint(
                'POLYGON ((190 150, 20 10, 160 70, 190 150))',
                ST_Buffer('POINT(80 160)', 30)        )) As ptwkt;
------------------------------------------
 POINT(131.59149149528952 101.89887534906197)

另请参阅

ST_3DClosestPoint, ST_Distance, ST_LongestLine, ST_ShortestLine, ST_MaxDistance