PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
lwgeodetic_tree.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) 2012-2015 Paul Ramsey <pramsey@cleverelephant.ca>
22 * Copyright (C) 2012-2015 Sandro Santilli <strk@kbt.io>
23 *
24 **********************************************************************/
25
26#include "liblwgeom_internal.h"
27#include "lwgeodetic_tree.h"
28#include "lwgeom_log.h"
29
30
31/* Internal prototype */
32static CIRC_NODE* circ_nodes_merge(CIRC_NODE** nodes, int num_nodes);
33static double circ_tree_distance_tree_internal(const CIRC_NODE* n1, const CIRC_NODE* n2, double threshold, double* min_dist, double* max_dist, GEOGRAPHIC_POINT* closest1, GEOGRAPHIC_POINT* closest2);
34
35
39static inline int
41{
42 return (node->num_nodes == 0);
43}
44
49void
51{
52 uint32_t i;
53 if ( ! node ) return;
54
55 if (node->nodes)
56 {
57 for (i = 0; i < node->num_nodes; i++)
58 circ_tree_free(node->nodes[i]);
59 lwfree(node->nodes);
60 }
61 lwfree(node);
62}
63
64
68static CIRC_NODE*
70{
71 POINT2D *p1, *p2;
72 POINT3D q1, q2, c;
73 GEOGRAPHIC_POINT g1, g2, gc;
74 CIRC_NODE *node;
75 double diameter;
76
77 p1 = (POINT2D*)getPoint_internal(pa, i);
78 p2 = (POINT2D*)getPoint_internal(pa, i+1);
79 geographic_point_init(p1->x, p1->y, &g1);
80 geographic_point_init(p2->x, p2->y, &g2);
81
82 LWDEBUGF(3,"edge #%d (%g %g, %g %g)", i, p1->x, p1->y, p2->x, p2->y);
83
84 diameter = sphere_distance(&g1, &g2);
85
86 /* Zero length edge, doesn't get a node */
87 if ( FP_EQUALS(diameter, 0.0) )
88 return NULL;
89
90 /* Allocate */
91 node = lwalloc(sizeof(CIRC_NODE));
92 node->p1 = p1;
93 node->p2 = p2;
94
95 /* Convert ends to X/Y/Z, sum, and normalize to get mid-point */
96 geog2cart(&g1, &q1);
97 geog2cart(&g2, &q2);
98 vector_sum(&q1, &q2, &c);
99 normalize(&c);
100 cart2geog(&c, &gc);
101 node->center = gc;
102 node->radius = diameter / 2.0;
103
104 LWDEBUGF(3,"edge #%d CENTER(%g %g) RADIUS=%g", i, gc.lon, gc.lat, node->radius);
105
106 /* Leaf has no children */
107 node->num_nodes = 0;
108 node->nodes = NULL;
109 node->edge_num = i;
110
111 /* Zero out metadata */
112 node->pt_outside.x = 0.0;
113 node->pt_outside.y = 0.0;
114 node->geom_type = 0;
115
116 return node;
117}
118
122static CIRC_NODE*
124{
125 CIRC_NODE* tree = lwalloc(sizeof(CIRC_NODE));
126 tree->p1 = tree->p2 = (POINT2D*)getPoint_internal(pa, 0);
127 geographic_point_init(tree->p1->x, tree->p1->y, &(tree->center));
128 tree->radius = 0.0;
129 tree->nodes = NULL;
130 tree->num_nodes = 0;
131 tree->edge_num = 0;
132 tree->geom_type = POINTTYPE;
133 tree->pt_outside.x = 0.0;
134 tree->pt_outside.y = 0.0;
135 return tree;
136}
137
142static int
143circ_node_compare(const void* v1, const void* v2)
144{
145 POINT2D p1, p2;
146 unsigned int u1, u2;
147 CIRC_NODE *c1 = *((CIRC_NODE**)v1);
148 CIRC_NODE *c2 = *((CIRC_NODE**)v2);
149 p1.x = rad2deg((c1->center).lon);
150 p1.y = rad2deg((c1->center).lat);
151 p2.x = rad2deg((c2->center).lon);
152 p2.y = rad2deg((c2->center).lat);
153 u1 = geohash_point_as_int(&p1);
154 u2 = geohash_point_as_int(&p2);
155 if ( u1 < u2 ) return -1;
156 if ( u1 > u2 ) return 1;
157 return 0;
158}
159
166static int
167circ_center_spherical(const GEOGRAPHIC_POINT* c1, const GEOGRAPHIC_POINT* c2, double distance, double offset, GEOGRAPHIC_POINT* center)
168{
169 /* Direction from c1 to c2 */
170 double dir = sphere_direction(c1, c2, distance);
171
172 LWDEBUGF(4,"calculating spherical center", dir);
173
174 LWDEBUGF(4,"dir is %g", dir);
175
176 /* Catch sphere_direction when it barfs */
177 if ( isnan(dir) )
178 return LW_FAILURE;
179
180 /* Center of new circle is projection from start point, using offset distance*/
181 return sphere_project(c1, offset, dir, center);
182}
183
191static int
192circ_center_cartesian(const GEOGRAPHIC_POINT* c1, const GEOGRAPHIC_POINT* c2, double distance, double offset, GEOGRAPHIC_POINT* center)
193{
194 POINT3D p1, p2;
195 POINT3D p1p2, pc;
196 double proportion = offset/distance;
197
198 LWDEBUG(4,"calculating cartesian center");
199
200 geog2cart(c1, &p1);
201 geog2cart(c2, &p2);
202
203 /* Difference between p2 and p1 */
204 p1p2.x = p2.x - p1.x;
205 p1p2.y = p2.y - p1.y;
206 p1p2.z = p2.z - p1.z;
207
208 /* Scale difference to proportion */
209 p1p2.x *= proportion;
210 p1p2.y *= proportion;
211 p1p2.z *= proportion;
212
213 /* Add difference to p1 to get approximate center point */
214 pc.x = p1.x + p1p2.x;
215 pc.y = p1.y + p1p2.y;
216 pc.z = p1.z + p1p2.z;
217 normalize(&pc);
218
219 /* Convert center point to geographics */
220 cart2geog(&pc, center);
221
222 return LW_SUCCESS;
223}
224
225
230static CIRC_NODE*
231circ_node_internal_new(CIRC_NODE** c, uint32_t num_nodes)
232{
233 CIRC_NODE *node = NULL;
234 GEOGRAPHIC_POINT new_center, c1;
235 double new_radius;
236 double offset1, dist, D, r1, ri;
237 uint32_t i, new_geom_type;
238
239 LWDEBUGF(3, "called with %d nodes --", num_nodes);
240
241 /* Can't do anything w/ empty input */
242 if ( num_nodes < 1 )
243 return node;
244
245 /* Initialize calculation with values of the first circle */
246 new_center = c[0]->center;
247 new_radius = c[0]->radius;
248 new_geom_type = c[0]->geom_type;
249
250 /* Merge each remaining circle into the new circle */
251 for ( i = 1; i < num_nodes; i++ )
252 {
253 c1 = new_center;
254 r1 = new_radius;
255
256 dist = sphere_distance(&c1, &(c[i]->center));
257 ri = c[i]->radius;
258
259 /* Promote geometry types up the tree, getting more and more collected */
260 /* Go until we find a value */
261 if ( ! new_geom_type )
262 {
263 new_geom_type = c[i]->geom_type;
264 }
265 /* Promote singleton to a multi-type */
266 else if ( ! lwtype_is_collection(new_geom_type) )
267 {
268 /* Anonymous collection if types differ */
269 if ( new_geom_type != c[i]->geom_type )
270 {
271 new_geom_type = COLLECTIONTYPE;
272 }
273 else
274 {
275 new_geom_type = lwtype_get_collectiontype(new_geom_type);
276 }
277 }
278 /* If we can't add next feature to this collection cleanly, promote again to anonymous collection */
279 else if ( new_geom_type != lwtype_get_collectiontype(c[i]->geom_type) )
280 {
281 new_geom_type = COLLECTIONTYPE;
282 }
283
284
285 LWDEBUGF(3, "distance between new (%g %g) and %i (%g %g) is %g", c1.lon, c1.lat, i, c[i]->center.lon, c[i]->center.lat, dist);
286
287 if ( FP_EQUALS(dist, 0) )
288 {
289 LWDEBUG(3, " distance between centers is zero");
290 new_radius = r1 + 2*dist;
291 new_center = c1;
292 }
293 else if ( dist < fabs(r1 - ri) )
294 {
295 /* new contains next */
296 if ( r1 > ri )
297 {
298 LWDEBUG(3, " c1 contains ci");
299 new_center = c1;
300 new_radius = r1;
301 }
302 /* next contains new */
303 else
304 {
305 LWDEBUG(3, " ci contains c1");
306 new_center = c[i]->center;
307 new_radius = ri;
308 }
309 }
310 else
311 {
312 LWDEBUG(3, " calculating new center");
313 /* New circle diameter */
314 D = dist + r1 + ri;
315 LWDEBUGF(3," D is %g", D);
316
317 /* New radius */
318 new_radius = D / 2.0;
319
320 /* Distance from cn1 center to the new center */
321 offset1 = ri + (D - (2.0*r1 + 2.0*ri)) / 2.0;
322 LWDEBUGF(3," offset1 is %g", offset1);
323
324 /* Sometimes the sphere_direction function fails... this causes the center calculation */
325 /* to fail too. In that case, we're going to fall back to a cartesian calculation, which */
326 /* is less exact, so we also have to pad the radius by (hack alert) an arbitrary amount */
327 /* which is hopefully always big enough to contain the input edges */
328 if ( circ_center_spherical(&c1, &(c[i]->center), dist, offset1, &new_center) == LW_FAILURE )
329 {
330 circ_center_cartesian(&c1, &(c[i]->center), dist, offset1, &new_center);
331 new_radius *= 1.1;
332 }
333 }
334 LWDEBUGF(3, " new center is (%g %g) new radius is %g", new_center.lon, new_center.lat, new_radius);
335 }
336
337 node = lwalloc(sizeof(CIRC_NODE));
338 node->p1 = NULL;
339 node->p2 = NULL;
340 node->center = new_center;
341 node->radius = new_radius;
342 node->num_nodes = num_nodes;
343 node->nodes = c;
344 node->edge_num = -1;
345 node->geom_type = new_geom_type;
346 node->pt_outside.x = 0.0;
347 node->pt_outside.y = 0.0;
348 return node;
349}
350
356{
357 int num_edges;
358 int i, j;
359 CIRC_NODE **nodes;
360 CIRC_NODE *node;
361 CIRC_NODE *tree;
362
363 /* Can't do anything with no points */
364 if ( pa->npoints < 1 )
365 return NULL;
366
367 /* Special handling for a single point */
368 if ( pa->npoints == 1 )
369 return circ_node_leaf_point_new(pa);
370
371 /* First create a flat list of nodes, one per edge. */
372 num_edges = pa->npoints - 1;
373 nodes = lwalloc(sizeof(CIRC_NODE*) * pa->npoints);
374 j = 0;
375 for ( i = 0; i < num_edges; i++ )
376 {
377 node = circ_node_leaf_new(pa, i);
378 if ( node ) /* Not zero length? */
379 nodes[j++] = node;
380 }
381
382 /* Special case: only zero-length edges. Make a point node. */
383 if ( j == 0 ) {
384 lwfree(nodes);
385 return circ_node_leaf_point_new(pa);
386 }
387
388 /* Merge the node list pairwise up into a tree */
389 tree = circ_nodes_merge(nodes, j);
390
391 /* Free the old list structure, leaving the tree in place */
392 lwfree(nodes);
393
394 return tree;
395}
396
402static void
403circ_nodes_sort(CIRC_NODE** nodes, int num_nodes)
404{
405 qsort(nodes, num_nodes, sizeof(CIRC_NODE*), circ_node_compare);
406}
407
408
409static CIRC_NODE*
410circ_nodes_merge(CIRC_NODE** nodes, int num_nodes)
411{
412 CIRC_NODE **inodes = NULL;
413 int num_children = num_nodes;
414 int inode_num = 0;
415 int num_parents = 0;
416 int j;
417
418 /* TODO, roll geom_type *up* as tree is built, changing to collection types as simple types are merged
419 * TODO, change the distance algorithm to drive down to simple types first, test pip on poly/other cases, then test edges
420 */
421
422 while( num_children > 1 )
423 {
424 for ( j = 0; j < num_children; j++ )
425 {
426 inode_num = (j % CIRC_NODE_SIZE);
427 if ( inode_num == 0 )
428 inodes = lwalloc(sizeof(CIRC_NODE*)*CIRC_NODE_SIZE);
429
430 inodes[inode_num] = nodes[j];
431
432 if ( inode_num == CIRC_NODE_SIZE-1 )
433 nodes[num_parents++] = circ_node_internal_new(inodes, CIRC_NODE_SIZE);
434 }
435
436 /* Clean up any remaining nodes... */
437 if ( inode_num == 0 )
438 {
439 /* Promote solo nodes without merging */
440 nodes[num_parents++] = inodes[0];
441 lwfree(inodes);
442 }
443 else if ( inode_num < CIRC_NODE_SIZE-1 )
444 {
445 /* Merge spare nodes */
446 nodes[num_parents++] = circ_node_internal_new(inodes, inode_num+1);
447 }
448
449 num_children = num_parents;
450 num_parents = 0;
451 }
452
453 /* Return a reference to the head of the tree */
454 return nodes[0];
455}
456
457
462{
463 if ( circ_node_is_leaf(node) )
464 {
465 pt->x = node->p1->x;
466 pt->y = node->p1->y;
467 return LW_SUCCESS;
468 }
469 else
470 {
471 return circ_tree_get_point(node->nodes[0], pt);
472 }
473}
474
476{
477 POINT3D center3d;
479 // if (node->radius >= M_PI) return LW_FAILURE;
480 geog2cart(&(node->center), &center3d);
481 vector_scale(&center3d, -1.0);
482 cart2geog(&center3d, &g);
483 pt->x = rad2deg(g.lon);
484 pt->y = rad2deg(g.lat);
485 return LW_SUCCESS;
486}
487
488
495int circ_tree_contains_point(const CIRC_NODE* node, const POINT2D* pt, const POINT2D* pt_outside, int level, int* on_boundary)
496{
497 GEOGRAPHIC_POINT closest;
498 GEOGRAPHIC_EDGE stab_edge, edge;
499 POINT3D S1, S2, E1, E2;
500 double d;
501 uint32_t i, c;
502
503 /* Construct a stabline edge from our "inside" to our known outside point */
504 geographic_point_init(pt->x, pt->y, &(stab_edge.start));
505 geographic_point_init(pt_outside->x, pt_outside->y, &(stab_edge.end));
506 geog2cart(&(stab_edge.start), &S1);
507 geog2cart(&(stab_edge.end), &S2);
508
509 LWDEBUGF(3, "%*s entered", level, "");
510
511 /*
512 * If the stabline doesn't cross within the radius of a node, there's no
513 * way it can cross.
514 */
515
516 LWDEBUGF(3, "%*s :working on node %p, edge_num %d, radius %g, center POINT(%.12g %.12g)", level, "", node, node->edge_num, node->radius, rad2deg(node->center.lon), rad2deg(node->center.lat));
517 d = edge_distance_to_point(&stab_edge, &(node->center), &closest);
518 LWDEBUGF(3, "%*s :edge_distance_to_point=%g, node_radius=%g", level, "", d, node->radius);
519 if ( FP_LTEQ(d, node->radius) )
520 {
521 LWDEBUGF(3,"%*s :entering this branch (%p)", level, "", node);
522
523 /* Return the crossing number of this leaf */
524 if ( circ_node_is_leaf(node) )
525 {
526 int inter;
527 LWDEBUGF(3, "%*s :leaf node calculation (edge %d)", level, "", node->edge_num);
528 geographic_point_init(node->p1->x, node->p1->y, &(edge.start));
529 geographic_point_init(node->p2->x, node->p2->y, &(edge.end));
530 geog2cart(&(edge.start), &E1);
531 geog2cart(&(edge.end), &E2);
532
533 inter = edge_intersects(&S1, &S2, &E1, &E2);
534 LWDEBUGF(3, "%*s :inter = %d", level, "", inter);
535
536 if ( inter & PIR_INTERSECTS )
537 {
538 LWDEBUGF(3,"%*s ::got stab line edge_intersection with this edge!", level, "");
539 /* To avoid double counting crossings-at-a-vertex, */
540 /* always ignore crossings at "lower" ends of edges*/
541 GEOGRAPHIC_POINT e1, e2;
542 cart2geog(&E1,&e1); cart2geog(&E2,&e2);
543
544 LWDEBUGF(3,"%*s LINESTRING(%.15g %.15g,%.15g %.15g)", level, "",
545 pt->x, pt->y,
546 pt_outside->x, pt_outside->y
547 );
548
549 LWDEBUGF(3,"%*s LINESTRING(%.15g %.15g,%.15g %.15g)", level, "",
550 rad2deg(e1.lon), rad2deg(e1.lat),
551 rad2deg(e2.lon), rad2deg(e2.lat)
552 );
553
554 if ( inter & PIR_B_TOUCH_RIGHT || inter & PIR_COLINEAR )
555 {
556 LWDEBUGF(3,"%*s ::rejecting stab line grazing by left-side edge", level, "");
557 return 0;
558 }
559 else
560 {
561 LWDEBUGF(3,"%*s ::accepting stab line intersection", level, "");
562 return 1;
563 }
564 }
565 else
566 {
567 LWDEBUGF(3,"%*s edge does not intersect", level, "");
568 }
569 }
570 /* Or, add up the crossing numbers of all children of this node. */
571 else
572 {
573 c = 0;
574 for ( i = 0; i < node->num_nodes; i++ )
575 {
576 LWDEBUGF(3,"%*s calling circ_tree_contains_point on child %d!", level, "", i);
577 c += circ_tree_contains_point(node->nodes[i], pt, pt_outside, level + 1, on_boundary);
578 }
579 return c % 2;
580 }
581 }
582 else
583 {
584 LWDEBUGF(3,"%*s skipping this branch (%p)", level, "", node);
585 }
586 return 0;
587}
588
589static double
591{
592 double d = sphere_distance(&(n1->center), &(n2->center));
593 double r1 = n1->radius;
594 double r2 = n2->radius;
595
596 if ( d < r1 + r2 )
597 return 0.0;
598
599 return d - r1 - r2;
600}
601
602static double
604{
605 return sphere_distance(&(n1->center), &(n2->center)) + n1->radius + n2->radius;
606}
607
608double
609circ_tree_distance_tree(const CIRC_NODE* n1, const CIRC_NODE* n2, const SPHEROID* spheroid, double threshold)
610{
611 double min_dist = FLT_MAX;
612 double max_dist = FLT_MAX;
613 GEOGRAPHIC_POINT closest1, closest2;
614 /* Quietly decrease the threshold just a little to avoid cases where */
615 /* the actual spheroid distance is larger than the sphere distance */
616 /* causing the return value to be larger than the threshold value */
617 double threshold_radians = 0.95 * threshold / spheroid->radius;
618
619 circ_tree_distance_tree_internal(n1, n2, threshold_radians, &min_dist, &max_dist, &closest1, &closest2);
620
621 /* Spherical case */
622 if ( spheroid->a == spheroid->b )
623 {
624 return spheroid->radius * sphere_distance(&closest1, &closest2);
625 }
626 else
627 {
628 return spheroid_distance(&closest1, &closest2, spheroid);
629 }
630}
631
632
633/***********************************************************************
634* Internal node sorting routine to make distance calculations faster?
635*/
636
637struct sort_node {
639 double d;
640};
641
642static int
643circ_nodes_sort_cmp(const void *a, const void *b)
644{
645 struct sort_node *node_a = (struct sort_node *)(a);
646 struct sort_node *node_b = (struct sort_node *)(b);
647 if (node_a->d < node_b->d) return -1;
648 else if (node_a->d > node_b->d) return 1;
649 else return 0;
650}
651
652static void
654{
655 uint32_t i;
657
658 /* Copy incoming nodes into sorting array and calculate */
659 /* distance to the target node */
660 for (i = 0; i < num_nodes; i++)
661 {
662 sort_nodes[i].node = nodes[i];
663 sort_nodes[i].d = sphere_distance(&(nodes[i]->center), &(target_node->center));
664 }
665
666 /* Sort the nodes and copy the result back into the input array */
667 qsort(sort_nodes, num_nodes, sizeof(struct sort_node), circ_nodes_sort_cmp);
668 for (i = 0; i < num_nodes; i++)
669 {
670 nodes[i] = sort_nodes[i].node;
671 }
672 return;
673}
674
675/***********************************************************************/
676
677static double
678circ_tree_distance_tree_internal(const CIRC_NODE* n1, const CIRC_NODE* n2, double threshold, double* min_dist, double* max_dist, GEOGRAPHIC_POINT* closest1, GEOGRAPHIC_POINT* closest2)
679{
680 double max;
681 double d, d_min;
682 uint32_t i;
683
684 LWDEBUGF(4, "entered, min_dist=%.8g max_dist=%.8g, type1=%d, type2=%d", *min_dist, *max_dist, n1->geom_type, n2->geom_type);
685
686 // printf("-==-\n");
687 // circ_tree_print(n1, 0);
688 // printf("--\n");
689 // circ_tree_print(n2, 0);
690
691 /* Short circuit if we've already hit the minimum */
692 if( *min_dist < threshold || *min_dist == 0.0 )
693 return *min_dist;
694
695 /* If your minimum is greater than anyone's maximum, you can't hold the winner */
696 if( circ_node_min_distance(n1, n2) > *max_dist )
697 {
698 LWDEBUGF(4, "pruning pair %p, %p", n1, n2);
699 return FLT_MAX;
700 }
701
702 /* If your maximum is a new low, we'll use that as our new global tolerance */
704 LWDEBUGF(5, "max %.8g", max);
705 if( max < *max_dist )
706 *max_dist = max;
707
708 /* Polygon on one side, primitive type on the other. Check for point-in-polygon */
709 /* short circuit. */
710 if ( n1->geom_type == POLYGONTYPE && n2->geom_type && ! lwtype_is_collection(n2->geom_type) )
711 {
712 POINT2D pt;
714 LWDEBUGF(4, "n1 is polygon, testing if contains (%.5g,%.5g)", pt.x, pt.y);
715 if ( circ_tree_contains_point(n1, &pt, &(n1->pt_outside), 0, NULL) )
716 {
717 LWDEBUG(4, "it does");
718 *min_dist = 0.0;
721 return *min_dist;
722 }
723 }
724 /* Polygon on one side, primitive type on the other. Check for point-in-polygon */
725 /* short circuit. */
726 if ( n2->geom_type == POLYGONTYPE && n1->geom_type && ! lwtype_is_collection(n1->geom_type) )
727 {
728 POINT2D pt;
730 LWDEBUGF(4, "n2 is polygon, testing if contains (%.5g,%.5g)", pt.x, pt.y);
731 if ( circ_tree_contains_point(n2, &pt, &(n2->pt_outside), 0, NULL) )
732 {
733 LWDEBUG(4, "it does");
736 *min_dist = 0.0;
737 return *min_dist;
738 }
739 }
740
741 /* Both leaf nodes, do a real distance calculation */
743 {
744 double d;
746 LWDEBUGF(4, "testing leaf pair [%d], [%d]", n1->edge_num, n2->edge_num);
747 /* One of the nodes is a point */
748 if ( n1->p1 == n1->p2 || n2->p1 == n2->p2 )
749 {
752
753 /* Both nodes are points! */
754 if ( n1->p1 == n1->p2 && n2->p1 == n2->p2 )
755 {
756 geographic_point_init(n1->p1->x, n1->p1->y, &gp1);
757 geographic_point_init(n2->p1->x, n2->p1->y, &gp2);
758 close1 = gp1; close2 = gp2;
759 d = sphere_distance(&gp1, &gp2);
760 }
761 /* Node 1 is a point */
762 else if ( n1->p1 == n1->p2 )
763 {
764 geographic_point_init(n1->p1->x, n1->p1->y, &gp1);
765 geographic_point_init(n2->p1->x, n2->p1->y, &(e.start));
766 geographic_point_init(n2->p2->x, n2->p2->y, &(e.end));
767 close1 = gp1;
769 }
770 /* Node 2 is a point */
771 else
772 {
773 geographic_point_init(n2->p1->x, n2->p1->y, &gp1);
774 geographic_point_init(n1->p1->x, n1->p1->y, &(e.start));
775 geographic_point_init(n1->p2->x, n1->p2->y, &(e.end));
776 close1 = gp1;
778 }
779 LWDEBUGF(4, " got distance %g", d);
780 }
781 /* Both nodes are edges */
782 else
783 {
786 POINT3D A1, A2, B1, B2;
787 geographic_point_init(n1->p1->x, n1->p1->y, &(e1.start));
788 geographic_point_init(n1->p2->x, n1->p2->y, &(e1.end));
789 geographic_point_init(n2->p1->x, n2->p1->y, &(e2.start));
790 geographic_point_init(n2->p2->x, n2->p2->y, &(e2.end));
791 geog2cart(&(e1.start), &A1);
792 geog2cart(&(e1.end), &A2);
793 geog2cart(&(e2.start), &B1);
794 geog2cart(&(e2.end), &B2);
795 if ( edge_intersects(&A1, &A2, &B1, &B2) )
796 {
797 d = 0.0;
798 edge_intersection(&e1, &e2, &g);
799 close1 = close2 = g;
800 }
801 else
802 {
804 }
805 LWDEBUGF(4, "edge_distance_to_edge returned %g", d);
806 }
807 if ( d < *min_dist )
808 {
809 *min_dist = d;
810 *closest1 = close1;
811 *closest2 = close2;
812 }
813 return d;
814 }
815 else
816 {
817 d_min = FLT_MAX;
818 /* Drive the recursion into the COLLECTION types first so we end up with */
819 /* pairings of primitive geometries that can be forced into the point-in-polygon */
820 /* tests above. */
821 if ( n1->geom_type && lwtype_is_collection(n1->geom_type) )
822 {
823 circ_internal_nodes_sort(n1->nodes, n1->num_nodes, n2);
824 for ( i = 0; i < n1->num_nodes; i++ )
825 {
826 d = circ_tree_distance_tree_internal(n1->nodes[i], n2, threshold, min_dist, max_dist, closest1, closest2);
827 d_min = FP_MIN(d_min, d);
828 }
829 }
830 else if ( n2->geom_type && lwtype_is_collection(n2->geom_type) )
831 {
832 circ_internal_nodes_sort(n2->nodes, n2->num_nodes, n1);
833 for ( i = 0; i < n2->num_nodes; i++ )
834 {
835 d = circ_tree_distance_tree_internal(n1, n2->nodes[i], threshold, min_dist, max_dist, closest1, closest2);
836 d_min = FP_MIN(d_min, d);
837 }
838 }
839 else if ( ! circ_node_is_leaf(n1) )
840 {
841 circ_internal_nodes_sort(n1->nodes, n1->num_nodes, n2);
842 for ( i = 0; i < n1->num_nodes; i++ )
843 {
844 d = circ_tree_distance_tree_internal(n1->nodes[i], n2, threshold, min_dist, max_dist, closest1, closest2);
845 d_min = FP_MIN(d_min, d);
846 }
847 }
848 else if ( ! circ_node_is_leaf(n2) )
849 {
850 circ_internal_nodes_sort(n2->nodes, n2->num_nodes, n1);
851 for ( i = 0; i < n2->num_nodes; i++ )
852 {
853 d = circ_tree_distance_tree_internal(n1, n2->nodes[i], threshold, min_dist, max_dist, closest1, closest2);
854 d_min = FP_MIN(d_min, d);
855 }
856 }
857 else
858 {
859 /* Never get here */
860 }
861
862 return d_min;
863 }
864}
865
866
867
868
869
870void circ_tree_print(const CIRC_NODE* node, int depth)
871{
872 uint32_t i;
873
875 {
876 printf("%*s[%d] C(%.5g %.5g) R(%.5g) ((%.5g %.5g),(%.5g,%.5g))",
877 3*depth + 6, "NODE", node->edge_num,
879 node->radius,
880 node->p1->x, node->p1->y,
881 node->p2->x, node->p2->y
882 );
883 if ( node->geom_type )
884 {
886 }
887 if ( node->geom_type == POLYGONTYPE )
888 {
889 printf(" O(%.5g %.5g)", node->pt_outside.x, node->pt_outside.y);
890 }
891 printf("\n");
892
893 }
894 else
895 {
896 printf("%*s C(%.5g %.5g) R(%.5g)",
897 3*depth + 6, "NODE",
899 node->radius
900 );
901 if ( node->geom_type )
902 {
904 }
905 if ( node->geom_type == POLYGONTYPE )
906 {
907 printf(" O(%.15g %.15g)", node->pt_outside.x, node->pt_outside.y);
908 }
909 printf("\n");
910 }
911 for ( i = 0; i < node->num_nodes; i++ )
912 {
913 circ_tree_print(node->nodes[i], depth + 1);
914 }
915 return;
916}
917
918
919static CIRC_NODE*
927
928static CIRC_NODE*
936
937static CIRC_NODE*
939{
940 uint32_t i = 0, j = 0;
941 CIRC_NODE** nodes;
943
944 /* One ring? Handle it like a line. */
945 if ( lwpoly->nrings == 1 )
946 {
947 node = circ_tree_new(lwpoly->rings[0]);
948 }
949 else
950 {
951 /* Calculate a tree for each non-trivial ring of the polygon */
952 nodes = lwalloc(lwpoly->nrings * sizeof(CIRC_NODE*));
953 for ( i = 0; i < lwpoly->nrings; i++ )
954 {
955 node = circ_tree_new(lwpoly->rings[i]);
956 if ( node )
957 nodes[j++] = node;
958 }
959 /* Put the trees into a spatially correlated order */
960 circ_nodes_sort(nodes, j);
961 /* Merge the trees pairwise up to a parent node and return */
962 node = circ_nodes_merge(nodes, j);
963 /* Don't need the working list any more */
964 lwfree(nodes);
965 }
966
967 /* Metadata about polygons, we need this to apply P-i-P tests */
968 /* selectively when doing distance calculations */
971
972 return node;
973}
974
975static CIRC_NODE*
977{
978 uint32_t i = 0, j = 0;
979 CIRC_NODE** nodes;
981
982 /* One geometry? Done! */
983 if ( lwcol->ngeoms == 1 )
984 return lwgeom_calculate_circ_tree(lwcol->geoms[0]);
985
986 /* Calculate a tree for each sub-geometry*/
987 nodes = lwalloc(lwcol->ngeoms * sizeof(CIRC_NODE*));
988 for ( i = 0; i < lwcol->ngeoms; i++ )
989 {
991 if ( node )
992 nodes[j++] = node;
993 }
994 /* Put the trees into a spatially correlated order */
995 circ_nodes_sort(nodes, j);
996 /* Merge the trees pairwise up to a parent node and return */
997 node = circ_nodes_merge(nodes, j);
998 /* Don't need the working list any more */
999 lwfree(nodes);
1001 return node;
1002}
1003
1004CIRC_NODE*
1006{
1007 if ( lwgeom_is_empty(lwgeom) )
1008 return NULL;
1009
1010 switch ( lwgeom->type )
1011 {
1012 case POINTTYPE:
1014 case LINETYPE:
1016 case POLYGONTYPE:
1018 case MULTIPOINTTYPE:
1019 case MULTILINETYPE:
1020 case MULTIPOLYGONTYPE:
1021 case COLLECTIONTYPE:
1023 default:
1024 lwerror("Unable to calculate spherical index tree for type %s", lwtype_name(lwgeom->type));
1025 return NULL;
1026 }
1027
1028}
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition lwutil.c:216
uint32_t lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition lwgeom.c:1114
#define COLLECTIONTYPE
Definition liblwgeom.h:122
#define LW_FAILURE
Definition liblwgeom.h:110
#define MULTILINETYPE
Definition liblwgeom.h:120
#define LINETYPE
Definition liblwgeom.h:117
#define LW_SUCCESS
Definition liblwgeom.h:111
unsigned int geohash_point_as_int(POINT2D *pt)
#define MULTIPOINTTYPE
Definition liblwgeom.h:119
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition lwgeom.c:1087
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition liblwgeom.h:116
void * lwalloc(size_t size)
Definition lwutil.c:227
#define MULTIPOLYGONTYPE
Definition liblwgeom.h:121
void lwfree(void *mem)
Definition lwutil.c:242
#define POLYGONTYPE
Definition liblwgeom.h:118
#define FP_MIN(A, B)
#define FP_EQUALS(A, B)
#define FP_LTEQ(A, B)
void normalize(POINT3D *p)
Normalize to a unit vector.
Definition lwgeodetic.c:615
void vector_scale(POINT3D *n, double scale)
Scale a vector out by a factor.
Definition lwgeodetic.c:487
void cart2geog(const POINT3D *p, GEOGRAPHIC_POINT *g)
Convert cartesian coordinates on unit sphere to spherical coordinates.
Definition lwgeodetic.c:414
int lwpoly_pt_outside(const LWPOLY *poly, POINT2D *pt_outside)
int sphere_project(const GEOGRAPHIC_POINT *r, double distance, double azimuth, GEOGRAPHIC_POINT *n)
Given a starting location r, a distance and an azimuth to the new point, compute the location of the ...
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition lwgeodetic.c:180
double sphere_distance(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e)
Given two points on a unit sphere, calculate their distance apart in radians.
Definition lwgeodetic.c:948
double sphere_direction(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e, double d)
Given two points on a unit sphere, calculate the direction from s to e.
Definition lwgeodetic.c:975
int edge_intersection(const GEOGRAPHIC_EDGE *e1, const GEOGRAPHIC_EDGE *e2, GEOGRAPHIC_POINT *g)
Returns true if an intersection can be calculated, and places it in *g.
void vector_sum(const POINT3D *a, const POINT3D *b, POINT3D *n)
Calculate the sum of two vectors.
Definition lwgeodetic.c:465
uint32_t edge_intersects(const POINT3D *A1, const POINT3D *A2, const POINT3D *B1, const POINT3D *B2)
Returns non-zero if edges A and B interact.
double edge_distance_to_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *gp, GEOGRAPHIC_POINT *closest)
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesian coordinates on unit sphere.
Definition lwgeodetic.c:404
double edge_distance_to_edge(const GEOGRAPHIC_EDGE *e1, const GEOGRAPHIC_EDGE *e2, GEOGRAPHIC_POINT *closest1, GEOGRAPHIC_POINT *closest2)
Calculate the distance between two edges.
#define rad2deg(r)
Definition lwgeodetic.h:81
double spheroid_distance(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const SPHEROID *spheroid)
Computes the shortest distance along the surface of the spheroid between two points,...
Definition lwspheroid.c:79
#define PIR_COLINEAR
Definition lwgeodetic.h:89
#define PIR_INTERSECTS
Definition lwgeodetic.h:88
#define PIR_B_TOUCH_RIGHT
Definition lwgeodetic.h:92
static CIRC_NODE * lwcollection_calculate_circ_tree(const LWCOLLECTION *lwcol)
double circ_tree_distance_tree(const CIRC_NODE *n1, const CIRC_NODE *n2, const SPHEROID *spheroid, double threshold)
CIRC_NODE * lwgeom_calculate_circ_tree(const LWGEOM *lwgeom)
int circ_tree_get_point(const CIRC_NODE *node, POINT2D *pt)
Returns a POINT2D that is a vertex of the input shape.
static double circ_node_max_distance(const CIRC_NODE *n1, const CIRC_NODE *n2)
static CIRC_NODE * lwpoly_calculate_circ_tree(const LWPOLY *lwpoly)
static int circ_nodes_sort_cmp(const void *a, const void *b)
static CIRC_NODE * circ_node_leaf_point_new(const POINTARRAY *pa)
Return a point node (zero radius, referencing one point)
static double circ_tree_distance_tree_internal(const CIRC_NODE *n1, const CIRC_NODE *n2, double threshold, double *min_dist, double *max_dist, GEOGRAPHIC_POINT *closest1, GEOGRAPHIC_POINT *closest2)
void circ_tree_print(const CIRC_NODE *node, int depth)
static int circ_center_spherical(const GEOGRAPHIC_POINT *c1, const GEOGRAPHIC_POINT *c2, double distance, double offset, GEOGRAPHIC_POINT *center)
Given the centers of two circles, and the offset distance we want to put the new center between them ...
static int circ_node_is_leaf(const CIRC_NODE *node)
Internal nodes have their point references set to NULL.
void circ_tree_free(CIRC_NODE *node)
Recurse from top of node tree and free all children.
static CIRC_NODE * circ_node_internal_new(CIRC_NODE **c, uint32_t num_nodes)
Create a new internal node, calculating the new measure range for the node, and storing pointers to t...
static void circ_internal_nodes_sort(CIRC_NODE **nodes, uint32_t num_nodes, const CIRC_NODE *target_node)
static int circ_center_cartesian(const GEOGRAPHIC_POINT *c1, const GEOGRAPHIC_POINT *c2, double distance, double offset, GEOGRAPHIC_POINT *center)
Where the circ_center_spherical() function fails, we need a fall-back.
CIRC_NODE * circ_tree_new(const POINTARRAY *pa)
Build a tree of nodes from a point array, one node per edge.
static CIRC_NODE * circ_node_leaf_new(const POINTARRAY *pa, int i)
Create a new leaf node, storing pointers back to the end points for later.
static CIRC_NODE * lwline_calculate_circ_tree(const LWLINE *lwline)
static CIRC_NODE * lwpoint_calculate_circ_tree(const LWPOINT *lwpoint)
int circ_tree_get_point_outside(const CIRC_NODE *node, POINT2D *pt)
static CIRC_NODE * circ_nodes_merge(CIRC_NODE **nodes, int num_nodes)
static double circ_node_min_distance(const CIRC_NODE *n1, const CIRC_NODE *n2)
static int circ_node_compare(const void *v1, const void *v2)
Comparing on geohash ensures that nearby nodes will be close to each other in the list.
static void circ_nodes_sort(CIRC_NODE **nodes, int num_nodes)
Given a list of nodes, sort them into a spatially consistent order, then pairwise merge them up into ...
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 CIRC_NODE_SIZE
#define LWDEBUG(level, msg)
Definition lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition lwgeom_log.h:88
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition lwutil.c:190
static uint8_t * getPoint_internal(const POINTARRAY *pa, uint32_t n)
Definition lwinline.h:67
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition lwinline.h:135
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition lwinline.h:193
static double distance(double x1, double y1, double x2, double y2)
Definition lwtree.c:1032
GEOGRAPHIC_POINT start
Definition lwgeodetic.h:64
GEOGRAPHIC_POINT end
Definition lwgeodetic.h:65
Two-point great circle segment from a to b.
Definition lwgeodetic.h:63
Point in spherical coordinates on the world.
Definition lwgeodetic.h:54
double y
Definition liblwgeom.h:376
double x
Definition liblwgeom.h:376
double z
Definition liblwgeom.h:388
double x
Definition liblwgeom.h:388
double y
Definition liblwgeom.h:388
uint32_t npoints
Definition liblwgeom.h:413
double radius
Definition liblwgeom.h:366
double a
Definition liblwgeom.h:361
double b
Definition liblwgeom.h:362
uint32_t num_nodes
POINT2D * p2
struct circ_node ** nodes
POINT2D * p1
POINT2D pt_outside
GEOGRAPHIC_POINT center
uint32_t geom_type
Note that p1 and p2 are pointers into an independent POINTARRAY, do not free them.
CIRC_NODE * node