ST_makeTrajectory
Constructs a trajectory object.
Syntax
Syntax 1
trajectory ST_makeTrajectory(leaftype type, geometry spatial, tsrange timespan, cstring attrs_json);Syntax 2
trajectory ST_makeTrajectory(leaftype type, geometry spatial, timestamp start, timestamp end, cstring attrs_json);Syntax 3
trajectory ST_makeTrajectory(leaftype type, geometry spatial, timestamp[] timeline, cstring attrs_json);Syntax 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);Syntax 5
trajectory ST_makeTrajectory(anyarray rows, bool hasz, cstring[] attrnames);
Parameters
Parameter | Description |
type | The type of the trajectory. Currently, only ST_POINT is supported. |
spatial | The trajectory spatial object based on the LineString or Point type. |
timespan | A tsrange that contains the start time and the end time. |
start | The start time of the trajectory. |
end | The end time of the trajectory. |
timeline | The timeline of the trajectory. The number of time points must be the same as the number of points in the LineString. |
attrs_json | The trajectory attributes and events in JSON format. This parameter can be set to null. |
attr_field_names | The names of all fields that represent the trajectory attributes (array). |
x | The x coordinates used to construct the geometry object (array). |
y | The y coordinates used to construct the geometry object (array). |
srid | The spatial reference identifier, which must exist. |
rows | The table used to represent the trajectory. The first column must be of the TimeStamp type, and the second and third columns must be of the float8 type. |
hasz | Specifies whether the trajectory is three-dimensional.
|
attrnames | The names of the attributes in the trajectory. If you do not specify this parameter, the default value is |
The format of attrs_json is as follows:
{ "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" } ] }leafcountThe number of trajectory points contained in the trajectory. The value must be the same as the number of spatial points in the spatial object. The value is also the number of element values contained in each attribute field. All attribute fields must contain the same number of elements.
attributesThe attribute items, which contain the field definitions and value sequences of all attributes. This parameter must exist at the same time as leafcount. Attribute definitions and requirements:
An attribute name can contain up to 60 characters.
type: the field type. The five data types integer, float, string, timestamp, and bool are supported.
length: the field length:
integer supports a length of 1, 2, 4, or 8.
float supports a length of 4 or 8.
string supports a custom length. If you do not specify the length, the default length is 64. The maximum length is 253. The length value is the actual number of characters and does not include the end identifier.
The length of timestamp is optional. The default value is 8.
The length of bool is optional. The default value is 1.
nullable: whether the field accepts null values. true allows null values, and false does not. The default value is true.
value: the sequence of field values, expressed as a JSON array. A null element value is expressed as null.
eventsThe trajectory events. Multiple events are expressed as a JSON array. Each array element is expressed as a JSON "key:value" pair, in which the key is the event type and the value is the event time.
If the time parameter that you pass is timespan or start and end, time points are interpolated based on the number of points in spatial.
You can aggregate the rows of a table and convert them into a trajectory by using the function
array_agg(row(table.*)). For more information, see Syntax 5.If none of the preceding syntax items meet your requirements, you can customize the MakeTrajectory function. The first six parameters are fixed, and the subsequent parameters can be customized based on your business requirements:
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;NoteIn the example,
x.yis the Ganos version, which can be viewed by running ST_Version. For example, for version 4.5, the value islibpg-trajectory-4.5.If you get a permissions error when creating a custom function, submit a ticket.
Examples
-- (1) ST_MakeTrajectory with timestamp range
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"}]}'); st_maketrajectory
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"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"}]}}
(1 row)
-- (2) ST_MakeTrajectory with start timestamp and end timestamp
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"}]}'); st_maketrajectory
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"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"}]}}
(1 row)
-- (3) ST_MakeTrajectory with timestamp array
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"}]}'); st_maketrajectory
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"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"}]}}
(1 row)
-- (4) json is null
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);
st_maketrajectory
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"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"]}}
(1 row)
-- (5) ST_MakeTrajectory make from points
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);
st_maketrajectory
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"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]}}}}
(1 row)
-- (6) ST_MakeTrajectory from table
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');
select ST_MakeTrajectory(array_agg(row(tjrows.*)), false, '{"id","attr"}'::cstring[]) from tjrows;
st_maketrajectory
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------
{"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,"valu
e":["the first point","the second point","the third point"]}}}}
(1 row)