PostGIS  3.0.6dev-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 
41 /*
42  * Return the measure at which interpolated points on the two
43  * input lines are at the smallest distance.
44  */
45 Datum ST_ClosestPointOfApproach(PG_FUNCTION_ARGS);
47 Datum ST_ClosestPointOfApproach(PG_FUNCTION_ARGS)
48 {
49  GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
50  GSERIALIZED *gs1 = PG_GETARG_GSERIALIZED_P(1);
51  /* All checks already performed by liblwgeom, not worth checking again */
54  double m = lwgeom_tcpa(g0, g1, NULL);
55  lwgeom_free(g0);
56  lwgeom_free(g1);
57  PG_FREE_IF_COPY(gs0, 0);
58  PG_FREE_IF_COPY(gs1, 1);
59  if ( m < 0 ) PG_RETURN_NULL();
60  PG_RETURN_FLOAT8(m);
61 }
62 
63 /*
64  * Does the object correctly model a trajectory ?
65  * Must be a LINESTRINGM with growing measures
66  */
67 Datum ST_IsValidTrajectory(PG_FUNCTION_ARGS);
69 Datum ST_IsValidTrajectory(PG_FUNCTION_ARGS)
70 {
71  GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
72  /* All checks already performed by liblwgeom, not worth checking again */
74  int ret = lwgeom_is_trajectory(g0);
75  lwgeom_free(g0);
76  PG_RETURN_BOOL(ret == LW_TRUE);
77 }
78 
79 /*
80  * Return the distance between two trajectories at their
81  * closest point of approach.
82  */
83 Datum ST_DistanceCPA(PG_FUNCTION_ARGS);
85 Datum ST_DistanceCPA(PG_FUNCTION_ARGS)
86 {
87  GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
88  GSERIALIZED *gs1 = PG_GETARG_GSERIALIZED_P(1);
89  /* All checks already performed by liblwgeom, not worth checking again */
92  double mindist;
93  double m = lwgeom_tcpa(g0, g1, &mindist);
94  lwgeom_free(g0);
95  lwgeom_free(g1);
96  PG_FREE_IF_COPY(gs0, 0);
97  PG_FREE_IF_COPY(gs1, 1);
98  if ( m < 0 ) PG_RETURN_NULL();
99  PG_RETURN_FLOAT8(mindist);
100 }
101 
102 /*
103  * Return true if the distance between two trajectories at their
104  * closest point of approach is within the given max.
105  */
106 Datum ST_CPAWithin(PG_FUNCTION_ARGS);
108 Datum ST_CPAWithin(PG_FUNCTION_ARGS)
109 {
110  GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
111  GSERIALIZED *gs1 = PG_GETARG_GSERIALIZED_P(1);
112  double maxdist = PG_GETARG_FLOAT8(2);
113  /* All checks already performed by liblwgeom, not worth checking again */
114  LWGEOM *g0 = lwgeom_from_gserialized(gs0);
115  LWGEOM *g1 = lwgeom_from_gserialized(gs1);
116  int ret = lwgeom_cpa_within(g0, g1, maxdist);
117  lwgeom_free(g0);
118  lwgeom_free(g1);
119  PG_FREE_IF_COPY(gs0, 0);
120  PG_FREE_IF_COPY(gs1, 1);
121  PG_RETURN_BOOL( ret == LW_TRUE );
122 }
123 
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
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:2462
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:107
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)