ST_ClosestPoint — 返回 g1 上最接近 g2 的 2D 点。 这是从一个几何体到另一个几何体的最短直线的第一个点。
geometry ST_ClosestPoint(geometry geom1, geometry geom2);
geography ST_ClosestPoint(geography geom1, geography geom2, boolean use_spheroid = true);
返回 geom1 上最接近 geom2 的二维点。 这是几何图形之间最短线的第一个点(由 ST_ShortestLine计算)。
|
|
|
如果您有 3D 几何图形,您可能更喜欢使用 ST_3DClosestPoint。 |
增强:3.4.0 - 支持地理。
可用性:1.1.0
Point 和 LineString 的最近点是点本身。 LineString 和 Point 的最近点是线上的点。
SELECT ST_ClosestPoint(pt, line) AS cp_pt_line, 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)
Find the closest point on polygon A to polygon B.
WITH source AS (
SELECT 'POLYGON ((190 150,20 10,160 70,190 150))'::geometry AS a,
ST_Buffer('POINT(80 160)'::geometry, 30) AS b
)
SELECT b AS polygon_b,
ST_ClosestPoint(a, b) AS closest_point
FROM source;
POLYGON((110 160,109.424 154.147,107.716 148.519,104.944 143.333,101.213 138.787,96.667 135.056,91.481 132.284,85.853 130.576,80 130,74.147 130.576,68.519 132.284,63.333 135.056,58.787 138.787,55.056 143.333,52.284 148.519,50.576 154.147,50 160,50.576 165.853,52.284 171.481,55.056 176.667,58.787 181.213,63.333 184.944,68.519 187.716,74.147 189.424,80 190,85.853 189.424,91.481 187.716,96.667 184.944,101.213 181.213,104.944 176.667,107.716 171.481,109.424 165.853,110 160)) | POINT(131.59149149528952 101.89887534906195)