- #3139, ST_BoundingDiagonal (Sandro Santilli / Boundless)
- #3129, ST_IsValidTrajectory (Sandro Santilli / Boundless)
- #3128, ST_ClosestPointOfApproach (Sandro Santilli / Boundless)
+ - #3152, ST_DistanceCPA (Sandro Santilli / Boundless)
- Canonical output for index key types
- ST_SwapOrdinates (Sandro Santilli / Boundless)
- #2918, Use GeographicLib functions for geodetics (Mike Toews)
<title>See Also</title>
<para>
<xref linkend="ST_IsValidTrajectory" />,
+<xref linkend="ST_DistanceCPA" />,
<xref linkend="ST_LocateAlong" />,
+<xref linkend="ST_AddMeasure" />
+ </para>
+ </refsection>
+ </refentry>
+
+ <refentry id="ST_DistanceCPA">
+
+ <refnamediv>
+ <refname>ST_DistanceCPA</refname>
+ <refpurpose>
+Returns the distance between closest points of approach in two trajectories.
+ </refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+ <funcsynopsis>
+ <funcprototype>
+ <funcdef>float8 <function>ST_DistanceCPA</function></funcdef>
+ <paramdef><type>geometry </type> <parameter>track1</parameter></paramdef>
+ <paramdef><type>geometry </type> <parameter>track2</parameter></paramdef>
+ </funcprototype>
+ </funcsynopsis>
+ </refsynopsisdiv>
+
+ <refsection>
+ <title>Description</title>
+
+ <para>
+Returns the minimum distance two moving objects have ever been each-other.
+Inputs must be valid trajectories as checked by
+<xref linkend="ST_IsValidTrajectory" />.
+Null is returned if the trajectories do not overlap on the M range.
+ </para>
+
+ <para>Availability: 2.2.0</para>
+ <para>&Z_support;</para>
+ </refsection>
+
+
+ <refsection>
+ <title>Examples</title>
+<programlisting>
+-- Return the minimum distance of two objects moving between 10:00 and 11:00
+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
+)
+SELECT ST_DistanceCPA(a,b) distance FROM inp;
+
+ distance
+------------------
+ 1.96036833151395
+</programlisting>
+ </refsection>
+
+ <!-- Optionally add a "See Also" section -->
+ <refsection>
+ <title>See Also</title>
+ <para>
+<xref linkend="ST_IsValidTrajectory" />,
+<xref linkend="ST_ClosestPointOfApproach" />,
<xref linkend="ST_AddMeasure" />
</para>
</refsection>
lwgeom_free(g0);
PG_RETURN_BOOL(ret == LW_TRUE);
}
+
+/*
+ * Return the distance between two trajectories at their
+ * closest point of approach.
+ */
+Datum ST_DistanceCPA(PG_FUNCTION_ARGS);
+PG_FUNCTION_INFO_V1(ST_DistanceCPA);
+Datum ST_DistanceCPA(PG_FUNCTION_ARGS)
+{
+ GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
+ GSERIALIZED *gs1 = PG_GETARG_GSERIALIZED_P(1);
+ /* All checks already performed by liblwgeom, not worth checking again */
+ LWGEOM *g0 = lwgeom_from_gserialized(gs0);
+ LWGEOM *g1 = lwgeom_from_gserialized(gs1);
+ double mindist;
+ double m = lwgeom_tcpa(g0, g1, &mindist);
+ lwgeom_free(g0);
+ lwgeom_free(g1);
+ PG_FREE_IF_COPY(gs0, 0);
+ PG_FREE_IF_COPY(gs1, 1);
+ PG_RETURN_FLOAT8(mindist);
+}
+
+
AS 'MODULE_PATHNAME', 'ST_ClosestPointOfApproach'
LANGUAGE 'c' IMMUTABLE STRICT;
+-- Availability: 2.2.0
+CREATE OR REPLACE FUNCTION ST_DistanceCPA(geometry, geometry)
+ RETURNS float8
+ AS 'MODULE_PATHNAME', 'gserialized_distance_cpa' -- ST_DistanceCPA
+ LANGUAGE 'c' IMMUTABLE STRICT;
+
-- Availability: 2.2.0
CREATE OR REPLACE FUNCTION ST_IsValidTrajectory(geometry)
RETURNS bool
SELECT 'cpa#3136',
ST_ClosestPointOfApproach(g2,g1), ST_ClosestPointOfApproach(g1,g2)
FROM inp;
+
+----------------------------------------
+--
+-- ST_DistanceCPA
+--
+----------------------------------------
+
+SELECT 'cpad1', ST_DistanceCPA('LINESTRINGM(0 0 0, 1 0 1)'::geometry
+ ,'LINESTRINGM(0 0 0, 1 0 1)'::geometry);
+SELECT 'cpad2', ST_DistanceCPA('LINESTRINGM(0 0 0, 1 0 1)'::geometry
+ ,'LINESTRINGM(0 0 1, 1 0 2)'::geometry);
+SELECT 'cpad3', ST_DistanceCPA('LINESTRING(0 0 0 0, 1 0 0 1)'::geometry
+ ,'LINESTRING(0 0 3 0, 1 0 2 1)'::geometry);
+SELECT 'invalid', ST_DistanceCPA('LINESTRING(0 0 0, 1 0 0)'::geometry
+ ,'LINESTRING(0 0 3 0, 1 0 2 1)'::geometry);
cpa4|10
cpa5|
cpa#3136|80000002|80000002
+cpad1|0
+cpad2|1
+cpad3|2
+ERROR: Both input geometries must have a measure dimension