ST_trajAttrsMeanMax

更新时间:
复制 MD 格式

Returns the maximum average value of a trajectory attribute field using the MEAN-MAX algorithm.

Syntax

SETOF record ST_trajAttrsMeanMax(trajectory traj, cstring attr_field_name, out interval duration, out float8 max);

Parameters

ParameterDescription
trajThe trajectory object.
attr_field_nameThe name of the attribute field to evaluate.

Return value

Returns a set of records. Each record contains the following output fields:

FieldTypeDescription
durationintervalThe sliding window size (time range).
maxfloat8The maximum of all average values computed across windows of this size.

Description

The MEAN-MAX algorithm applies a sliding window across the trajectory timeline. For each window size, it computes the average value of the specified attribute field within every window of that size, then returns the maximum of those averages.

MEAN-MAX algorithm

Constraints:

  • Supports integer and float attribute field types only.

  • The specified attribute field cannot contain null values.

Examples

The following examples use a trajectory with four STPOINT observations at one-hour intervals, with velocity values of 120.0, 130.0, 140.0, and 120.0.

Example 1: Return results as a composite type

With traj AS (
    Select ST_makeTrajectory('STPOINT', 'LINESTRING(1 1, 6 6, 9 8, 10 12)'::geometry,
    ARRAY['2010-01-01 11:30'::timestamp, '2010-01-01 12:30', '2010-01-01 13:30', '2010-01-01 14:30'],
    '{"leafcount":4, "attributes":{"velocity": {"type": "float", "length": 8,"nullable": true,"value": [120.0, 130.0, 140.0, 120.0]}, "power": {"type": "float", "length": 4,"nullable": true,"value": [120.0, 130.0, 140.0, 120.0]}}}') a)
Select st_trajAttrsMeanMax(a, 'velocity') from traj;
 st_trajattrsmeanmax
---------------------
 ("@ 1 hour",135)
 ("@ 2 hours",130)
 ("@ 3 hours",127.5)
(3 rows)

Example 2: Expand output into separate columns

With traj AS (
    Select ST_makeTrajectory('STPOINT', 'LINESTRING(1 1, 6 6, 9 8, 10 12)'::geometry,
    ARRAY['2010-01-01 11:30'::timestamp, '2010-01-01 12:30', '2010-01-01 13:30', '2010-01-01 14:30'],
    '{"leafcount":4, "attributes":{"velocity": {"type": "float", "length": 8,"nullable": true,"value": [120.0, 130.0, 140.0, 120.0]}, "power": {"type": "float", "length": 4,"nullable": true,"value": [120.0, 130.0, 140.0, 120.0]}}}') a)
Select (st_trajAttrsMeanMax(a, 'velocity')).* from traj;
 duration |  max
----------+-------
 01:00:00 |   135
 02:00:00 |   130
 03:00:00 | 127.5
(3 rows)

The output shows one row per window size. For a 1-hour window, the maximum average velocity is 135; for a 2-hour window, 130; for a 3-hour window (spanning the full trajectory), 127.5.