空間ジオメトリ、タイムスタンプ、および任意の属性データから軌道オブジェクトを構築します。
構文
構文 1 — 時間範囲指定:
trajectory ST_makeTrajectory(leaftype type, geometry spatial, tsrange timespan, cstring attrs_json);構文 2 — 開始および終了タイムスタンプ指定:
trajectory ST_makeTrajectory(leaftype type, geometry spatial, timestamp start, timestamp end, cstring attrs_json);構文 3 — タイムスタンプ配列指定:
trajectory ST_makeTrajectory(leaftype type, geometry spatial, timestamp[] timeline, cstring attrs_json);構文 4 — 座標配列指定:
trajectory ST_makeTrajectory(leaftype type, float8[] x, float8[] y, integer srid, timestamp[] timeline, text[] attr_field_names, int4[] attr_int4, float8[] attr_float8, text[] attr_cstring, anyarray attr_any);構文 5 — テーブル行からの構築:
trajectory ST_makeTrajectory(anyarray rows, bool hasz, cstring[] attrnames);パラメーター
| パラメーター | 説明 |
|---|---|
type | 軌道のリーフタイプです。ST_POINT のみがサポートされています。 |
spatial | 空間ジオメトリオブジェクトで、LineString または Point として記述されます。 |
timespan | 軌道の時間範囲です。これは開始時刻および終了時刻を含む閉区間です。 |
start | 軌道の開始時刻です。 |
end | 軌道の終了時刻です。 |
timeline | 軌道のタイムラインです。タイムポイントの数は LineString 内のポイント数と一致する必要があります。 |
attrs_json | JSON 形式の軌道属性およびイベントです。null を受け付けます。 |
x | 空間ジオメトリオブジェクトの X 軸座標配列です。 |
y | 空間ジオメトリオブジェクトの Y 軸座標配列です。 |
srid | 軌道の空間参照識別子 (SRID) です。必須パラメーターです。 |
attr_field_names | 軌道内のすべての属性フィールドの名前です。 |
rows | 軌道データを格納するテーブルです。最初の列は timestamp、2 列目および 3 列目は float8 である必要があります。 |
hasz | 軌道が 3 次元であるかどうかを指定します。有効な値は以下のとおりです:true(3D 軌道 — 4 列目は float8 である必要があります)および false(2D 軌道 — 4 列目は軌道属性を表します)。 |
attrnames | 軌道属性の名前です。デフォルト値は attr1, attr2, ... です。 |
attrs_json のフォーマット
attrs_json パラメーターには、以下の構造を持つ JSON オブジェクトを指定できます:
{
"leafcount": 3,
"attributes": {
"velocity": {
"type": "integer",
"length": 2,
"nullable": true,
"value": [120, null, 140]
},
"accuracy": {
"type": "float",
"length": 4,
"nullable": false,
"value": [120, 130, 140]
},
"bearing": {
"type": "float",
"length": 8,
"nullable": false,
"value": [120, 130, 140]
},
"vesname": {
"type": "string",
"length": 20,
"nullable": true,
"value": ["dsff", "fgsd", null]
},
"active": {
"type": "timestamp",
"nullable": false,
"value": [
"Fri Jan 01 14:30:00 2010",
"Fri Jan 01 15:00:00 2010",
"Fri Jan 01 15:30:00 2010"
]
}
},
"events": [
{"1": "Fri Jan 01 14:30:00 2010"},
{"2": "Fri Jan 01 15:00:00 2010"},
{"3": "Fri Jan 01 15:30:00 2010"}
]
}leafcount:軌道ポイントの数です。空間ジオメトリオブジェクト内のポイント数と一致する必要があります。すべての属性フィールドは、同じ数の値を持つ必要があります。
attributes:フィールド定義および値のシーケンスを含む軌道属性です。leafcount を指定する場合は必須です。
属性名は最大 60 文字まで使用できます。
type:フィールドのデータの型です。有効な値はinteger、float、string、timestamp、およびboolです。length:フィールド値の長さです。typeによって有効な値が異なります:integer:1、2、4、または 8float:4 または 8string:要件に応じて任意の値。デフォルトは 64、最大は 253 です。長さは終端識別子を除く実際の文字数をカウントします。timestamp:固定値 8。設定できません。bool:固定値 1。設定できません。
nullable:フィールドが null 値を受け付けるかどうかです。有効な値はtrueおよびfalseです。デフォルト値はtrueです。value:フィールド値の JSON 配列です。nullを使用して null 要素を表します。
events:軌道イベントの JSON 配列です。各要素はキーと値のペアであり、キーはイベントタイプ、値はイベント時間です。
注意事項
時間補間:
timespan(構文 1)またはstart/end(構文 2)を指定すると、ST_makeTrajectoryは空間ポイントの数に基づいてタイムポイントを補間し、軌道のタイムラインを生成します。テーブルからの構築:テーブル行を軌道に集約するには、
array_agg(row(table.*))を使用します(構文 5)。カスタム構文:組み込みの構文が要件を満たさない場合、最初の 6 つの固定パラメーターの後に追加パラメーターを指定してカスタム関数を作成します:
CREATE OR REPLACE FUNCTION _ST_MakeTrajectory( type leaftype, x float8[], y float8[], srid integer, timespan timestamp[], attrs_name cstring[], attr1 float8[], attr2 float4[], attr3 timestamp[] ) RETURNS trajectory AS '$libdir/libpg-trajectory-x.y', 'sqltr_traj_make_all_array' LANGUAGE 'c' IMMUTABLE PARALLEL SAFE;x.yを Ganos のバージョン番号に置き換えます。たとえば、Ganos 4.5 の場合はlibpg-trajectory-4.5を使用します。ST_Version()を呼び出して、現在の Ganos バージョンを照会できます。
例
構文 1 — 時間範囲から軌道を構築します。tsrange を指定すると、関数は空間ポイントにわたってタイムポイントを補間します。
SELECT ST_MakeTrajectory(
'STPOINT'::leaftype,
ST_GeomFromText('LINESTRING (114 35, 115 36, 116 37)', 4326),
'[2010-01-01 14:30, 2010-01-01 15:30)'::tsrange,
'{"leafcount":3,"attributes":{"velocity":{"type":"integer","length":2,"nullable":true,"value":[120,130,140]},"accuracy":{"type":"float","length":4,"nullable":false,"value":[120,130,140]},"bearing":{"type":"float","length":8,"nullable":false,"value":[120,130,140]},"vesname":{"type":"string","length":20,"nullable":true,"value":["adsf","sdf","sdfff"]},"active":{"type":"timestamp","nullable":false,"value":["Fri Jan 01 14:30:00 2010","Fri Jan 01 15:00:00 2010","Fri Jan 01 15:30:00 2010"]}},"events":[{"1":"Fri Jan 01 14:30:00 2010"},{"2":"Fri Jan 01 15:00:00 2010"},{"3":"Fri Jan 01 15:30:00 2010"}]}'
);出力:
{"trajectory":{"version":1,"type":"STPOINT","leafcount":3,"start_time":"2010-01-01 14:30:00","end_time":"2010-01-01 15:30:00","spatial":"SRID=4326;LINESTRING(114 35,115 36,116 37)","timeline":["2010-01-01 14:30:00","2010-01-01 15:00:00","2010-01-01 15:30:00"],"attributes":{"leafcount":3,"velocity":{"type":"integer","length":2,"nullable":true,"value":[120,130,140]},"accuracy":{"type":"float","length":4,"nullable":false,"value":[120.0,130.0,140.0]},"bearing":{"type":"float","length":8,"nullable":false,"value":[120.0,130.0,140.0]},"vesname":{"type":"string","length":20,"nullable":true,"value":["adsf","sdf","sdfff"]},"active":{"type":"timestamp","length":8,"nullable":false,"value":["2010-01-01 14:30:00","2010-01-01 15:00:00","2010-01-01 15:30:00"]}},"events":[{"1":"2010-01-01 14:30:00"},{"2":"2010-01-01 15:00:00"},{"3":"2010-01-01 15:30:00"}]}}構文 2 — 明示的な開始および終了タイムスタンプから軌道を構築します:
SELECT ST_MakeTrajectory(
'STPOINT'::leaftype,
ST_GeomFromText('LINESTRING (114 35, 115 36, 116 37)', 4326),
'2010-01-01 14:30'::timestamp,
'2010-01-01 15:30'::timestamp,
'{"leafcount":3,"attributes":{"velocity":{"type":"integer","length":2,"nullable":true,"value":[120,130,140]},"accuracy":{"type":"float","length":4,"nullable":false,"value":[120,130,140]},"bearing":{"type":"float","length":8,"nullable":false,"value":[120,130,140]},"vesname":{"type":"string","length":20,"nullable":true,"value":["adsf","sdf","sdfff"]},"active":{"type":"timestamp","nullable":false,"value":["Fri Jan 01 14:30:00 2010","Fri Jan 01 15:00:00 2010","Fri Jan 01 15:30:00 2010"]}},"events":[{"1":"Fri Jan 01 14:30:00 2010"},{"2":"Fri Jan 01 15:00:00 2010"},{"3":"Fri Jan 01 15:30:00 2010"}]}'
);構文 3 — 明示的なタイムスタンプ配列を使用した軌道の構築:
SELECT ST_MakeTrajectory(
'STPOINT'::leaftype,
ST_GeomFromText('LINESTRING (114 35, 115 36, 116 37)', 4326),
ARRAY['2010-01-01 14:30'::timestamp, '2010-01-01 15:00'::timestamp, '2010-01-01 15:30'::timestamp],
'{"leafcount":3,"attributes":{"velocity":{"type":"integer","length":2,"nullable":true,"value":[120,130,140]},"accuracy":{"type":"float","length":4,"nullable":false,"value":[120,130,140]},"bearing":{"type":"float","length":8,"nullable":false,"value":[120,130,140]},"vesname":{"type":"string","length":20,"nullable":true,"value":["adsf","sdf","sdfff"]},"active":{"type":"timestamp","nullable":false,"value":["Fri Jan 01 14:30:00 2010","Fri Jan 01 15:00:00 2010","Fri Jan 01 15:30:00 2010"]}},"events":[{"1":"Fri Jan 01 14:30:00 2010"},{"2":"Fri Jan 01 15:00:00 2010"},{"3":"Fri Jan 01 15:30:00 2010"}]}'
);属性なしの構文 1 — null を attrs_json に渡すことで、属性データを省略できます:
SELECT ST_MakeTrajectory(
'STPOINT'::leaftype,
ST_GeomFromText('LINESTRING (114 35, 115 36, 116 37)', 4326),
'[2010-01-01 14:30, 2010-01-01 15:30)'::tsrange,
null
);出力:
{"trajectory":{"leafsize":3,"starttime":"Fri Jan 01 14:30:00 2010","endtime":"Fri Jan 01 15:30:00 2010","spatial":"LINESTRING(114 35,115 36,116 37)","timeline":["Fri Jan 01 14:30:00 2010","Fri Jan 01 15:00:00 2010","Fri Jan 01 15:30:00 2010"]}}構文 4 — 座標配列から軌道を構築します:
SELECT ST_MakeTrajectory(
'STPOINT'::leaftype,
ARRAY[1::float8],
ARRAY[2::float8],
4326,
ARRAY['2010-01-01 11:30'::timestamp],
ARRAY['velocity'],
ARRAY[1::int4],
NULL,
NULL,
NULL::anyarray
);出力:
{"trajectory":{"version":1,"type":"STPOINT","leafcount":1,"start_time":"2010-01-01 11:30:00","end_time":"2010-01-01 11:30:00","spatial":"SRID=4326;POINT(1 2)","timeline":["2010-01-01 11:30:00"],"attributes":{"leafcount":1,"velocity":{"type":"integer","length":4,"nullable":true,"value":[1]}}}}構文 5 — array_agg を使用してテーブル行から軌道を構築します:
-- テーブルを作成し、サンプル行を挿入します
CREATE TABLE tjrows (t timestamp, x double precision, y double precision, id int, attr text);
INSERT INTO tjrows VALUES
('2000-01-01 10:00:00', 3, 5, 1, 'the first point'),
('2000-01-01 11:00:00', 4, 6, 2, 'the second point'),
('2000-01-01 11:05:00', 5, 7, 3, 'the third point');
-- 軌道を構築します。最初の列はタイムスタンプ、2 列目および 3 列目は X/Y 座標です。
SELECT ST_MakeTrajectory(array_agg(row(tjrows.*)), false, '{"id","attr"}'::cstring[])
FROM tjrows;出力:
{"trajectory":{"version":1,"type":"STPOINT","leafcount":3,"start_time":"2000-01-01 10:00:00","end_time":"2000-01-01 11:05:00","spatial":"LINESTRING(3 5,4 6,5 7)","timeline":["2000-01-01 10:00:00","2000-01-01 11:00:00","2000-01-01 11:05:00"],"attributes":{"leafcount":3,"id":{"type":"integer","length":4,"nullable":true,"value":[1,2,3]},"attr":{"type":"string","length":64,"nullable":true,"value":["the first point","the second point","the third point"]}}}}