PostGIS  2.4.9dev-r@@SVN_REVISION@@
geography_measurement_trees.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^
22  *
23  **********************************************************************/
24 
26 
27 
28 /*
29 * Specific tree types include all the generic slots and
30 * their own slots for their trees. We put the implementation
31 * for the CircTreeGeomCache here because we can't shove
32 * the PgSQL specific bits of the code (fcinfo) back into
33 * liblwgeom, where most of the circtree logic lives.
34 */
35 typedef struct {
36  int type; // <GeomCache>
39  size_t geom1_size; //
40  size_t geom2_size; //
41  int32 argnum; // </GeomCache>
44 
45 
46 
50 static int
51 CircTreeBuilder(const LWGEOM* lwgeom, GeomCache* cache)
52 {
53  CircTreeGeomCache* circ_cache = (CircTreeGeomCache*)cache;
54  CIRC_NODE* tree = lwgeom_calculate_circ_tree(lwgeom);
55 
56  if ( circ_cache->index )
57  {
58  circ_tree_free(circ_cache->index);
59  circ_cache->index = 0;
60  }
61  if ( ! tree )
62  return LW_FAILURE;
63 
64  circ_cache->index = tree;
65  return LW_SUCCESS;
66 }
67 
68 static int
69 CircTreeFreer(GeomCache* cache)
70 {
71  CircTreeGeomCache* circ_cache = (CircTreeGeomCache*)cache;
72  if ( circ_cache->index )
73  {
74  circ_tree_free(circ_cache->index);
75  circ_cache->index = 0;
76  circ_cache->argnum = 0;
77  }
78  return LW_SUCCESS;
79 }
80 
81 static GeomCache*
83 {
84  CircTreeGeomCache* cache = palloc(sizeof(CircTreeGeomCache));
85  memset(cache, 0, sizeof(CircTreeGeomCache));
86  return (GeomCache*)cache;
87 }
88 
89 static GeomCacheMethods CircTreeCacheMethods =
90 {
91  CIRC_CACHE_ENTRY,
95 };
96 
97 static CircTreeGeomCache *
98 GetCircTreeGeomCache(FunctionCallInfo fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2)
99 {
100  return (CircTreeGeomCache*)GetGeomCache(fcinfo, &CircTreeCacheMethods, g1, g2);
101 }
102 
103 static int
104 CircTreePIP(const CIRC_NODE* tree1, const GSERIALIZED* g1, const POINT4D* in_point)
105 {
106  int tree1_type = gserialized_get_type(g1);
107  GBOX gbox1;
108  GEOGRAPHIC_POINT in_gpoint;
109  POINT3D in_point3d;
110 
111  POSTGIS_DEBUGF(3, "tree1_type=%d", tree1_type);
112 
113  /* If the tree'ed argument is a polygon, do the P-i-P using the tree-based P-i-P */
114  if ( tree1_type == POLYGONTYPE || tree1_type == MULTIPOLYGONTYPE )
115  {
116  POSTGIS_DEBUG(3, "tree is a polygon, using tree PiP");
117  /* Need a gbox to calculate an outside point */
118  if ( LW_FAILURE == gserialized_get_gbox_p(g1, &gbox1) )
119  {
120  LWGEOM* lwgeom1 = lwgeom_from_gserialized(g1);
121  POSTGIS_DEBUG(3, "unable to read gbox from gserialized, calculating from scratch");
122  lwgeom_calculate_gbox_geodetic(lwgeom1, &gbox1);
123  lwgeom_free(lwgeom1);
124  }
125 
126  /* Flip the candidate point into geographics */
127  geographic_point_init(in_point->x, in_point->y, &in_gpoint);
128  geog2cart(&in_gpoint, &in_point3d);
129 
130  /* If the candidate isn't in the tree box, it's not in the tree area */
131  if ( ! gbox_contains_point3d(&gbox1, &in_point3d) )
132  {
133  POSTGIS_DEBUG(3, "in_point3d is not inside the tree gbox, CircTreePIP returning FALSE");
134  return LW_FALSE;
135  }
136  /* The candidate point is in the box, so it *might* be inside the tree */
137  else
138  {
139  POINT2D pt2d_outside; /* latlon */
140  POINT2D pt2d_inside;
141  pt2d_inside.x = in_point->x;
142  pt2d_inside.y = in_point->y;
143  /* Calculate a definitive outside point */
144  if (gbox_pt_outside(&gbox1, &pt2d_outside) == LW_FAILURE)
145  if (circ_tree_get_point_outside(tree1, &pt2d_outside) == LW_FAILURE)
146  lwpgerror("%s: Unable to generate outside point!", __func__);
147 
148  POSTGIS_DEBUGF(3, "p2d_inside=POINT(%g %g) p2d_outside=POINT(%g %g)", pt2d_inside.x, pt2d_inside.y, pt2d_outside.x, pt2d_outside.y);
149  /* Test the candidate point for strict containment */
150  POSTGIS_DEBUG(3, "calling circ_tree_contains_point for PiP test");
151  return circ_tree_contains_point(tree1, &pt2d_inside, &pt2d_outside, 0, NULL);
152  }
153  }
154  else
155  {
156  POSTGIS_DEBUG(3, "tree1 not polygonal, so CircTreePIP returning FALSE");
157  return LW_FALSE;
158  }
159 }
160 
161 static int
162 geography_distance_cache_tolerance(FunctionCallInfo fcinfo,
163  const GSERIALIZED *g1,
164  const GSERIALIZED *g2,
165  const SPHEROID *s,
166  double tolerance,
167  double *distance)
168 {
169  CircTreeGeomCache* tree_cache = NULL;
170 
171  int type1 = gserialized_get_type(g1);
172  int type2 = gserialized_get_type(g2);
173 
174  Assert(distance);
175 
176  /* Two points? Get outa here... */
177  if ( type1 == POINTTYPE && type2 == POINTTYPE )
178  return LW_FAILURE;
179 
180  /* Fetch/build our cache, if appropriate, etc... */
181  tree_cache = GetCircTreeGeomCache(fcinfo, g1, g2);
182 
183  /* OK, we have an index at the ready! Use it for the one tree argument and */
184  /* fill in the other tree argument */
185  if ( tree_cache && tree_cache->argnum && tree_cache->index )
186  {
187  CIRC_NODE* circtree_cached = tree_cache->index;
188  CIRC_NODE* circtree = NULL;
189  const GSERIALIZED* g_cached;
190  const GSERIALIZED* g;
191  LWGEOM* lwgeom = NULL;
192  int geomtype_cached;
193  int geomtype;
194  POINT4D p4d;
195 
196  /* We need to dynamically build a tree for the uncached side of the function call */
197  if ( tree_cache->argnum == 1 )
198  {
199  g_cached = g1;
200  g = g2;
201  geomtype_cached = type1;
202  geomtype = type2;
203  }
204  else if ( tree_cache->argnum == 2 )
205  {
206  g_cached = g2;
207  g = g1;
208  geomtype_cached = type2;
209  geomtype = type1;
210  }
211  else
212  {
213  lwpgerror("geography_distance_cache this cannot happen!");
214  return LW_FAILURE;
215  }
216 
217  lwgeom = lwgeom_from_gserialized(g);
218  if ( geomtype_cached == POLYGONTYPE || geomtype_cached == MULTIPOLYGONTYPE )
219  {
220  lwgeom_startpoint(lwgeom, &p4d);
221  if ( CircTreePIP(circtree_cached, g_cached, &p4d) )
222  {
223  *distance = 0.0;
224  lwgeom_free(lwgeom);
225  return LW_SUCCESS;
226  }
227  }
228 
229  circtree = lwgeom_calculate_circ_tree(lwgeom);
230  if ( geomtype == POLYGONTYPE || geomtype == MULTIPOLYGONTYPE )
231  {
232  POINT2D p2d;
233  circ_tree_get_point(circtree_cached, &p2d);
234  p4d.x = p2d.x;
235  p4d.y = p2d.y;
236  if ( CircTreePIP(circtree, g, &p4d) )
237  {
238  *distance = 0.0;
239  circ_tree_free(circtree);
240  lwgeom_free(lwgeom);
241  return LW_SUCCESS;
242  }
243  }
244 
245  *distance = circ_tree_distance_tree(circtree_cached, circtree, s, tolerance);
246  circ_tree_free(circtree);
247  lwgeom_free(lwgeom);
248  return LW_SUCCESS;
249  }
250  else
251  {
252  return LW_FAILURE;
253  }
254 }
255 
256 int
257 geography_distance_cache(FunctionCallInfo fcinfo,
258  const GSERIALIZED *g1,
259  const GSERIALIZED *g2,
260  const SPHEROID *s,
261  double *distance)
262 {
263  return geography_distance_cache_tolerance(fcinfo, g1, g2, s, FP_TOLERANCE, distance);
264 }
265 
266 int
267 geography_dwithin_cache(FunctionCallInfo fcinfo,
268  const GSERIALIZED *g1,
269  const GSERIALIZED *g2,
270  const SPHEROID *s,
271  double tolerance,
272  int *dwithin)
273 {
274  double distance;
275  /* Ticket #2422, difference between sphere and spheroid distance can trip up the */
276  /* threshold shortcircuit (stopping a calculation before the spheroid distance is actually */
277  /* below the threshold. Lower in the code line, we actually reduce the threshold a little to */
278  /* avoid this. */
279  /* Correct fix: propogate the spheroid information all the way to the bottom of the calculation */
280  /* so the "right thing" can be done in all cases. */
281  if ( LW_SUCCESS == geography_distance_cache_tolerance(fcinfo, g1, g2, s, tolerance, &distance) )
282  {
283  *dwithin = (distance <= (tolerance + FP_TOLERANCE) ? LW_TRUE : LW_FALSE);
284  return LW_SUCCESS;
285  }
286  return LW_FAILURE;
287 }
288 
289 int
290 geography_tree_distance(const GSERIALIZED* g1, const GSERIALIZED* g2, const SPHEROID* s, double tolerance, double* distance)
291 {
292  CIRC_NODE* circ_tree1 = NULL;
293  CIRC_NODE* circ_tree2 = NULL;
294  LWGEOM* lwgeom1 = NULL;
295  LWGEOM* lwgeom2 = NULL;
296  POINT4D pt1, pt2;
297 
298  lwgeom1 = lwgeom_from_gserialized(g1);
299  lwgeom2 = lwgeom_from_gserialized(g2);
300  circ_tree1 = lwgeom_calculate_circ_tree(lwgeom1);
301  circ_tree2 = lwgeom_calculate_circ_tree(lwgeom2);
302  lwgeom_startpoint(lwgeom1, &pt1);
303  lwgeom_startpoint(lwgeom2, &pt2);
304 
305  if ( CircTreePIP(circ_tree1, g1, &pt2) || CircTreePIP(circ_tree2, g2, &pt1) )
306  {
307  *distance = 0.0;
308  }
309  else
310  {
311  /* Calculate tree/tree distance */
312  *distance = circ_tree_distance_tree(circ_tree1, circ_tree2, s, tolerance);
313  }
314 
315  circ_tree_free(circ_tree1);
316  circ_tree_free(circ_tree2);
317  lwgeom_free(lwgeom1);
318  lwgeom_free(lwgeom2);
319  return LW_SUCCESS;
320 }
int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *box)
Read the bounding box off a serialization and calculate one if it is not already there.
Definition: g_serialized.c:642
double x
Definition: liblwgeom.h:352
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:86
int lwgeom_calculate_gbox_geodetic(const LWGEOM *geom, GBOX *gbox)
Calculate the geodetic bounding box for an LWGEOM.
Definition: lwgeodetic.c:3012
static int CircTreeFreer(GeomCache *cache)
unsigned int int32
Definition: shpopen.c:273
Note that p1 and p2 are pointers into an independent POINTARRAY, do not free them.
static int geography_distance_cache_tolerance(FunctionCallInfo fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double tolerance, double *distance)
int geography_distance_cache(FunctionCallInfo fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double *distance)
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int circ_tree_get_point(const CIRC_NODE *node, POINT2D *pt)
Returns a POINT2D that is a vertex of the input shape.
int lwgeom_startpoint(const LWGEOM *lwgeom, POINT4D *pt)
Definition: lwgeom.c:1881
#define POLYGONTYPE
Definition: liblwgeom.h:87
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
int gbox_contains_point3d(const GBOX *gbox, const POINT3D *pt)
Return true if the point is inside the gbox.
Definition: g_box.c:259
#define LW_SUCCESS
Definition: liblwgeom.h:80
static int CircTreeBuilder(const LWGEOM *lwgeom, GeomCache *cache)
Builder, freeer and public accessor for cached CIRC_NODE trees.
static GeomCache * CircTreeAllocator(void)
double circ_tree_distance_tree(const CIRC_NODE *n1, const CIRC_NODE *n2, const SPHEROID *spheroid, double threshold)
int geography_tree_distance(const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double tolerance, double *distance)
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:52
CIRC_NODE * lwgeom_calculate_circ_tree(const LWGEOM *lwgeom)
int circ_tree_contains_point(const CIRC_NODE *node, const POINT2D *pt, const POINT2D *pt_outside, int level, int *on_boundary)
Walk the tree and count intersections between the stab line and the edges.
#define LW_FAILURE
Definition: liblwgeom.h:79
static int CircTreePIP(const CIRC_NODE *tree1, const GSERIALIZED *g1, const POINT4D *in_point)
static CircTreeGeomCache * GetCircTreeGeomCache(FunctionCallInfo fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2)
double x
Definition: liblwgeom.h:328
#define LW_FALSE
Definition: liblwgeom.h:77
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
int circ_tree_get_point_outside(const CIRC_NODE *node, POINT2D *pt)
#define FP_TOLERANCE
Floating point comparators.
char * s
Definition: cu_in_wkt.c:23
double y
Definition: liblwgeom.h:328
Datum distance(PG_FUNCTION_ARGS)
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesion coordinates on unit sphere.
Definition: lwgeodetic.c:400
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
void circ_tree_free(CIRC_NODE *node)
Recurse from top of node tree and free all children.
int geography_dwithin_cache(FunctionCallInfo fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double tolerance, int *dwithin)
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition: lwgeodetic.c:180
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
int gbox_pt_outside(const GBOX *gbox, POINT2D *pt_outside)
Calculate a spherical point that falls outside the geocentric gbox.
Definition: lwgeodetic.c:1548
static GeomCacheMethods CircTreeCacheMethods
double y
Definition: liblwgeom.h:352