PostGIS  2.5.7dev-r@@SVN_REVISION@@
lwgeom_functions_temporal.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright (C) 2015 Sandro Santilli <strk@kbt.io>
22  *
23  **********************************************************************/
24 
25 
26 #include "postgres.h"
27 #include "fmgr.h"
28 #include "utils/elog.h"
29 #include "utils/array.h"
30 #include "utils/geo_decls.h"
31 
32 #include "../postgis_config.h"
33 #include "liblwgeom.h"
34 #include "lwgeom_pg.h"
35 
36 #include <math.h>
37 #include <float.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <errno.h>
41 
42 
43 /*
44  * Return the measure at which interpolated points on the two
45  * input lines are at the smallest distance.
46  */
47 Datum ST_ClosestPointOfApproach(PG_FUNCTION_ARGS);
49 Datum ST_ClosestPointOfApproach(PG_FUNCTION_ARGS)
50 {
51  GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
52  GSERIALIZED *gs1 = PG_GETARG_GSERIALIZED_P(1);
53  /* All checks already performed by liblwgeom, not worth checking again */
56  double m = lwgeom_tcpa(g0, g1, NULL);
57  lwgeom_free(g0);
58  lwgeom_free(g1);
59  PG_FREE_IF_COPY(gs0, 0);
60  PG_FREE_IF_COPY(gs1, 1);
61  if ( m < 0 ) PG_RETURN_NULL();
62  PG_RETURN_FLOAT8(m);
63 }
64 
65 /*
66  * Does the object correctly model a trajectory ?
67  * Must be a LINESTRINGM with growing measures
68  */
69 Datum ST_IsValidTrajectory(PG_FUNCTION_ARGS);
71 Datum ST_IsValidTrajectory(PG_FUNCTION_ARGS)
72 {
73  GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
74  /* All checks already performed by liblwgeom, not worth checking again */
76  int ret = lwgeom_is_trajectory(g0);
77  lwgeom_free(g0);
78  PG_RETURN_BOOL(ret == LW_TRUE);
79 }
80 
81 /*
82  * Return the distance between two trajectories at their
83  * closest point of approach.
84  */
85 Datum ST_DistanceCPA(PG_FUNCTION_ARGS);
87 Datum ST_DistanceCPA(PG_FUNCTION_ARGS)
88 {
89  GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
90  GSERIALIZED *gs1 = PG_GETARG_GSERIALIZED_P(1);
91  /* All checks already performed by liblwgeom, not worth checking again */
94  double mindist;
95  double m = lwgeom_tcpa(g0, g1, &mindist);
96  lwgeom_free(g0);
97  lwgeom_free(g1);
98  PG_FREE_IF_COPY(gs0, 0);
99  PG_FREE_IF_COPY(gs1, 1);
100  if ( m < 0 ) PG_RETURN_NULL();
101  PG_RETURN_FLOAT8(mindist);
102 }
103 
104 /*
105  * Return true if the distance between two trajectories at their
106  * closest point of approach is within the given max.
107  */
108 Datum ST_CPAWithin(PG_FUNCTION_ARGS);
110 Datum ST_CPAWithin(PG_FUNCTION_ARGS)
111 {
112  GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
113  GSERIALIZED *gs1 = PG_GETARG_GSERIALIZED_P(1);
114  double maxdist = PG_GETARG_FLOAT8(2);
115  /* All checks already performed by liblwgeom, not worth checking again */
116  LWGEOM *g0 = lwgeom_from_gserialized(gs0);
117  LWGEOM *g1 = lwgeom_from_gserialized(gs1);
118  int ret = lwgeom_cpa_within(g0, g1, maxdist);
119  lwgeom_free(g0);
120  lwgeom_free(g1);
121  PG_FREE_IF_COPY(gs0, 0);
122  PG_FREE_IF_COPY(gs1, 1);
123  PG_RETURN_BOOL( ret == LW_TRUE );
124 }
125 
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
int lwgeom_is_trajectory(const LWGEOM *geom)
Return LW_TRUE or LW_FALSE depending on whether or not a geometry is a linestring with measure value ...
Definition: lwgeom.c:2472
double lwgeom_tcpa(const LWGEOM *g1, const LWGEOM *g2, double *mindist)
Find the time of closest point of approach.
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
int lwgeom_cpa_within(const LWGEOM *g1, const LWGEOM *g2, double maxdist)
Is the closest point of approach within a distance ?
This library is the generic geometry handling section of PostGIS.
Datum ST_ClosestPointOfApproach(PG_FUNCTION_ARGS)
Datum ST_DistanceCPA(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(ST_ClosestPointOfApproach)
Datum ST_IsValidTrajectory(PG_FUNCTION_ARGS)
Datum ST_CPAWithin(PG_FUNCTION_ARGS)