This topic describes the ST_JaccardSimilarity function, which calculates the Jaccard index based on two trajectories or sub-trajectories.

Syntax

record ST_JaccardSimilarity(trajectory tr1, trajectory tr2, double tol_dist, 
                            text unit default '{}', interval tol_time default NULL, 
                            timestamp ts default '-infinity', timestamp te default 'infinity') ; 

Parameters

Parameter Description
tr1 The trajectory numbered 1.
tr2 The trajectory numbered 2.
tol_dist The tolerance that is allowed for the distance between a pair of trajectory points. Unit: meters.
unit The JSON string that specifies how to calculate the distance.
tol_time The tolerance that is allowed for the time difference between a pair of trajectory points. The default value is NULL. If you set this parameter to NULL or a negative value, this function calculates the Jaccard index based only on the distance and does not consider the time difference.
ts The start of the time range for the calculation. The default value is -infinity. If you specify this parameter, this function calculates only the sub-trajectories between the specified start time and end time.
te The end of the time range for the calculation. The default value is infinity. If you specify this parameter, this function calculates only the sub-trajectories between the specified start time and end time.

The following table describes the fields in the unit parameter.

Field Type Default value Description
Projection string None The coordinate system to which the specified trajectories or sub-trajectories are re-projected. Valid values:
  • auto: This function dynamically selects the most suitable coordinate system based on the longitude and latitude coordinates of the specified trajectories or sub-trajectories. Then, this function re-projects the specified trajectories or sub-trajectories to the selected coordinate system. The supported coordinate systems include Lambert Azimuthal and UTM. You do not need to specify the Unit parameter again. The unit is meters.
  • srid: This function re-projects the specified trajectories or sub-trajectories based on the spatial reference identifier (SRID) that is specified by this parameter.
Note If you do not specify this parameter, this function performs the calculation based on the original coordinate system.
Unit string null The unit that is used to measure the distance. Valid values:
  • null: No units are specified. This function calculates the Euclidean distance between each pair of trajectory points based on the coordinates of these trajectory points.
  • M: This function calculates the distance between each pair of trajectory points based on the unit that is used for the spatial reference of the specified trajectories or sub-trajectories. In most cases, the distance is measured in the unit of meters.
useSpheroid bool true Specifies whether to use an ellipsoid. You can specify this parameter if you set the Unit parameter to M. Valid values:
  • true: An ellipsoid is used. This function can obtain accurate distances.
  • false: A sphere that represents the earth is used. This function can obtain approximate distances.

The following table describes the response parameters.

Parameter Type Description
nleaf1 int The number of trajectory points where Trajectory 1 intersects with Trajectory 2.
nleaf2 int The number of trajectory points where Trajectory 2 intersects with Trajectory 1.
Note The return value of this parameter may differ from that of the nleaf1 parameter. Assume that Trajectory 1 passes the same trajectory point on Trajectory 2 twice. In this case, the return value of the nleaf1 parameter is 1, and the return value of the nleaf2 parameter is 2.
inter1 int The number of trajectory points between which the distance from Trajectory 1 to Trajectory 2 meets both the specified time and distance tolerances.
inter2 int The number of trajectory points between which the distance from Trajectory 2 to Trajectory 1 meets both the specified time and distance tolerances.
jaccard_lower double The minimum distance that is calculated based on the Jaccard index. The value of this parameter is calculated based on the following formula: Minimum distance = inter/(nleaf1 + nleaf2 - inter).
Note In the preceding formula, the value of the inter parameter is the smaller value between the inter1 and inter2 parameters.
jaccard_upper double The maximum distance that is calculated based on the Jaccard index. The value of this parameter is calculated based on the following formula: Maximum distance = inter/(nleaf1 + nleaf2 - inter).
Note In the preceding formula, the value of the inter parameter is the larger one between the inter1 and inter2 parameters.

Description

The Jaccard index of two sets is the number of elements in the intersection of these sets divided by the number of elements in the union of these sets. For two trajectories, the definition of the Jaccard index is extended to calculate the number of trajectory points where Trajectory 1 intersects with Trajectory 2, the number of trajectory points where Trajectory 2 intersects with Trajectory 1, and the distance between each pair of trajectory points. The distances are calculated based on the preceding formulas that are used to calculate the values of the jaccard_lower and jaccard_upper parameters.

Example

With traj as(
SELECT ST_makeTrajectory('STPOINT'::leaftype, 'SRID=4326;LINESTRING(114.49211 37.97921,114.49211 37.97921,114.49211 37.97921,114.49211 37.97921)'::geometry,
    ARRAY[to_timestamp(1590287775) AT TIME ZONE 'UTC', to_timestamp(1590287778) AT TIME ZONE 'UTC', to_timestamp(1590302169) AT TIME ZONE 'UTC',to_timestamp(1590302171) AT TIME ZONE 'UTC'], '{}') a,
    ST_makeTrajectory('STPOINT'::leaftype, 'SRID=4326;LINESTRING(114.49211 37.97921,114.49211 37.97921,114.49211 37.97921,114.49211 37.97921,114.49145 37.97781,114.49145 37.97781,114.49145 37.97781,114.49145 37.97781,114.49145 37.97781,114.49145 37.97781,114.49145 37.97781,114.49145 37.97781,114.49145 37.97781,114.49145 37.97781,114.49211 37.97921,114.49211 37.97921,114.49211 37.97921,114.49211 37.97921,114.49211 37.97921,114.49211 37.97921)'::geometry,
    ARRAY[ to_timestamp(1590287765) AT TIME ZONE 'UTC', to_timestamp(1590287771) AT TIME ZONE 'UTC', to_timestamp(1590287778) AT TIME ZONE 'UTC', to_timestamp(1590287780) AT TIME ZONE 'UTC', to_timestamp(1590295992) AT TIME ZONE 'UTC', to_timestamp(1590295997) AT TIME ZONE 'UTC', to_timestamp(1590296013) AT TIME ZONE 'UTC', to_timestamp(1590296018) AT TIME ZONE 'UTC',  to_timestamp(1590296025) AT TIME ZONE 'UTC', to_timestamp(1590296032) AT TIME ZONE 'UTC', to_timestamp(1590296055) AT TIME ZONE 'UTC', to_timestamp(1590296073) AT TIME ZONE 'UTC', to_timestamp(1590296081) AT TIME ZONE 'UTC', to_timestamp(1590296081) AT TIME ZONE 'UTC', to_timestamp(1590302169) AT TIME ZONE 'UTC', to_timestamp(1590302174) AT TIME ZONE 'UTC',  to_timestamp(1590302176) AT TIME ZONE 'UTC', to_timestamp(1590302176) AT TIME ZONE 'UTC', to_timestamp(1590302172) AT TIME ZONE 'UTC', to_timestamp(1590302176) AT TIME ZONE 'UTC'],'{}') b
)
select ST_JaccardSimilarity(a,b, 100,'{"unit":"M"}','20 second', '2020-05-23'::timestamptz AT TIME ZONE 'UTC', '2020-05-26'::timestamptz AT TIME ZONE 'UTC') from traj;
       st_jaccardsimilarity        
-----------------------------------
 (4,20,4,10,0.2,0.714285714285714)
(1 row)