Aggregate functions

Updated at:

Spatio-temporal aggregate functions sort out-of-order points and perform aggregate calculations. These functions are useful in scenarios that generate large amounts of spatio-temporal data and require trajectory analysis, such as the Internet of Vehicles (IoV), Geographic Information System (GIS), and the Internet of Things (IoT).

Engine and version

Important

Lindorm SQL must be version 2.6.8 or later. For more information, see SQL version guide.

Function list

The following table lists the aggregate functions that Lindorm Ganos supports.

Function

Description

ST_Length_Rows

Sorts out-of-order points by time and connects them to form a trajectory. This function calculates and returns the spherical length of the trajectory in meters (m).

ST_Resample

Sorts out-of-order points by time and downsamples them based on specified time and space thresholds to generate a trajectory.

ST_TrajectoryProfile

Sorts out-of-order points by time to form a trajectory, splits the trajectory into segments based on a time threshold, and returns the coordinates and time of the start and end points for each segment.

ST_Length_Rows

Sorts out-of-order points by time and connects them to form a trajectory. This function calculates and returns the spherical length of the trajectory in meters (m).

You can use the ST_Length_Rows function with the GROUP BY statement to return the spherical length of a trajectory formed by aggregating points in a specified column.

Important

You can use a primary key to ensure the order of the points. Otherwise, the calculated length may be incorrect. For example, in an IoV scenario, you can use the vehicle ID and a timestamp as the primary key to ensure the order of the points.

Syntax

BigDecimal ST_Length_Rows(point column)

Parameters

Parameter

Description

point column

The specified Geometry object. The type is POINT.

Examples

The queries in the examples are based on the following `CREATE TABLE` statement and sample data:

CREATE TABLE test_len (carid VARCHAR, collect_time TIMESTAMP, p GEOMETRY(POINT), PRIMARY KEY(carid, collect_time));

INSERT INTO test_len (carid, collect_time,p) VALUES('car1', '2023-09-12 00:04:02', ST_GeomFromText('POINT (111.40269 35.61695)')),
       ('car1', '2023-09-12 00:09:07', ST_GeomFromText('POINT (111.40127 35.616096)')),
       ('car1', '2023-09-12 00:14:03', ST_GeomFromText('POINT (111.400604 35.616013)')),
       ('car1', '2023-09-12 00:20:13', ST_GeomFromText('POINT (111.399734 35.613983)')),
       ('car1', '2023-09-12 00:27:21', ST_GeomFromText('POINT (111.40217 35.616386)')),
       ('car2', '2023-09-12 00:27:21', ST_GeomFromText('POINT (111.40217 35.616386)'));
  • Example 1: Calculate the spherical length of the trajectory formed by all points in column `p` where `carid` is `car1`.

    SELECT ST_LENGTH_ROWS(p) FROM test_len WHERE carid='car1';

    The following result is returned:

    +--------------------+
    | st_length_rows(p)  |
    +--------------------+
    | 805.55323541493414 |
    +--------------------+
  • Example 2: Calculate the spherical length of each trajectory after grouping and aggregating the points by `carid`.

    SELECT carid,ST_LENGTH_ROWS(p) as len FROM test_len WHERE carid<='car2' GROUP BY carid;

    The following result is returned:

    +-------+--------------------+
    | carid |        len         |
    +-------+--------------------+
    | car1  | 805.55323541493414 |
    | car2  | 0                  |
    +-------+--------------------+

ST_Resample

Sorts out-of-order points by time and downsamples them based on specified time and space thresholds to generate a trajectory.

The ST_Resample function requires a single trajectory object and a filter condition for the range of trajectory points. This function cannot be used with the GROUP BY statement.

Syntax

String ST_Resample(String geomColumnName, String timeColumnName, String config)
String ST_Resample(String xColumnName, String yColumnName, String timeColumnName, String config) 

Parameters

Parameter

Description

geomColumnName

The specified Geometry object. The type is POINT.

