將多條軌跡合并為一條。
文法
trajectory ST_TrajMerge (trajectory SET_ trajs);
trajectory ST_TrajMerge (trajectory SET trajs, bool doSort, bool doDeduplicate);
trajectory ST_TrajMerge (trajectory[] trajs, bool doSort default true, bool doDeduplicate default true);
參數
參數名稱 | 描述 |
trajs | 需要彙總的軌跡列或軌跡數組。 |
doSort | 是否需要對結果的軌跡點進行排序,預設值為 |
doDeduplicate | 是否需要對結果軌跡去除重複點,預設值為 |
傳回值
合并後的一條軌跡。
描述
將多條軌跡合并為一條。
支援使用doSort參數將彙總的軌跡和軌跡點進行重新排序,也支援使用doDeduplicate參數將相鄰軌跡的重複軌跡點合并(例如前一條軌跡的終點是後一條軌跡的起點)。
建議將doSort和doDeduplicate選擇為true(預設值),雖然略微降低效能,但可以保障結果的準確。
樣本
準備資料:
CREATE TABLE test_trajs_merge(traj trajectory); INSERT INTO test_trajs_merge VALUES ('{"trajectory":{"version":1,"type":"STPOINT","leafcount":2,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-02 00:00:00","spatial":"LINESTRING(0 0,4 0)","timeline":["2000-01-01 00:00:00","2000-01-02 00:00:00"]}}'), ('{"trajectory":{"version":1,"type":"STPOINT","leafcount":2,"start_time":"2000-01-02 00:00:00","end_time":"2000-01-03 00:00:00","spatial":"LINESTRING(4 0,2 0)","timeline":["2000-01-02 00:00:00","2000-01-03 00:00:00"]}}'), ('{"trajectory":{"version":1,"type":"STPOINT","leafcount":2,"start_time":"2000-01-04 00:00:00","end_time":"2000-01-05 00:00:00","spatial":"LINESTRING(2 2,3 3)","timeline":["2000-01-04 00:00:00","2000-01-05 00:00:00"]}}'), ('{"trajectory":{"version":1,"type":"STPOINT","leafcount":2,"start_time":"2000-01-03 00:00:00","end_time":"2000-01-04 00:00:00","spatial":"LINESTRING(3 0,2 2)","timeline":["2000-01-03 00:00:00","2000-01-04 00:00:00"]}}'), ('TRAJECTORY EMPTY'), (NULL);
使用情境:
不排序和去除重複點:
SELECT st_TrajMerge(traj, false, false) FROM test_trajs_merge;
結果顯示如下,可能有多個重複點且時間亂序:
{"trajectory":{"version":1,"type":"STPOINT","leafcount":8,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-05 00:00:00","spatial":"LINESTRING(0 0,4 0,4 0,2 0,2 2,3 3,3 0,2 2)","timeline":["2000-01-01 00:00:00","2000-01-02 00:00:00","2000-01-02 00:00:00","2000-01-03 00:00:00","2000-01-04 00:00:00","2000-01-05 00:00:00","2000-01-03 00:00:00","2000-01-04 00:00:00"]}}
排序但不去除重複點:
SELECT st_TrajMerge(traj, true, false) FROM test_trajs_merge;
結果顯示如下,時間正確但有重複點:
{"trajectory":{"version":1,"type":"STPOINT","leafcount":8,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-05 00:00:00","spatial":"LINESTRING(0 0,4 0,4 0,3 0,2 0,2 2,2 2,3 3)","timeline":["2000-01-01 00:00:00","2000-01-02 00:00:00","2000-01-02 00:00:00","2000-01-03 00:00:00","2000-01-03 00:00:00","2000-01-04 00:00:00","2000-01-04 00:00:00","2000-01-05 00:00:00"]}}
去除重複點但不排序:
SELECT st_TrajMerge(traj, false, true) FROM test_trajs_merge;
結果顯示如下,部分時間戳記錯誤,且錯誤部分的重複點沒有被去除:
{"trajectory":{"version":1,"type":"STPOINT","leafcount":7,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-04 00:00:00","spatial":"LINESTRING(0 0,4 0,2 0,2 2,3 3,3 0,2 2)","timeline":["2000-01-01 00:00:00","2000-01-02 00:00:00","2000-01-03 00:00:00","2000-01-04 00:00:00","2000-01-05 00:00:00","2000-01-03 00:00:00","2000-01-04 00:00:00"]}}
去除重複點並排序:
SELECT st_TrajMerge(traj) FROM test_trajs_merge;
結果顯示如下:
{"trajectory":{"version":1,"type":"STPOINT","leafcount":6,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-05 00:00:00","spatial":"LINESTRING(0 0,4 0,3 0,2 0,2 2,3 3)","timeline":["2000-01-01 00:00:00","2000-01-02 00:00:00","2000-01-03 00:00:00","2000-01-03 00:00:00","2000-01-04 00:00:00","2000-01-05 00:00:00"]}}
使用軌跡數組作為參數,去除重複點並排序:
SELECT st_TrajMerge(array_agg(traj), true, true) FROM test_trajs_merge;
結果顯示如下:
{"trajectory":{"version":1,"type":"STPOINT","leafcount":6,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-05 00:00:00","spatial":"LINESTRING(0 0,4 0,3 0,2 0,2 2,3 3)","timeline":["2000-01-01 00:00:00","2000-01-02 00:00:00","2000-01-03 00:00:00","2000-01-03 00:00:00","2000-01-04 00:00:00","2000-01-05 00:00:00"]}}