Inserts a new attribute into a trajectory at a specified position.
Syntax
trajectory ST_InsertAttr(trajectory traj, anyarray arr, text name, integer loc default -1);Return value
The modified trajectory as a trajectory value, with the new attribute inserted at the specified position.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
traj | trajectory | — | The original trajectory. |
arr | anyarray | — | An array of values for the new attribute. |
name | text | — | The name of the new attribute. |
loc | integer | -1 | The position at which to insert the new attribute. Valid values: [-n, n], where n is the number of existing attributes. When omitted, the attribute is appended after the rightmost existing attribute (equivalent to loc = -1). |
Usage notes
Positive index (
0ton): Counts from the left.0inserts before the first attribute;ninserts after the nth attribute from the left.Negative index (
-1to-n): Counts from the right.-1appends after the last attribute;-ninserts after the leftmost attribute.
Examples
Append an attribute to the end (default behavior)
Omitting loc is equivalent to loc = -1, which appends the new attribute after all existing attributes.
With traj as (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]}, "acceleration": {"type": "string", "length": 20, "nullable" : true,"value": ["120", "130", "140"]}, "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"]}}}') as tj)
SELECT ST_InsertAttr(tj, ARRAY[1, 4, 6], 'add') from traj;Insert an attribute after the 3rd attribute from the left
With traj as (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]}, "acceleration": {"type": "string", "length": 20, "nullable" : true,"value": ["120", "130", "140"]}, "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"]}}}') as tj)
SELECT ST_InsertAttr(tj, ARRAY[1, 4, 6], 'add', 3) from traj;Result:
{"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]},"add":{"type":"integer","length":4,"nullable":true,"value":[1,4,6]},"acceleration":{"type":"string","length":20,"nullable":true,"value":["120","130","140"]},"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"]}}}}Insert an attribute before the first attribute
Use loc = 0 to place the new attribute before all existing attributes.
With traj as (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]}, "acceleration": {"type": "string", "length": 20, "nullable" : true,"value": ["120", "130", "140"]}, "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"]}}}') as tj)
SELECT ST_InsertAttr(tj, ARRAY[1, 4, 6], 'add', 0) from traj;What's next
ST_AppendAttr: Append an attribute to the end of a trajectory.
ST_SetAttr: Update the values of an existing trajectory attribute.
ST_DeleteAttr: Remove an attribute from a trajectory.