xColumnName

The longitude column. The type is DOUBLE. This parameter must be used with the yColumnName parameter.

yColumnName

The latitude column. The type is DOUBLE. This parameter must be used with the xColumnName parameter.

timeColumnName

The time column. Trajectory points are constructed in ascending order based on this time column. The supported types are TIME, TIMESTAMP, and LONG. The unit for the LONG type must be milliseconds (ms).

config

The downsampling parameters specified in JSON format. The type is STRING. The format is `{"parameter_name": value}`.

`config` includes the following optional parameters:

Parameter

Description

downsample_time

The time threshold for downsampling. After downsampling, the time interval between adjacent trajectory points is greater than this value.

The unit is milliseconds (ms). The default value is -1. The supported types are INT and LONG.

downsample_distance

The distance threshold for downsampling. This is the calculation threshold used when simplifying the geometry of the trajectory based on a specified simplification algorithm (vw, dp, or topologypreserving).

The unit is the same as the unit of the point coordinates. The default value is -1.0. The supported types are INT, LONG, FLOAT, and DOUBLE.

simplifier

The simplification algorithm used for downsampling. The type is STRING. The following options are supported:

  • vw: The Visvalingam-Whyatt algorithm.

  • dp: The Douglas-Peucker algorithm.

  • topologypreserving: A Douglas-Peucker algorithm that preserves the topology of the trajectory linestring.

    This parameter takes effect only when used with downsample_distance. The default value is topologypreserving.

Return value

