名前

ST_Line_Locate_Point — ラインストリング上の,与えたポイントへの最短点を,二次元長に対する割合として0から1の区間で返します.

概要

float ST_Line_Locate_Point(geometry a_linestring, geometry a_point);

説明

ラインストリング上の,与えたポイントへの最短点を,二次元ラインストリングの総延長に対する割合として0から1の区間で返します.

返された位置は,ポイント(ST_Line_Interpolate_Point)または,部分ラインストリング(ST_Line_Substring)の抽出に使用することができます.

この関数は,住所番号に近づくのに便利です(訳注: 道路方式の住居表示の場合).

初出バージョン: 1.1.0

--ストリートに沿った,ストリート番号の点のラフな探索です.
--なお,foo全体は住居の重心とストリートのように見えるダミーデータです.
--T_DWithInで対象とするストリートから大きく外れる住居を除外しています.
SELECT ST_AsText(house_loc) As as_text_house_loc,
	startstreet_num +
		CAST( (endstreet_num - startstreet_num)
			* ST_Line_Locate_Point(street_line, house_loc) As integer) As street_num
FROM
(SELECT ST_GeomFromText('LINESTRING(1 2, 3 4)') As street_line,
	ST_MakePoint(x*1.01,y*1.03) As house_loc, 10 As startstreet_num,
		20 As endstreet_num
FROM generate_series(1,3) x CROSS JOIN generate_series(2,4) As y)
As foo
WHERE ST_DWithin(street_line, house_loc, 0.2);

 as_text_house_loc | street_num
-------------------+------------
 POINT(1.01 2.06)  |         10
 POINT(2.02 3.09)  |         15
 POINT(3.03 4.12)  |         20

 --find closest point on a line to a point or other geometry
 SELECT ST_AsText(ST_Line_Interpolate_Point(foo.the_line, ST_Line_Locate_Point(foo.the_line, ST_GeomFromText('POINT(4 3)'))))
FROM (SELECT ST_GeomFromText('LINESTRING(1 2, 4 5, 6 7)') As the_line) As foo;
   st_astext
----------------
 POINT(3 4)

関連情報

ST_DWithin, ST_Length2D, ST_Line_Interpolate_Point, ST_Line_Substring