ST_ClosestPointOfApproach
What is ST_ClosestPointOfApproach?
ST_ClosestPointOfApproach is a PostGIS function that returns the M value at which two measured trajectories are closest to each other in 3D space. M typically encodes time, so the result tells you when the two objects came closest.
ST_ClosestPointOfApproach(geometry trajectory_a, geometry trajectory_b) → floatBoth inputs must be valid trajectories (see ST_IsValidTrajectory). The function returns NULL if the trajectories do not overlap in M (time).
When would you use ST_ClosestPointOfApproach?
Use ST_ClosestPointOfApproach (CPA) in air, maritime, or robotics domains to compute when two moving objects came closest. Common applications include detecting near-miss events between aircraft, assessing collision risk between ships, or analysing proximity between drones in shared airspace.
1SELECT a.id, b.id,
2 ST_ClosestPointOfApproach(a.track, b.track) AS cpa_time
3FROM vessels a, vessels b
4WHERE a.id < b.id
5 AND ST_ClosestPointOfApproach(a.track, b.track) IS NOT NULL;FAQs
What does the returned M mean?
It is the shared measure (typically a timestamp) at which the two trajectories achieve their minimum 3D separation. Convert back to a timestamp by reversing whatever encoding you used for M (epoch seconds, hours-since-midnight, etc.).
What if the trajectories do not overlap in time?
The function returns NULL. CPA is only meaningful over the intersection of the two trajectories' M ranges — if one vessel has already left port before another arrives, there is no CPA.
Does ST_ClosestPointOfApproach use 3D distance?
Yes. If the trajectories have Z, the CPA considers full 3D distance. For 2D trajectories, Z is treated as 0 and the CPA reduces to planar distance.
How is it related to ST_DistanceCPA and ST_CPAWithin?
ST_ClosestPointOfApproach returns the when. ST_DistanceCPA returns the how close — the minimum separation at the CPA moment. ST_CPAWithin returns a boolean: whether the CPA distance is within a given threshold. They are the three building blocks of proximity analysis for moving objects.