PostGIS  2.2.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  * Copyright (C) 2015 Sandro Santilli <strk@keybit.net>
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU General Public Licence (version 2 or later).
10  * See the COPYING file.
11  *
12  **********************************************************************/
13 
14 #include "postgres.h"
15 #include "fmgr.h"
16 #include "utils/elog.h"
17 #include "utils/array.h"
18 #include "utils/geo_decls.h"
19 
20 #include "../postgis_config.h"
21 #include "liblwgeom.h"
22 #include "lwgeom_pg.h"
23 
24 #include <math.h>
25 #include <float.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <errno.h>
29 
30 
31 /*
32  * Return the measure at which interpolated points on the two
33  * input lines are at the smallest distance.
34  */
35 Datum ST_ClosestPointOfApproach(PG_FUNCTION_ARGS);
37 Datum ST_ClosestPointOfApproach(PG_FUNCTION_ARGS)
38 {
39  GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
40  GSERIALIZED *gs1 = PG_GETARG_GSERIALIZED_P(1);
41  /* All checks already performed by liblwgeom, not worth checking again */
44  double m = lwgeom_tcpa(g0, g1, NULL);
45  lwgeom_free(g0);
46  lwgeom_free(g1);
47  PG_FREE_IF_COPY(gs0, 0);
48  PG_FREE_IF_COPY(gs1, 1);
49  if ( m < 0 ) PG_RETURN_NULL();
50  PG_RETURN_FLOAT8(m);
51 }
52 
53 /*
54  * Does the object correctly model a trajectory ?
55  * Must be a LINESTRINGM with growing measures
56  */
57 Datum ST_IsValidTrajectory(PG_FUNCTION_ARGS);
59 Datum ST_IsValidTrajectory(PG_FUNCTION_ARGS)
60 {
61  GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
62  /* All checks already performed by liblwgeom, not worth checking again */
64  int ret = lwgeom_is_trajectory(g0);
65  lwgeom_free(g0);
66  PG_RETURN_BOOL(ret == LW_TRUE);
67 }
68 
69 /*
70  * Return the distance between two trajectories at their
71  * closest point of approach.
72  */
73 Datum ST_DistanceCPA(PG_FUNCTION_ARGS);
75 Datum ST_DistanceCPA(PG_FUNCTION_ARGS)
76 {
77  GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
78  GSERIALIZED *gs1 = PG_GETARG_GSERIALIZED_P(1);
79  /* All checks already performed by liblwgeom, not worth checking again */
82  double mindist;
83  double m = lwgeom_tcpa(g0, g1, &mindist);
84  lwgeom_free(g0);
85  lwgeom_free(g1);
86  PG_FREE_IF_COPY(gs0, 0);
87  PG_FREE_IF_COPY(gs1, 1);
88  if ( m < 0 ) PG_RETURN_NULL();
89  PG_RETURN_FLOAT8(mindist);
90 }
91 
92 /*
93  * Return true if the distance between two trajectories at their
94  * closest point of approach is within the given max.
95  */
96 Datum ST_CPAWithin(PG_FUNCTION_ARGS);
98 Datum ST_CPAWithin(PG_FUNCTION_ARGS)
99 {
100  GSERIALIZED *gs0 = PG_GETARG_GSERIALIZED_P(0);
101  GSERIALIZED *gs1 = PG_GETARG_GSERIALIZED_P(1);
102  double maxdist = PG_GETARG_FLOAT8(2);
103  /* All checks already performed by liblwgeom, not worth checking again */
104  LWGEOM *g0 = lwgeom_from_gserialized(gs0);
105  LWGEOM *g1 = lwgeom_from_gserialized(gs1);
106  int ret = lwgeom_cpa_within(g0, g1, maxdist);
107  lwgeom_free(g0);
108  lwgeom_free(g1);
109  PG_FREE_IF_COPY(gs0, 0);
110  PG_FREE_IF_COPY(gs1, 1);
111  PG_RETURN_BOOL( ret == LW_TRUE );
112 }
113 
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:2034
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
PG_FUNCTION_INFO_V1(ST_ClosestPointOfApproach)
int lwgeom_cpa_within(const LWGEOM *g1, const LWGEOM *g2, double maxdist)
Is the closest point of approach within a distance ?
Datum ST_CPAWithin(PG_FUNCTION_ARGS)
Datum ST_ClosestPointOfApproach(PG_FUNCTION_ARGS)
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:61
Datum ST_DistanceCPA(PG_FUNCTION_ARGS)
double lwgeom_tcpa(const LWGEOM *g1, const LWGEOM *g2, double *mindist)
Find the time of closest point of approach.
Datum ST_IsValidTrajectory(PG_FUNCTION_ARGS)
This library is the generic geometry handling section of PostGIS.