Name

ST_ClosestPointOfApproach — 返回两条轨迹最接近点处的测量值。

Synopsis

float8 ST_ClosestPointOfApproach(geometry track1, geometry track2);

描述

返回沿着给定轨迹插值的点之间距离最短的最小度量值。

输入必须是经 ST_IsValidTrajectory 检查的有效轨迹。 如果轨迹在其 M 范围内不重叠,则返回 Null。

要获取计算得到的度量值上的实际点,请使用 ST_LocateAlong

可用性:2.2.0

该函数支持 3d 并且不会丢失 z-index。

示例

-- Return the time in which two objects moving between 10:00 and 11:00
-- are closest to each other and their distance at that point
WITH inp AS ( SELECT
  ST_AddMeasure('LINESTRING Z (0 0 0, 10 0 5)'::geometry,
    extract(epoch from '2015-05-26 10:00'::timestamptz),
    extract(epoch from '2015-05-26 11:00'::timestamptz)
  ) a,
  ST_AddMeasure('LINESTRING Z (0 2 10, 12 1 2)'::geometry,
    extract(epoch from '2015-05-26 10:00'::timestamptz),
    extract(epoch from '2015-05-26 11:00'::timestamptz)
  ) b
), cpa AS (
  SELECT ST_ClosestPointOfApproach(a,b) m FROM inp
), points AS (
  SELECT ST_GeometryN(ST_LocateAlong(a,m),1) pa,
         ST_GeometryN(ST_LocateAlong(b,m),1) pb
  FROM inp, cpa
)
SELECT to_timestamp(m) t,
       ST_Distance(pa,pb) distance,
       ST_AsText(pa, 2) AS pa, ST_AsText(pb, 2) AS pb
FROM points, cpa;

               t               |      distance      |                  pa                  |                   pb                   
-------------------------------+--------------------+--------------------------------------+----------------------------------------
 2015-05-26 10:45:31.034483-07 | 1.9603683315139542 | POINT ZM (7.59 0 3.79 1432662331.03) | POINT ZM (9.1 1.24 3.93 1432662331.03)