ST_trajAttrsAsText

更新时间:
复制 MD 格式

Returns all values of a string-type attribute field from a trajectory object as a text[] array, one element per leaf point in timeline order. NULL values in the attribute are preserved as NULL elements in the returned array.

This function casts attribute values of any type to text. For type-safe retrieval, use the corresponding typed function instead — for example, ST_trajAttrsAsFloat8 for float attributes or ST_trajAttrsAsBool for bool attributes.

Syntax

text[] ST_trajAttrsAsText(trajectory traj, text attr_name)

Parameters

ParameterDescription
trajThe trajectory object.
attr_nameThe name of the attribute field.

Examples

The following examples use a two-point trajectory with an angle attribute (string type), a tngel2 attribute (timestamp type), and a bearing attribute (bool type).

Example 1: Retrieve a string attribute

WITH traj AS (
  SELECT '{"trajectory":{"version":1,"type":"STPOINT","leafcount":2,
    "start_time":"2010-01-01 11:30:00","end_time":"2010-01-01 12:30:00",
    "spatial":"SRID=4326;LINESTRING(1 1,3 5)",
    "timeline":["2010-01-01 11:30:00","2010-01-01 12:30:00"],
    "attributes":{"leafcount":2,
      "angle":{"type":"string","length":64,"nullable":true,"value":["test",null]}
    }}}'::trajectory a
)
SELECT ST_trajAttrsAsText(a, 'angle') FROM traj;

Output:

 st_trajattrsastext
--------------------
 {test,NULL}
(1 row)

The first leaf point has the value "test" and the second is null, so the result is {test,NULL}.

Example 2: Retrieve a timestamp attribute as text

ST_trajAttrsAsText casts all attribute types to text. When applied to a timestamp attribute, each timestamp value is returned as its text representation.

WITH traj AS (
  SELECT '{"trajectory":{"version":1,"type":"STPOINT","leafcount":2,
    "start_time":"2010-01-01 11:30:00","end_time":"2010-01-01 12:30:00",
    "spatial":"SRID=4326;LINESTRING(1 1,3 5)",
    "timeline":["2010-01-01 11:30:00","2010-01-01 12:30:00"],
    "attributes":{"leafcount":2,
      "tngel2":{"type":"timestamp","length":8,"nullable":true,"value":["2010-01-01 12:30:00",null]}
    }}}'::trajectory a
)
SELECT ST_trajAttrsAsText(a, 'tngel2') FROM traj;

Output:

      st_trajattrsastext
------------------------------
 {"2010-01-01 12:30:00",NULL}
(1 row)

Example 3: Retrieve a boolean attribute as text

When applied to a bool attribute, true is returned as t and false as f.

WITH traj AS (
  SELECT '{"trajectory":{"version":1,"type":"STPOINT","leafcount":2,
    "start_time":"2010-01-01 11:30:00","end_time":"2010-01-01 12:30:00",
    "spatial":"SRID=4326;LINESTRING(1 1,3 5)",
    "timeline":["2010-01-01 11:30:00","2010-01-01 12:30:00"],
    "attributes":{"leafcount":2,
      "bearing":{"type":"bool","length":1,"nullable":true,"value":[null,true]}
    }}}'::trajectory a
)
SELECT ST_trajAttrsAsText(a, 'bearing') FROM traj;

Output:

 st_trajattrsastext
--------------------
 {NULL,t}
(1 row)