Returns a sequence of trajectory points sorted by time after downsampling. The return value is a STRING in the following format: [{\"x\":longitude,\"y\":latitude,\"t\":time}, {\"x\":longitude,\"y\":latitude,\"t\":time}, ...].

Example

The query in this example is based on the following `CREATE TABLE` statement and sample data:

CREATE TABLE gps_points (account_id VARCHAR, collect_time TIMESTAMP, gps_point GEOMETRY(POINT), PRIMARY KEY(account_id, collect_time));

INSERT INTO gps_points(account_id, collect_time, gps_point) VALUES ('001', '2023-11-10 11:00:30', ST_MakePoint(113.665431, 34.773)),
       ('001', '2023-11-10 11:00:31', ST_MakePoint(113.665432, 34.773)),
       ('001', '2023-11-10 11:00:32', ST_MakePoint(113.665433, 34.773)),
       ('001', '2023-11-10 11:00:33', ST_MakePoint(113.665434, 34.774));

Query the `gps_points` table for the trajectory of a specified vehicle within a specified time range. Return the trajectory after it is downsampled using the Visvalingam-Whyatt algorithm with a distance threshold of 0.0001.

SELECT ST_Resample(gps_point, collect_time,'{"downsample_distance": 0.0001, "simplifier": "vw"}') as resampled_traj FROM gps_points WHERE collect_time >=  '2023-11-10 00:00:00' and collect_time <= '2023-11-11 00:00:00'  and account_id='001';

The following result is returned:

+----------------------------------------------------------------------------------------------------------------------+
|                                             resampled_traj                                                           |
+----------------------------------------------------------------------------------------------------------------------+
| [{"x":113.665431,"y":34.773,"t":"2023-11-10 11:00:30.0"},{"x":113.665434,"y":34.774,"t":"2023-11-10 11:00:33.0"}]                                           |
+----------------------------------------------------------------------------------------------------------------------+

ST_TrajectoryProfile

Sorts out-of-order points by time to form a trajectory, splits the trajectory into segments based on a time threshold, and returns the coordinates and time of the start and end points for each segment.

The ST_TrajectoryProfile function must be used with the GROUP BY statement to calculate the coordinates and time of the start and end points for each trajectory segment in each group after aggregation.

The ST_TrajectoryProfile function is also often used with spatial relationship functions, such as ST_DWithinSphere, to calculate the coordinates and time of the first point when a trajectory segment enters a specified area and the last point before it leaves.

Syntax

String ST_TrajectoryProfile(String geomColumnName, String timeColumnName,long thresh)
String ST_TrajectoryProfile(String xColumnName, String yColumnName, String timeColumnName,long thresh)

Parameters

Parameter

Description

geomColumnName

The specified Geometry object. The type is POINT.

Note

Use this parameter if you store point data as the Point type.

xColumnName

The longitude column. The type is DOUBLE. This parameter must be used with the yColumnName parameter.

Note

Use this parameter if you store point data in separate longitude and latitude columns.

yColumnName

The latitude column. The type is DOUBLE. This parameter must be used with the xColumnName parameter.

Note

Use this parameter if you store point data in separate longitude and latitude columns.

timeColumnName

The time column. Trajectory points are constructed in ascending order based on this time column.

thresh

Optional. The time threshold in milliseconds (ms). After the points are sorted by time, if the time interval between two adjacent trajectory points is less than this threshold, they belong to the same trajectory segment. If the interval is greater than this threshold, they belong to different segments. The default value is 10000 ms.

Return value

Returns a JSON STRING in the following format:

{"Trajectory Segment ID":"{\"endY\":End Latitude,\"endX\":End Longitude,\"startY\":Start Latitude,\"startTime\":Start Time,\"startX\":Start Longitude,\"endTime\":End Time}"}.

Example

The query in this example is based on the following `CREATE TABLE` statement and sample data:

CREATE TABLE test_traj (carid VARCHAR, collect_time TIMESTAMP, p GEOMETRY(POINT), status VARCHAR, PRIMARY KEY(z-order(p), carid, collect_time));

INSERT INTO test_traj (carid, collect_time, p, status) VALUES('car1', '2023-09-12 00:04:02', ST_GeomFromText('POINT (111.40269 35.61695)'), 'normal'),
    ('car1', '2023-09-12 00:09:07', ST_GeomFromText('POINT (111.40127 35.616096)'), 'normal'),
    ('car1', '2023-09-12 00:14:03', ST_GeomFromText('POINT (111.400604 35.616013)'), 'normal'),
    ('car1', '2023-09-12 00:20:13', ST_GeomFromText('POINT (111.399734 35.613983)'), 'normal'),
    ('car1', '2023-09-12 00:27:21', ST_GeomFromText('POINT (111.40217 35.616386)'), 'normal'),
    ('car2', '2023-09-12 00:27:21', ST_GeomFromText('POINT (111.40217 35.616386)'), 'normal');

Query the `test_traj` table for the entry and exit point coordinates and times of trajectory segments collected by each device within a specified time and space range.

SELECT carid, st_trajectoryprofile(p, collect_time, 30000) as trajprofile FROM test_traj WHERE st_dwithinsphere(st_geomfromtext('POINT (111.40217 35.616386)'), p, 100.0) and collect_time >= '2023-09-12 00:09:07' and collect_time <= '2023-09-20 00:09:07' group by carid;
Note

If the query uses a secondary index, you can add all columns involved in the query to the index as included columns. This includes the aggregation key, filter condition columns, and function parameter columns. This practice avoids lookups in the primary table. For example, in the preceding query, the secondary index must include the carid, p, and collect_time columns.

The following result is returned:

+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| carid |                                                                                                                                          trajprofile                                                                                                                                          |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| car1  | {"0":"{\"endY\":35.616096,\"endX\":111.40127,\"startY\":35.616096,\"startTime\":1694448547000,\"startX\":111.40127,\"endTime\":1694448547000}","1":"{\"endY\":35.616386,\"endX\":111.40217,\"startY\":35.616386,\"startTime\":1694449641000,\"startX\":111.40217,\"endTime\":1694449641000}"} |
| car2  | {"0":"{\"endY\":35.616386,\"endX\":111.40217,\"startY\":35.616386,\"startTime\":1694449641000,\"startX\":111.40217,\"endTime\":1694449641000}"}                                                                                                                                               |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+