PostGIS  3.2.2dev-r@@SVN_REVISION@@
lwgeodetic.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 2009 Paul Ramsey <pramsey@cleverelephant.ca>
22  * Copyright 2009 David Skea <David.Skea@gov.bc.ca>
23  *
24  **********************************************************************/
25 
26 
27 #include "liblwgeom_internal.h"
28 #include "lwgeodetic.h"
29 #include "lwgeom_log.h"
30 
37 
41 static int
42 point3d_equals(const POINT3D *p1, const POINT3D *p2)
43 {
44  return FP_EQUALS(p1->x, p2->x) && FP_EQUALS(p1->y, p2->y) && FP_EQUALS(p1->z, p2->z);
45 }
46 
50 double longitude_radians_normalize(double lon)
51 {
52  if ( lon == -1.0 * M_PI )
53  return M_PI;
54  if ( lon == -2.0 * M_PI )
55  return 0.0;
56 
57  if ( lon > 2.0 * M_PI )
58  lon = remainder(lon, 2.0 * M_PI);
59 
60  if ( lon < -2.0 * M_PI )
61  lon = remainder(lon, -2.0 * M_PI);
62 
63  if ( lon > M_PI )
64  lon = -2.0 * M_PI + lon;
65 
66  if ( lon < -1.0 * M_PI )
67  lon = 2.0 * M_PI + lon;
68 
69  if ( lon == -2.0 * M_PI )
70  lon *= -1.0;
71 
72  return lon;
73 }
74 
78 double latitude_radians_normalize(double lat)
79 {
80 
81  if ( lat > 2.0 * M_PI )
82  lat = remainder(lat, 2.0 * M_PI);
83 
84  if ( lat < -2.0 * M_PI )
85  lat = remainder(lat, -2.0 * M_PI);
86 
87  if ( lat > M_PI )
88  lat = M_PI - lat;
89 
90  if ( lat < -1.0 * M_PI )
91  lat = -1.0 * M_PI - lat;
92 
93  if ( lat > M_PI_2 )
94  lat = M_PI - lat;
95 
96  if ( lat < -1.0 * M_PI_2 )
97  lat = -1.0 * M_PI - lat;
98 
99  return lat;
100 }
101 
106 double longitude_degrees_normalize(double lon)
107 {
108  if ( lon > 360.0 )
109  lon = remainder(lon, 360.0);
110 
111  if ( lon < -360.0 )
112  lon = remainder(lon, -360.0);
113 
114  if ( lon > 180.0 )
115  lon = -360.0 + lon;
116 
117  if ( lon < -180.0 )
118  lon = 360 + lon;
119 
120  if ( lon == -180.0 )
121  return 180.0;
122 
123  if ( lon == -360.0 )
124  return 0.0;
125 
126  return lon;
127 }
128 
133 double latitude_degrees_normalize(double lat)
134 {
135 
136  if ( lat > 360.0 )
137  lat = remainder(lat, 360.0);
138 
139  if ( lat < -360.0 )
140  lat = remainder(lat, -360.0);
141 
142  if ( lat > 180.0 )
143  lat = 180.0 - lat;
144 
145  if ( lat < -180.0 )
146  lat = -180.0 - lat;
147 
148  if ( lat > 90.0 )
149  lat = 180.0 - lat;
150 
151  if ( lat < -90.0 )
152  lat = -180.0 - lat;
153 
154  return lat;
155 }
156 
160 void point_shift(GEOGRAPHIC_POINT *p, double shift)
161 {
162  double lon = p->lon + shift;
163  if ( lon > M_PI )
164  p->lon = -1.0 * M_PI + (lon - M_PI);
165  else
166  p->lon = lon;
167  return;
168 }
169 
171 {
172  return FP_EQUALS(g1->lat, g2->lat) && FP_EQUALS(g1->lon, g2->lon);
173 }
174 
180 void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
181 {
184 }
185 
187 double
189 {
190  double d[6];
191  int i;
192  double zmin = FLT_MAX;
193  double zmax = -1 * FLT_MAX;
194  POINT3D pt;
195 
196  /* Take a copy of the box corners so we can treat them as a list */
197  /* Elements are xmin, xmax, ymin, ymax, zmin, zmax */
198  memcpy(d, &(gbox->xmin), 6*sizeof(double));
199 
200  /* Generate all 8 corner vectors of the box */
201  for ( i = 0; i < 8; i++ )
202  {
203  pt.x = d[i / 4];
204  pt.y = d[2 + (i % 4) / 2];
205  pt.z = d[4 + (i % 2)];
206  normalize(&pt);
207  if ( pt.z < zmin ) zmin = pt.z;
208  if ( pt.z > zmax ) zmax = pt.z;
209  }
210  return asin(zmax) - asin(zmin);
211 }
212 
214 double
216 {
217  double d[6];
218  int i, j;
219  POINT3D pt[3];
220  double maxangle;
221  double magnitude;
222 
223  /* Take a copy of the box corners so we can treat them as a list */
224  /* Elements are xmin, xmax, ymin, ymax, zmin, zmax */
225  memcpy(d, &(gbox->xmin), 6*sizeof(double));
226 
227  /* Start with the bottom corner */
228  pt[0].x = gbox->xmin;
229  pt[0].y = gbox->ymin;
230  magnitude = sqrt(pt[0].x*pt[0].x + pt[0].y*pt[0].y);
231  pt[0].x /= magnitude;
232  pt[0].y /= magnitude;
233 
234  /* Generate all 8 corner vectors of the box */
235  /* Find the vector furthest from our seed vector */
236  for ( j = 0; j < 2; j++ )
237  {
238  maxangle = -1 * FLT_MAX;
239  for ( i = 0; i < 4; i++ )
240  {
241  double angle, dotprod;
242  POINT3D pt_n;
243 
244  pt_n.x = d[i / 2];
245  pt_n.y = d[2 + (i % 2)];
246  magnitude = sqrt(pt_n.x*pt_n.x + pt_n.y*pt_n.y);
247  pt_n.x /= magnitude;
248  pt_n.y /= magnitude;
249  pt_n.z = 0.0;
250 
251  dotprod = pt_n.x*pt[j].x + pt_n.y*pt[j].y;
252  angle = acos(dotprod > 1.0 ? 1.0 : dotprod);
253  if ( angle > maxangle )
254  {
255  pt[j+1] = pt_n;
256  maxangle = angle;
257  }
258  }
259  }
260 
261  /* Return the distance between the two furthest vectors */
262  return maxangle;
263 }
264 
266 int
267 gbox_centroid(const GBOX* gbox, POINT2D* out)
268 {
269  double d[6];
271  POINT3D pt;
272  int i;
273 
274  /* Take a copy of the box corners so we can treat them as a list */
275  /* Elements are xmin, xmax, ymin, ymax, zmin, zmax */
276  memcpy(d, &(gbox->xmin), 6*sizeof(double));
277 
278  /* Zero out our return vector */
279  pt.x = pt.y = pt.z = 0.0;
280 
281  for ( i = 0; i < 8; i++ )
282  {
283  POINT3D pt_n;
284 
285  pt_n.x = d[i / 4];
286  pt_n.y = d[2 + ((i % 4) / 2)];
287  pt_n.z = d[4 + (i % 2)];
288  normalize(&pt_n);
289 
290  pt.x += pt_n.x;
291  pt.y += pt_n.y;
292  pt.z += pt_n.z;
293  }
294 
295  pt.x /= 8.0;
296  pt.y /= 8.0;
297  pt.z /= 8.0;
298  normalize(&pt);
299 
300  cart2geog(&pt, &g);
303 
304  return LW_SUCCESS;
305 }
306 
316 static int gbox_check_poles(GBOX *gbox)
317 {
318  int rv = LW_FALSE;
319 #if POSTGIS_DEBUG_LEVEL >= 4
320  char *gbox_str = gbox_to_string(gbox);
321  LWDEBUG(4, "checking poles");
322  LWDEBUGF(4, "gbox %s", gbox_str);
323  lwfree(gbox_str);
324 #endif
325  /* Z axis */
326  if (gbox->xmin < 0.0 && gbox->xmax > 0.0 &&
327  gbox->ymin < 0.0 && gbox->ymax > 0.0)
328  {
329  /* Extrema lean positive */
330  if ((gbox->zmin > 0.0) && (gbox->zmax > 0.0))
331  {
332  LWDEBUG(4, "enclosed positive z axis");
333  gbox->zmax = 1.0;
334  }
335  /* Extrema lean negative */
336  else if ((gbox->zmin < 0.0) && (gbox->zmax < 0.0))
337  {
338  LWDEBUG(4, "enclosed negative z axis");
339  gbox->zmin = -1.0;
340  }
341  /* Extrema both sides! */
342  else
343  {
344  LWDEBUG(4, "enclosed both z axes");
345  gbox->zmin = -1.0;
346  gbox->zmax = 1.0;
347  }
348  rv = LW_TRUE;
349  }
350 
351  /* Y axis */
352  if (gbox->xmin < 0.0 && gbox->xmax > 0.0 &&
353  gbox->zmin < 0.0 && gbox->zmax > 0.0)
354  {
355  if ((gbox->ymin > 0.0) && (gbox->ymax > 0.0))
356  {
357  LWDEBUG(4, "enclosed positive y axis");
358  gbox->ymax = 1.0;
359  }
360  else if ((gbox->ymin < 0.0) && (gbox->ymax < 0.0))
361  {
362  LWDEBUG(4, "enclosed negative y axis");
363  gbox->ymin = -1.0;
364  }
365  else
366  {
367  LWDEBUG(4, "enclosed both y axes");
368  gbox->ymax = 1.0;
369  gbox->ymin = -1.0;
370  }
371  rv = LW_TRUE;
372  }
373 
374  /* X axis */
375  if (gbox->ymin < 0.0 && gbox->ymax > 0.0 &&
376  gbox->zmin < 0.0 && gbox->zmax > 0.0)
377  {
378  if ((gbox->xmin > 0.0) && (gbox->xmax > 0.0))
379  {
380  LWDEBUG(4, "enclosed positive x axis");
381  gbox->xmax = 1.0;
382  }
383  else if ((gbox->xmin < 0.0) && (gbox->xmax < 0.0))
384  {
385  LWDEBUG(4, "enclosed negative x axis");
386  gbox->xmin = -1.0;
387  }
388  else
389  {
390  LWDEBUG(4, "enclosed both x axes");
391  gbox->xmax = 1.0;
392  gbox->xmin = -1.0;
393  }
394 
395  rv = LW_TRUE;
396  }
397 
398  return rv;
399 }
400 
405 {
406  p->x = cos(g->lat) * cos(g->lon);
407  p->y = cos(g->lat) * sin(g->lon);
408  p->z = sin(g->lat);
409 }
410 
415 {
416  g->lon = atan2(p->y, p->x);
417  g->lat = asin(p->z);
418 }
419 
423 void ll2cart(const POINT2D *g, POINT3D *p)
424 {
425  double x_rad = M_PI * g->x / 180.0;
426  double y_rad = M_PI * g->y / 180.0;
427  double cos_y_rad = cos(y_rad);
428  p->x = cos_y_rad * cos(x_rad);
429  p->y = cos_y_rad * sin(x_rad);
430  p->z = sin(y_rad);
431 }
432 
446 static double dot_product(const POINT3D *p1, const POINT3D *p2)
447 {
448  return (p1->x*p2->x) + (p1->y*p2->y) + (p1->z*p2->z);
449 }
450 
454 static void cross_product(const POINT3D *a, const POINT3D *b, POINT3D *n)
455 {
456  n->x = a->y * b->z - a->z * b->y;
457  n->y = a->z * b->x - a->x * b->z;
458  n->z = a->x * b->y - a->y * b->x;
459  return;
460 }
461 
465 void vector_sum(const POINT3D *a, const POINT3D *b, POINT3D *n)
466 {
467  n->x = a->x + b->x;
468  n->y = a->y + b->y;
469  n->z = a->z + b->z;
470  return;
471 }
472 
476 static void vector_difference(const POINT3D *a, const POINT3D *b, POINT3D *n)
477 {
478  n->x = a->x - b->x;
479  n->y = a->y - b->y;
480  n->z = a->z - b->z;
481  return;
482 }
483 
487 void vector_scale(POINT3D *n, double scale)
488 {
489  n->x *= scale;
490  n->y *= scale;
491  n->z *= scale;
492  return;
493 }
494 
495 /*
496 * static inline double vector_magnitude(const POINT3D* v)
497 * {
498 * return sqrt(v->x*v->x + v->y*v->y + v->z*v->z);
499 * }
500 */
501 
505 double vector_angle(const POINT3D* v1, const POINT3D* v2)
506 {
507  POINT3D v3, normal;
508  double angle, x, y;
509 
510  cross_product(v1, v2, &normal);
511  normalize(&normal);
512  cross_product(&normal, v1, &v3);
513 
514  x = dot_product(v1, v2);
515  y = dot_product(v2, &v3);
516 
517  angle = atan2(y, x);
518  return angle;
519 }
520 
524 static void normalize2d(POINT2D *p)
525 {
526  double d = sqrt(p->x*p->x + p->y*p->y);
527  if (FP_IS_ZERO(d))
528  {
529  p->x = p->y = 0.0;
530  return;
531  }
532  p->x = p->x / d;
533  p->y = p->y / d;
534  return;
535 }
536 
541 void unit_normal(const POINT3D *P1, const POINT3D *P2, POINT3D *normal)
542 {
543  double p_dot = dot_product(P1, P2);
544  POINT3D P3;
545 
546  /* If edge is really large, calculate a narrower equivalent angle A1/A3. */
547  if ( p_dot < 0 )
548  {
549  vector_sum(P1, P2, &P3);
550  normalize(&P3);
551  }
552  /* If edge is narrow, calculate a wider equivalent angle A1/A3. */
553  else if ( p_dot > 0.95 )
554  {
555  vector_difference(P2, P1, &P3);
556  normalize(&P3);
557  }
558  /* Just keep the current angle in A1/A3. */
559  else
560  {
561  P3 = *P2;
562  }
563 
564  /* Normals to the A-plane and B-plane */
565  cross_product(P1, &P3, normal);
566  normalize(normal);
567 }
568 
573 void vector_rotate(const POINT3D* v1, const POINT3D* v2, double angle, POINT3D* n)
574 {
575  POINT3D u;
576  double cos_a = cos(angle);
577  double sin_a = sin(angle);
578  double uxuy, uyuz, uxuz;
579  double ux2, uy2, uz2;
580  double rxx, rxy, rxz, ryx, ryy, ryz, rzx, rzy, rzz;
581 
582  /* Need a unit vector normal to rotate around */
583  unit_normal(v1, v2, &u);
584 
585  uxuy = u.x * u.y;
586  uxuz = u.x * u.z;
587  uyuz = u.y * u.z;
588 
589  ux2 = u.x * u.x;
590  uy2 = u.y * u.y;
591  uz2 = u.z * u.z;
592 
593  rxx = cos_a + ux2 * (1 - cos_a);
594  rxy = uxuy * (1 - cos_a) - u.z * sin_a;
595  rxz = uxuz * (1 - cos_a) + u.y * sin_a;
596 
597  ryx = uxuy * (1 - cos_a) + u.z * sin_a;
598  ryy = cos_a + uy2 * (1 - cos_a);
599  ryz = uyuz * (1 - cos_a) - u.x * sin_a;
600 
601  rzx = uxuz * (1 - cos_a) - u.y * sin_a;
602  rzy = uyuz * (1 - cos_a) + u.x * sin_a;
603  rzz = cos_a + uz2 * (1 - cos_a);
604 
605  n->x = rxx * v1->x + rxy * v1->y + rxz * v1->z;
606  n->y = ryx * v1->x + ryy * v1->y + ryz * v1->z;
607  n->z = rzx * v1->x + rzy * v1->y + rzz * v1->z;
608 
609  normalize(n);
610 }
611 
616 {
617  double d = sqrt(p->x*p->x + p->y*p->y + p->z*p->z);
618  if (FP_IS_ZERO(d))
619  {
620  p->x = p->y = p->z = 0.0;
621  return;
622  }
623  p->x = p->x / d;
624  p->y = p->y / d;
625  p->z = p->z / d;
626  return;
627 }
628 
629 
635 {
636  double lon_qpp = (q->lon + p->lon) / -2.0;
637  double lon_qmp = (q->lon - p->lon) / 2.0;
638  double sin_p_lat_minus_q_lat = sin(p->lat-q->lat);
639  double sin_p_lat_plus_q_lat = sin(p->lat+q->lat);
640  double sin_lon_qpp = sin(lon_qpp);
641  double sin_lon_qmp = sin(lon_qmp);
642  double cos_lon_qpp = cos(lon_qpp);
643  double cos_lon_qmp = cos(lon_qmp);
644  a->x = sin_p_lat_minus_q_lat * sin_lon_qpp * cos_lon_qmp -
645  sin_p_lat_plus_q_lat * cos_lon_qpp * sin_lon_qmp;
646  a->y = sin_p_lat_minus_q_lat * cos_lon_qpp * cos_lon_qmp +
647  sin_p_lat_plus_q_lat * sin_lon_qpp * sin_lon_qmp;
648  a->z = cos(p->lat) * cos(q->lat) * sin(q->lon-p->lon);
649 }
650 
651 void x_to_z(POINT3D *p)
652 {
653  double tmp = p->z;
654  p->z = p->x;
655  p->x = tmp;
656 }
657 
658 void y_to_z(POINT3D *p)
659 {
660  double tmp = p->z;
661  p->z = p->y;
662  p->y = tmp;
663 }
664 
665 
667 {
668  double sign_s = SIGNUM(s->lon);
669  double sign_e = SIGNUM(e->lon);
670  double ss = fabs(s->lon);
671  double ee = fabs(e->lon);
672  if ( sign_s == sign_e )
673  {
674  return LW_FALSE;
675  }
676  else
677  {
678  double dl = ss + ee;
679  if ( dl < M_PI )
680  return LW_FALSE;
681  else if ( FP_EQUALS(dl, M_PI) )
682  return LW_FALSE;
683  else
684  return LW_TRUE;
685  }
686 }
687 
693 static int
695 {
696  POINT3D normal, pt;
697  double w;
698  /* Normal to the plane defined by e */
699  robust_cross_product(&(e->start), &(e->end), &normal);
700  normalize(&normal);
701  geog2cart(p, &pt);
702  /* We expect the dot product of with normal with any vector in the plane to be zero */
703  w = dot_product(&normal, &pt);
704  LWDEBUGF(4,"dot product %.9g",w);
705  if ( FP_IS_ZERO(w) )
706  {
707  LWDEBUG(4, "point is on plane (dot product is zero)");
708  return 0;
709  }
710 
711  if ( w < 0 )
712  return -1;
713  else
714  return 1;
715 }
716 
720 static double
722 {
723  POINT3D normal1, normal2;
724  robust_cross_product(b, a, &normal1);
725  robust_cross_product(b, c, &normal2);
726  normalize(&normal1);
727  normalize(&normal2);
728  return sphere_distance_cartesian(&normal1, &normal2);
729 }
730 
740 static double
742 {
743  double angle_a, angle_b, angle_c;
744  double area_radians = 0.0;
745  int side;
746  GEOGRAPHIC_EDGE e;
747 
748  angle_a = sphere_angle(b,a,c);
749  angle_b = sphere_angle(a,b,c);
750  angle_c = sphere_angle(b,c,a);
751 
752  area_radians = angle_a + angle_b + angle_c - M_PI;
753 
754  /* What's the direction of the B/C edge? */
755  e.start = *a;
756  e.end = *b;
757  side = edge_point_side(&e, c);
758 
759  /* Co-linear points implies no area */
760  if ( side == 0 )
761  return 0.0;
762 
763  /* Add the sign to the area */
764  return side * area_radians;
765 }
766 
767 
768 
776 {
777  int side = edge_point_side(e, p);
778  if ( side == 0 )
779  return LW_TRUE;
780 
781  return LW_FALSE;
782 }
783 
789 {
790  POINT3D vcp, vs, ve, vp;
791  double vs_dot_vcp, vp_dot_vcp;
792  geog2cart(&(e->start), &vs);
793  geog2cart(&(e->end), &ve);
794  /* Antipodal case, everything is inside. */
795  if ( vs.x == -1.0 * ve.x && vs.y == -1.0 * ve.y && vs.z == -1.0 * ve.z )
796  return LW_TRUE;
797  geog2cart(p, &vp);
798  /* The normalized sum bisects the angle between start and end. */
799  vector_sum(&vs, &ve, &vcp);
800  normalize(&vcp);
801  /* The projection of start onto the center defines the minimum similarity */
802  vs_dot_vcp = dot_product(&vs, &vcp);
803  LWDEBUGF(4,"vs_dot_vcp %.19g",vs_dot_vcp);
804  /* The projection of candidate p onto the center */
805  vp_dot_vcp = dot_product(&vp, &vcp);
806  LWDEBUGF(4,"vp_dot_vcp %.19g",vp_dot_vcp);
807  /* If p is more similar than start then p is inside the cone */
808  LWDEBUGF(4,"fabs(vp_dot_vcp - vs_dot_vcp) %.39g",fabs(vp_dot_vcp - vs_dot_vcp));
809 
810  /*
811  ** We want to test that vp_dot_vcp is >= vs_dot_vcp but there are
812  ** numerical stability issues for values that are very very nearly
813  ** equal. Unfortunately there are also values of vp_dot_vcp that are legitimately
814  ** very close to but still less than vs_dot_vcp which we also need to catch.
815  ** The tolerance of 10-17 seems to do the trick on 32-bit and 64-bit architectures,
816  ** for the test cases here.
817  ** However, tuning the tolerance value feels like a dangerous hack.
818  ** Fundamentally, the problem is that this test is so sensitive.
819  */
820 
821  /* 1.1102230246251565404236316680908203125e-16 */
822 
823  if ( vp_dot_vcp > vs_dot_vcp || fabs(vp_dot_vcp - vs_dot_vcp) < 2e-16 )
824  {
825  LWDEBUG(4, "point is in cone");
826  return LW_TRUE;
827  }
828  LWDEBUG(4, "point is not in cone");
829  return LW_FALSE;
830 }
831 
836 {
837  GEOGRAPHIC_EDGE g;
839  double slon = fabs((e->start).lon) + fabs((e->end).lon);
840  double dlon = fabs(fabs((e->start).lon) - fabs((e->end).lon));
841  double slat = (e->start).lat + (e->end).lat;
842 
843  LWDEBUGF(4, "e.start == GPOINT(%.6g %.6g) ", (e->start).lat, (e->start).lon);
844  LWDEBUGF(4, "e.end == GPOINT(%.6g %.6g) ", (e->end).lat, (e->end).lon);
845  LWDEBUGF(4, "p == GPOINT(%.6g %.6g) ", p->lat, p->lon);
846 
847  /* Copy values into working registers */
848  g = *e;
849  q = *p;
850 
851  /* Vertical plane, we need to do this calculation in latitude */
852  if ( FP_EQUALS( g.start.lon, g.end.lon ) )
853  {
854  LWDEBUG(4, "vertical plane, we need to do this calculation in latitude");
855  /* Supposed to be co-planar... */
856  if ( ! FP_EQUALS( q.lon, g.start.lon ) )
857  return LW_FALSE;
858 
859  if ( ( g.start.lat <= q.lat && q.lat <= g.end.lat ) ||
860  ( g.end.lat <= q.lat && q.lat <= g.start.lat ) )
861  {
862  return LW_TRUE;
863  }
864  else
865  {
866  return LW_FALSE;
867  }
868  }
869 
870  /* Over the pole, we need normalize latitude and do this calculation in latitude */
871  if ( FP_EQUALS( slon, M_PI ) && ( SIGNUM(g.start.lon) != SIGNUM(g.end.lon) || FP_EQUALS(dlon, M_PI) ) )
872  {
873  LWDEBUG(4, "over the pole...");
874  /* Antipodal, everything (or nothing?) is inside */
875  if ( FP_EQUALS( slat, 0.0 ) )
876  return LW_TRUE;
877 
878  /* Point *is* the north pole */
879  if ( slat > 0.0 && FP_EQUALS(q.lat, M_PI_2 ) )
880  return LW_TRUE;
881 
882  /* Point *is* the south pole */
883  if ( slat < 0.0 && FP_EQUALS(q.lat, -1.0 * M_PI_2) )
884  return LW_TRUE;
885 
886  LWDEBUG(4, "coplanar?...");
887 
888  /* Supposed to be co-planar... */
889  if ( ! FP_EQUALS( q.lon, g.start.lon ) )
890  return LW_FALSE;
891 
892  LWDEBUG(4, "north or south?...");
893 
894  /* Over north pole, test based on south pole */
895  if ( slat > 0.0 )
896  {
897  LWDEBUG(4, "over the north pole...");
898  if ( q.lat > FP_MIN(g.start.lat, g.end.lat) )
899  return LW_TRUE;
900  else
901  return LW_FALSE;
902  }
903  else
904  /* Over south pole, test based on north pole */
905  {
906  LWDEBUG(4, "over the south pole...");
907  if ( q.lat < FP_MAX(g.start.lat, g.end.lat) )
908  return LW_TRUE;
909  else
910  return LW_FALSE;
911  }
912  }
913 
914  /* Dateline crossing, flip everything to the opposite hemisphere */
915  else if ( slon > M_PI && ( SIGNUM(g.start.lon) != SIGNUM(g.end.lon) ) )
916  {
917  LWDEBUG(4, "crosses dateline, flip longitudes...");
918  if ( g.start.lon > 0.0 )
919  g.start.lon -= M_PI;
920  else
921  g.start.lon += M_PI;
922  if ( g.end.lon > 0.0 )
923  g.end.lon -= M_PI;
924  else
925  g.end.lon += M_PI;
926 
927  if ( q.lon > 0.0 )
928  q.lon -= M_PI;
929  else
930  q.lon += M_PI;
931  }
932 
933  if ( ( g.start.lon <= q.lon && q.lon <= g.end.lon ) ||
934  ( g.end.lon <= q.lon && q.lon <= g.start.lon ) )
935  {
936  LWDEBUG(4, "true, this edge contains point");
937  return LW_TRUE;
938  }
939 
940  LWDEBUG(4, "false, this edge does not contain point");
941  return LW_FALSE;
942 }
943 
944 
949 {
950  double d_lon = e->lon - s->lon;
951  double cos_d_lon = cos(d_lon);
952  double cos_lat_e = cos(e->lat);
953  double sin_lat_e = sin(e->lat);
954  double cos_lat_s = cos(s->lat);
955  double sin_lat_s = sin(s->lat);
956 
957  double a1 = POW2(cos_lat_e * sin(d_lon));
958  double a2 = POW2(cos_lat_s * sin_lat_e - sin_lat_s * cos_lat_e * cos_d_lon);
959  double a = sqrt(a1 + a2);
960  double b = sin_lat_s * sin_lat_e + cos_lat_s * cos_lat_e * cos_d_lon;
961  return atan2(a, b);
962 }
963 
967 double sphere_distance_cartesian(const POINT3D *s, const POINT3D *e)
968 {
969  return acos(FP_MIN(1.0, dot_product(s, e)));
970 }
971 
975 double sphere_direction(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e, double d)
976 {
977  double heading = 0.0;
978  double f;
979 
980  /* Starting from the poles? Special case. */
981  if ( FP_IS_ZERO(cos(s->lat)) )
982  return (s->lat > 0.0) ? M_PI : 0.0;
983 
984  f = (sin(e->lat) - sin(s->lat) * cos(d)) / (sin(d) * cos(s->lat));
985  if ( FP_EQUALS(f, 1.0) )
986  heading = 0.0;
987  else if ( FP_EQUALS(f, -1.0) )
988  heading = M_PI;
989  else if ( fabs(f) > 1.0 )
990  {
991  LWDEBUGF(4, "f = %g", f);
992  heading = acos(f);
993  }
994  else
995  heading = acos(f);
996 
997  if ( sin(e->lon - s->lon) < 0.0 )
998  heading = -1 * heading;
999 
1000  return heading;
1001 }
1002 
1003 #if 0 /* unused */
1015 static double sphere_excess(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const GEOGRAPHIC_POINT *c)
1016 {
1017  double a_dist = sphere_distance(b, c);
1018  double b_dist = sphere_distance(c, a);
1019  double c_dist = sphere_distance(a, b);
1020  double hca = sphere_direction(c, a, b_dist);
1021  double hcb = sphere_direction(c, b, a_dist);
1022  double sign = SIGNUM(hcb-hca);
1023  double ss = (a_dist + b_dist + c_dist) / 2.0;
1024  double E = tan(ss/2.0)*tan((ss-a_dist)/2.0)*tan((ss-b_dist)/2.0)*tan((ss-c_dist)/2.0);
1025  return 4.0 * atan(sqrt(fabs(E))) * sign;
1026 }
1027 #endif
1028 
1029 
1035 {
1036  if ( edge_point_in_cone(e, p) && edge_point_on_plane(e, p) )
1037  /* if ( edge_contains_coplanar_point(e, p) && edge_point_on_plane(e, p) ) */
1038  {
1039  LWDEBUG(4, "point is on edge");
1040  return LW_TRUE;
1041  }
1042  LWDEBUG(4, "point is not on edge");
1043  return LW_FALSE;
1044 }
1045 
1049 double z_to_latitude(double z, int top)
1050 {
1051  double sign = SIGNUM(z);
1052  double tlat = acos(z);
1053  LWDEBUGF(4, "inputs: z(%.8g) sign(%.8g) tlat(%.8g)", z, sign, tlat);
1054  if (FP_IS_ZERO(z))
1055  {
1056  if (top) return M_PI_2;
1057  else return -1.0 * M_PI_2;
1058  }
1059  if (fabs(tlat) > M_PI_2 )
1060  {
1061  tlat = sign * (M_PI - fabs(tlat));
1062  }
1063  else
1064  {
1065  tlat = sign * tlat;
1066  }
1067  LWDEBUGF(4, "output: tlat(%.8g)", tlat);
1068  return tlat;
1069 }
1070 
1076 int clairaut_cartesian(const POINT3D *start, const POINT3D *end, GEOGRAPHIC_POINT *g_top, GEOGRAPHIC_POINT *g_bottom)
1077 {
1078  POINT3D t1, t2;
1079  GEOGRAPHIC_POINT vN1, vN2;
1080  LWDEBUG(4,"entering function");
1081  unit_normal(start, end, &t1);
1082  unit_normal(end, start, &t2);
1083  LWDEBUGF(4, "unit normal t1 == POINT(%.8g %.8g %.8g)", t1.x, t1.y, t1.z);
1084  LWDEBUGF(4, "unit normal t2 == POINT(%.8g %.8g %.8g)", t2.x, t2.y, t2.z);
1085  cart2geog(&t1, &vN1);
1086  cart2geog(&t2, &vN2);
1087  g_top->lat = z_to_latitude(t1.z,LW_TRUE);
1088  g_top->lon = vN2.lon;
1089  g_bottom->lat = z_to_latitude(t2.z,LW_FALSE);
1090  g_bottom->lon = vN1.lon;
1091  LWDEBUGF(4, "clairaut top == GPOINT(%.6g %.6g)", g_top->lat, g_top->lon);
1092  LWDEBUGF(4, "clairaut bottom == GPOINT(%.6g %.6g)", g_bottom->lat, g_bottom->lon);
1093  return LW_SUCCESS;
1094 }
1095 
1102 {
1103  POINT3D t1, t2;
1104  GEOGRAPHIC_POINT vN1, vN2;
1105  LWDEBUG(4,"entering function");
1106  robust_cross_product(start, end, &t1);
1107  normalize(&t1);
1108  robust_cross_product(end, start, &t2);
1109  normalize(&t2);
1110  LWDEBUGF(4, "unit normal t1 == POINT(%.8g %.8g %.8g)", t1.x, t1.y, t1.z);
1111  LWDEBUGF(4, "unit normal t2 == POINT(%.8g %.8g %.8g)", t2.x, t2.y, t2.z);
1112  cart2geog(&t1, &vN1);
1113  cart2geog(&t2, &vN2);
1114  g_top->lat = z_to_latitude(t1.z,LW_TRUE);
1115  g_top->lon = vN2.lon;
1116  g_bottom->lat = z_to_latitude(t2.z,LW_FALSE);
1117  g_bottom->lon = vN1.lon;
1118  LWDEBUGF(4, "clairaut top == GPOINT(%.6g %.6g)", g_top->lat, g_top->lon);
1119  LWDEBUGF(4, "clairaut bottom == GPOINT(%.6g %.6g)", g_bottom->lat, g_bottom->lon);
1120  return LW_SUCCESS;
1121 }
1122 
1128 {
1129  POINT3D ea, eb, v;
1130  LWDEBUGF(4, "e1 start(%.20g %.20g) end(%.20g %.20g)", e1->start.lat, e1->start.lon, e1->end.lat, e1->end.lon);
1131  LWDEBUGF(4, "e2 start(%.20g %.20g) end(%.20g %.20g)", e2->start.lat, e2->start.lon, e2->end.lat, e2->end.lon);
1132 
1133  LWDEBUGF(4, "e1 start(%.20g %.20g) end(%.20g %.20g)", rad2deg(e1->start.lon), rad2deg(e1->start.lat), rad2deg(e1->end.lon), rad2deg(e1->end.lat));
1134  LWDEBUGF(4, "e2 start(%.20g %.20g) end(%.20g %.20g)", rad2deg(e2->start.lon), rad2deg(e2->start.lat), rad2deg(e2->end.lon), rad2deg(e2->end.lat));
1135 
1136  if ( geographic_point_equals(&(e1->start), &(e2->start)) )
1137  {
1138  *g = e1->start;
1139  return LW_TRUE;
1140  }
1141  if ( geographic_point_equals(&(e1->end), &(e2->end)) )
1142  {
1143  *g = e1->end;
1144  return LW_TRUE;
1145  }
1146  if ( geographic_point_equals(&(e1->end), &(e2->start)) )
1147  {
1148  *g = e1->end;
1149  return LW_TRUE;
1150  }
1151  if ( geographic_point_equals(&(e1->start), &(e2->end)) )
1152  {
1153  *g = e1->start;
1154  return LW_TRUE;
1155  }
1156 
1157  robust_cross_product(&(e1->start), &(e1->end), &ea);
1158  normalize(&ea);
1159  robust_cross_product(&(e2->start), &(e2->end), &eb);
1160  normalize(&eb);
1161  LWDEBUGF(4, "e1 cross product == POINT(%.12g %.12g %.12g)", ea.x, ea.y, ea.z);
1162  LWDEBUGF(4, "e2 cross product == POINT(%.12g %.12g %.12g)", eb.x, eb.y, eb.z);
1163  LWDEBUGF(4, "fabs(dot_product(ea, eb)) == %.14g", fabs(dot_product(&ea, &eb)));
1164  if ( FP_EQUALS(fabs(dot_product(&ea, &eb)), 1.0) )
1165  {
1166  LWDEBUGF(4, "parallel edges found! dot_product = %.12g", dot_product(&ea, &eb));
1167  /* Parallel (maybe equal) edges! */
1168  /* Hack alert, only returning ONE end of the edge right now, most do better later. */
1169  /* Hack alert #2, returning a value of 2 to indicate a co-linear crossing event. */
1170  if ( edge_contains_point(e1, &(e2->start)) )
1171  {
1172  *g = e2->start;
1173  return 2;
1174  }
1175  if ( edge_contains_point(e1, &(e2->end)) )
1176  {
1177  *g = e2->end;
1178  return 2;
1179  }
1180  if ( edge_contains_point(e2, &(e1->start)) )
1181  {
1182  *g = e1->start;
1183  return 2;
1184  }
1185  if ( edge_contains_point(e2, &(e1->end)) )
1186  {
1187  *g = e1->end;
1188  return 2;
1189  }
1190  }
1191  unit_normal(&ea, &eb, &v);
1192  LWDEBUGF(4, "v == POINT(%.12g %.12g %.12g)", v.x, v.y, v.z);
1193  g->lat = atan2(v.z, sqrt(v.x * v.x + v.y * v.y));
1194  g->lon = atan2(v.y, v.x);
1195  LWDEBUGF(4, "g == GPOINT(%.12g %.12g)", g->lat, g->lon);
1196  LWDEBUGF(4, "g == POINT(%.12g %.12g)", rad2deg(g->lon), rad2deg(g->lat));
1197  if ( edge_contains_point(e1, g) && edge_contains_point(e2, g) )
1198  {
1199  return LW_TRUE;
1200  }
1201  else
1202  {
1203  LWDEBUG(4, "flipping point to other side of sphere");
1204  g->lat = -1.0 * g->lat;
1205  g->lon = g->lon + M_PI;
1206  if ( g->lon > M_PI )
1207  {
1208  g->lon = -1.0 * (2.0 * M_PI - g->lon);
1209  }
1210  if ( edge_contains_point(e1, g) && edge_contains_point(e2, g) )
1211  {
1212  return LW_TRUE;
1213  }
1214  }
1215  return LW_FALSE;
1216 }
1217 
1219 {
1220  double d1 = 1000000000.0, d2, d3, d_nearest;
1221  POINT3D n, p, k;
1222  GEOGRAPHIC_POINT gk, g_nearest;
1223 
1224  /* Zero length edge, */
1225  if ( geographic_point_equals(&(e->start), &(e->end)) )
1226  {
1227  if (closest)
1228  *closest = e->start;
1229 
1230  return sphere_distance(&(e->start), gp);
1231  }
1232 
1233  robust_cross_product(&(e->start), &(e->end), &n);
1234  normalize(&n);
1235  geog2cart(gp, &p);
1236  vector_scale(&n, dot_product(&p, &n));
1237  vector_difference(&p, &n, &k);
1238  normalize(&k);
1239  cart2geog(&k, &gk);
1240  if ( edge_point_in_cone(e, &gk) )
1241  {
1242  d1 = sphere_distance(gp, &gk);
1243  }
1244  d2 = sphere_distance(gp, &(e->start));
1245  d3 = sphere_distance(gp, &(e->end));
1246 
1247  d_nearest = d1;
1248  g_nearest = gk;
1249 
1250  if ( d2 < d_nearest )
1251  {
1252  d_nearest = d2;
1253  g_nearest = e->start;
1254  }
1255  if ( d3 < d_nearest )
1256  {
1257  d_nearest = d3;
1258  g_nearest = e->end;
1259  }
1260  if (closest)
1261  *closest = g_nearest;
1262 
1263  return d_nearest;
1264 }
1265 
1272 {
1273  double d;
1274  GEOGRAPHIC_POINT gcp1s, gcp1e, gcp2s, gcp2e, c1, c2;
1275  double d1s = edge_distance_to_point(e1, &(e2->start), &gcp1s);
1276  double d1e = edge_distance_to_point(e1, &(e2->end), &gcp1e);
1277  double d2s = edge_distance_to_point(e2, &(e1->start), &gcp2s);
1278  double d2e = edge_distance_to_point(e2, &(e1->end), &gcp2e);
1279 
1280  d = d1s;
1281  c1 = gcp1s;
1282  c2 = e2->start;
1283 
1284  if ( d1e < d )
1285  {
1286  d = d1e;
1287  c1 = gcp1e;
1288  c2 = e2->end;
1289  }
1290 
1291  if ( d2s < d )
1292  {
1293  d = d2s;
1294  c1 = e1->start;
1295  c2 = gcp2s;
1296  }
1297 
1298  if ( d2e < d )
1299  {
1300  d = d2e;
1301  c1 = e1->end;
1302  c2 = gcp2e;
1303  }
1304 
1305  if ( closest1 ) *closest1 = c1;
1306  if ( closest2 ) *closest2 = c2;
1307 
1308  return d;
1309 }
1310 
1311 
1316 int sphere_project(const GEOGRAPHIC_POINT *r, double distance, double azimuth, GEOGRAPHIC_POINT *n)
1317 {
1318  double d = distance;
1319  double lat1 = r->lat;
1320  double lon1 = r->lon;
1321  double lat2, lon2;
1322 
1323  lat2 = asin(sin(lat1)*cos(d) + cos(lat1)*sin(d)*cos(azimuth));
1324 
1325  /* If we're going straight up or straight down, we don't need to calculate the longitude */
1326  /* TODO: this isn't quite true, what if we're going over the pole? */
1327  if ( FP_EQUALS(azimuth, M_PI) || FP_EQUALS(azimuth, 0.0) )
1328  {
1329  lon2 = r->lon;
1330  }
1331  else
1332  {
1333  lon2 = lon1 + atan2(sin(azimuth)*sin(d)*cos(lat1), cos(d)-sin(lat1)*sin(lat2));
1334  }
1335 
1336  if ( isnan(lat2) || isnan(lon2) )
1337  return LW_FAILURE;
1338 
1339  n->lat = lat2;
1340  n->lon = lon2;
1341 
1342  return LW_SUCCESS;
1343 }
1344 
1345 
1347 {
1348  int steps = 1000000;
1349  int i;
1350  double dx, dy, dz;
1351  double distance = sphere_distance(&(e->start), &(e->end));
1352  POINT3D pn, p, start, end;
1353 
1354  /* Edge is zero length, just return the naive box */
1355  if ( FP_IS_ZERO(distance) )
1356  {
1357  LWDEBUG(4, "edge is zero length. returning");
1358  geog2cart(&(e->start), &start);
1359  geog2cart(&(e->end), &end);
1360  gbox_init_point3d(&start, gbox);
1361  gbox_merge_point3d(&end, gbox);
1362  return LW_SUCCESS;
1363  }
1364 
1365  /* Edge is antipodal (one point on each side of the globe),
1366  set the box to contain the whole world and return */
1367  if ( FP_EQUALS(distance, M_PI) )
1368  {
1369  LWDEBUG(4, "edge is antipodal. setting to maximum size box, and returning");
1370  gbox->xmin = gbox->ymin = gbox->zmin = -1.0;
1371  gbox->xmax = gbox->ymax = gbox->zmax = 1.0;
1372  return LW_SUCCESS;
1373  }
1374 
1375  /* Walk along the chord between start and end incrementally,
1376  normalizing at each step. */
1377  geog2cart(&(e->start), &start);
1378  geog2cart(&(e->end), &end);
1379  dx = (end.x - start.x)/steps;
1380  dy = (end.y - start.y)/steps;
1381  dz = (end.z - start.z)/steps;
1382  p = start;
1383  gbox->xmin = gbox->xmax = p.x;
1384  gbox->ymin = gbox->ymax = p.y;
1385  gbox->zmin = gbox->zmax = p.z;
1386  for ( i = 0; i < steps; i++ )
1387  {
1388  p.x += dx;
1389  p.y += dy;
1390  p.z += dz;
1391  pn = p;
1392  normalize(&pn);
1393  gbox_merge_point3d(&pn, gbox);
1394  }
1395  return LW_SUCCESS;
1396 }
1397 
1410 int edge_calculate_gbox(const POINT3D *A1, const POINT3D *A2, GBOX *gbox)
1411 {
1412  POINT2D R1, R2, RX, O;
1413  POINT3D AN, A3;
1414  POINT3D X[6];
1415  int i, o_side;
1416 
1417  /* Initialize the box with the edge end points */
1418  gbox_init_point3d(A1, gbox);
1419  gbox_merge_point3d(A2, gbox);
1420 
1421  /* Zero length edge, just return! */
1422  if ( p3d_same(A1, A2) )
1423  return LW_SUCCESS;
1424 
1425  /* Error out on antipodal edge */
1426  if ( FP_EQUALS(A1->x, -1*A2->x) && FP_EQUALS(A1->y, -1*A2->y) && FP_EQUALS(A1->z, -1*A2->z) )
1427  {
1428  lwerror("Antipodal (180 degrees long) edge detected!");
1429  return LW_FAILURE;
1430  }
1431 
1432  /* Create A3, a vector in the plane of A1/A2, orthogonal to A1 */
1433  unit_normal(A1, A2, &AN);
1434  unit_normal(&AN, A1, &A3);
1435 
1436  /* Project A1 and A2 into the 2-space formed by the plane A1/A3 */
1437  R1.x = 1.0;
1438  R1.y = 0.0;
1439  R2.x = dot_product(A2, A1);
1440  R2.y = dot_product(A2, &A3);
1441 
1442  /* Initialize our 3-space axis points (x+, x-, y+, y-, z+, z-) */
1443  memset(X, 0, sizeof(POINT3D) * 6);
1444  X[0].x = X[2].y = X[4].z = 1.0;
1445  X[1].x = X[3].y = X[5].z = -1.0;
1446 
1447  /* Initialize a 2-space origin point. */
1448  O.x = O.y = 0.0;
1449  /* What side of the line joining R1/R2 is O? */
1450  o_side = lw_segment_side(&R1, &R2, &O);
1451 
1452  /* Add any extrema! */
1453  for ( i = 0; i < 6; i++ )
1454  {
1455  /* Convert 3-space axis points to 2-space unit vectors */
1456  RX.x = dot_product(&(X[i]), A1);
1457  RX.y = dot_product(&(X[i]), &A3);
1458  normalize2d(&RX);
1459 
1460  /* Any axis end on the side of R1/R2 opposite the origin */
1461  /* is an extreme point in the arc, so we add the 3-space */
1462  /* version of the point on R1/R2 to the gbox */
1463  if ( lw_segment_side(&R1, &R2, &RX) != o_side )
1464  {
1465  POINT3D Xn;
1466  Xn.x = RX.x * A1->x + RX.y * A3.x;
1467  Xn.y = RX.x * A1->y + RX.y * A3.y;
1468  Xn.z = RX.x * A1->z + RX.y * A3.z;
1469 
1470  gbox_merge_point3d(&Xn, gbox);
1471  }
1472  }
1473 
1474  return LW_SUCCESS;
1475 }
1476 
1477 /*
1478 * When we have a globe-covering gbox but we still want an outside
1479 * point, we do this Very Bad Hack, which is look at the first two points
1480 * in the ring and then nudge a point to the left of that arc.
1481 * There is an assumption of convexity built in there, as well as that
1482 * the shape doesn't have a sharp reversal in it. It's ugly, but
1483 * it fixes some common cases (large selection polygons) that users
1484 * are generating. At some point all of geodetic needs a clean-room
1485 * rewrite.
1486 * There is also an assumption of CCW exterior ring, which is how the
1487 * GeoJSON spec defined geographic ring orientation.
1488 */
1489 static int lwpoly_pt_outside_hack(const LWPOLY *poly, POINT2D *pt_outside)
1490 {
1491  GEOGRAPHIC_POINT g1, g2, gSum;
1492  POINT4D p1, p2;
1493  POINT3D q1, q2, qMid, qCross, qSum;
1494  POINTARRAY *pa;
1495  if (lwgeom_is_empty((LWGEOM*)poly))
1496  return LW_FAILURE;
1497  if (poly->nrings < 1)
1498  return LW_FAILURE;
1499  pa = poly->rings[0];
1500  if (pa->npoints < 2)
1501  return LW_FAILURE;
1502 
1503  /* First two points of ring */
1504  getPoint4d_p(pa, 0, &p1);
1505  getPoint4d_p(pa, 1, &p2);
1506  /* Convert to XYZ unit vectors */
1507  geographic_point_init(p1.x, p1.y, &g1);
1508  geographic_point_init(p2.x, p2.y, &g2);
1509  geog2cart(&g1, &q1);
1510  geog2cart(&g2, &q2);
1511  /* Mid-point of first two points */
1512  vector_sum(&q1, &q2, &qMid);
1513  normalize(&qMid);
1514  /* Cross product of first two points (perpendicular) */
1515  cross_product(&q1, &q2, &qCross);
1516  normalize(&qCross);
1517  /* Invert it to put it outside, and scale down */
1518  vector_scale(&qCross, -0.2);
1519  /* Project midpoint to the right */
1520  vector_sum(&qMid, &qCross, &qSum);
1521  normalize(&qSum);
1522  /* Convert back to lon/lat */
1523  cart2geog(&qSum, &gSum);
1524  pt_outside->x = rad2deg(gSum.lon);
1525  pt_outside->y = rad2deg(gSum.lat);
1526  return LW_SUCCESS;
1527 }
1528 
1529 int lwpoly_pt_outside(const LWPOLY *poly, POINT2D *pt_outside)
1530 {
1531  int rv;
1532  /* Make sure we have boxes */
1533  if ( poly->bbox )
1534  {
1535  rv = gbox_pt_outside(poly->bbox, pt_outside);
1536  }
1537  else
1538  {
1539  GBOX gbox;
1540  lwgeom_calculate_gbox_geodetic((LWGEOM*)poly, &gbox);
1541  rv = gbox_pt_outside(&gbox, pt_outside);
1542  }
1543 
1544  if (rv == LW_FALSE)
1545  return lwpoly_pt_outside_hack(poly, pt_outside);
1546 
1547  return rv;
1548 }
1549 
1554 int gbox_pt_outside(const GBOX *gbox, POINT2D *pt_outside)
1555 {
1556  double grow = M_PI / 180.0 / 60.0; /* one arc-minute */
1557  int i;
1558  GBOX ge;
1559  POINT3D corners[8];
1560  POINT3D pt;
1561  GEOGRAPHIC_POINT g;
1562 
1563  while ( grow < M_PI )
1564  {
1565  /* Assign our box and expand it slightly. */
1566  ge = *gbox;
1567  if ( ge.xmin > -1 ) ge.xmin -= grow;
1568  if ( ge.ymin > -1 ) ge.ymin -= grow;
1569  if ( ge.zmin > -1 ) ge.zmin -= grow;
1570  if ( ge.xmax < 1 ) ge.xmax += grow;
1571  if ( ge.ymax < 1 ) ge.ymax += grow;
1572  if ( ge.zmax < 1 ) ge.zmax += grow;
1573 
1574  /* Build our eight corner points */
1575  corners[0].x = ge.xmin;
1576  corners[0].y = ge.ymin;
1577  corners[0].z = ge.zmin;
1578 
1579  corners[1].x = ge.xmin;
1580  corners[1].y = ge.ymax;
1581  corners[1].z = ge.zmin;
1582 
1583  corners[2].x = ge.xmin;
1584  corners[2].y = ge.ymin;
1585  corners[2].z = ge.zmax;
1586 
1587  corners[3].x = ge.xmax;
1588  corners[3].y = ge.ymin;
1589  corners[3].z = ge.zmin;
1590 
1591  corners[4].x = ge.xmax;
1592  corners[4].y = ge.ymax;
1593  corners[4].z = ge.zmin;
1594 
1595  corners[5].x = ge.xmax;
1596  corners[5].y = ge.ymin;
1597  corners[5].z = ge.zmax;
1598 
1599  corners[6].x = ge.xmin;
1600  corners[6].y = ge.ymax;
1601  corners[6].z = ge.zmax;
1602 
1603  corners[7].x = ge.xmax;
1604  corners[7].y = ge.ymax;
1605  corners[7].z = ge.zmax;
1606 
1607  LWDEBUG(4, "trying to use a box corner point...");
1608  for ( i = 0; i < 8; i++ )
1609  {
1610  normalize(&(corners[i]));
1611  LWDEBUGF(4, "testing corner %d: POINT(%.8g %.8g %.8g)", i, corners[i].x, corners[i].y, corners[i].z);
1612  if ( ! gbox_contains_point3d(gbox, &(corners[i])) )
1613  {
1614  LWDEBUGF(4, "corner %d is outside our gbox", i);
1615  pt = corners[i];
1616  normalize(&pt);
1617  cart2geog(&pt, &g);
1618  pt_outside->x = rad2deg(g.lon);
1619  pt_outside->y = rad2deg(g.lat);
1620  LWDEBUGF(4, "returning POINT(%.8g %.8g) as outside point", pt_outside->x, pt_outside->y);
1621  return LW_SUCCESS;
1622  }
1623  }
1624 
1625  /* Try a wider growth to push the corners outside the original box. */
1626  grow *= 2.0;
1627  }
1628 
1629  /* This should never happen! */
1630  // lwerror("BOOM! Could not generate outside point!");
1631  return LW_FAILURE;
1632 }
1633 
1634 
1636  const POINT3D *p1, const POINT3D *p2, /* 3-space points we are interpolating between */
1637  const POINT4D *v1, const POINT4D *v2, /* real values and z/m values */
1638  double d, double max_seg_length, /* current segment length and segment limit */
1639  POINTARRAY *pa) /* write out results here */
1640 {
1641  GEOGRAPHIC_POINT g;
1642  /* Reached the terminal leaf in recursion. Add */
1643  /* the left-most point to the pointarray here */
1644  /* We recurse down the left side first, so outputs should */
1645  /* end up added to the array in order this way */
1646  if (d <= max_seg_length)
1647  {
1648  POINT4D p;
1649  cart2geog(p1, &g);
1650  p.x = v1->x;
1651  p.y = v1->y;
1652  p.z = v1->z;
1653  p.m = v1->m;
1654  return ptarray_append_point(pa, &p, LW_FALSE);
1655  }
1656  /* Find the mid-point and recurse on the left and then the right */
1657  else
1658  {
1659  /* Calculate mid-point */
1660  POINT3D mid;
1661  mid.x = (p1->x + p2->x) / 2.0;
1662  mid.y = (p1->y + p2->y) / 2.0;
1663  mid.z = (p1->z + p2->z) / 2.0;
1664  normalize(&mid);
1665 
1666  /* Calculate z/m mid-values */
1667  POINT4D midv;
1668  cart2geog(&mid, &g);
1669  midv.x = rad2deg(g.lon);
1670  midv.y = rad2deg(g.lat);
1671  midv.z = (v1->z + v2->z) / 2.0;
1672  midv.m = (v1->m + v2->m) / 2.0;
1673  /* Recurse on the left first */
1674  ptarray_segmentize_sphere_edge_recursive(p1, &mid, v1, &midv, d/2.0, max_seg_length, pa);
1675  ptarray_segmentize_sphere_edge_recursive(&mid, p2, &midv, v2, d/2.0, max_seg_length, pa);
1676  return LW_SUCCESS;
1677  }
1678 }
1679 
1685 static POINTARRAY*
1686 ptarray_segmentize_sphere(const POINTARRAY *pa_in, double max_seg_length)
1687 {
1688  POINTARRAY *pa_out;
1689  int hasz = ptarray_has_z(pa_in);
1690  int hasm = ptarray_has_m(pa_in);
1691  POINT4D p1, p2;
1692  POINT3D q1, q2;
1693  GEOGRAPHIC_POINT g1, g2;
1694  uint32_t i;
1695 
1696  /* Just crap out on crazy input */
1697  if ( ! pa_in )
1698  lwerror("%s: null input pointarray", __func__);
1699  if ( max_seg_length <= 0.0 )
1700  lwerror("%s: maximum segment length must be positive", __func__);
1701 
1702  /* Empty starting array */
1703  pa_out = ptarray_construct_empty(hasz, hasm, pa_in->npoints);
1704 
1705  /* Simple loop per edge */
1706  for (i = 1; i < pa_in->npoints; i++)
1707  {
1708  getPoint4d_p(pa_in, i-1, &p1);
1709  getPoint4d_p(pa_in, i, &p2);
1710  geographic_point_init(p1.x, p1.y, &g1);
1711  geographic_point_init(p2.x, p2.y, &g2);
1712 
1713  /* Skip duplicate points (except in case of 2-point lines!) */
1714  if ((pa_in->npoints > 2) && p4d_same(&p1, &p2))
1715  continue;
1716 
1717  /* How long is this edge? */
1718  double d = sphere_distance(&g1, &g2);
1719 
1720  if (d > max_seg_length)
1721  {
1722  geog2cart(&g1, &q1);
1723  geog2cart(&g2, &q2);
1724  /* 3-d end points, XYZM end point, current edge size, min edge size */
1725  ptarray_segmentize_sphere_edge_recursive(&q1, &q2, &p1, &p2, d, max_seg_length, pa_out);
1726  }
1727  /* If we don't segmentize, we need to add first point manually */
1728  else
1729  {
1730  ptarray_append_point(pa_out, &p1, LW_TRUE);
1731  }
1732  }
1733  /* Always add the last point */
1734  ptarray_append_point(pa_out, &p2, LW_TRUE);
1735  return pa_out;
1736 }
1737 
1744 LWGEOM*
1745 lwgeom_segmentize_sphere(const LWGEOM *lwg_in, double max_seg_length)
1746 {
1747  POINTARRAY *pa_out;
1748  LWLINE *lwline;
1749  LWPOLY *lwpoly_in, *lwpoly_out;
1750  LWCOLLECTION *lwcol_in, *lwcol_out;
1751  uint32_t i;
1752 
1753  /* Reflect NULL */
1754  if ( ! lwg_in )
1755  return NULL;
1756 
1757  /* Clone empty */
1758  if ( lwgeom_is_empty(lwg_in) )
1759  return lwgeom_clone(lwg_in);
1760 
1761  switch (lwg_in->type)
1762  {
1763  case MULTIPOINTTYPE:
1764  case POINTTYPE:
1765  return lwgeom_clone_deep(lwg_in);
1766  break;
1767  case LINETYPE:
1768  lwline = lwgeom_as_lwline(lwg_in);
1769  pa_out = ptarray_segmentize_sphere(lwline->points, max_seg_length);
1770  return lwline_as_lwgeom(lwline_construct(lwg_in->srid, NULL, pa_out));
1771  break;
1772  case POLYGONTYPE:
1773  lwpoly_in = lwgeom_as_lwpoly(lwg_in);
1774  lwpoly_out = lwpoly_construct_empty(lwg_in->srid, lwgeom_has_z(lwg_in), lwgeom_has_m(lwg_in));
1775  for ( i = 0; i < lwpoly_in->nrings; i++ )
1776  {
1777  pa_out = ptarray_segmentize_sphere(lwpoly_in->rings[i], max_seg_length);
1778  lwpoly_add_ring(lwpoly_out, pa_out);
1779  }
1780  return lwpoly_as_lwgeom(lwpoly_out);
1781  break;
1782  case MULTILINETYPE:
1783  case MULTIPOLYGONTYPE:
1784  case COLLECTIONTYPE:
1785  lwcol_in = lwgeom_as_lwcollection(lwg_in);
1786  lwcol_out = lwcollection_construct_empty(lwg_in->type, lwg_in->srid, lwgeom_has_z(lwg_in), lwgeom_has_m(lwg_in));
1787  for ( i = 0; i < lwcol_in->ngeoms; i++ )
1788  {
1789  lwcollection_add_lwgeom(lwcol_out, lwgeom_segmentize_sphere(lwcol_in->geoms[i], max_seg_length));
1790  }
1791  return lwcollection_as_lwgeom(lwcol_out);
1792  break;
1793  default:
1794  lwerror("lwgeom_segmentize_sphere: unsupported input geometry type: %d - %s",
1795  lwg_in->type, lwtype_name(lwg_in->type));
1796  break;
1797  }
1798 
1799  lwerror("lwgeom_segmentize_sphere got to the end of the function, should not happen");
1800  return NULL;
1801 }
1802 
1803 
1808 double
1810 {
1811  uint32_t i;
1812  const POINT2D *p;
1813  GEOGRAPHIC_POINT a, b, c;
1814  double area = 0.0;
1815 
1816  /* Return zero on nonsensical inputs */
1817  if ( ! pa || pa->npoints < 4 )
1818  return 0.0;
1819 
1820  p = getPoint2d_cp(pa, 0);
1821  geographic_point_init(p->x, p->y, &a);
1822  p = getPoint2d_cp(pa, 1);
1823  geographic_point_init(p->x, p->y, &b);
1824 
1825  for ( i = 2; i < pa->npoints-1; i++ )
1826  {
1827  p = getPoint2d_cp(pa, i);
1828  geographic_point_init(p->x, p->y, &c);
1829  area += sphere_signed_area(&a, &b, &c);
1830  b = c;
1831  }
1832 
1833  return fabs(area);
1834 }
1835 
1836 
1837 static double ptarray_distance_spheroid(const POINTARRAY *pa1, const POINTARRAY *pa2, const SPHEROID *s, double tolerance, int check_intersection)
1838 {
1839  GEOGRAPHIC_EDGE e1, e2;
1840  GEOGRAPHIC_POINT g1, g2;
1841  GEOGRAPHIC_POINT nearest1, nearest2;
1842  POINT3D A1, A2, B1, B2;
1843  const POINT2D *p;
1844  double distance;
1845  uint32_t i, j;
1846  int use_sphere = (s->a == s->b ? 1 : 0);
1847 
1848  /* Make result really big, so that everything will be smaller than it */
1849  distance = FLT_MAX;
1850 
1851  /* Empty point arrays? Return negative */
1852  if ( pa1->npoints == 0 || pa2->npoints == 0 )
1853  return -1.0;
1854 
1855  /* Handle point/point case here */
1856  if ( pa1->npoints == 1 && pa2->npoints == 1 )
1857  {
1858  p = getPoint2d_cp(pa1, 0);
1859  geographic_point_init(p->x, p->y, &g1);
1860  p = getPoint2d_cp(pa2, 0);
1861  geographic_point_init(p->x, p->y, &g2);
1862  /* Sphere special case, axes equal */
1863  distance = s->radius * sphere_distance(&g1, &g2);
1864  if ( use_sphere )
1865  return distance;
1866  /* Below tolerance, actual distance isn't of interest */
1867  else if ( distance < 0.95 * tolerance )
1868  return distance;
1869  /* Close or greater than tolerance, get the real answer to be sure */
1870  else
1871  return spheroid_distance(&g1, &g2, s);
1872  }
1873 
1874  /* Handle point/line case here */
1875  if ( pa1->npoints == 1 || pa2->npoints == 1 )
1876  {
1877  /* Handle one/many case here */
1878  uint32_t i;
1879  const POINTARRAY *pa_one;
1880  const POINTARRAY *pa_many;
1881 
1882  if ( pa1->npoints == 1 )
1883  {
1884  pa_one = pa1;
1885  pa_many = pa2;
1886  }
1887  else
1888  {
1889  pa_one = pa2;
1890  pa_many = pa1;
1891  }
1892 
1893  /* Initialize our point */
1894  p = getPoint2d_cp(pa_one, 0);
1895  geographic_point_init(p->x, p->y, &g1);
1896 
1897  /* Initialize start of line */
1898  p = getPoint2d_cp(pa_many, 0);
1899  geographic_point_init(p->x, p->y, &(e1.start));
1900 
1901  /* Iterate through the edges in our line */
1902  for ( i = 1; i < pa_many->npoints; i++ )
1903  {
1904  double d;
1905  p = getPoint2d_cp(pa_many, i);
1906  geographic_point_init(p->x, p->y, &(e1.end));
1907  /* Get the spherical distance between point and edge */
1908  d = s->radius * edge_distance_to_point(&e1, &g1, &g2);
1909  /* New shortest distance! Record this distance / location */
1910  if ( d < distance )
1911  {
1912  distance = d;
1913  nearest2 = g2;
1914  }
1915  /* We've gotten closer than the tolerance... */
1916  if ( d < tolerance )
1917  {
1918  /* Working on a sphere? The answer is correct, return */
1919  if ( use_sphere )
1920  {
1921  return d;
1922  }
1923  /* Far enough past the tolerance that the spheroid calculation won't change things */
1924  else if ( d < tolerance * 0.95 )
1925  {
1926  return d;
1927  }
1928  /* On a spheroid and near the tolerance? Confirm that we are *actually* closer than tolerance */
1929  else
1930  {
1931  d = spheroid_distance(&g1, &nearest2, s);
1932  /* Yes, closer than tolerance, return! */
1933  if ( d < tolerance )
1934  return d;
1935  }
1936  }
1937  e1.start = e1.end;
1938  }
1939 
1940  /* On sphere, return answer */
1941  if ( use_sphere )
1942  return distance;
1943  /* On spheroid, calculate final answer based on closest approach */
1944  else
1945  return spheroid_distance(&g1, &nearest2, s);
1946 
1947  }
1948 
1949  /* Initialize start of line 1 */
1950  p = getPoint2d_cp(pa1, 0);
1951  geographic_point_init(p->x, p->y, &(e1.start));
1952  geog2cart(&(e1.start), &A1);
1953 
1954 
1955  /* Handle line/line case */
1956  for ( i = 1; i < pa1->npoints; i++ )
1957  {
1958  p = getPoint2d_cp(pa1, i);
1959  geographic_point_init(p->x, p->y, &(e1.end));
1960  geog2cart(&(e1.end), &A2);
1961 
1962  /* Initialize start of line 2 */
1963  p = getPoint2d_cp(pa2, 0);
1964  geographic_point_init(p->x, p->y, &(e2.start));
1965  geog2cart(&(e2.start), &B1);
1966 
1967  for ( j = 1; j < pa2->npoints; j++ )
1968  {
1969  double d;
1970 
1971  p = getPoint2d_cp(pa2, j);
1972  geographic_point_init(p->x, p->y, &(e2.end));
1973  geog2cart(&(e2.end), &B2);
1974 
1975  LWDEBUGF(4, "e1.start == GPOINT(%.6g %.6g) ", e1.start.lat, e1.start.lon);
1976  LWDEBUGF(4, "e1.end == GPOINT(%.6g %.6g) ", e1.end.lat, e1.end.lon);
1977  LWDEBUGF(4, "e2.start == GPOINT(%.6g %.6g) ", e2.start.lat, e2.start.lon);
1978  LWDEBUGF(4, "e2.end == GPOINT(%.6g %.6g) ", e2.end.lat, e2.end.lon);
1979 
1980  if ( check_intersection && edge_intersects(&A1, &A2, &B1, &B2) )
1981  {
1982  LWDEBUG(4,"edge intersection! returning 0.0");
1983  return 0.0;
1984  }
1985  d = s->radius * edge_distance_to_edge(&e1, &e2, &g1, &g2);
1986  LWDEBUGF(4,"got edge_distance_to_edge %.8g", d);
1987 
1988  if ( d < distance )
1989  {
1990  distance = d;
1991  nearest1 = g1;
1992  nearest2 = g2;
1993  }
1994  if ( d < tolerance )
1995  {
1996  if ( use_sphere )
1997  {
1998  return d;
1999  }
2000  else
2001  {
2002  d = spheroid_distance(&nearest1, &nearest2, s);
2003  if ( d < tolerance )
2004  return d;
2005  }
2006  }
2007 
2008  /* Copy end to start to allow a new end value in next iteration */
2009  e2.start = e2.end;
2010  B1 = B2;
2011  }
2012 
2013  /* Copy end to start to allow a new end value in next iteration */
2014  e1.start = e1.end;
2015  A1 = A2;
2016  LW_ON_INTERRUPT(return -1.0);
2017  }
2018  LWDEBUGF(4,"finished all loops, returning %.8g", distance);
2019 
2020  if ( use_sphere )
2021  return distance;
2022  else
2023  return spheroid_distance(&nearest1, &nearest2, s);
2024 }
2025 
2026 
2033 double lwgeom_area_sphere(const LWGEOM *lwgeom, const SPHEROID *spheroid)
2034 {
2035  int type;
2036  double radius2 = spheroid->radius * spheroid->radius;
2037 
2038  assert(lwgeom);
2039 
2040  /* No area in nothing */
2041  if ( lwgeom_is_empty(lwgeom) )
2042  return 0.0;
2043 
2044  /* Read the geometry type number */
2045  type = lwgeom->type;
2046 
2047  /* Anything but polygons and collections returns zero */
2048  if ( ! ( type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE ) )
2049  return 0.0;
2050 
2051  /* Actually calculate area */
2052  if ( type == POLYGONTYPE )
2053  {
2054  LWPOLY *poly = (LWPOLY*)lwgeom;
2055  uint32_t i;
2056  double area = 0.0;
2057 
2058  /* Just in case there's no rings */
2059  if ( poly->nrings < 1 )
2060  return 0.0;
2061 
2062  /* First, the area of the outer ring */
2063  area += radius2 * ptarray_area_sphere(poly->rings[0]);
2064 
2065  /* Subtract areas of inner rings */
2066  for ( i = 1; i < poly->nrings; i++ )
2067  {
2068  area -= radius2 * ptarray_area_sphere(poly->rings[i]);
2069  }
2070  return area;
2071  }
2072 
2073  /* Recurse into sub-geometries to get area */
2074  if ( type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE )
2075  {
2076  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom;
2077  uint32_t i;
2078  double area = 0.0;
2079 
2080  for ( i = 0; i < col->ngeoms; i++ )
2081  {
2082  area += lwgeom_area_sphere(col->geoms[i], spheroid);
2083  }
2084  return area;
2085  }
2086 
2087  /* Shouldn't get here. */
2088  return 0.0;
2089 }
2090 
2091 
2101 LWPOINT* lwgeom_project_spheroid(const LWPOINT *r, const SPHEROID *spheroid, double distance, double azimuth)
2102 {
2103  GEOGRAPHIC_POINT geo_source, geo_dest;
2104  POINT4D pt_dest;
2105  double x, y;
2106  POINTARRAY *pa;
2107  LWPOINT *lwp;
2108 
2109  /* Normalize distance to be positive*/
2110  if ( distance < 0.0 ) {
2111  distance = -distance;
2112  azimuth += M_PI;
2113  }
2114 
2115  /* Normalize azimuth */
2116  azimuth -= 2.0 * M_PI * floor(azimuth / (2.0 * M_PI));
2117 
2118  /* Check the distance validity */
2119  if ( distance > (M_PI * spheroid->radius) )
2120  {
2121  lwerror("Distance must not be greater than %g", M_PI * spheroid->radius);
2122  return NULL;
2123  }
2124 
2125  /* Convert to ta geodetic point */
2126  x = lwpoint_get_x(r);
2127  y = lwpoint_get_y(r);
2128  geographic_point_init(x, y, &geo_source);
2129 
2130  /* Try the projection */
2131  if( spheroid_project(&geo_source, spheroid, distance, azimuth, &geo_dest) == LW_FAILURE )
2132  {
2133  LWDEBUGF(3, "Unable to project from (%g %g) with azimuth %g and distance %g", x, y, azimuth, distance);
2134  lwerror("Unable to project from (%g %g) with azimuth %g and distance %g", x, y, azimuth, distance);
2135  return NULL;
2136  }
2137 
2138  /* Build the output LWPOINT */
2139  pa = ptarray_construct(0, 0, 1);
2140  pt_dest.x = rad2deg(longitude_radians_normalize(geo_dest.lon));
2141  pt_dest.y = rad2deg(latitude_radians_normalize(geo_dest.lat));
2142  pt_dest.z = pt_dest.m = 0.0;
2143  ptarray_set_point4d(pa, 0, &pt_dest);
2144  lwp = lwpoint_construct(r->srid, NULL, pa);
2146  return lwp;
2147 }
2148 
2149 
2158 double lwgeom_azumith_spheroid(const LWPOINT *r, const LWPOINT *s, const SPHEROID *spheroid)
2159 {
2160  GEOGRAPHIC_POINT g1, g2;
2161  double x1, y1, x2, y2, az;
2162 
2163  /* Convert r to a geodetic point */
2164  x1 = lwpoint_get_x(r);
2165  y1 = lwpoint_get_y(r);
2166  geographic_point_init(x1, y1, &g1);
2167 
2168  /* Convert s to a geodetic point */
2169  x2 = lwpoint_get_x(s);
2170  y2 = lwpoint_get_y(s);
2171  geographic_point_init(x2, y2, &g2);
2172 
2173  /* Same point, return NaN */
2174  if ( FP_EQUALS(x1, x2) && FP_EQUALS(y1, y2) )
2175  {
2176  return NAN;
2177  }
2178 
2179  /* Do the direction calculation */
2180  az = spheroid_direction(&g1, &g2, spheroid);
2181  /* Ensure result is positive */
2182  return az < -0 ? 2*M_PI + az : az;
2183  // return az;
2184 }
2185 
2192 double lwgeom_distance_spheroid(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2, const SPHEROID *spheroid, double tolerance)
2193 {
2194  uint8_t type1, type2;
2195  int check_intersection = LW_FALSE;
2196  GBOX gbox1, gbox2;
2197 
2198  gbox_init(&gbox1);
2199  gbox_init(&gbox2);
2200 
2201  assert(lwgeom1);
2202  assert(lwgeom2);
2203 
2204  LWDEBUGF(4, "entered function, tolerance %.8g", tolerance);
2205 
2206  /* What's the distance to an empty geometry? We don't know.
2207  Return a negative number so the caller can catch this case. */
2208  if ( lwgeom_is_empty(lwgeom1) || lwgeom_is_empty(lwgeom2) )
2209  {
2210  return -1.0;
2211  }
2212 
2213  type1 = lwgeom1->type;
2214  type2 = lwgeom2->type;
2215 
2216  /* Make sure we have boxes */
2217  if ( lwgeom1->bbox )
2218  gbox1 = *(lwgeom1->bbox);
2219  else
2220  lwgeom_calculate_gbox_geodetic(lwgeom1, &gbox1);
2221 
2222  /* Make sure we have boxes */
2223  if ( lwgeom2->bbox )
2224  gbox2 = *(lwgeom2->bbox);
2225  else
2226  lwgeom_calculate_gbox_geodetic(lwgeom2, &gbox2);
2227 
2228  /* If the boxes aren't disjoint, we have to check for edge intersections */
2229  if ( gbox_overlaps(&gbox1, &gbox2) )
2230  check_intersection = LW_TRUE;
2231 
2232  /* Point/line combinations can all be handled with simple point array iterations */
2233  if ( ( type1 == POINTTYPE || type1 == LINETYPE ) &&
2234  ( type2 == POINTTYPE || type2 == LINETYPE ) )
2235  {
2236  POINTARRAY *pa1, *pa2;
2237 
2238  if ( type1 == POINTTYPE )
2239  pa1 = ((LWPOINT*)lwgeom1)->point;
2240  else
2241  pa1 = ((LWLINE*)lwgeom1)->points;
2242 
2243  if ( type2 == POINTTYPE )
2244  pa2 = ((LWPOINT*)lwgeom2)->point;
2245  else
2246  pa2 = ((LWLINE*)lwgeom2)->points;
2247 
2248  return ptarray_distance_spheroid(pa1, pa2, spheroid, tolerance, check_intersection);
2249  }
2250 
2251  /* Point/Polygon cases, if point-in-poly, return zero, else return distance. */
2252  if ( ( type1 == POLYGONTYPE && type2 == POINTTYPE ) ||
2253  ( type2 == POLYGONTYPE && type1 == POINTTYPE ) )
2254  {
2255  const POINT2D *p;
2256  LWPOLY *lwpoly;
2257  LWPOINT *lwpt;
2258  double distance = FLT_MAX;
2259  uint32_t i;
2260 
2261  if ( type1 == POINTTYPE )
2262  {
2263  lwpt = (LWPOINT*)lwgeom1;
2264  lwpoly = (LWPOLY*)lwgeom2;
2265  }
2266  else
2267  {
2268  lwpt = (LWPOINT*)lwgeom2;
2269  lwpoly = (LWPOLY*)lwgeom1;
2270  }
2271  p = getPoint2d_cp(lwpt->point, 0);
2272 
2273  /* Point in polygon implies zero distance */
2274  if ( lwpoly_covers_point2d(lwpoly, p) )
2275  {
2276  return 0.0;
2277  }
2278 
2279  /* Not inside, so what's the actual distance? */
2280  for ( i = 0; i < lwpoly->nrings; i++ )
2281  {
2282  double ring_distance = ptarray_distance_spheroid(lwpoly->rings[i], lwpt->point, spheroid, tolerance, check_intersection);
2283  if ( ring_distance < distance )
2284  distance = ring_distance;
2285  if ( distance < tolerance )
2286  return distance;
2287  }
2288  return distance;
2289  }
2290 
2291  /* Line/polygon case, if start point-in-poly, return zero, else return distance. */
2292  if ( ( type1 == POLYGONTYPE && type2 == LINETYPE ) ||
2293  ( type2 == POLYGONTYPE && type1 == LINETYPE ) )
2294  {
2295  const POINT2D *p;
2296  LWPOLY *lwpoly;
2297  LWLINE *lwline;
2298  double distance = FLT_MAX;
2299  uint32_t i;
2300 
2301  if ( type1 == LINETYPE )
2302  {
2303  lwline = (LWLINE*)lwgeom1;
2304  lwpoly = (LWPOLY*)lwgeom2;
2305  }
2306  else
2307  {
2308  lwline = (LWLINE*)lwgeom2;
2309  lwpoly = (LWPOLY*)lwgeom1;
2310  }
2311  p = getPoint2d_cp(lwline->points, 0);
2312 
2313  LWDEBUG(4, "checking if a point of line is in polygon");
2314 
2315  /* Point in polygon implies zero distance */
2316  if ( lwpoly_covers_point2d(lwpoly, p) )
2317  return 0.0;
2318 
2319  LWDEBUG(4, "checking ring distances");
2320 
2321  /* Not contained, so what's the actual distance? */
2322  for ( i = 0; i < lwpoly->nrings; i++ )
2323  {
2324  double ring_distance = ptarray_distance_spheroid(lwpoly->rings[i], lwline->points, spheroid, tolerance, check_intersection);
2325  LWDEBUGF(4, "ring[%d] ring_distance = %.8g", i, ring_distance);
2326  if ( ring_distance < distance )
2327  distance = ring_distance;
2328  if ( distance < tolerance )
2329  return distance;
2330  }
2331  LWDEBUGF(4, "all rings checked, returning distance = %.8g", distance);
2332  return distance;
2333 
2334  }
2335 
2336  /* Polygon/polygon case, if start point-in-poly, return zero, else
2337  * return distance. */
2338  if (type1 == POLYGONTYPE && type2 == POLYGONTYPE)
2339  {
2340  const POINT2D* p;
2341  LWPOLY* lwpoly1 = (LWPOLY*)lwgeom1;
2342  LWPOLY* lwpoly2 = (LWPOLY*)lwgeom2;
2343  double distance = FLT_MAX;
2344  uint32_t i, j;
2345 
2346  /* Point of 2 in polygon 1 implies zero distance */
2347  p = getPoint2d_cp(lwpoly1->rings[0], 0);
2348  if (lwpoly_covers_point2d(lwpoly2, p)) return 0.0;
2349 
2350  /* Point of 1 in polygon 2 implies zero distance */
2351  p = getPoint2d_cp(lwpoly2->rings[0], 0);
2352  if (lwpoly_covers_point2d(lwpoly1, p)) return 0.0;
2353 
2354  /* Not contained, so what's the actual distance? */
2355  for (i = 0; i < lwpoly1->nrings; i++)
2356  {
2357  for (j = 0; j < lwpoly2->nrings; j++)
2358  {
2359  double ring_distance =
2361  lwpoly1->rings[i],
2362  lwpoly2->rings[j],
2363  spheroid,
2364  tolerance,
2365  check_intersection);
2366  if (ring_distance < distance)
2367  distance = ring_distance;
2368  if (distance < tolerance) return distance;
2369  }
2370  }
2371  return distance;
2372  }
2373 
2374  /* Recurse into collections */
2375  if ( lwtype_is_collection(type1) )
2376  {
2377  uint32_t i;
2378  double distance = FLT_MAX;
2379  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom1;
2380 
2381  for ( i = 0; i < col->ngeoms; i++ )
2382  {
2383  double geom_distance = lwgeom_distance_spheroid(
2384  col->geoms[i], lwgeom2, spheroid, tolerance);
2385  if ( geom_distance < distance )
2386  distance = geom_distance;
2387  if ( distance < tolerance )
2388  return distance;
2389  }
2390  return distance;
2391  }
2392 
2393  /* Recurse into collections */
2394  if ( lwtype_is_collection(type2) )
2395  {
2396  uint32_t i;
2397  double distance = FLT_MAX;
2398  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom2;
2399 
2400  for ( i = 0; i < col->ngeoms; i++ )
2401  {
2402  double geom_distance = lwgeom_distance_spheroid(lwgeom1, col->geoms[i], spheroid, tolerance);
2403  if ( geom_distance < distance )
2404  distance = geom_distance;
2405  if ( distance < tolerance )
2406  return distance;
2407  }
2408  return distance;
2409  }
2410 
2411 
2412  lwerror("arguments include unsupported geometry type (%s, %s)", lwtype_name(type1), lwtype_name(type1));
2413  return -1.0;
2414 
2415 }
2416 
2417 
2418 int lwgeom_covers_lwgeom_sphere(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
2419 {
2420  int type1, type2;
2421  GBOX gbox1, gbox2;
2422  gbox1.flags = gbox2.flags = 0;
2423 
2424  assert(lwgeom1);
2425  assert(lwgeom2);
2426 
2427  type1 = lwgeom1->type;
2428  type2 = lwgeom2->type;
2429 
2430  /* dim(geom2) > dim(geom1) always returns false (because geom2 is bigger) */
2431  if ( (type1 == POINTTYPE && type2 == LINETYPE)
2432  || (type1 == POINTTYPE && type2 == POLYGONTYPE)
2433  || (type1 == LINETYPE && type2 == POLYGONTYPE) )
2434  {
2435  LWDEBUG(4, "dimension of geom2 is bigger than geom1");
2436  return LW_FALSE;
2437  }
2438 
2439  /* Make sure we have boxes */
2440  if ( lwgeom1->bbox )
2441  gbox1 = *(lwgeom1->bbox);
2442  else
2443  lwgeom_calculate_gbox_geodetic(lwgeom1, &gbox1);
2444 
2445  /* Make sure we have boxes */
2446  if ( lwgeom2->bbox )
2447  gbox2 = *(lwgeom2->bbox);
2448  else
2449  lwgeom_calculate_gbox_geodetic(lwgeom2, &gbox2);
2450 
2451 
2452  /* Handle the polygon/point case */
2453  if ( type1 == POLYGONTYPE && type2 == POINTTYPE )
2454  {
2455  POINT2D pt_to_test;
2456  getPoint2d_p(((LWPOINT*)lwgeom2)->point, 0, &pt_to_test);
2457  return lwpoly_covers_point2d((LWPOLY*)lwgeom1, &pt_to_test);
2458  }
2459  else if ( type1 == POLYGONTYPE && type2 == LINETYPE)
2460  {
2461  return lwpoly_covers_lwline((LWPOLY*)lwgeom1, (LWLINE*)lwgeom2);
2462  }
2463  else if ( type1 == POLYGONTYPE && type2 == POLYGONTYPE)
2464  {
2465  return lwpoly_covers_lwpoly((LWPOLY*)lwgeom1, (LWPOLY*)lwgeom2);
2466  }
2467  else if ( type1 == LINETYPE && type2 == POINTTYPE)
2468  {
2469  return lwline_covers_lwpoint((LWLINE*)lwgeom1, (LWPOINT*)lwgeom2);
2470  }
2471  else if ( type1 == LINETYPE && type2 == LINETYPE)
2472  {
2473  return lwline_covers_lwline((LWLINE*)lwgeom1, (LWLINE*)lwgeom2);
2474  }
2475  else if ( type1 == POINTTYPE && type2 == POINTTYPE)
2476  {
2477  return lwpoint_same((LWPOINT*)lwgeom1, (LWPOINT*)lwgeom2);
2478  }
2479 
2480  /* If any of the first argument parts covers the second argument, it's true */
2481  if ( lwtype_is_collection( type1 ) )
2482  {
2483  uint32_t i;
2484  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom1;
2485 
2486  for ( i = 0; i < col->ngeoms; i++ )
2487  {
2488  if ( lwgeom_covers_lwgeom_sphere(col->geoms[i], lwgeom2) )
2489  {
2490  return LW_TRUE;
2491  }
2492  }
2493  return LW_FALSE;
2494  }
2495 
2496  /* Only if all of the second arguments are covered by the first argument is the condition true */
2497  if ( lwtype_is_collection( type2 ) )
2498  {
2499  uint32_t i;
2500  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom2;
2501 
2502  for ( i = 0; i < col->ngeoms; i++ )
2503  {
2504  if ( ! lwgeom_covers_lwgeom_sphere(lwgeom1, col->geoms[i]) )
2505  {
2506  return LW_FALSE;
2507  }
2508  }
2509  return LW_TRUE;
2510  }
2511 
2512  /* Don't get here */
2513  lwerror("lwgeom_covers_lwgeom_sphere: reached end of function without resolution");
2514  return LW_FALSE;
2515 
2516 }
2517 
2523 int lwpoly_covers_point2d(const LWPOLY *poly, const POINT2D *pt_to_test)
2524 {
2525  uint32_t i;
2526  int in_hole_count = 0;
2527  POINT3D p;
2528  GEOGRAPHIC_POINT gpt_to_test;
2529  POINT2D pt_outside;
2530  GBOX gbox;
2531 #if POSTGIS_DEBUG_LEVEL >= 4
2532  char *geom_ewkt;
2533 #endif
2534  gbox.flags = 0;
2535 
2536  /* Nulls and empties don't contain anything! */
2537  if ( ! poly || lwgeom_is_empty((LWGEOM*)poly) )
2538  {
2539  LWDEBUG(4,"returning false, geometry is empty or null");
2540  return LW_FALSE;
2541  }
2542 
2543  /* Make sure we have boxes */
2544  if ( poly->bbox )
2545  gbox = *(poly->bbox);
2546  else
2547  lwgeom_calculate_gbox_geodetic((LWGEOM*)poly, &gbox);
2548 
2549  /* Point not in box? Done! */
2550  geographic_point_init(pt_to_test->x, pt_to_test->y, &gpt_to_test);
2551  geog2cart(&gpt_to_test, &p);
2552  if ( ! gbox_contains_point3d(&gbox, &p) )
2553  {
2554  LWDEBUG(4, "the point is not in the box!");
2555  return LW_FALSE;
2556  }
2557 
2558  /* Calculate our outside point from the gbox */
2559  lwpoly_pt_outside(poly, &pt_outside);
2560 
2561  LWDEBUGF(4, "pt_outside POINT(%.18g %.18g)", pt_outside.x, pt_outside.y);
2562  LWDEBUGF(4, "pt_to_test POINT(%.18g %.18g)", pt_to_test->x, pt_to_test->y);
2563 #if POSTGIS_DEBUG_LEVEL >= 4
2564  geom_ewkt = lwgeom_to_ewkt((LWGEOM*)poly);
2565  LWDEBUGF(4, "polygon %s", geom_ewkt);
2566  lwfree(geom_ewkt);
2567  geom_ewkt = gbox_to_string(&gbox);
2568  LWDEBUGF(4, "gbox %s", geom_ewkt);
2569  lwfree(geom_ewkt);
2570 #endif
2571 
2572  /* Not in outer ring? We're done! */
2573  if ( ! ptarray_contains_point_sphere(poly->rings[0], &pt_outside, pt_to_test) )
2574  {
2575  LWDEBUG(4,"returning false, point is outside ring");
2576  return LW_FALSE;
2577  }
2578 
2579  LWDEBUGF(4, "testing %d rings", poly->nrings);
2580 
2581  /* But maybe point is in a hole... */
2582  for ( i = 1; i < poly->nrings; i++ )
2583  {
2584  LWDEBUGF(4, "ring test loop %d", i);
2585  /* Count up hole containment. Odd => outside boundary. */
2586  if ( ptarray_contains_point_sphere(poly->rings[i], &pt_outside, pt_to_test) )
2587  in_hole_count++;
2588  }
2589 
2590  LWDEBUGF(4, "in_hole_count == %d", in_hole_count);
2591 
2592  if ( in_hole_count % 2 )
2593  {
2594  LWDEBUG(4,"returning false, inner ring containment count is odd");
2595  return LW_FALSE;
2596  }
2597 
2598  LWDEBUG(4,"returning true, inner ring containment count is even");
2599  return LW_TRUE;
2600 }
2601 
2607 int lwpoly_covers_lwpoly(const LWPOLY *poly1, const LWPOLY *poly2)
2608 {
2609  uint32_t i;
2610 
2611  /* Nulls and empties don't contain anything! */
2612  if ( ! poly1 || lwgeom_is_empty((LWGEOM*)poly1) )
2613  {
2614  LWDEBUG(4,"returning false, geometry1 is empty or null");
2615  return LW_FALSE;
2616  }
2617 
2618  /* Nulls and empties don't contain anything! */
2619  if ( ! poly2 || lwgeom_is_empty((LWGEOM*)poly2) )
2620  {
2621  LWDEBUG(4,"returning false, geometry2 is empty or null");
2622  return LW_FALSE;
2623  }
2624 
2625  /* check if all vertices of poly2 are inside poly1 */
2626  for (i = 0; i < poly2->nrings; i++)
2627  {
2628  if (LW_FALSE == lwpoly_covers_pointarray(poly1, poly2->rings[i]))
2629  {
2630  LWDEBUG(4,"returning false, geometry2 has point outside of geometry1");
2631  return LW_FALSE;
2632  }
2633  }
2634 
2635  /* check for any edge intersections, so nothing is partially outside of poly1 */
2636  for (i = 0; i < poly2->nrings; i++)
2637  {
2638  if (LW_TRUE == lwpoly_intersects_line(poly1, poly2->rings[i]))
2639  {
2640  LWDEBUG(4,"returning false, geometry2 is partially outside of geometry1");
2641  return LW_FALSE;
2642  }
2643  }
2644 
2645  /* no abort condition found, so the poly2 should be completly inside poly1 */
2646  return LW_TRUE;
2647 }
2648 
2652 int lwpoly_covers_lwline(const LWPOLY *poly, const LWLINE *line)
2653 {
2654  /* Nulls and empties don't contain anything! */
2655  if ( ! poly || lwgeom_is_empty((LWGEOM*)poly) )
2656  {
2657  LWDEBUG(4,"returning false, geometry1 is empty or null");
2658  return LW_FALSE;
2659  }
2660 
2661  /* Nulls and empties don't contain anything! */
2662  if ( ! line || lwgeom_is_empty((LWGEOM*)line) )
2663  {
2664  LWDEBUG(4,"returning false, geometry2 is empty or null");
2665  return LW_FALSE;
2666  }
2667 
2668  if (LW_FALSE == lwpoly_covers_pointarray(poly, line->points))
2669  {
2670  LWDEBUG(4,"returning false, geometry2 has point outside of geometry1");
2671  return LW_FALSE;
2672  }
2673 
2674  /* check for any edge intersections, so nothing is partially outside of poly1 */
2675  if (LW_TRUE == lwpoly_intersects_line(poly, line->points))
2676  {
2677  LWDEBUG(4,"returning false, geometry2 is partially outside of geometry1");
2678  return LW_FALSE;
2679  }
2680 
2681  /* no abort condition found, so the poly2 should be completely inside poly1 */
2682  return LW_TRUE;
2683 }
2684 
2688 int lwpoly_covers_pointarray(const LWPOLY* lwpoly, const POINTARRAY* pta)
2689 {
2690  uint32_t i;
2691  for (i = 0; i < pta->npoints; i++) {
2692  const POINT2D* pt_to_test = getPoint2d_cp(pta, i);
2693 
2694  if ( LW_FALSE == lwpoly_covers_point2d(lwpoly, pt_to_test) ) {
2695  LWDEBUG(4,"returning false, geometry2 has point outside of geometry1");
2696  return LW_FALSE;
2697  }
2698  }
2699 
2700  return LW_TRUE;
2701 }
2702 
2707 int lwpoly_intersects_line(const LWPOLY* lwpoly, const POINTARRAY* line)
2708 {
2709  uint32_t i, j, k;
2710  POINT3D pa1, pa2, pb1, pb2;
2711  for (i = 0; i < lwpoly->nrings; i++)
2712  {
2713  for (j = 0; j < lwpoly->rings[i]->npoints - 1; j++)
2714  {
2715  const POINT2D* a1 = getPoint2d_cp(lwpoly->rings[i], j);
2716  const POINT2D* a2 = getPoint2d_cp(lwpoly->rings[i], j+1);
2717 
2718  /* Set up our stab line */
2719  ll2cart(a1, &pa1);
2720  ll2cart(a2, &pa2);
2721 
2722  for (k = 0; k < line->npoints - 1; k++)
2723  {
2724  const POINT2D* b1 = getPoint2d_cp(line, k);
2725  const POINT2D* b2 = getPoint2d_cp(line, k+1);
2726 
2727  /* Set up our stab line */
2728  ll2cart(b1, &pb1);
2729  ll2cart(b2, &pb2);
2730 
2731  int inter = edge_intersects(&pa1, &pa2, &pb1, &pb2);
2732 
2733  /* ignore same edges */
2734  if (inter & PIR_INTERSECTS
2735  && !(inter & PIR_B_TOUCH_RIGHT || inter & PIR_COLINEAR) )
2736  {
2737  return LW_TRUE;
2738  }
2739  }
2740  }
2741  }
2742 
2743  return LW_FALSE;
2744 }
2745 
2749 int lwline_covers_lwpoint(const LWLINE* lwline, const LWPOINT* lwpoint)
2750 {
2751  uint32_t i;
2752  GEOGRAPHIC_POINT p;
2753  GEOGRAPHIC_EDGE e;
2754 
2755  for ( i = 0; i < lwline->points->npoints - 1; i++)
2756  {
2757  const POINT2D* a1 = getPoint2d_cp(lwline->points, i);
2758  const POINT2D* a2 = getPoint2d_cp(lwline->points, i+1);
2759 
2760  geographic_point_init(a1->x, a1->y, &(e.start));
2761  geographic_point_init(a2->x, a2->y, &(e.end));
2762 
2763  geographic_point_init(lwpoint_get_x(lwpoint), lwpoint_get_y(lwpoint), &p);
2764 
2765  if ( edge_contains_point(&e, &p) ) {
2766  return LW_TRUE;
2767  }
2768  }
2769 
2770  return LW_FALSE;
2771 }
2772 
2778 int lwline_covers_lwline(const LWLINE* lwline1, const LWLINE* lwline2)
2779 {
2780  uint32_t i, j;
2781  GEOGRAPHIC_EDGE e1, e2;
2782  GEOGRAPHIC_POINT p1, p2;
2783  int start = LW_FALSE;
2784  int changed = LW_FALSE;
2785 
2786  /* first point on line */
2787  if ( ! lwline_covers_lwpoint(lwline1, lwline_get_lwpoint(lwline2, 0)))
2788  {
2789  LWDEBUG(4,"returning false, first point of line2 is not covered by line1");
2790  return LW_FALSE;
2791  }
2792 
2793  /* last point on line */
2794  if ( ! lwline_covers_lwpoint(lwline1, lwline_get_lwpoint(lwline2, lwline2->points->npoints - 1)))
2795  {
2796  LWDEBUG(4,"returning false, last point of line2 is not covered by line1");
2797  return LW_FALSE;
2798  }
2799 
2800  j = 0;
2801  i = 0;
2802  while (i < lwline1->points->npoints - 1 && j < lwline2->points->npoints - 1)
2803  {
2804  changed = LW_FALSE;
2805  const POINT2D* a1 = getPoint2d_cp(lwline1->points, i);
2806  const POINT2D* a2 = getPoint2d_cp(lwline1->points, i+1);
2807  const POINT2D* b1 = getPoint2d_cp(lwline2->points, j);
2808  const POINT2D* b2 = getPoint2d_cp(lwline2->points, j+1);
2809 
2810  geographic_point_init(a1->x, a1->y, &(e1.start));
2811  geographic_point_init(a2->x, a2->y, &(e1.end));
2812  geographic_point_init(b1->x, b1->y, &p2);
2813 
2814  /* we already know, that the last point is on line1, so we're done */
2815  if ( j == lwline2->points->npoints - 1)
2816  {
2817  return LW_TRUE;
2818  }
2819  else if (start == LW_TRUE)
2820  {
2821  /* point is on current line1 edge, check next point in line2 */
2822  if ( edge_contains_point(&e1, &p2)) {
2823  j++;
2824  changed = LW_TRUE;
2825  }
2826 
2827  geographic_point_init(a1->x, a1->y, &(e2.start));
2828  geographic_point_init(a2->x, b2->y, &(e2.end));
2829  geographic_point_init(a1->x, a1->y, &p1);
2830 
2831  /* point is on current line2 edge, check next point in line1 */
2832  if ( edge_contains_point(&e2, &p1)) {
2833  i++;
2834  changed = LW_TRUE;
2835  }
2836 
2837  /* no edge progressed -> point left one line */
2838  if ( changed == LW_FALSE )
2839  {
2840  LWDEBUG(4,"returning false, found point not covered by both lines");
2841  return LW_FALSE;
2842  }
2843  else
2844  {
2845  continue;
2846  }
2847  }
2848 
2849  /* find first edge to cover line2 */
2850  if (edge_contains_point(&e1, &p2))
2851  {
2852  start = LW_TRUE;
2853  }
2854 
2855  /* next line1 edge */
2856  i++;
2857  }
2858 
2859  /* no uncovered point found */
2860  return LW_TRUE;
2861 }
2862 
2864 {
2865  uint32_t i;
2866  int first = LW_TRUE;
2867  const POINT2D *p;
2868  POINT3D A1, A2;
2869  GBOX edge_gbox;
2870 
2871  assert(gbox);
2872  assert(pa);
2873 
2874  gbox_init(&edge_gbox);
2875  edge_gbox.flags = gbox->flags;
2876 
2877  if ( pa->npoints == 0 ) return LW_FAILURE;
2878 
2879  if ( pa->npoints == 1 )
2880  {
2881  p = getPoint2d_cp(pa, 0);
2882  ll2cart(p, &A1);
2883  gbox->xmin = gbox->xmax = A1.x;
2884  gbox->ymin = gbox->ymax = A1.y;
2885  gbox->zmin = gbox->zmax = A1.z;
2886  return LW_SUCCESS;
2887  }
2888 
2889  p = getPoint2d_cp(pa, 0);
2890  ll2cart(p, &A1);
2891 
2892  for ( i = 1; i < pa->npoints; i++ )
2893  {
2894 
2895  p = getPoint2d_cp(pa, i);
2896  ll2cart(p, &A2);
2897 
2898  edge_calculate_gbox(&A1, &A2, &edge_gbox);
2899 
2900  /* Initialize the box */
2901  if ( first )
2902  {
2903  gbox_duplicate(&edge_gbox, gbox);
2904  first = LW_FALSE;
2905  }
2906  /* Expand the box where necessary */
2907  else
2908  {
2909  gbox_merge(&edge_gbox, gbox);
2910  }
2911 
2912  A1 = A2;
2913  }
2914 
2915  return LW_SUCCESS;
2916 }
2917 
2918 static int lwpoint_calculate_gbox_geodetic(const LWPOINT *point, GBOX *gbox)
2919 {
2920  assert(point);
2921  return ptarray_calculate_gbox_geodetic(point->point, gbox);
2922 }
2923 
2924 static int lwline_calculate_gbox_geodetic(const LWLINE *line, GBOX *gbox)
2925 {
2926  assert(line);
2927  return ptarray_calculate_gbox_geodetic(line->points, gbox);
2928 }
2929 
2930 static int lwpolygon_calculate_gbox_geodetic(const LWPOLY *poly, GBOX *gbox)
2931 {
2932  GBOX ringbox;
2933  uint32_t i;
2934  int first = LW_TRUE;
2935  assert(poly);
2936  if ( poly->nrings == 0 )
2937  return LW_FAILURE;
2938  ringbox.flags = gbox->flags;
2939  for ( i = 0; i < poly->nrings; i++ )
2940  {
2941  if ( ptarray_calculate_gbox_geodetic(poly->rings[i], &ringbox) == LW_FAILURE )
2942  return LW_FAILURE;
2943  if ( first )
2944  {
2945  gbox_duplicate(&ringbox, gbox);
2946  first = LW_FALSE;
2947  }
2948  else
2949  {
2950  gbox_merge(&ringbox, gbox);
2951  }
2952  }
2953 
2954  /* If the box wraps a poly, push that axis to the absolute min/max as appropriate */
2955  gbox_check_poles(gbox);
2956 
2957  return LW_SUCCESS;
2958 }
2959 
2960 static int lwtriangle_calculate_gbox_geodetic(const LWTRIANGLE *triangle, GBOX *gbox)
2961 {
2962  assert(triangle);
2963  return ptarray_calculate_gbox_geodetic(triangle->points, gbox);
2964 }
2965 
2966 
2968 {
2969  GBOX subbox = {0};
2970  uint32_t i;
2971  int result = LW_FAILURE;
2972  int first = LW_TRUE;
2973  assert(coll);
2974  if ( coll->ngeoms == 0 )
2975  return LW_FAILURE;
2976 
2977  subbox.flags = gbox->flags;
2978 
2979  for ( i = 0; i < coll->ngeoms; i++ )
2980  {
2981  if ( lwgeom_calculate_gbox_geodetic((LWGEOM*)(coll->geoms[i]), &subbox) == LW_SUCCESS )
2982  {
2983  /* Keep a copy of the sub-bounding box for later */
2984  if ( coll->geoms[i]->bbox )
2985  lwfree(coll->geoms[i]->bbox);
2986  coll->geoms[i]->bbox = gbox_copy(&subbox);
2987  if ( first )
2988  {
2989  gbox_duplicate(&subbox, gbox);
2990  first = LW_FALSE;
2991  }
2992  else
2993  {
2994  gbox_merge(&subbox, gbox);
2995  }
2996  result = LW_SUCCESS;
2997  }
2998  }
2999  return result;
3000 }
3001 
3003 {
3004  int result = LW_FAILURE;
3005  LWDEBUGF(4, "got type %d", geom->type);
3006 
3007  /* Add a geodetic flag to the incoming gbox */
3008  gbox->flags = lwflags(FLAGS_GET_Z(geom->flags),FLAGS_GET_M(geom->flags),1);
3009 
3010  switch (geom->type)
3011  {
3012  case POINTTYPE:
3014  break;
3015  case LINETYPE:
3016  result = lwline_calculate_gbox_geodetic((LWLINE *)geom, gbox);
3017  break;
3018  case POLYGONTYPE:
3020  break;
3021  case TRIANGLETYPE:
3023  break;
3024  case MULTIPOINTTYPE:
3025  case MULTILINETYPE:
3026  case MULTIPOLYGONTYPE:
3027  case POLYHEDRALSURFACETYPE:
3028  case TINTYPE:
3029  case COLLECTIONTYPE:
3031  break;
3032  default:
3033  lwerror("lwgeom_calculate_gbox_geodetic: unsupported input geometry type: %d - %s",
3034  geom->type, lwtype_name(geom->type));
3035  break;
3036  }
3037  return result;
3038 }
3039 
3040 
3041 
3042 static int ptarray_check_geodetic(const POINTARRAY *pa)
3043 {
3044  uint32_t t;
3045  POINT2D pt;
3046 
3047  assert(pa);
3048 
3049  for (t=0; t<pa->npoints; t++)
3050  {
3051  getPoint2d_p(pa, t, &pt);
3052  /* printf( "%d (%g, %g)\n", t, pt.x, pt.y); */
3053  if ( pt.x < -180.0 || pt.y < -90.0 || pt.x > 180.0 || pt.y > 90.0 )
3054  return LW_FALSE;
3055  }
3056 
3057  return LW_TRUE;
3058 }
3059 
3060 static int lwpoint_check_geodetic(const LWPOINT *point)
3061 {
3062  assert(point);
3063  return ptarray_check_geodetic(point->point);
3064 }
3065 
3066 static int lwline_check_geodetic(const LWLINE *line)
3067 {
3068  assert(line);
3069  return ptarray_check_geodetic(line->points);
3070 }
3071 
3072 static int lwpoly_check_geodetic(const LWPOLY *poly)
3073 {
3074  uint32_t i = 0;
3075  assert(poly);
3076 
3077  for ( i = 0; i < poly->nrings; i++ )
3078  {
3079  if ( ptarray_check_geodetic(poly->rings[i]) == LW_FALSE )
3080  return LW_FALSE;
3081  }
3082  return LW_TRUE;
3083 }
3084 
3085 static int lwtriangle_check_geodetic(const LWTRIANGLE *triangle)
3086 {
3087  assert(triangle);
3088  return ptarray_check_geodetic(triangle->points);
3089 }
3090 
3091 
3093 {
3094  uint32_t i = 0;
3095  assert(col);
3096 
3097  for ( i = 0; i < col->ngeoms; i++ )
3098  {
3099  if ( lwgeom_check_geodetic(col->geoms[i]) == LW_FALSE )
3100  return LW_FALSE;
3101  }
3102  return LW_TRUE;
3103 }
3104 
3106 {
3107  if ( lwgeom_is_empty(geom) )
3108  return LW_TRUE;
3109 
3110  switch (geom->type)
3111  {
3112  case POINTTYPE:
3113  return lwpoint_check_geodetic((LWPOINT *)geom);
3114  case LINETYPE:
3115  return lwline_check_geodetic((LWLINE *)geom);
3116  case POLYGONTYPE:
3117  return lwpoly_check_geodetic((LWPOLY *)geom);
3118  case TRIANGLETYPE:
3119  return lwtriangle_check_geodetic((LWTRIANGLE *)geom);
3120  case MULTIPOINTTYPE:
3121  case MULTILINETYPE:
3122  case MULTIPOLYGONTYPE:
3123  case POLYHEDRALSURFACETYPE:
3124  case TINTYPE:
3125  case COLLECTIONTYPE:
3126  return lwcollection_check_geodetic((LWCOLLECTION *)geom);
3127  default:
3128  lwerror("lwgeom_check_geodetic: unsupported input geometry type: %d - %s",
3129  geom->type, lwtype_name(geom->type));
3130  }
3131  return LW_FALSE;
3132 }
3133 
3135 {
3136  uint32_t t;
3137  int changed = LW_FALSE;
3138  POINT4D pt;
3139 
3140  assert(pa);
3141 
3142  for ( t=0; t < pa->npoints; t++ )
3143  {
3144  getPoint4d_p(pa, t, &pt);
3145  if ( pt.x < -180.0 || pt.x > 180.0 || pt.y < -90.0 || pt.y > 90.0 )
3146  {
3147  pt.x = longitude_degrees_normalize(pt.x);
3148  pt.y = latitude_degrees_normalize(pt.y);
3149  ptarray_set_point4d(pa, t, &pt);
3150  changed = LW_TRUE;
3151  }
3152  }
3153  return changed;
3154 }
3155 
3157 {
3158  assert(point);
3159  return ptarray_force_geodetic(point->point);
3160 }
3161 
3163 {
3164  assert(line);
3165  return ptarray_force_geodetic(line->points);
3166 }
3167 
3169 {
3170  uint32_t i = 0;
3171  int changed = LW_FALSE;
3172  assert(poly);
3173 
3174  for ( i = 0; i < poly->nrings; i++ )
3175  {
3176  if ( ptarray_force_geodetic(poly->rings[i]) == LW_TRUE )
3177  changed = LW_TRUE;
3178  }
3179  return changed;
3180 }
3181 
3183 {
3184  uint32_t i = 0;
3185  int changed = LW_FALSE;
3186  assert(col);
3187 
3188  for ( i = 0; i < col->ngeoms; i++ )
3189  {
3190  if ( lwgeom_force_geodetic(col->geoms[i]) == LW_TRUE )
3191  changed = LW_TRUE;
3192  }
3193  return changed;
3194 }
3195 
3197 {
3198  switch ( lwgeom_get_type(geom) )
3199  {
3200  case POINTTYPE:
3201  return lwpoint_force_geodetic((LWPOINT *)geom);
3202  case LINETYPE:
3203  return lwline_force_geodetic((LWLINE *)geom);
3204  case POLYGONTYPE:
3205  return lwpoly_force_geodetic((LWPOLY *)geom);
3206  case MULTIPOINTTYPE:
3207  case MULTILINETYPE:
3208  case MULTIPOLYGONTYPE:
3209  case COLLECTIONTYPE:
3210  return lwcollection_force_geodetic((LWCOLLECTION *)geom);
3211  default:
3212  lwerror("unsupported input geometry type: %d", lwgeom_get_type(geom));
3213  }
3214  return LW_FALSE;
3215 }
3216 
3217 
3219 {
3220  GEOGRAPHIC_POINT a, b;
3221  double za = 0.0, zb = 0.0;
3222  POINT4D p;
3223  uint32_t i;
3224  int hasz = LW_FALSE;
3225  double length = 0.0;
3226  double seglength = 0.0;
3227 
3228  /* Return zero on non-sensical inputs */
3229  if ( ! pa || pa->npoints < 2 )
3230  return 0.0;
3231 
3232  /* See if we have a third dimension */
3233  hasz = FLAGS_GET_Z(pa->flags);
3234 
3235  /* Initialize first point */
3236  getPoint4d_p(pa, 0, &p);
3237  geographic_point_init(p.x, p.y, &a);
3238  if ( hasz )
3239  za = p.z;
3240 
3241  /* Loop and sum the length for each segment */
3242  for ( i = 1; i < pa->npoints; i++ )
3243  {
3244  seglength = 0.0;
3245  getPoint4d_p(pa, i, &p);
3246  geographic_point_init(p.x, p.y, &b);
3247  if ( hasz )
3248  zb = p.z;
3249 
3250  /* Special sphere case */
3251  if ( s->a == s->b )
3252  seglength = s->radius * sphere_distance(&a, &b);
3253  /* Spheroid case */
3254  else
3255  seglength = spheroid_distance(&a, &b, s);
3256 
3257  /* Add in the vertical displacement if we're in 3D */
3258  if ( hasz )
3259  seglength = sqrt( (zb-za)*(zb-za) + seglength*seglength );
3260 
3261  /* Add this segment length to the total */
3262  length += seglength;
3263 
3264  /* B gets incremented in the next loop, so we save the value here */
3265  a = b;
3266  za = zb;
3267  }
3268  return length;
3269 }
3270 
3271 double lwgeom_length_spheroid(const LWGEOM *geom, const SPHEROID *s)
3272 {
3273  int type;
3274  uint32_t i = 0;
3275  double length = 0.0;
3276 
3277  assert(geom);
3278 
3279  /* No area in nothing */
3280  if ( lwgeom_is_empty(geom) )
3281  return 0.0;
3282 
3283  type = geom->type;
3284 
3285  if ( type == POINTTYPE || type == MULTIPOINTTYPE )
3286  return 0.0;
3287 
3288  if ( type == LINETYPE )
3289  return ptarray_length_spheroid(((LWLINE*)geom)->points, s);
3290 
3291  if ( type == POLYGONTYPE )
3292  {
3293  LWPOLY *poly = (LWPOLY*)geom;
3294  for ( i = 0; i < poly->nrings; i++ )
3295  {
3296  length += ptarray_length_spheroid(poly->rings[i], s);
3297  }
3298  return length;
3299  }
3300 
3301  if ( type == TRIANGLETYPE )
3302  return ptarray_length_spheroid(((LWTRIANGLE*)geom)->points, s);
3303 
3304  if ( lwtype_is_collection( type ) )
3305  {
3306  LWCOLLECTION *col = (LWCOLLECTION*)geom;
3307 
3308  for ( i = 0; i < col->ngeoms; i++ )
3309  {
3310  length += lwgeom_length_spheroid(col->geoms[i], s);
3311  }
3312  return length;
3313  }
3314 
3315  lwerror("unsupported type passed to lwgeom_length_sphere");
3316  return 0.0;
3317 }
3318 
3325 static int
3327 {
3328 
3329  uint32_t i;
3330  POINT4D p;
3331  int altered = LW_FALSE;
3332  int rv = LW_FALSE;
3333  static double tolerance = 1e-10;
3334 
3335  if ( ! pa )
3336  lwerror("ptarray_nudge_geodetic called with null input");
3337 
3338  for(i = 0; i < pa->npoints; i++ )
3339  {
3340  getPoint4d_p(pa, i, &p);
3341  if ( p.x < -180.0 && (-180.0 - p.x < tolerance) )
3342  {
3343  p.x = -180.0;
3344  altered = LW_TRUE;
3345  }
3346  if ( p.x > 180.0 && (p.x - 180.0 < tolerance) )
3347  {
3348  p.x = 180.0;
3349  altered = LW_TRUE;
3350  }
3351  if ( p.y < -90.0 && (-90.0 - p.y < tolerance) )
3352  {
3353  p.y = -90.0;
3354  altered = LW_TRUE;
3355  }
3356  if ( p.y > 90.0 && (p.y - 90.0 < tolerance) )
3357  {
3358  p.y = 90.0;
3359  altered = LW_TRUE;
3360  }
3361  if ( altered == LW_TRUE )
3362  {
3363  ptarray_set_point4d(pa, i, &p);
3364  altered = LW_FALSE;
3365  rv = LW_TRUE;
3366  }
3367  }
3368  return rv;
3369 }
3370 
3377 int
3379 {
3380  int type;
3381  uint32_t i = 0;
3382  int rv = LW_FALSE;
3383 
3384  assert(geom);
3385 
3386  /* No points in nothing */
3387  if ( lwgeom_is_empty(geom) )
3388  return LW_FALSE;
3389 
3390  type = geom->type;
3391 
3392  if ( type == POINTTYPE )
3393  return ptarray_nudge_geodetic(((LWPOINT*)geom)->point);
3394 
3395  if ( type == LINETYPE )
3396  return ptarray_nudge_geodetic(((LWLINE*)geom)->points);
3397 
3398  if ( type == POLYGONTYPE )
3399  {
3400  LWPOLY *poly = (LWPOLY*)geom;
3401  for ( i = 0; i < poly->nrings; i++ )
3402  {
3403  int n = ptarray_nudge_geodetic(poly->rings[i]);
3404  rv = (rv == LW_TRUE ? rv : n);
3405  }
3406  return rv;
3407  }
3408 
3409  if ( type == TRIANGLETYPE )
3410  return ptarray_nudge_geodetic(((LWTRIANGLE*)geom)->points);
3411 
3412  if ( lwtype_is_collection( type ) )
3413  {
3414  LWCOLLECTION *col = (LWCOLLECTION*)geom;
3415 
3416  for ( i = 0; i < col->ngeoms; i++ )
3417  {
3418  int n = lwgeom_nudge_geodetic(col->geoms[i]);
3419  rv = (rv == LW_TRUE ? rv : n);
3420  }
3421  return rv;
3422  }
3423 
3424  lwerror("unsupported type (%s) passed to lwgeom_nudge_geodetic", lwtype_name(type));
3425  return rv;
3426 }
3427 
3428 
3432 static int
3433 point_in_cone(const POINT3D *A1, const POINT3D *A2, const POINT3D *P)
3434 {
3435  POINT3D AC; /* Center point of A1/A2 */
3436  double min_similarity, similarity;
3437 
3438  /* Boundary case */
3439  if (point3d_equals(A1, P) || point3d_equals(A2, P))
3440  return LW_TRUE;
3441 
3442  /* The normalized sum bisects the angle between start and end. */
3443  vector_sum(A1, A2, &AC);
3444  normalize(&AC);
3445 
3446  /* The projection of start onto the center defines the minimum similarity */
3447  min_similarity = dot_product(A1, &AC);
3448 
3449  /* If the edge is sufficiently curved, use the dot product test */
3450  if (fabs(1.0 - min_similarity) > 1e-10)
3451  {
3452  /* The projection of candidate p onto the center */
3453  similarity = dot_product(P, &AC);
3454 
3455  /* If the projection of the candidate is larger than */
3456  /* the projection of the start point, the candidate */
3457  /* must be closer to the center than the start, so */
3458  /* therefor inside the cone */
3459  if (similarity > min_similarity)
3460  {
3461  return LW_TRUE;
3462  }
3463  else
3464  {
3465  return LW_FALSE;
3466  }
3467  }
3468  else
3469  {
3470  /* Where the edge is very narrow, the dot product test */
3471  /* fails, but we can use the almost-planar nature of the */
3472  /* problem space then to test if the vector from the */
3473  /* candidate to the start point in a different direction */
3474  /* to the vector from candidate to end point */
3475  /* If so, then candidate is between start and end */
3476  POINT3D PA1, PA2;
3477  vector_difference(P, A1, &PA1);
3478  vector_difference(P, A2, &PA2);
3479  normalize(&PA1);
3480  normalize(&PA2);
3481  if (dot_product(&PA1, &PA2) < 0.0)
3482  {
3483  return LW_TRUE;
3484  }
3485  else
3486  {
3487  return LW_FALSE;
3488  }
3489  }
3490  return LW_FALSE;
3491 }
3492 
3493 
3494 
3499 static int
3500 dot_product_side(const POINT3D *p, const POINT3D *q)
3501 {
3502  double dp = dot_product(p, q);
3503 
3504  if ( FP_IS_ZERO(dp) )
3505  return 0;
3506 
3507  return dp < 0.0 ? -1 : 1;
3508 }
3509 
3514 uint32_t
3515 edge_intersects(const POINT3D *A1, const POINT3D *A2, const POINT3D *B1, const POINT3D *B2)
3516 {
3517  POINT3D AN, BN, VN; /* Normals to plane A and plane B */
3518  double ab_dot;
3519  int a1_side, a2_side, b1_side, b2_side;
3520  int rv = PIR_NO_INTERACT;
3521 
3522  /* Normals to the A-plane and B-plane */
3523  unit_normal(A1, A2, &AN);
3524  unit_normal(B1, B2, &BN);
3525 
3526  /* Are A-plane and B-plane basically the same? */
3527  ab_dot = dot_product(&AN, &BN);
3528 
3529  /*
3530  * https://trac.osgeo.org/postgis/ticket/5765
3531  * Failure because the colinearity check was
3532  * triggering due to an overly loose equality
3533  * check here.
3534  * if ( FP_EQUALS(fabs(ab_dot), 1.0) )
3535  */
3536  if ( 1.0 - fabs(ab_dot) <= 10e-16 )
3537  {
3538  /* Co-linear case */
3539  if ( point_in_cone(A1, A2, B1) || point_in_cone(A1, A2, B2) ||
3540  point_in_cone(B1, B2, A1) || point_in_cone(B1, B2, A2) )
3541  {
3542  rv |= PIR_INTERSECTS;
3543  rv |= PIR_COLINEAR;
3544  }
3545  return rv;
3546  }
3547 
3548  /* What side of plane-A and plane-B do the end points */
3549  /* of A and B fall? */
3550  a1_side = dot_product_side(&BN, A1);
3551  a2_side = dot_product_side(&BN, A2);
3552  b1_side = dot_product_side(&AN, B1);
3553  b2_side = dot_product_side(&AN, B2);
3554 
3555  /* Both ends of A on the same side of plane B. */
3556  if ( a1_side == a2_side && a1_side != 0 )
3557  {
3558  /* No intersection. */
3559  return PIR_NO_INTERACT;
3560  }
3561 
3562  /* Both ends of B on the same side of plane A. */
3563  if ( b1_side == b2_side && b1_side != 0 )
3564  {
3565  /* No intersection. */
3566  return PIR_NO_INTERACT;
3567  }
3568 
3569  /* A straddles B and B straddles A, so... */
3570  if ( a1_side != a2_side && (a1_side + a2_side) == 0 &&
3571  b1_side != b2_side && (b1_side + b2_side) == 0 )
3572  {
3573  /* Have to check if intersection point is inside both arcs */
3574  unit_normal(&AN, &BN, &VN);
3575  if ( point_in_cone(A1, A2, &VN) && point_in_cone(B1, B2, &VN) )
3576  {
3577  return PIR_INTERSECTS;
3578  }
3579 
3580  /* Have to check if intersection point is inside both arcs */
3581  vector_scale(&VN, -1);
3582  if ( point_in_cone(A1, A2, &VN) && point_in_cone(B1, B2, &VN) )
3583  {
3584  return PIR_INTERSECTS;
3585  }
3586 
3587  return PIR_NO_INTERACT;
3588  }
3589 
3590  /* The rest are all intersects variants... */
3591  rv |= PIR_INTERSECTS;
3592 
3593  /* A touches B */
3594  if ( a1_side == 0 )
3595  {
3596  /* Touches at A1, A2 is on what side? */
3597  rv |= (a2_side < 0 ? PIR_A_TOUCH_RIGHT : PIR_A_TOUCH_LEFT);
3598  }
3599  else if ( a2_side == 0 )
3600  {
3601  /* Touches at A2, A1 is on what side? */
3602  rv |= (a1_side < 0 ? PIR_A_TOUCH_RIGHT : PIR_A_TOUCH_LEFT);
3603  }
3604 
3605  /* B touches A */
3606  if ( b1_side == 0 )
3607  {
3608  /* Touches at B1, B2 is on what side? */
3609  rv |= (b2_side < 0 ? PIR_B_TOUCH_RIGHT : PIR_B_TOUCH_LEFT);
3610  }
3611  else if ( b2_side == 0 )
3612  {
3613  /* Touches at B2, B1 is on what side? */
3614  rv |= (b1_side < 0 ? PIR_B_TOUCH_RIGHT : PIR_B_TOUCH_LEFT);
3615  }
3616 
3617  return rv;
3618 }
3619 
3628 int ptarray_contains_point_sphere(const POINTARRAY *pa, const POINT2D *pt_outside, const POINT2D *pt_to_test)
3629 {
3630  POINT3D S1, S2; /* Stab line end points */
3631  POINT3D E1, E2; /* Edge end points (3-space) */
3632  POINT2D p; /* Edge end points (lon/lat) */
3633  uint32_t count = 0, i, inter;
3634 
3635  /* Null input, not enough points for a ring? You ain't closed! */
3636  if ( ! pa || pa->npoints < 4 )
3637  return LW_FALSE;
3638 
3639  /* Set up our stab line */
3640  ll2cart(pt_to_test, &S1);
3641  ll2cart(pt_outside, &S2);
3642 
3643  /* Initialize first point */
3644  getPoint2d_p(pa, 0, &p);
3645  ll2cart(&p, &E1);
3646 
3647  /* Walk every edge and see if the stab line hits it */
3648  for ( i = 1; i < pa->npoints; i++ )
3649  {
3650  LWDEBUGF(4, "testing edge (%d)", i);
3651  LWDEBUGF(4, " start point == POINT(%.12g %.12g)", p.x, p.y);
3652 
3653  /* Read next point. */
3654  getPoint2d_p(pa, i, &p);
3655  ll2cart(&p, &E2);
3656 
3657  /* Skip over too-short edges. */
3658  if ( point3d_equals(&E1, &E2) )
3659  {
3660  continue;
3661  }
3662 
3663  /* Our test point is on an edge end! Point is "in ring" by our definition */
3664  if ( point3d_equals(&S1, &E1) )
3665  {
3666  return LW_TRUE;
3667  }
3668 
3669  /* Calculate relationship between stab line and edge */
3670  inter = edge_intersects(&S1, &S2, &E1, &E2);
3671 
3672  /* We have some kind of interaction... */
3673  if ( inter & PIR_INTERSECTS )
3674  {
3675  /* If the stabline is touching the edge, that implies the test point */
3676  /* is on the edge, so we're done, the point is in (on) the ring. */
3677  if ( (inter & PIR_A_TOUCH_RIGHT) || (inter & PIR_A_TOUCH_LEFT) )
3678  {
3679  return LW_TRUE;
3680  }
3681 
3682  /* It's a touching interaction, disregard all the left-side ones. */
3683  /* It's a co-linear intersection, ignore those. */
3684  if ( inter & PIR_B_TOUCH_RIGHT || inter & PIR_COLINEAR )
3685  {
3686  /* Do nothing, to avoid double counts. */
3687  LWDEBUGF(4," edge (%d) crossed, disregarding to avoid double count", i, count);
3688  }
3689  else
3690  {
3691  /* Increment crossingn count. */
3692  count++;
3693  LWDEBUGF(4," edge (%d) crossed, count == %d", i, count);
3694  }
3695  }
3696  else
3697  {
3698  LWDEBUGF(4," edge (%d) did not cross", i);
3699  }
3700 
3701  /* Increment to next edge */
3702  E1 = E2;
3703  }
3704 
3705  LWDEBUGF(4,"final count == %d", count);
3706 
3707  /* An odd number of crossings implies containment! */
3708  if ( count % 2 )
3709  {
3710  return LW_TRUE;
3711  }
3712 
3713  return LW_FALSE;
3714 }
char * s
Definition: cu_in_wkt.c:23
char * r
Definition: cu_in_wkt.c:24
static char * w
Definition: cu_out_twkb.c:25
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
int gbox_merge(const GBOX *new_box, GBOX *merge_box)
Update the merged GBOX to be large enough to include itself and the new box.
Definition: gbox.c:257
void gbox_duplicate(const GBOX *original, GBOX *duplicate)
Copy the values of original GBOX into duplicate.
Definition: gbox.c:433
int gbox_contains_point3d(const GBOX *gbox, const POINT3D *pt)
Return true if the point is inside the gbox.
Definition: gbox.c:247
int gbox_merge_point3d(const POINT3D *p, GBOX *gbox)
Update the GBOX to be large enough to include itself and the new point.
Definition: gbox.c:228
int gbox_overlaps(const GBOX *g1, const GBOX *g2)
Return LW_TRUE if the GBOX overlaps, LW_FALSE otherwise.
Definition: gbox.c:283
void gbox_init(GBOX *gbox)
Zero out all the entries in the GBOX.
Definition: gbox.c:40
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: gbox.c:426
int gbox_init_point3d(const POINT3D *p, GBOX *gbox)
Initialize a GBOX using the values of the point.
Definition: gbox.c:239
char * gbox_to_string(const GBOX *gbox)
Allocate a string representation of the GBOX, based on dimensionality of flags.
Definition: gbox.c:392
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:162
void lwgeom_set_geodetic(LWGEOM *geom, int value)
Set the FLAGS geodetic bit on geometry an all sub-geometries and pointlists.
Definition: lwgeom.c:947
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:322
#define LW_FALSE
Definition: liblwgeom.h:108
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:292
#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
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:312
#define MULTIPOINTTYPE
Definition: liblwgeom.h:119
double lwpoint_get_x(const LWPOINT *point)
Definition: lwpoint.c:63
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:512
int lwpoly_add_ring(LWPOLY *poly, POINTARRAY *pa)
Add a ring, allocating extra space if necessary.
Definition: lwpoly.c:247
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:343
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
Definition: ptarray.c:51
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:917
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition: lwgeom.c:1088
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:548
#define FLAGS_GET_Z(flags)
Definition: liblwgeom.h:179
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
#define TINTYPE
Definition: liblwgeom.h:130
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:121
void lwfree(void *mem)
Definition: lwutil.c:242
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:327
#define POLYGONTYPE
Definition: liblwgeom.h:118
LWPOINT * lwpoint_construct(int32_t srid, GBOX *bbox, POINTARRAY *point)
Definition: lwpoint.c:129
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:128
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:59
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
Definition: lwcollection.c:92
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:180
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:126
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:474
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE,...
Definition: ptarray.c:147
#define TRIANGLETYPE
Definition: liblwgeom.h:129
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:188
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:216
lwflags_t lwflags(int hasz, int hasm, int geodetic)
Construct a new flags bitmask.
Definition: lwutil.c:471
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:107
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:198
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:924
LWPOLY * lwpoly_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwpoly.c:161
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
Definition: lwgeom_api.c:370
double lwpoint_get_y(const LWPOINT *point)
Definition: lwpoint.c:76
LWPOINT * lwline_get_lwpoint(const LWLINE *line, uint32_t where)
Returns freshly allocated LWPOINT that corresponds to the index where.
Definition: lwline.c:309
int p4d_same(const POINT4D *p1, const POINT4D *p2)
Definition: lwalgorithm.c:32
int p3d_same(const POINT3D *p1, const POINT3D *p2)
Definition: lwalgorithm.c:41
#define LW_ON_INTERRUPT(x)
#define SIGNUM(n)
Macro that returns: -1 if n < 0, 1 if n > 0, 0 if n == 0.
#define FP_MAX(A, B)
#define FP_MIN(A, B)
#define FP_EQUALS(A, B)
int ptarray_has_z(const POINTARRAY *pa)
Definition: ptarray.c:37
int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
lw_segment_side()
Definition: lwalgorithm.c:65
int ptarray_has_m(const POINTARRAY *pa)
Definition: ptarray.c:44
#define FP_IS_ZERO(A)
char lwpoint_same(const LWPOINT *p1, const LWPOINT *p2)
Definition: lwpoint.c:264
int clairaut_geographic(const GEOGRAPHIC_POINT *start, const GEOGRAPHIC_POINT *end, GEOGRAPHIC_POINT *g_top, GEOGRAPHIC_POINT *g_bottom)
Computes the pole of the great circle disk which is the intersection of the great circle with the lin...
Definition: lwgeodetic.c:1101
static int lwline_check_geodetic(const LWLINE *line)
Definition: lwgeodetic.c:3066
static int lwcollection_check_geodetic(const LWCOLLECTION *col)
Definition: lwgeodetic.c:3092
int lwpoly_covers_point2d(const LWPOLY *poly, const POINT2D *pt_to_test)
Given a polygon (lon/lat decimal degrees) and point (lon/lat decimal degrees) and a guaranteed outsid...
Definition: lwgeodetic.c:2523
void normalize(POINT3D *p)
Normalize to a unit vector.
Definition: lwgeodetic.c:615
static int lwpoly_check_geodetic(const LWPOLY *poly)
Definition: lwgeodetic.c:3072
int lwline_covers_lwpoint(const LWLINE *lwline, const LWPOINT *lwpoint)
return LW_TRUE if any of the line segments covers the point
Definition: lwgeodetic.c:2749
int lwpoly_intersects_line(const LWPOLY *lwpoly, const POINTARRAY *line)
Checks if any edges of lwpoly intersect with the line formed by the pointarray return LW_TRUE if any ...
Definition: lwgeodetic.c:2707
double longitude_radians_normalize(double lon)
Convert a longitude to the range of -PI,PI.
Definition: lwgeodetic.c:50
int clairaut_cartesian(const POINT3D *start, const POINT3D *end, GEOGRAPHIC_POINT *g_top, GEOGRAPHIC_POINT *g_bottom)
Computes the pole of the great circle disk which is the intersection of the great circle with the lin...
Definition: lwgeodetic.c:1076
LWPOINT * lwgeom_project_spheroid(const LWPOINT *r, const SPHEROID *spheroid, double distance, double azimuth)
Calculate a projected point given a source point, a distance and a bearing.
Definition: lwgeodetic.c:2101
void point_shift(GEOGRAPHIC_POINT *p, double shift)
Shift a point around by a number of radians.
Definition: lwgeodetic.c:160
static int lwpoly_force_geodetic(LWPOLY *poly)
Definition: lwgeodetic.c:3168
LWGEOM * lwgeom_segmentize_sphere(const LWGEOM *lwg_in, double max_seg_length)
Create a new, densified geometry where no segment is longer than max_seg_length.
Definition: lwgeodetic.c:1745
double lwgeom_distance_spheroid(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2, const SPHEROID *spheroid, double tolerance)
Calculate the distance between two LWGEOMs, using the coordinates are longitude and latitude.
Definition: lwgeodetic.c:2192
static int lwcollection_force_geodetic(LWCOLLECTION *col)
Definition: lwgeodetic.c:3182
static int lwtriangle_check_geodetic(const LWTRIANGLE *triangle)
Definition: lwgeodetic.c:3085
int gbox_pt_outside(const GBOX *gbox, POINT2D *pt_outside)
Given a unit geocentric gbox, return a lon/lat (degrees) coordinate point point that is guaranteed to...
Definition: lwgeodetic.c:1554
double sphere_distance_cartesian(const POINT3D *s, const POINT3D *e)
Given two unit vectors, calculate their distance apart in radians.
Definition: lwgeodetic.c:967
static int ptarray_force_geodetic(POINTARRAY *pa)
Definition: lwgeodetic.c:3134
void vector_rotate(const POINT3D *v1, const POINT3D *v2, double angle, POINT3D *n)
Rotates v1 through an angle (in radians) within the plane defined by v1/v2, returns the rotated vecto...
Definition: lwgeodetic.c:573
static int lwline_force_geodetic(LWLINE *line)
Definition: lwgeodetic.c:3162
static int lwcollection_calculate_gbox_geodetic(const LWCOLLECTION *coll, GBOX *gbox)
Definition: lwgeodetic.c:2967
double ptarray_area_sphere(const POINTARRAY *pa)
Returns the area of the ring (ring must be closed) in square radians (surface of the sphere is 4*PI).
Definition: lwgeodetic.c:1809
static int point_in_cone(const POINT3D *A1, const POINT3D *A2, const POINT3D *P)
Utility function for checking if P is within the cone defined by A1/A2.
Definition: lwgeodetic.c:3433
int lwpoly_covers_lwpoly(const LWPOLY *poly1, const LWPOLY *poly2)
Given a polygon1 check if all points of polygon2 are inside polygon1 and no intersections of the poly...
Definition: lwgeodetic.c:2607
static int gbox_check_poles(GBOX *gbox)
Check to see if this geocentric gbox is wrapped around a pole.
Definition: lwgeodetic.c:316
int lwpoly_covers_pointarray(const LWPOLY *lwpoly, const POINTARRAY *pta)
return LW_TRUE if all points are inside the polygon
Definition: lwgeodetic.c:2688
int ptarray_contains_point_sphere(const POINTARRAY *pa, const POINT2D *pt_outside, const POINT2D *pt_to_test)
This routine returns LW_TRUE if the stabline joining the pt_outside and pt_to_test crosses the ring a...
Definition: lwgeodetic.c:3628
int lwgeom_calculate_gbox_geodetic(const LWGEOM *geom, GBOX *gbox)
Calculate the geodetic bounding box for an LWGEOM.
Definition: lwgeodetic.c:3002
static int ptarray_check_geodetic(const POINTARRAY *pa)
Definition: lwgeodetic.c:3042
static POINTARRAY * ptarray_segmentize_sphere(const POINTARRAY *pa_in, double max_seg_length)
Create a new point array with no segment longer than the input segment length (expressed in radians!...
Definition: lwgeodetic.c:1686
static int lwpoint_check_geodetic(const LWPOINT *point)
Definition: lwgeodetic.c:3060
int edge_point_on_plane(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *p)
Returns true if the point p is on the great circle plane.
Definition: lwgeodetic.c:775
static double ptarray_distance_spheroid(const POINTARRAY *pa1, const POINTARRAY *pa2, const SPHEROID *s, double tolerance, int check_intersection)
Definition: lwgeodetic.c:1837
double latitude_radians_normalize(double lat)
Convert a latitude to the range of -PI/2,PI/2.
Definition: lwgeodetic.c:78
void vector_scale(POINT3D *n, double scale)
Scale a vector out by a factor.
Definition: lwgeodetic.c:487
int lwgeom_check_geodetic(const LWGEOM *geom)
Check that coordinates of LWGEOM are all within the geodetic range (-180, -90, 180,...
Definition: lwgeodetic.c:3105
void cart2geog(const POINT3D *p, GEOGRAPHIC_POINT *g)
Convert cartesian coordinates on unit sphere to spherical coordinates.
Definition: lwgeodetic.c:414
void y_to_z(POINT3D *p)
Definition: lwgeodetic.c:658
double gbox_angular_height(const GBOX *gbox)
Returns the angular height (latitudinal span) of the box in radians.
Definition: lwgeodetic.c:188
int lwpoly_covers_lwline(const LWPOLY *poly, const LWLINE *line)
Definition: lwgeodetic.c:2652
int lwgeom_covers_lwgeom_sphere(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
Calculate covers predicate for two lwgeoms on the sphere.
Definition: lwgeodetic.c:2418
double gbox_angular_width(const GBOX *gbox)
Returns the angular width (longitudinal span) of the box in radians.
Definition: lwgeodetic.c:215
static int lwpoint_force_geodetic(LWPOINT *point)
Definition: lwgeodetic.c:3156
int lwpoly_pt_outside(const LWPOLY *poly, POINT2D *pt_outside)
Definition: lwgeodetic.c:1529
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 ...
Definition: lwgeodetic.c:1316
void ll2cart(const POINT2D *g, POINT3D *p)
Convert lon/lat coordinates to cartesian coordinates on unit sphere.
Definition: lwgeodetic.c:423
static int lwline_calculate_gbox_geodetic(const LWLINE *line, GBOX *gbox)
Definition: lwgeodetic.c:2924
static void normalize2d(POINT2D *p)
Normalize to a unit vector.
Definition: lwgeodetic.c:524
int gbox_geocentric_slow
For testing geodetic bounding box, we have a magic global variable.
Definition: lwgeodetic.c:36
int edge_point_in_cone(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *p)
Returns true if the point p is inside the cone defined by the two ends of the edge e.
Definition: lwgeodetic.c:788
double longitude_degrees_normalize(double lon)
Convert a longitude to the range of -180,180.
Definition: lwgeodetic.c:106
double z_to_latitude(double z, int top)
Used in great circle to compute the pole of the great circle.
Definition: lwgeodetic.c:1049
void robust_cross_product(const GEOGRAPHIC_POINT *p, const GEOGRAPHIC_POINT *q, POINT3D *a)
Computes the cross product of two vectors using their lat, lng representations.
Definition: lwgeodetic.c:634
static int lwpoly_pt_outside_hack(const LWPOLY *poly, POINT2D *pt_outside)
Definition: lwgeodetic.c:1489
int ptarray_calculate_gbox_geodetic(const POINTARRAY *pa, GBOX *gbox)
Calculate geodetic (x/y/z) box and add values to gbox.
Definition: lwgeodetic.c:2863
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition: lwgeodetic.c:180
static int dot_product_side(const POINT3D *p, const POINT3D *q)
Utility function for edge_intersects(), signum with a tolerance in determining if the value is zero.
Definition: lwgeodetic.c:3500
double ptarray_length_spheroid(const POINTARRAY *pa, const SPHEROID *s)
Definition: lwgeodetic.c:3218
void unit_normal(const POINT3D *P1, const POINT3D *P2, POINT3D *normal)
Calculates the unit normal to two vectors, trying to avoid problems with over-narrow or over-wide cas...
Definition: lwgeodetic.c:541
int lwgeom_force_geodetic(LWGEOM *geom)
Force coordinates of LWGEOM into geodetic range (-180, -90, 180, 90)
Definition: lwgeodetic.c:3196
static int ptarray_segmentize_sphere_edge_recursive(const POINT3D *p1, const POINT3D *p2, const POINT4D *v1, const POINT4D *v2, double d, double max_seg_length, POINTARRAY *pa)
Definition: lwgeodetic.c:1635
static void vector_difference(const POINT3D *a, const POINT3D *b, POINT3D *n)
Calculate the difference of two vectors.
Definition: lwgeodetic.c:476
int lwgeom_nudge_geodetic(LWGEOM *geom)
When features are snapped or sometimes they are just this way, they are very close to the geodetic bo...
Definition: lwgeodetic.c:3378
static void cross_product(const POINT3D *a, const POINT3D *b, POINT3D *n)
Calculate the cross product of two vectors.
Definition: lwgeodetic.c:454
static int edge_point_side(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *p)
Returns -1 if the point is to the left of the plane formed by the edge, 1 if the point is to the righ...
Definition: lwgeodetic.c:694
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
static double dot_product(const POINT3D *p1, const POINT3D *p2)
Convert cartesian coordinates on unit sphere to lon/lat coordinates static void cart2ll(const POINT3D...
Definition: lwgeodetic.c:446
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_calculate_gbox_slow(const GEOGRAPHIC_EDGE *e, GBOX *gbox)
Definition: lwgeodetic.c:1346
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.
Definition: lwgeodetic.c:1127
void vector_sum(const POINT3D *a, const POINT3D *b, POINT3D *n)
Calculate the sum of two vectors.
Definition: lwgeodetic.c:465
static int lwtriangle_calculate_gbox_geodetic(const LWTRIANGLE *triangle, GBOX *gbox)
Definition: lwgeodetic.c:2960
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.
Definition: lwgeodetic.c:3515
double lwgeom_length_spheroid(const LWGEOM *geom, const SPHEROID *s)
Calculate the geodetic length of a lwgeom on the unit sphere.
Definition: lwgeodetic.c:3271
int edge_calculate_gbox(const POINT3D *A1, const POINT3D *A2, GBOX *gbox)
The magic function, given an edge in spherical coordinates, calculate a 3D bounding box that fully co...
Definition: lwgeodetic.c:1410
double edge_distance_to_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *gp, GEOGRAPHIC_POINT *closest)
Definition: lwgeodetic.c:1218
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesian coordinates on unit sphere.
Definition: lwgeodetic.c:404
int gbox_centroid(const GBOX *gbox, POINT2D *out)
Computes the average(ish) center of the box and returns success.
Definition: lwgeodetic.c:267
static double sphere_signed_area(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const GEOGRAPHIC_POINT *c)
Computes the spherical area of a triangle.
Definition: lwgeodetic.c:741
static int point3d_equals(const POINT3D *p1, const POINT3D *p2)
Utility function for ptarray_contains_point_sphere()
Definition: lwgeodetic.c:42
int lwline_covers_lwline(const LWLINE *lwline1, const LWLINE *lwline2)
Check if first and last point of line2 are covered by line1 and then each point in between has to be ...
Definition: lwgeodetic.c:2778
int edge_contains_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *p)
Returns true if the point p is on the minor edge defined by the end points of e.
Definition: lwgeodetic.c:1034
static double sphere_angle(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const GEOGRAPHIC_POINT *c)
Returns the angle in radians at point B of the triangle formed by A-B-C.
Definition: lwgeodetic.c:721
void x_to_z(POINT3D *p)
Definition: lwgeodetic.c:651
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.
Definition: lwgeodetic.c:1271
static int ptarray_nudge_geodetic(POINTARRAY *pa)
When features are snapped or sometimes they are just this way, they are very close to the geodetic bo...
Definition: lwgeodetic.c:3326
int geographic_point_equals(const GEOGRAPHIC_POINT *g1, const GEOGRAPHIC_POINT *g2)
Definition: lwgeodetic.c:170
double lwgeom_azumith_spheroid(const LWPOINT *r, const LWPOINT *s, const SPHEROID *spheroid)
Calculate a bearing (azimuth) given a source and destination point.
Definition: lwgeodetic.c:2158
int crosses_dateline(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e)
Definition: lwgeodetic.c:666
static int lwpoint_calculate_gbox_geodetic(const LWPOINT *point, GBOX *gbox)
Definition: lwgeodetic.c:2918
double latitude_degrees_normalize(double lat)
Convert a latitude to the range of -90,90.
Definition: lwgeodetic.c:133
double vector_angle(const POINT3D *v1, const POINT3D *v2)
Angle between two unit vectors.
Definition: lwgeodetic.c:505
int edge_contains_coplanar_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *p)
True if the longitude of p is within the range of the longitude of the ends of e.
Definition: lwgeodetic.c:835
double lwgeom_area_sphere(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the area of an LWGEOM.
Definition: lwgeodetic.c:2033
static int lwpolygon_calculate_gbox_geodetic(const LWPOLY *poly, GBOX *gbox)
Definition: lwgeodetic.c:2930
#define rad2deg(r)
Definition: lwgeodetic.h:81
#define POW2(x)
Definition: lwgeodetic.h:48
#define PIR_A_TOUCH_LEFT
Definition: lwgeodetic.h:91
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 NAN
Definition: lwgeodetic.h:37
int spheroid_project(const GEOGRAPHIC_POINT *r, const SPHEROID *spheroid, double distance, double azimuth, GEOGRAPHIC_POINT *g)
Given a location, an azimuth and a distance, computes the location of the projected point.
Definition: lwspheroid.c:128
#define PIR_INTERSECTS
Definition: lwgeodetic.h:88
double spheroid_direction(const GEOGRAPHIC_POINT *r, const GEOGRAPHIC_POINT *s, const SPHEROID *spheroid)
Computes the forward azimuth of the geodesic joining two points on the spheroid, using the inverse ge...
Definition: lwspheroid.c:105
#define deg2rad(d)
Conversion functions.
Definition: lwgeodetic.h:80
#define PIR_A_TOUCH_RIGHT
Definition: lwgeodetic.h:90
#define PIR_B_TOUCH_RIGHT
Definition: lwgeodetic.h:92
#define PIR_B_TOUCH_LEFT
Definition: lwgeodetic.h:93
#define PIR_NO_INTERACT
Bitmask elements for edge_intersects() return value.
Definition: lwgeodetic.h:87
#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 const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:101
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwinline.h:145
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:203
static double distance(double x1, double y1, double x2, double y2)
Definition: lwtree.c:1032
int count
Definition: genraster.py:57
type
Definition: ovdump.py:42
double ymax
Definition: liblwgeom.h:371
double zmax
Definition: liblwgeom.h:373
double xmax
Definition: liblwgeom.h:369
double zmin
Definition: liblwgeom.h:372
double ymin
Definition: liblwgeom.h:370
double xmin
Definition: liblwgeom.h:368
lwflags_t flags
Definition: liblwgeom.h:367
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
uint32_t ngeoms
Definition: liblwgeom.h:594
LWGEOM ** geoms
Definition: liblwgeom.h:589
uint8_t type
Definition: liblwgeom.h:476
GBOX * bbox
Definition: liblwgeom.h:472
int32_t srid
Definition: liblwgeom.h:474
lwflags_t flags
Definition: liblwgeom.h:475
POINTARRAY * points
Definition: liblwgeom.h:497
POINTARRAY * point
Definition: liblwgeom.h:485
uint8_t type
Definition: liblwgeom.h:488
POINTARRAY ** rings
Definition: liblwgeom.h:533
uint32_t nrings
Definition: liblwgeom.h:538
GBOX * bbox
Definition: liblwgeom.h:532
POINTARRAY * points
Definition: liblwgeom.h:509
double y
Definition: liblwgeom.h:404
double x
Definition: liblwgeom.h:404
double z
Definition: liblwgeom.h:416
double x
Definition: liblwgeom.h:416
double y
Definition: liblwgeom.h:416
double m
Definition: liblwgeom.h:428
double x
Definition: liblwgeom.h:428
double z
Definition: liblwgeom.h:428
double y
Definition: liblwgeom.h:428
lwflags_t flags
Definition: liblwgeom.h:445
uint32_t npoints
Definition: liblwgeom.h:441
double radius
Definition: liblwgeom.h:394