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_LineLocatePointnyc_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 表格。

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

檢視原始 (紅色星號) 及事件 (藍色圓形) 點與街道,您便可以瞭解事件是如何定位在最近的街道線上的。

_images/lrs1.jpg

注意事項

線性參考函數令人驚訝的一種用途與線性參考模型無關。如此上方所示,可使用此函數將點定位至線性特徵上。基於預期參考線性網路的 GPS 追蹤或其他輸入等使用案例,定位是一個實用的可用功能。

25.2. 函數清單