名前

ST_ClosestPoint — g2に最も接近するg1上の2次元のポイントを返します。これは最短線の最初のポイントです。

概要

geometry ST_ClosestPoint(geometry g1, geometry g2);

説明

g2に最も接近するg1上の2次元のポイントを返します。これは最短線の最初のポイントです。

[注記]

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

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(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)
                                

ポリゴンA上のポリゴン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)
                                

関連情報

ST_3DClosestPoint,ST_Distance, ST_LongestLine, ST_ShortestLine, ST_MaxDistance