ST_makeTrajectory

更新时间:
复制 MD 格式

Constructs a trajectory object from spatial geometry data and a time sequence.

Syntax

ST_makeTrajectory has five variants. Choose the one that matches how your time data is structured.

Syntax 1: Time range

Use when you have a start and end time and want the function to interpolate intermediate timestamps.

trajectory ST_makeTrajectory(leaftype type, geometry spatial, tsrange timespan, cstring attrs_json);

Syntax 2: Start and end timestamps

Use when you have explicit start and end timestamps and want the function to interpolate intermediate timestamps.

trajectory ST_makeTrajectory(leaftype type, geometry spatial, timestamp start, timestamp end, cstring attrs_json);

Syntax 3: Timestamp array

Use when you have a precise timestamp for each trajectory point.

trajectory ST_makeTrajectory(leaftype type, geometry spatial, timestamp[] timeline, cstring attrs_json);

Syntax 4: Coordinate arrays

Use when your spatial data is stored as separate x and y coordinate arrays rather than a geometry object.

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: Table rows

Use when trajectory points are stored in a database table. Pass rows using array_agg(row(table.*)).

trajectory ST_makeTrajectory(anyarray rows, bool hasz, cstring[] attrnames);

Parameters

Common parameters (Syntaxes 1–3)

ParameterTypeRequiredDescription
typeleaftypeYesTrajectory type. Set to ST_POINT.
spatialgeometryYesSpatial geometry of the trajectory. Accepts a LineString or Point object.
timespantsrangeYes (Syntax 1)Time range of the trajectory as a closed interval that includes both endpoints.
starttimestampYes (Syntax 2)Start timestamp of the trajectory.
endtimestampYes (Syntax 2)End timestamp of the trajectory.
timelinetimestamp[]Yes (Syntax 3)Array of timestamps, one per point in the LineString. The number of timestamps must equal the number of spatial points.
attrs_jsoncstringNoTrajectory attributes and events in JSON format. Accepts null.
When you use Syntax 1 or Syntax 2, ST_makeTrajectory interpolates intermediate timestamps based on the number of spatial points in the geometry.

Parameters for Syntax 4

ParameterTypeRequiredDescription
typeleaftypeYesTrajectory type. Set to ST_POINT.
xfloat8[]Yesx-coordinates of the trajectory points.
yfloat8[]Yesy-coordinates of the trajectory points.
sridintegerYesSpatial reference system identifier (SRID) of the trajectory.
timelinetimestamp[]YesTimestamps for the trajectory points.
attr_field_namestext[]YesNames of all attribute fields.
attr_int4int4[]NoAttribute values of type int4.
attr_float8float8[]NoAttribute values of type float8.
attr_cstringtext[]NoAttribute values of type cstring.
attr_anyanyarrayNoAttribute values of any other type.

Parameters for Syntax 5

ParameterTypeRequiredDescription
rowsanyarrayYesAggregated table rows. The first column must be TIMESTAMP, the second and third columns must be FLOAT8. If hasz is true, the fourth column must also be FLOAT8.
haszbooleanYesWhether to build a three-dimensional trajectory. true: 3D trajectory (the fourth column is used as the z-coordinate). false: 2D trajectory (the fourth column is treated as an attribute).
attrnamescstring[]NoNames of the trajectory attributes. Defaults to attr1, attr2, ... if not specified.

The attrs_json parameter

The attrs_json parameter defines trajectory attributes and events using the following JSON structure:

{
  "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: The total number of trajectory points. This value must match the number of spatial points in the geometry object and the number of values in each attribute field's value array.

attributes: Defines the attribute fields attached to each trajectory point. Required when leafcount is specified. Each attribute field has the following properties:

PropertyDescription
Field nameUp to 60 characters.
typeData type of the field. Valid values: integer, float, string, timestamp, bool.
lengthStorage length of the field value. Valid values depend on type: integer (1, 2, 4, 8); float (4, 8); string (default 64, maximum 253; length = number of characters, excluding the null terminator); timestamp (fixed at 8, cannot be set); bool (fixed at 1, cannot be set).
nullableWhether the field accepts null values. Valid values: true, false. Default: true.
valueJSON array of field values, one per trajectory point. Use null to represent a missing value.

events: A JSON array of key-value pairs that record events along the trajectory. Each pair maps an event type (key) to the time the event occurred (value).

Examples

Syntax 1: Time range

The function interpolates three evenly spaced timestamps between the start and end time, one for each point in the LineString.

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"}]}'
);

The output is a trajectory JSON object with the interpolated timeline and the attribute values you provided:

{"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"],...}}

Syntax 2: Start and end timestamps

Pass separate start and end timestamps instead of a tsrange. The function interpolates timestamps the same way as Syntax 1.

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"}]}'
);

Syntax 3: Timestamp array

Provide an explicit timestamp for each point. The number of timestamps must equal the number of points in the LineString.

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"}]}'
);

Syntax 1 with no attributes

Pass null as attrs_json to create a trajectory with no attribute fields.

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
);

Syntax 4: Coordinate arrays

Build a single-point trajectory from x and y coordinate arrays, with a velocity attribute stored as int4.

SELECT ST_MakeTrajectory(
  'STPOINT'::leaftype,
  ARRAY[1::float8],   -- x-coordinates
  ARRAY[2::float8],   -- y-coordinates
  4326,               -- SRID
  ARRAY['2010-01-01 11:30'::timestamp],
  ARRAY['velocity'],  -- attribute field names
  ARRAY[1::int4],     -- int4 attribute values
  NULL,               -- float8 attribute values
  NULL,               -- cstring attribute values
  NULL::anyarray      -- other attribute values
);

Syntax 5: Table rows

Convert rows from a database table into a trajectory object. This is useful for transforming historical point records into trajectory objects in bulk.

-- Create a sample table with timestamp and coordinate columns
CREATE TABLE location_points (
  t  timestamp,
  x  double precision,
  y  double precision,
  id int,
  attr text
);

INSERT INTO location_points 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');

-- Build a 2D trajectory, treating the 4th column onward as attributes
SELECT ST_MakeTrajectory(
  array_agg(row(location_points.*)),
  false,                          -- 2D trajectory
  '{"id","attr"}'::cstring[]      -- attribute field names
)
FROM location_points;

The output trajectory includes the spatial geometry derived from the x and y columns and the id and attr columns as attribute fields.

Usage notes

  • To create a custom variant of ST_makeTrajectory that accepts different attribute types beyond those in Syntax 4, define a function that wraps sqltr_traj_make_all_array with the first six fixed parameters followed by your custom attribute parameters:

    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;

    Replace x.y with the GanosBase version. For example, for GanosBase 4.5, use libpg-trajectory-4.5. To check your GanosBase version, call ST_Version().Ganos

  • If you get a permissions error when creating a custom function, .