軌跡表
更新時間:
當(dāng)需要針對軌跡進(jìn)行操作,或是希望將點聚合成為軌跡降低存儲成本時,推薦使用軌跡表。
以某出行APP的數(shù)據(jù)為例,其建表語句為:
CREATE TABLE trajectory_table(
userid numeric PRIMARY KEY,
traj trajectory
);
創(chuàng)建軌跡支持多種方式,具體如下:
將點表轉(zhuǎn)化為軌跡,具體請參見點表。
由于執(zhí)行GROUP BY操作時不一定保證點的次序,因此需要調(diào)用ST_Sort()函數(shù),將軌跡點根據(jù)時間重新排序。
INSERT INTO trajectory_table SELECT userid, ST_Sort(ST_MakeTrajectory(pnts.tjraw, true, '{"intensity"}'::cstring[])) FROM (SELECT sample_points.userid, array_agg( ROW(sample_points.sample_time, sample_points.x, sample_points.y, sample_points.z, sample_points.intensity)) AS tjraw FROM sample_points GROUP BY userid ) pnts;
ST_makeTrajectory提供多種方式構(gòu)造軌跡:
說明本示例僅展示ID為1的軌跡,通過Geometry類型+時間戳數(shù)組+屬性JSON串構(gòu)造軌跡。
INSERT INTO trajectory_table SELECT 3, ST_MakeTrajectory( 'STPOINT'::leaftype, st_geomfromtext('LINESTRING(114.35 39.28 4,114.36 39.28 4,114.35 39.29 4)', 4326), ARRAY['2020-04-11 17:42:30'::timestamp,'2020-04-11 17:43:30'::timestamp,'2020-04-11 17:45:00'::timestamp], '{"leafcount":3,"attributes":{"intensity":{"type":"integer","length":4,"nullable":true,"value":[80,30,50]}}}' );
說明ST_makeTrajectory函數(shù)提供多種方式構(gòu)造軌跡,一些方式需要依賴Ganos的Geometry幾何類型,更多信息,請參見幾何類型。
JSON字符串構(gòu)建軌跡:
INSERT INTO trajectory_table SELECT 4, '{"trajectory":{"version":1,"type":"STPOINT","leafcount":3,"start_time":"2020-04-11 17:42:30","end_time":"2020-04-11 17:45:00","spatial":"SRID=4326;LINESTRING(114.35 39.28 4,114.36 39.28 4,114.35 39.29 4)","timeline":["2020-04-11 17:42:30","2020-04-11 17:43:30","2020-04-11 17:45:00"],"attributes":{"leafcount":3,"intensity":{"type":"integer","length":4,"nullable":true,"value":[80,30,50]}}}}' ::trajectory;
對于整條的軌跡,支持進(jìn)行時空相關(guān)的操作,例如相交、包含的判斷等。通過建立R樹索引,可以加速空間查詢。
--創(chuàng)建索引。
CREATE INDEX ON trajectory_table USING gist(traj);
--查找所有與一個二維矩形范圍相交的軌跡,返回其userid。
SELECT userid FROM trajectory_table WHERE ST_2DIntersects(traj, ST_MakeEnvelope(114.33,39.28,14.331,39.282));
文檔內(nèi)容是否對您有幫助?