Name

ST_ClosestPoint — g1上にある、g2と最近傍となる2次元ポイントを返します。これは、あるジオメトリから他のジオメトリへの最短ラインの一つ目のポイントです。

Synopsis

geometry ST_ClosestPoint(geometry geom1, geometry geom2);

geography ST_ClosestPoint(geography geom1, geography geom2, boolean use_spheroid = true);

説明

geom2と最近傍となるgeom1上の2次元ポイントを返します。これは、ジオメトリ間の最短ライン (ST_ShortestLineで生成されるものと同じです)の一つ目のポイントです。

[Note]

3次元ジオメトリの場合にはST_3DClosestPointの方が良いでしょう。

Enhanced: 3.4.0 - ジオグラフィに対応しました。

Availability: 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