25. 線形参照¶
線形参照 (「動的分割」とも) は線の地物の基本集合を参照することで記述できる地物の表現手法です。線形参照を使ってモデル化される地物の一般的な例は次の通りです:
高速道路網に沿ったマイル値を使って参照される高速道路資産
道路維持作業、マイル値を計測値として二つのポイント間の道路網に沿って発生するものとして参照されます。
水生インベントリ。魚類の存在が二つの距離計測値の間に存在するものとして記録されます。
流れの水文学的特徴 (「リーチ」) で、起点からの開始距離と終了距離で記録されます。
線形参照の利点は、従属する空間観測は基本観測と別に記録されている必要がなく、従属的な観測が新しいジオメトリの自動追跡することを知ることで、基本観測レイヤの更新が可能となる点です。
注釈
線形参照に関する Esri 用語規則では、線の空間地物の基本テーブルと、空間地物への外部キー参照と参照される地物に沿った計測値を持つ「イベント」という非空間テーブルを持つことになっています。ここでは、「イベントテーブル」という語はこの非空間テーブルを指します。
25.1. 線形参照の生成¶
線形ネットワークへの参照を行いたい既存のポイントテーブルがある場合には、ST_LineLocatePoint 関数を使います。これは、ラインとポイントを取り、ポイント位置をラインに沿った比率で返します。
-- Simple example of locating a point half-way along a line
SELECT ST_LineLocatePoint('LINESTRING(0 0, 2 2)', 'POINT(1 1)');
-- Answer 0.5
-- What if the point is not on the line? It projects to closest point
SELECT ST_LineLocatePoint('LINESTRING(0 0, 2 2)', 'POINT(0 2)');
-- Answer 0.5
ST_LineLocatePoint を使うと nyc_subway_stations をストリートに関連する「イベントテーブル」に変換することができます。
-- All the SQL below is in aid of creating the new event table
CREATE TABLE nyc_subway_station_events AS
-- We first need to get a candidate set of maybe-closest
-- streets, ordered by id and distance...
WITH ordered_nearest AS (
SELECT
ST_GeometryN(streets.geom,1) AS streets_geom,
streets.gid AS streets_gid,
subways.geom AS subways_geom,
subways.gid AS subways_gid,
ST_Distance(streets.geom, subways.geom) AS distance
FROM nyc_streets streets
JOIN nyc_subway_stations subways
ON ST_DWithin(streets.geom, subways.geom, 200)
ORDER BY subways_gid, distance ASC
)
-- We use the 'distinct on' PostgreSQL feature to get the first
-- street (the nearest) for each unique street gid. We can then
-- pass that one street into ST_LineLocatePoint along with
-- its candidate subway station to calculate the measure.
SELECT
DISTINCT ON (subways_gid)
subways_gid,
streets_gid,
ST_LineLocatePoint(streets_geom, subways_geom) AS measure,
distance
FROM ordered_nearest;
-- Primary keys are useful for visualization softwares
ALTER TABLE nyc_subway_station_events ADD PRIMARY KEY (subways_gid);
ひとたびイベントテーブルを持つと、空間ビューに戻るいのが楽しくなります。派生元のポイントに対する相対的なイベントの表示が可能となるためです。
測定からポイントに行くには、ST_LineInterpolatePoint 関数を使用します。前の単純な例を逆にしたものを次に示します:
-- Simple example of locating a point half-way along a line
SELECT ST_AsText(ST_LineInterpolatePoint('LINESTRING(0 0, 2 2)', 0.5));
-- Answer POINT(1 1)
nyc_subway_station_events テーブルを nyc_streets テーブルに結合でき、元の nyc_subway_stations テーブルを参照することなく、空間イベントのポイントを生成するための measure 属性を使用できます。
-- New view that turns events back into spatial objects
CREATE OR REPLACE VIEW nyc_subway_stations_lrs AS
SELECT
events.subways_gid,
ST_LineInterpolatePoint(ST_GeometryN(streets.geom, 1), events.measure)AS geom,
events.streets_gid
FROM nyc_subway_station_events events
JOIN nyc_streets streets
ON (streets.gid = events.streets_gid);
元のポイント (赤い星印) とイベント (青い円) のポイントとストリートを表示すると、イベントが最も近いストリートのラインに直接スナップされていることが分かります。
注釈
線形参照関数の驚くべき使い方のひとつは、線形参照モデルとは何の関係もありません。上にある通り、ポイントを線形地物にスナップするための関数を使用できます。線形ネットワークを参照すると想定される GPS トラックまたは他の入力といったユースケースには、スナップは有効にすると手軽な機能です。
25.2. 関数リスト¶
ST_LineInterpolatePoint(geometry A, double measure): ラインに沿った補間ポイントを返します。
ST_LineLocatePoint(geometry A, geometry B): 指定したポイントに最も近い LINESTRING 上の点の位置を 0 から 1 までの間の浮動所数点を返します。
ST_LineSubstring(geometry A, double from, double to): 2次元長の合計を基準にした割合で与えた始点と終点の範囲で作られる LINESTRING の一部を返します。
ST_LocateAlong(geometry A, double measure): 指定したM値に合致する要素を持つ派生ジオメトリコレクション値を返します。
ST_LocateBetween(geometry A, double from, double to): 指定したM値範囲に包括的に合致する要素を持つ派生ジオメトリコレクション値を返します。
ST_AddMeasure(geometry A, double from, double to): Converts a linestring or multilinestring into a measured path (M values). Think of it as a road where you want to assign house numbers progressively from start to end. This function assigns "measure" values (like house numbers, kilometers, or travel times) that increase linearly along the line. If the geometry has no M coordinates, they are added. If it already has M values, they are overwritten. Only LINESTRING and MULTILINESTRING are supported.
Example (house numbers): START (house 10) --- intermediate points --- END (house 100) → each point gets a proportional value.

