2 つの軌跡または部分軌跡間のジャカード指数を計算し、類似度スコアの下限と上限を返します。
構文
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');この関数は、以下のフィールドを含むレコードを返します:
| フィールド | 型 | 説明 |
|---|---|---|
nleaf1 | int | tr2 と交差する tr1 内の軌跡点の数。 |
nleaf2 | int | tr1 と交差する tr2 内の軌跡点の数。この値は nleaf1 と異なる場合があります。たとえば、tr1 の 2 つの異なる点が tr2 の同じ 1 つの点に近接している場合、nleaf1 は 2、nleaf2 は 1 になります。 |
inter1 | int | tr2 への距離が距離許容差と時間許容差の両方を満たす tr1 内の軌跡点の数。 |
inter2 | int | tr1 への距離が距離許容差と時間許容差の両方を満たす tr2 内の軌跡点の数。 |
jaccard_lower | double | ジャカード指数の下限。 min(inter1, inter2) / (nleaf1 + nleaf2 - min(inter1, inter2)) として計算されます。 |
jaccard_upper | double | ジャカード指数の上限。 max(inter1, inter2) / (nleaf1 + nleaf2 - max(inter1, inter2)) として計算されます。 |
パラメーター
| パラメーター | 型 | デフォルト | 説明 |
|---|---|---|---|
tr1 | trajectory | — | 1 つ目の軌跡。 |
tr2 | trajectory | — | 2 つ目の軌跡。 |
tol_dist | double | — | 一致する軌跡点のペア間で許容される最大距離 (単位:メートル)。 |
unit | text | '{}' | 距離の計算方法を制御する JSON 文字列。 詳細については、「unit パラメーターのフィールド」をご参照ください。 |
tol_time | interval | NULL | 一致する軌跡点のペア間で許容される最大時間差。 NULL または負の値の場合、この関数はタイムスタンプを無視し、距離のみに基づいて点を照合します。 |
ts | timestamp | -infinity | 時間範囲の開始時刻。指定した場合、この関数は ts と te の間の部分軌跡のみを比較します。 |
te | timestamp | infinity | 時間範囲の終了時刻。指定した場合、この関数は ts と te の間の部分軌跡のみを比較します。 |
unit パラメーターのフィールド
| フィールド | 型 | デフォルト | 説明 |
|---|---|---|---|
Projection | string | None | 軌跡の再投影先となる座標系。有効な値: auto (経度と緯度に基づいてランベルト正積方位図法または UTM を動的に選択します。距離の単位はメートルです)、srid (指定された空間参照系識別子 (SRID) を使用して再投影します)。省略した場合、計算には元の座標系が使用されます。 |
Unit | string | null | 距離測定の単位。有効な値: null (生の座標に基づくユークリッド距離)、M (軌跡の空間参照に基づく距離、通常はメートル)。 |
useSpheroid | bool | true | Unit が M の場合に楕円体モデルを使用するかどうかを指定します。 true:正確な距離を求めるために楕円体を使用します。 false:おおよその距離を求めるために球面を使用します。 |
仕組み
2 つの集合に対する古典的なジャッカード係数は、共通部分のサイズを和集合のサイズで割った値です。軌跡の場合、これは空間的 (およびオプションで時間的) なマッチングに拡張されます。すなわち、tr2 内に tol_dist メートル以内 (また、tol_time が設定されている場合は、指定された時間枠内) の点が存在する場合に、tr1 内の軌跡上の点は tr2 と交差すると見なされます。
マッチング関係が対称的ではないこと、つまり tr1 が tr2 上のある点の近くを 1 回通過する場合と tr2 が同じ点の近くを 2 回通過する場合とでカウントが異なることから、この関数は単一の値ではなく 2 つの境界を返します。
jaccard_lower uses the smaller intersection count (
min(inter1, inter2)), giving a conservative estimate of overlap. Use this when a confirmed minimum level of similarity is required.jaccard_upper uses the larger intersection count (
max(inter1, inter2)), giving an optimistic estimate of overlap. Use this to capture the broadest possible match between the two trajectories.
どちらの値も、0 (オーバーラップなし) から 1 (完全なオーバーラップ) の範囲です。
Example
The following example compares two trajectories within a three-day time window, using a 100-meter distance tolerance and a 20-second time tolerance.
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;Output:
st_jaccardsimilarity
-----------------------------------
(4,20,4,10,0.2,0.714285714285714)
(1 row)The result maps to the return fields as follows:
| Field | Value | Meaning |
|---|---|---|
nleaf1 | 4 | 4 points in tr1 intersect with tr2 |
nleaf2 | 20 | 20 points in tr2 intersect with tr1 |
inter1 | 4 | 4 points in tr1 meet the distance and time tolerances |
inter2 | 10 | 10 points in tr2 meet the distance and time tolerances |
jaccard_lower | 0.2 | Conservative similarity: 4 / (4 + 20 - 4) |
jaccard_upper | 0.714... | Optimistic similarity: 10 / (4 + 20 - 10) |