Return the smallest distance ever
{geo,tpoint} |=| {geo,tpoint} → float
SELECT tgeompoint '[Point(0 0)@2001-01-02, Point(1 1)@2001-01-04, Point(0 0)@2001-01-06)' |=| geometry 'Linestring(2 2,2 1,3 1)'; -- 1 SELECT tgeompoint '[Point(0 0)@2001-01-01, Point(1 1)@2001-01-03, Point(0 0)@2001-01-05)' |=| tgeompoint '[Point(2 0)@2001-01-02, Point(1 1)@2001-01-04, Point(2 2)@2001-01-06)'; -- 0.5 SELECT tgeompoint '[Point(0 0 0)@2001-01-01, Point(1 1 1)@2001-01-03, Point(0 0 0)@2001-01-05)' |=| tgeompoint '[Point(2 0 0)@2001-01-02, Point(1 1 1)@2001-01-04, Point(2 2 2)@2001-01-06)'; -- 0.5 SELECT tgeompoint 'Interp=Step;(Point(1 1)@2001-01-01, Point(3 1)@2001-01-03]' |=| geometry 'Linestring(1 3,2 2,3 3)'; -- 1.4142135623731
The operator |=|
can be used for doing nearest neighbor searches using a GiST or an SP-GiST index (see the section called “Indexing”). This operator corresponds to the PostGIS function ST_DistanceCPA
, altough the latter requires both arguments to be a trajectory.
SELECT ST_DistanceCPA( tgeompoint '[Point(0 0 0)@2001-01-01, Point(1 1 1)@2001-01-03, Point(0 0 0)@2001-01-05)'::geometry, tgeompoint '[Point(2 0 0)@2001-01-02, Point(1 1 1)@2001-01-04, Point(2 2 2)@2001-01-06)'::geometry); -- 0.5
Return the instant of the first temporal point at which the two arguments are at the nearest distance
nearestApproachInstant({geo,tpoint},{geo,tpoint}) → tpoint
The function will only return the first instant that it finds if there are more than one. The resulting instant may be at an exclusive bound.
SELECT asText(NearestApproachInstant(tgeompoint '(Point(1 1)@2001-01-01, Point(3 1)@2001-01-03]', geometry 'Linestring(1 3,2 2,3 3)')); -- POINT(2 1)@2001-01-02 SELECT asText(NearestApproachInstant(tgeompoint 'Interp=Step;(Point(1 1)@2001-01-01, Point(3 1)@2001-01-03]', geometry 'Linestring(1 3,2 2,3 3)')); -- POINT(1 1)@2001-01-01 SELECT asText(NearestApproachInstant(tgeompoint '(Point(1 1)@2001-01-01, Point(2 2)@2001-01-03]', tgeompoint '(Point(1 1)@2001-01-01, Point(4 1)@2001-01-03]')); -- POINT(1 1)@2001-01-01 SELECT asText(nearestApproachInstant(tgeompoint '[Point(0 0 0)@2001-01-01, Point(1 1 1)@2001-01-03, Point(0 0 0)@2001-01-05)', tgeompoint '[Point(2 0 0)@2001-01-02, Point(1 1 1)@2001-01-04, Point(2 2 2)@2001-01-06)')); -- POINT Z (0.75 0.75 0.75)@2001-01-03 12:00:00+00
Function nearestApproachInstant
generalizes the PostGIS function ST_ClosestPointOfApproach
. First, the latter function requires both arguments to be trajectories. Second, function nearestApproachInstant
returns both the point and the timestamp of the nearest point of approach while the PostGIS function only provides the timestamp as shown next.
SELECT to_timestamp(ST_ClosestPointOfApproach( tgeompoint '[Point(0 0 0)@2001-01-01, Point(1 1 1)@2001-01-03, Point(0 0 0)@2001-01-05)'::geometry, tgeompoint '[Point(2 0 0)@2001-01-02, Point(1 1 1)@2001-01-04, Point(2 2 2)@2001-01-06)'::geometry)); -- 2001-01-03 12:00:00+00
Return the line connecting the nearest approach point
shortestLine({geo,tpoint},{geo,tpoint}) → geo
The function will only return the first line that it finds if there are more than one.
SELECT ST_AsText(shortestLine(tgeompoint '(Point(1 1)@2001-01-01, Point(3 1)@2001-01-03]', geometry 'Linestring(1 3,2 2,3 3)')); -- LINESTRING(2 1,2 2) SELECT ST_AsText(shortestLine(tgeompoint 'Interp=Step;(Point(1 1)@2001-01-01, Point(3 1)@2001-01-03]', geometry 'Linestring(1 3,2 2,3 3)')); -- LINESTRING(1 1,2 2) SELECT ST_AsText(shortestLine( tgeompoint '[Point(0 0 0)@2001-01-01, Point(1 1 1)@2001-01-03, Point(0 0 0)@2001-01-05)', tgeompoint '[Point(2 0 0)@2001-01-02, Point(1 1 1)@2001-01-04, Point(2 2 2)@2001-01-06)')); -- LINESTRING Z (0.75 0.75 0.75,1.25 0.75 0.75)
Function shortestLine
can be used to obtain the result provided by the PostGIS function ST_CPAWithin
when both arguments are trajectories as shown next.
SELECT ST_Length(shortestLine( tgeompoint '[Point(0 0 0)@2001-01-01, Point(1 1 1)@2001-01-03, Point(0 0 0)@2001-01-05)', tgeompoint '[Point(2 0 0)@2001-01-02, Point(1 1 1)@2001-01-04, Point(2 2 2)@2001-01-06)')) <= 0.5; -- true SELECT ST_CPAWithin( tgeompoint '[Point(0 0 0)@2001-01-01, Point(1 1 1)@2001-01-03, Point(0 0 0)@2001-01-05)'::geometry, tgeompoint '[Point(2 0 0)@2001-01-02, Point(1 1 1)@2001-01-04, Point(2 2 2)@2001-01-06)'::geometry, 0.5); -- true
The temporal distance operator, denoted <->
, computes the distance at each instant of the intersection of the temporal extents of their arguments and results in a temporal float. Computing temporal distance is useful in many mobility applications. For example, a moving cluster (also known as convoy or flock) is defined as a set of objects that move close to each other for a long time interval. This requires to compute temporal distance between two moving objects.
The temporal distance operator accepts a geometry/geography restricted to a point or a temporal point as arguments. Notice that the temporal types only consider linear interpolation between values, while the distance is a root of a quadratic function. Therefore, the temporal distance operator gives a linear approximation of the actual distance value for temporal sequence points. In this case, the arguments are synchronized in the time dimension, and for each of the composing line segments of the arguments, the spatial distance between the start point, the end point, and the nearest point of approach is computed, as shown in the examples below.
Return the temporal distance
{point,tpoint} <-> {point,tpoint} → tfloat
SELECT tgeompoint '[Point(0 0)@2001-01-01, Point(1 1)@2001-01-03)' <-> geometry 'Point(0 1)'; -- [1@2001-01-01, 0.707106781186548@2001-01-02, 1@2001-01-03) SELECT tgeompoint '[Point(0 0)@2001-01-01, Point(1 1)@2001-01-03)' <-> tgeompoint '[Point(0 1)@2001-01-01, Point(1 0)@2001-01-03)'; -- [1@2001-01-01, 0@2001-01-02, 1@2001-01-03) SELECT tgeompoint '[Point(0 1)@2001-01-01, Point(0 0)@2001-01-03)' <-> tgeompoint '[Point(0 0)@2001-01-01, Point(1 0)@2001-01-03)'; -- [1@2001-01-01, 0.707106781186548@2001-01-02, 1@2001-01-03) SELECT tgeompoint '[Point(0 0)@2001-01-01, Point(1 1)@2001-01-02)' <-> tgeompoint '[Point(0 1)@2001-01-01, Point(1 2)@2001-01-02)'; -- [1@2001-01-01,1@2001-01-02)