PostGIS  3.4.0dev-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, cos_d_lon, cos_lat_e, sin_lat_e, cos_lat_s, sin_lat_s;
951  double a1, a2, a, b;
952 
953  if (FP_EQUALS(s->lat, e->lat) && FP_EQUALS(s->lon, e->lon)) return 0.0;
954  d_lon = e->lon - s->lon;
955  cos_d_lon = cos(d_lon);
956  cos_lat_e = cos(e->lat);
957  sin_lat_e = sin(e->lat);
958  cos_lat_s = cos(s->lat);
959  sin_lat_s = sin(s->lat);
960 
961  a1 = POW2(cos_lat_e * sin(d_lon));
962  a2 = POW2(cos_lat_s * sin_lat_e - sin_lat_s * cos_lat_e * cos_d_lon);
963  a = sqrt(a1 + a2);
964  b = sin_lat_s * sin_lat_e + cos_lat_s * cos_lat_e * cos_d_lon;
965  return atan2(a, b);
966 }
967 
971 double sphere_distance_cartesian(const POINT3D *s, const POINT3D *e)
972 {
973  return acos(FP_MIN(1.0, dot_product(s, e)));
974 }
975 
979 double sphere_direction(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e, double d)
980 {
981  double heading = 0.0;
982  double f;
983 
984  /* Starting from the poles? Special case. */
985  if ( FP_IS_ZERO(cos(s->lat)) )
986  return (s->lat > 0.0) ? M_PI : 0.0;
987 
988  f = (sin(e->lat) - sin(s->lat) * cos(d)) / (sin(d) * cos(s->lat));
989  if ( FP_EQUALS(f, 1.0) )
990  heading = 0.0;
991  else if ( FP_EQUALS(f, -1.0) )
992  heading = M_PI;
993  else if ( fabs(f) > 1.0 )
994  {
995  LWDEBUGF(4, "f = %g", f);
996  heading = acos(f);
997  }
998  else
999  heading = acos(f);
1000 
1001  if ( sin(e->lon - s->lon) < 0.0 )
1002  heading = -1 * heading;
1003 
1004  return heading;
1005 }
1006 
1007 #if 0 /* unused */
1019 static double sphere_excess(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const GEOGRAPHIC_POINT *c)
1020 {
1021  double a_dist = sphere_distance(b, c);
1022  double b_dist = sphere_distance(c, a);
1023  double c_dist = sphere_distance(a, b);
1024  double hca = sphere_direction(c, a, b_dist);
1025  double hcb = sphere_direction(c, b, a_dist);
1026  double sign = SIGNUM(hcb-hca);
1027  double ss = (a_dist + b_dist + c_dist) / 2.0;
1028  double E = tan(ss/2.0)*tan((ss-a_dist)/2.0)*tan((ss-b_dist)/2.0)*tan((ss-c_dist)/2.0);
1029  return 4.0 * atan(sqrt(fabs(E))) * sign;
1030 }
1031 #endif
1032 
1033 
1039 {
1040  if ( edge_point_in_cone(e, p) && edge_point_on_plane(e, p) )
1041  /* if ( edge_contains_coplanar_point(e, p) && edge_point_on_plane(e, p) ) */
1042  {
1043  LWDEBUG(4, "point is on edge");
1044  return LW_TRUE;
1045  }
1046  LWDEBUG(4, "point is not on edge");
1047  return LW_FALSE;
1048 }
1049 
1053 double z_to_latitude(double z, int top)
1054 {
1055  double sign = SIGNUM(z);
1056  double tlat = acos(z);
1057  LWDEBUGF(4, "inputs: z(%.8g) sign(%.8g) tlat(%.8g)", z, sign, tlat);
1058  if (FP_IS_ZERO(z))
1059  {
1060  if (top) return M_PI_2;
1061  else return -1.0 * M_PI_2;
1062  }
1063  if (fabs(tlat) > M_PI_2 )
1064  {
1065  tlat = sign * (M_PI - fabs(tlat));
1066  }
1067  else
1068  {
1069  tlat = sign * tlat;
1070  }
1071  LWDEBUGF(4, "output: tlat(%.8g)", tlat);
1072  return tlat;
1073 }
1074 
1080 int clairaut_cartesian(const POINT3D *start, const POINT3D *end, GEOGRAPHIC_POINT *g_top, GEOGRAPHIC_POINT *g_bottom)
1081 {
1082  POINT3D t1, t2;
1083  GEOGRAPHIC_POINT vN1, vN2;
1084  LWDEBUG(4,"entering function");
1085  unit_normal(start, end, &t1);
1086  unit_normal(end, start, &t2);
1087  LWDEBUGF(4, "unit normal t1 == POINT(%.8g %.8g %.8g)", t1.x, t1.y, t1.z);
1088  LWDEBUGF(4, "unit normal t2 == POINT(%.8g %.8g %.8g)", t2.x, t2.y, t2.z);
1089  cart2geog(&t1, &vN1);
1090  cart2geog(&t2, &vN2);
1091  g_top->lat = z_to_latitude(t1.z,LW_TRUE);
1092  g_top->lon = vN2.lon;
1093  g_bottom->lat = z_to_latitude(t2.z,LW_FALSE);
1094  g_bottom->lon = vN1.lon;
1095  LWDEBUGF(4, "clairaut top == GPOINT(%.6g %.6g)", g_top->lat, g_top->lon);
1096  LWDEBUGF(4, "clairaut bottom == GPOINT(%.6g %.6g)", g_bottom->lat, g_bottom->lon);
1097  return LW_SUCCESS;
1098 }
1099 
1106 {
1107  POINT3D t1, t2;
1108  GEOGRAPHIC_POINT vN1, vN2;
1109  LWDEBUG(4,"entering function");
1110  robust_cross_product(start, end, &t1);
1111  normalize(&t1);
1112  robust_cross_product(end, start, &t2);
1113  normalize(&t2);
1114  LWDEBUGF(4, "unit normal t1 == POINT(%.8g %.8g %.8g)", t1.x, t1.y, t1.z);
1115  LWDEBUGF(4, "unit normal t2 == POINT(%.8g %.8g %.8g)", t2.x, t2.y, t2.z);
1116  cart2geog(&t1, &vN1);
1117  cart2geog(&t2, &vN2);
1118  g_top->lat = z_to_latitude(t1.z,LW_TRUE);
1119  g_top->lon = vN2.lon;
1120  g_bottom->lat = z_to_latitude(t2.z,LW_FALSE);
1121  g_bottom->lon = vN1.lon;
1122  LWDEBUGF(4, "clairaut top == GPOINT(%.6g %.6g)", g_top->lat, g_top->lon);
1123  LWDEBUGF(4, "clairaut bottom == GPOINT(%.6g %.6g)", g_bottom->lat, g_bottom->lon);
1124  return LW_SUCCESS;
1125 }
1126 
1132 {
1133  POINT3D ea, eb, v;
1134  LWDEBUGF(4, "e1 start(%.20g %.20g) end(%.20g %.20g)", e1->start.lat, e1->start.lon, e1->end.lat, e1->end.lon);
1135  LWDEBUGF(4, "e2 start(%.20g %.20g) end(%.20g %.20g)", e2->start.lat, e2->start.lon, e2->end.lat, e2->end.lon);
1136 
1137  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));
1138  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));
1139 
1140  if ( geographic_point_equals(&(e1->start), &(e2->start)) )
1141  {
1142  *g = e1->start;
1143  return LW_TRUE;
1144  }
1145  if ( geographic_point_equals(&(e1->end), &(e2->end)) )
1146  {
1147  *g = e1->end;
1148  return LW_TRUE;
1149  }
1150  if ( geographic_point_equals(&(e1->end), &(e2->start)) )
1151  {
1152  *g = e1->end;
1153  return LW_TRUE;
1154  }
1155  if ( geographic_point_equals(&(e1->start), &(e2->end)) )
1156  {
1157  *g = e1->start;
1158  return LW_TRUE;
1159  }
1160 
1161  robust_cross_product(&(e1->start), &(e1->end), &ea);
1162  normalize(&ea);
1163  robust_cross_product(&(e2->start), &(e2->end), &eb);
1164  normalize(&eb);
1165  LWDEBUGF(4, "e1 cross product == POINT(%.12g %.12g %.12g)", ea.x, ea.y, ea.z);
1166  LWDEBUGF(4, "e2 cross product == POINT(%.12g %.12g %.12g)", eb.x, eb.y, eb.z);
1167  LWDEBUGF(4, "fabs(dot_product(ea, eb)) == %.14g", fabs(dot_product(&ea, &eb)));
1168  if ( FP_EQUALS(fabs(dot_product(&ea, &eb)), 1.0) )
1169  {
1170  LWDEBUGF(4, "parallel edges found! dot_product = %.12g", dot_product(&ea, &eb));
1171  /* Parallel (maybe equal) edges! */
1172  /* Hack alert, only returning ONE end of the edge right now, most do better later. */
1173  /* Hack alert #2, returning a value of 2 to indicate a co-linear crossing event. */
1174  if ( edge_contains_point(e1, &(e2->start)) )
1175  {
1176  *g = e2->start;
1177  return 2;
1178  }
1179  if ( edge_contains_point(e1, &(e2->end)) )
1180  {
1181  *g = e2->end;
1182  return 2;
1183  }
1184  if ( edge_contains_point(e2, &(e1->start)) )
1185  {
1186  *g = e1->start;
1187  return 2;
1188  }
1189  if ( edge_contains_point(e2, &(e1->end)) )
1190  {
1191  *g = e1->end;
1192  return 2;
1193  }
1194  }
1195  unit_normal(&ea, &eb, &v);
1196  LWDEBUGF(4, "v == POINT(%.12g %.12g %.12g)", v.x, v.y, v.z);
1197  g->lat = atan2(v.z, sqrt(v.x * v.x + v.y * v.y));
1198  g->lon = atan2(v.y, v.x);
1199  LWDEBUGF(4, "g == GPOINT(%.12g %.12g)", g->lat, g->lon);
1200  LWDEBUGF(4, "g == POINT(%.12g %.12g)", rad2deg(g->lon), rad2deg(g->lat));
1201  if ( edge_contains_point(e1, g) && edge_contains_point(e2, g) )
1202  {
1203  return LW_TRUE;
1204  }
1205  else
1206  {
1207  LWDEBUG(4, "flipping point to other side of sphere");
1208  g->lat = -1.0 * g->lat;
1209  g->lon = g->lon + M_PI;
1210  if ( g->lon > M_PI )
1211  {
1212  g->lon = -1.0 * (2.0 * M_PI - g->lon);
1213  }
1214  if ( edge_contains_point(e1, g) && edge_contains_point(e2, g) )
1215  {
1216  return LW_TRUE;
1217  }
1218  }
1219  return LW_FALSE;
1220 }
1221 
1223 {
1224  double d1 = 1000000000.0, d2, d3, d_nearest;
1225  POINT3D n, p, k;
1226  GEOGRAPHIC_POINT gk, g_nearest;
1227 
1228  /* Zero length edge, */
1229  if ( geographic_point_equals(&(e->start), &(e->end)) )
1230  {
1231  if (closest)
1232  *closest = e->start;
1233 
1234  return sphere_distance(&(e->start), gp);
1235  }
1236 
1237  robust_cross_product(&(e->start), &(e->end), &n);
1238  normalize(&n);
1239  geog2cart(gp, &p);
1240  vector_scale(&n, dot_product(&p, &n));
1241  vector_difference(&p, &n, &k);
1242  normalize(&k);
1243  cart2geog(&k, &gk);
1244  if ( edge_point_in_cone(e, &gk) )
1245  {
1246  d1 = sphere_distance(gp, &gk);
1247  }
1248  d2 = sphere_distance(gp, &(e->start));
1249  d3 = sphere_distance(gp, &(e->end));
1250 
1251  d_nearest = d1;
1252  g_nearest = gk;
1253 
1254  if ( d2 < d_nearest )
1255  {
1256  d_nearest = d2;
1257  g_nearest = e->start;
1258  }
1259  if ( d3 < d_nearest )
1260  {
1261  d_nearest = d3;
1262  g_nearest = e->end;
1263  }
1264  if (closest)
1265  *closest = g_nearest;
1266 
1267  return d_nearest;
1268 }
1269 
1276 {
1277  double d;
1278  GEOGRAPHIC_POINT gcp1s, gcp1e, gcp2s, gcp2e, c1, c2;
1279  double d1s = edge_distance_to_point(e1, &(e2->start), &gcp1s);
1280  double d1e = edge_distance_to_point(e1, &(e2->end), &gcp1e);
1281  double d2s = edge_distance_to_point(e2, &(e1->start), &gcp2s);
1282  double d2e = edge_distance_to_point(e2, &(e1->end), &gcp2e);
1283 
1284  d = d1s;
1285  c1 = gcp1s;
1286  c2 = e2->start;
1287 
1288  if ( d1e < d )
1289  {
1290  d = d1e;
1291  c1 = gcp1e;
1292  c2 = e2->end;
1293  }
1294 
1295  if ( d2s < d )
1296  {
1297  d = d2s;
1298  c1 = e1->start;
1299  c2 = gcp2s;
1300  }
1301 
1302  if ( d2e < d )
1303  {
1304  d = d2e;
1305  c1 = e1->end;
1306  c2 = gcp2e;
1307  }
1308 
1309  if ( closest1 ) *closest1 = c1;
1310  if ( closest2 ) *closest2 = c2;
1311 
1312  return d;
1313 }
1314 
1315 
1320 int sphere_project(const GEOGRAPHIC_POINT *r, double distance, double azimuth, GEOGRAPHIC_POINT *n)
1321 {
1322  double d = distance;
1323  double lat1 = r->lat;
1324  double lon1 = r->lon;
1325  double lat2, lon2;
1326 
1327  lat2 = asin(sin(lat1)*cos(d) + cos(lat1)*sin(d)*cos(azimuth));
1328 
1329  /* If we're going straight up or straight down, we don't need to calculate the longitude */
1330  /* TODO: this isn't quite true, what if we're going over the pole? */
1331  if ( FP_EQUALS(azimuth, M_PI) || FP_EQUALS(azimuth, 0.0) )
1332  {
1333  lon2 = r->lon;
1334  }
1335  else
1336  {
1337  lon2 = lon1 + atan2(sin(azimuth)*sin(d)*cos(lat1), cos(d)-sin(lat1)*sin(lat2));
1338  }
1339 
1340  if ( isnan(lat2) || isnan(lon2) )
1341  return LW_FAILURE;
1342 
1343  n->lat = lat2;
1344  n->lon = lon2;
1345 
1346  return LW_SUCCESS;
1347 }
1348 
1349 
1351 {
1352  int steps = 1000000;
1353  int i;
1354  double dx, dy, dz;
1355  double distance = sphere_distance(&(e->start), &(e->end));
1356  POINT3D pn, p, start, end;
1357 
1358  /* Edge is zero length, just return the naive box */
1359  if ( FP_IS_ZERO(distance) )
1360  {
1361  LWDEBUG(4, "edge is zero length. returning");
1362  geog2cart(&(e->start), &start);
1363  geog2cart(&(e->end), &end);
1364  gbox_init_point3d(&start, gbox);
1365  gbox_merge_point3d(&end, gbox);
1366  return LW_SUCCESS;
1367  }
1368 
1369  /* Edge is antipodal (one point on each side of the globe),
1370  set the box to contain the whole world and return */
1371  if ( FP_EQUALS(distance, M_PI) )
1372  {
1373  LWDEBUG(4, "edge is antipodal. setting to maximum size box, and returning");
1374  gbox->xmin = gbox->ymin = gbox->zmin = -1.0;
1375  gbox->xmax = gbox->ymax = gbox->zmax = 1.0;
1376  return LW_SUCCESS;
1377  }
1378 
1379  /* Walk along the chord between start and end incrementally,
1380  normalizing at each step. */
1381  geog2cart(&(e->start), &start);
1382  geog2cart(&(e->end), &end);
1383  dx = (end.x - start.x)/steps;
1384  dy = (end.y - start.y)/steps;
1385  dz = (end.z - start.z)/steps;
1386  p = start;
1387  gbox->xmin = gbox->xmax = p.x;
1388  gbox->ymin = gbox->ymax = p.y;
1389  gbox->zmin = gbox->zmax = p.z;
1390  for ( i = 0; i < steps; i++ )
1391  {
1392  p.x += dx;
1393  p.y += dy;
1394  p.z += dz;
1395  pn = p;
1396  normalize(&pn);
1397  gbox_merge_point3d(&pn, gbox);
1398  }
1399  return LW_SUCCESS;
1400 }
1401 
1414 int edge_calculate_gbox(const POINT3D *A1, const POINT3D *A2, GBOX *gbox)
1415 {
1416  POINT2D R1, R2, RX, O;
1417  POINT3D AN, A3;
1418  POINT3D X[6];
1419  int i, o_side;
1420 
1421  /* Initialize the box with the edge end points */
1422  gbox_init_point3d(A1, gbox);
1423  gbox_merge_point3d(A2, gbox);
1424 
1425  /* Zero length edge, just return! */
1426  if ( p3d_same(A1, A2) )
1427  return LW_SUCCESS;
1428 
1429  /* Error out on antipodal edge */
1430  if ( FP_EQUALS(A1->x, -1*A2->x) && FP_EQUALS(A1->y, -1*A2->y) && FP_EQUALS(A1->z, -1*A2->z) )
1431  {
1432  lwerror("Antipodal (180 degrees long) edge detected!");
1433  return LW_FAILURE;
1434  }
1435 
1436  /* Create A3, a vector in the plane of A1/A2, orthogonal to A1 */
1437  unit_normal(A1, A2, &AN);
1438  unit_normal(&AN, A1, &A3);
1439 
1440  /* Project A1 and A2 into the 2-space formed by the plane A1/A3 */
1441  R1.x = 1.0;
1442  R1.y = 0.0;
1443  R2.x = dot_product(A2, A1);
1444  R2.y = dot_product(A2, &A3);
1445 
1446  /* Initialize our 3-space axis points (x+, x-, y+, y-, z+, z-) */
1447  memset(X, 0, sizeof(POINT3D) * 6);
1448  X[0].x = X[2].y = X[4].z = 1.0;
1449  X[1].x = X[3].y = X[5].z = -1.0;
1450 
1451  /* Initialize a 2-space origin point. */
1452  O.x = O.y = 0.0;
1453  /* What side of the line joining R1/R2 is O? */
1454  o_side = lw_segment_side(&R1, &R2, &O);
1455 
1456  /* Add any extrema! */
1457  for ( i = 0; i < 6; i++ )
1458  {
1459  /* Convert 3-space axis points to 2-space unit vectors */
1460  RX.x = dot_product(&(X[i]), A1);
1461  RX.y = dot_product(&(X[i]), &A3);
1462  normalize2d(&RX);
1463 
1464  /* Any axis end on the side of R1/R2 opposite the origin */
1465  /* is an extreme point in the arc, so we add the 3-space */
1466  /* version of the point on R1/R2 to the gbox */
1467  if ( lw_segment_side(&R1, &R2, &RX) != o_side )
1468  {
1469  POINT3D Xn;
1470  Xn.x = RX.x * A1->x + RX.y * A3.x;
1471  Xn.y = RX.x * A1->y + RX.y * A3.y;
1472  Xn.z = RX.x * A1->z + RX.y * A3.z;
1473 
1474  gbox_merge_point3d(&Xn, gbox);
1475  }
1476  }
1477 
1478  return LW_SUCCESS;
1479 }
1480 
1481 /*
1482 * When we have a globe-covering gbox but we still want an outside
1483 * point, we do this Very Bad Hack, which is look at the first two points
1484 * in the ring and then nudge a point to the left of that arc.
1485 * There is an assumption of convexity built in there, as well as that
1486 * the shape doesn't have a sharp reversal in it. It's ugly, but
1487 * it fixes some common cases (large selection polygons) that users
1488 * are generating. At some point all of geodetic needs a clean-room
1489 * rewrite.
1490 * There is also an assumption of CCW exterior ring, which is how the
1491 * GeoJSON spec defined geographic ring orientation.
1492 */
1493 static int lwpoly_pt_outside_hack(const LWPOLY *poly, POINT2D *pt_outside)
1494 {
1495  GEOGRAPHIC_POINT g1, g2, gSum;
1496  POINT4D p1, p2;
1497  POINT3D q1, q2, qMid, qCross, qSum;
1498  POINTARRAY *pa;
1499  if (lwgeom_is_empty((LWGEOM*)poly))
1500  return LW_FAILURE;
1501  if (poly->nrings < 1)
1502  return LW_FAILURE;
1503  pa = poly->rings[0];
1504  if (pa->npoints < 2)
1505  return LW_FAILURE;
1506 
1507  /* First two points of ring */
1508  getPoint4d_p(pa, 0, &p1);
1509  getPoint4d_p(pa, 1, &p2);
1510  /* Convert to XYZ unit vectors */
1511  geographic_point_init(p1.x, p1.y, &g1);
1512  geographic_point_init(p2.x, p2.y, &g2);
1513  geog2cart(&g1, &q1);
1514  geog2cart(&g2, &q2);
1515  /* Mid-point of first two points */
1516  vector_sum(&q1, &q2, &qMid);
1517  normalize(&qMid);
1518  /* Cross product of first two points (perpendicular) */
1519  cross_product(&q1, &q2, &qCross);
1520  normalize(&qCross);
1521  /* Invert it to put it outside, and scale down */
1522  vector_scale(&qCross, -0.2);
1523  /* Project midpoint to the right */
1524  vector_sum(&qMid, &qCross, &qSum);
1525  normalize(&qSum);
1526  /* Convert back to lon/lat */
1527  cart2geog(&qSum, &gSum);
1528  pt_outside->x = rad2deg(gSum.lon);
1529  pt_outside->y = rad2deg(gSum.lat);
1530  return LW_SUCCESS;
1531 }
1532 
1533 int lwpoly_pt_outside(const LWPOLY *poly, POINT2D *pt_outside)
1534 {
1535  int rv;
1536  /* Make sure we have boxes */
1537  if ( poly->bbox )
1538  {
1539  rv = gbox_pt_outside(poly->bbox, pt_outside);
1540  }
1541  else
1542  {
1543  GBOX gbox;
1544  lwgeom_calculate_gbox_geodetic((LWGEOM*)poly, &gbox);
1545  rv = gbox_pt_outside(&gbox, pt_outside);
1546  }
1547 
1548  if (rv == LW_FALSE)
1549  return lwpoly_pt_outside_hack(poly, pt_outside);
1550 
1551  return rv;
1552 }
1553 
1558 int gbox_pt_outside(const GBOX *gbox, POINT2D *pt_outside)
1559 {
1560  double grow = M_PI / 180.0 / 60.0; /* one arc-minute */
1561  int i;
1562  GBOX ge;
1563  POINT3D corners[8];
1564  POINT3D pt;
1565  GEOGRAPHIC_POINT g;
1566 
1567  while ( grow < M_PI )
1568  {
1569  /* Assign our box and expand it slightly. */
1570  ge = *gbox;
1571  if ( ge.xmin > -1 ) ge.xmin -= grow;
1572  if ( ge.ymin > -1 ) ge.ymin -= grow;
1573  if ( ge.zmin > -1 ) ge.zmin -= grow;
1574  if ( ge.xmax < 1 ) ge.xmax += grow;
1575  if ( ge.ymax < 1 ) ge.ymax += grow;
1576  if ( ge.zmax < 1 ) ge.zmax += grow;
1577 
1578  /* Build our eight corner points */
1579  corners[0].x = ge.xmin;
1580  corners[0].y = ge.ymin;
1581  corners[0].z = ge.zmin;
1582 
1583  corners[1].x = ge.xmin;
1584  corners[1].y = ge.ymax;
1585  corners[1].z = ge.zmin;
1586 
1587  corners[2].x = ge.xmin;
1588  corners[2].y = ge.ymin;
1589  corners[2].z = ge.zmax;
1590 
1591  corners[3].x = ge.xmax;
1592  corners[3].y = ge.ymin;
1593  corners[3].z = ge.zmin;
1594 
1595  corners[4].x = ge.xmax;
1596  corners[4].y = ge.ymax;
1597  corners[4].z = ge.zmin;
1598 
1599  corners[5].x = ge.xmax;
1600  corners[5].y = ge.ymin;
1601  corners[5].z = ge.zmax;
1602 
1603  corners[6].x = ge.xmin;
1604  corners[6].y = ge.ymax;
1605  corners[6].z = ge.zmax;
1606 
1607  corners[7].x = ge.xmax;
1608  corners[7].y = ge.ymax;
1609  corners[7].z = ge.zmax;
1610 
1611  LWDEBUG(4, "trying to use a box corner point...");
1612  for ( i = 0; i < 8; i++ )
1613  {
1614  normalize(&(corners[i]));
1615  LWDEBUGF(4, "testing corner %d: POINT(%.8g %.8g %.8g)", i, corners[i].x, corners[i].y, corners[i].z);
1616  if ( ! gbox_contains_point3d(gbox, &(corners[i])) )
1617  {
1618  LWDEBUGF(4, "corner %d is outside our gbox", i);
1619  pt = corners[i];
1620  normalize(&pt);
1621  cart2geog(&pt, &g);
1622  pt_outside->x = rad2deg(g.lon);
1623  pt_outside->y = rad2deg(g.lat);
1624  LWDEBUGF(4, "returning POINT(%.8g %.8g) as outside point", pt_outside->x, pt_outside->y);
1625  return LW_SUCCESS;
1626  }
1627  }
1628 
1629  /* Try a wider growth to push the corners outside the original box. */
1630  grow *= 2.0;
1631  }
1632 
1633  /* This should never happen! */
1634  // lwerror("BOOM! Could not generate outside point!");
1635  return LW_FAILURE;
1636 }
1637 
1638 
1640  const POINT3D *p1, const POINT3D *p2, /* 3-space points we are interpolating between */
1641  const POINT4D *v1, const POINT4D *v2, /* real values and z/m values */
1642  double d, double max_seg_length, /* current segment length and segment limit */
1643  POINTARRAY *pa) /* write out results here */
1644 {
1645  GEOGRAPHIC_POINT g;
1646  /* Reached the terminal leaf in recursion. Add */
1647  /* the left-most point to the pointarray here */
1648  /* We recurse down the left side first, so outputs should */
1649  /* end up added to the array in order this way */
1650  if (d <= max_seg_length)
1651  {
1652  POINT4D p;
1653  cart2geog(p1, &g);
1654  p.x = v1->x;
1655  p.y = v1->y;
1656  p.z = v1->z;
1657  p.m = v1->m;
1658  return ptarray_append_point(pa, &p, LW_FALSE);
1659  }
1660  /* Find the mid-point and recurse on the left and then the right */
1661  else
1662  {
1663  /* Calculate mid-point */
1664  POINT3D mid;
1665  mid.x = (p1->x + p2->x) / 2.0;
1666  mid.y = (p1->y + p2->y) / 2.0;
1667  mid.z = (p1->z + p2->z) / 2.0;
1668  normalize(&mid);
1669 
1670  /* Calculate z/m mid-values */
1671  POINT4D midv;
1672  cart2geog(&mid, &g);
1673  midv.x = rad2deg(g.lon);
1674  midv.y = rad2deg(g.lat);
1675  midv.z = (v1->z + v2->z) / 2.0;
1676  midv.m = (v1->m + v2->m) / 2.0;
1677  /* Recurse on the left first */
1678  ptarray_segmentize_sphere_edge_recursive(p1, &mid, v1, &midv, d/2.0, max_seg_length, pa);
1679  ptarray_segmentize_sphere_edge_recursive(&mid, p2, &midv, v2, d/2.0, max_seg_length, pa);
1680  return LW_SUCCESS;
1681  }
1682 }
1683 
1689 static POINTARRAY*
1690 ptarray_segmentize_sphere(const POINTARRAY *pa_in, double max_seg_length)
1691 {
1692  POINTARRAY *pa_out;
1693  int hasz = ptarray_has_z(pa_in);
1694  int hasm = ptarray_has_m(pa_in);
1695  POINT4D p1, p2;
1696  POINT3D q1, q2;
1697  GEOGRAPHIC_POINT g1, g2;
1698  uint32_t i;
1699 
1700  /* Just crap out on crazy input */
1701  if ( ! pa_in )
1702  lwerror("%s: null input pointarray", __func__);
1703  if ( max_seg_length <= 0.0 )
1704  lwerror("%s: maximum segment length must be positive", __func__);
1705 
1706  /* Empty starting array */
1707  pa_out = ptarray_construct_empty(hasz, hasm, pa_in->npoints);
1708 
1709  /* Simple loop per edge */
1710  for (i = 1; i < pa_in->npoints; i++)
1711  {
1712  getPoint4d_p(pa_in, i-1, &p1);
1713  getPoint4d_p(pa_in, i, &p2);
1714  geographic_point_init(p1.x, p1.y, &g1);
1715  geographic_point_init(p2.x, p2.y, &g2);
1716 
1717  /* Skip duplicate points (except in case of 2-point lines!) */
1718  if ((pa_in->npoints > 2) && p4d_same(&p1, &p2))
1719  continue;
1720 
1721  /* How long is this edge? */
1722  double d = sphere_distance(&g1, &g2);
1723 
1724  if (d > max_seg_length)
1725  {
1726  geog2cart(&g1, &q1);
1727  geog2cart(&g2, &q2);
1728  /* 3-d end points, XYZM end point, current edge size, min edge size */
1729  ptarray_segmentize_sphere_edge_recursive(&q1, &q2, &p1, &p2, d, max_seg_length, pa_out);
1730  }
1731  /* If we don't segmentize, we need to add first point manually */
1732  else
1733  {
1734  ptarray_append_point(pa_out, &p1, LW_TRUE);
1735  }
1736  }
1737  /* Always add the last point */
1738  ptarray_append_point(pa_out, &p2, LW_TRUE);
1739  return pa_out;
1740 }
1741 
1748 LWGEOM*
1749 lwgeom_segmentize_sphere(const LWGEOM *lwg_in, double max_seg_length)
1750 {
1751  POINTARRAY *pa_out;
1752  LWLINE *lwline;
1753  LWPOLY *lwpoly_in, *lwpoly_out;
1754  LWCOLLECTION *lwcol_in, *lwcol_out;
1755  uint32_t i;
1756 
1757  /* Reflect NULL */
1758  if ( ! lwg_in )
1759  return NULL;
1760 
1761  /* Clone empty */
1762  if ( lwgeom_is_empty(lwg_in) )
1763  return lwgeom_clone(lwg_in);
1764 
1765  switch (lwg_in->type)
1766  {
1767  case MULTIPOINTTYPE:
1768  case POINTTYPE:
1769  return lwgeom_clone_deep(lwg_in);
1770  break;
1771  case LINETYPE:
1772  lwline = lwgeom_as_lwline(lwg_in);
1773  pa_out = ptarray_segmentize_sphere(lwline->points, max_seg_length);
1774  return lwline_as_lwgeom(lwline_construct(lwg_in->srid, NULL, pa_out));
1775  break;
1776  case POLYGONTYPE:
1777  lwpoly_in = lwgeom_as_lwpoly(lwg_in);
1778  lwpoly_out = lwpoly_construct_empty(lwg_in->srid, lwgeom_has_z(lwg_in), lwgeom_has_m(lwg_in));
1779  for ( i = 0; i < lwpoly_in->nrings; i++ )
1780  {
1781  pa_out = ptarray_segmentize_sphere(lwpoly_in->rings[i], max_seg_length);
1782  lwpoly_add_ring(lwpoly_out, pa_out);
1783  }
1784  return lwpoly_as_lwgeom(lwpoly_out);
1785  break;
1786  case MULTILINETYPE:
1787  case MULTIPOLYGONTYPE:
1788  case COLLECTIONTYPE:
1789  lwcol_in = lwgeom_as_lwcollection(lwg_in);
1790  lwcol_out = lwcollection_construct_empty(lwg_in->type, lwg_in->srid, lwgeom_has_z(lwg_in), lwgeom_has_m(lwg_in));
1791  for ( i = 0; i < lwcol_in->ngeoms; i++ )
1792  {
1793  lwcollection_add_lwgeom(lwcol_out, lwgeom_segmentize_sphere(lwcol_in->geoms[i], max_seg_length));
1794  }
1795  return lwcollection_as_lwgeom(lwcol_out);
1796  break;
1797  default:
1798  lwerror("lwgeom_segmentize_sphere: unsupported input geometry type: %d - %s",
1799  lwg_in->type, lwtype_name(lwg_in->type));
1800  break;
1801  }
1802 
1803  lwerror("lwgeom_segmentize_sphere got to the end of the function, should not happen");
1804  return NULL;
1805 }
1806 
1807 
1812 double
1814 {
1815  uint32_t i;
1816  const POINT2D *p;
1817  GEOGRAPHIC_POINT a, b, c;
1818  double area = 0.0;
1819 
1820  /* Return zero on nonsensical inputs */
1821  if ( ! pa || pa->npoints < 4 )
1822  return 0.0;
1823 
1824  p = getPoint2d_cp(pa, 0);
1825  geographic_point_init(p->x, p->y, &a);
1826  p = getPoint2d_cp(pa, 1);
1827  geographic_point_init(p->x, p->y, &b);
1828 
1829  for ( i = 2; i < pa->npoints-1; i++ )
1830  {
1831  p = getPoint2d_cp(pa, i);
1832  geographic_point_init(p->x, p->y, &c);
1833  area += sphere_signed_area(&a, &b, &c);
1834  b = c;
1835  }
1836 
1837  return fabs(area);
1838 }
1839 
1840 
1841 static double ptarray_distance_spheroid(const POINTARRAY *pa1, const POINTARRAY *pa2, const SPHEROID *s, double tolerance, int check_intersection)
1842 {
1843  GEOGRAPHIC_EDGE e1, e2;
1844  GEOGRAPHIC_POINT g1, g2;
1845  GEOGRAPHIC_POINT nearest1, nearest2;
1846  POINT3D A1, A2, B1, B2;
1847  const POINT2D *p;
1848  double distance;
1849  uint32_t i, j;
1850  int use_sphere = (s->a == s->b ? 1 : 0);
1851 
1852  /* Make result really big, so that everything will be smaller than it */
1853  distance = FLT_MAX;
1854 
1855  /* Empty point arrays? Return negative */
1856  if ( pa1->npoints == 0 || pa2->npoints == 0 )
1857  return -1.0;
1858 
1859  /* Handle point/point case here */
1860  if ( pa1->npoints == 1 && pa2->npoints == 1 )
1861  {
1862  p = getPoint2d_cp(pa1, 0);
1863  geographic_point_init(p->x, p->y, &g1);
1864  p = getPoint2d_cp(pa2, 0);
1865  geographic_point_init(p->x, p->y, &g2);
1866  /* Sphere special case, axes equal */
1867  distance = s->radius * sphere_distance(&g1, &g2);
1868  if ( use_sphere )
1869  return distance;
1870  /* Below tolerance, actual distance isn't of interest */
1871  else if ( distance < 0.95 * tolerance )
1872  return distance;
1873  /* Close or greater than tolerance, get the real answer to be sure */
1874  else
1875  return spheroid_distance(&g1, &g2, s);
1876  }
1877 
1878  /* Handle point/line case here */
1879  if ( pa1->npoints == 1 || pa2->npoints == 1 )
1880  {
1881  /* Handle one/many case here */
1882  uint32_t i;
1883  const POINTARRAY *pa_one;
1884  const POINTARRAY *pa_many;
1885 
1886  if ( pa1->npoints == 1 )
1887  {
1888  pa_one = pa1;
1889  pa_many = pa2;
1890  }
1891  else
1892  {
1893  pa_one = pa2;
1894  pa_many = pa1;
1895  }
1896 
1897  /* Initialize our point */
1898  p = getPoint2d_cp(pa_one, 0);
1899  geographic_point_init(p->x, p->y, &g1);
1900 
1901  /* Initialize start of line */
1902  p = getPoint2d_cp(pa_many, 0);
1903  geographic_point_init(p->x, p->y, &(e1.start));
1904 
1905  /* Iterate through the edges in our line */
1906  for ( i = 1; i < pa_many->npoints; i++ )
1907  {
1908  double d;
1909  p = getPoint2d_cp(pa_many, i);
1910  geographic_point_init(p->x, p->y, &(e1.end));
1911  /* Get the spherical distance between point and edge */
1912  d = s->radius * edge_distance_to_point(&e1, &g1, &g2);
1913  /* New shortest distance! Record this distance / location */
1914  if ( d < distance )
1915  {
1916  distance = d;
1917  nearest2 = g2;
1918  }
1919  /* We've gotten closer than the tolerance... */
1920  if ( d <= tolerance )
1921  {
1922  /* Working on a sphere? The answer is correct, return */
1923  if ( use_sphere )
1924  {
1925  return d;
1926  }
1927  /* Far enough past the tolerance that the spheroid calculation won't change things */
1928  else if ( d <= tolerance * 0.95 )
1929  {
1930  return d;
1931  }
1932  /* On a spheroid and near the tolerance? Confirm that we are *actually* closer than tolerance */
1933  else
1934  {
1935  d = spheroid_distance(&g1, &nearest2, s);
1936  /* Yes, closer than tolerance, return! */
1937  if ( d <= tolerance )
1938  return d;
1939  }
1940  }
1941  e1.start = e1.end;
1942  }
1943 
1944  /* On sphere, return answer */
1945  if ( use_sphere )
1946  return distance;
1947  /* On spheroid, calculate final answer based on closest approach */
1948  else
1949  return spheroid_distance(&g1, &nearest2, s);
1950 
1951  }
1952 
1953  /* Initialize start of line 1 */
1954  p = getPoint2d_cp(pa1, 0);
1955  geographic_point_init(p->x, p->y, &(e1.start));
1956  geog2cart(&(e1.start), &A1);
1957 
1958 
1959  /* Handle line/line case */
1960  for ( i = 1; i < pa1->npoints; i++ )
1961  {
1962  p = getPoint2d_cp(pa1, i);
1963  geographic_point_init(p->x, p->y, &(e1.end));
1964  geog2cart(&(e1.end), &A2);
1965 
1966  /* Initialize start of line 2 */
1967  p = getPoint2d_cp(pa2, 0);
1968  geographic_point_init(p->x, p->y, &(e2.start));
1969  geog2cart(&(e2.start), &B1);
1970 
1971  for ( j = 1; j < pa2->npoints; j++ )
1972  {
1973  double d;
1974 
1975  p = getPoint2d_cp(pa2, j);
1976  geographic_point_init(p->x, p->y, &(e2.end));
1977  geog2cart(&(e2.end), &B2);
1978 
1979  LWDEBUGF(4, "e1.start == GPOINT(%.6g %.6g) ", e1.start.lat, e1.start.lon);
1980  LWDEBUGF(4, "e1.end == GPOINT(%.6g %.6g) ", e1.end.lat, e1.end.lon);
1981  LWDEBUGF(4, "e2.start == GPOINT(%.6g %.6g) ", e2.start.lat, e2.start.lon);
1982  LWDEBUGF(4, "e2.end == GPOINT(%.6g %.6g) ", e2.end.lat, e2.end.lon);
1983 
1984  if ( check_intersection && edge_intersects(&A1, &A2, &B1, &B2) )
1985  {
1986  LWDEBUG(4,"edge intersection! returning 0.0");
1987  return 0.0;
1988  }
1989  d = s->radius * edge_distance_to_edge(&e1, &e2, &g1, &g2);
1990  LWDEBUGF(4,"got edge_distance_to_edge %.8g", d);
1991 
1992  if ( d < distance )
1993  {
1994  distance = d;
1995  nearest1 = g1;
1996  nearest2 = g2;
1997  }
1998  if ( d <= tolerance )
1999  {
2000  if ( use_sphere )
2001  {
2002  return d;
2003  }
2004  else
2005  {
2006  d = spheroid_distance(&nearest1, &nearest2, s);
2007  if ( d <= tolerance )
2008  return d;
2009  }
2010  }
2011 
2012  /* Copy end to start to allow a new end value in next iteration */
2013  e2.start = e2.end;
2014  B1 = B2;
2015  }
2016 
2017  /* Copy end to start to allow a new end value in next iteration */
2018  e1.start = e1.end;
2019  A1 = A2;
2020  LW_ON_INTERRUPT(return -1.0);
2021  }
2022  LWDEBUGF(4,"finished all loops, returning %.8g", distance);
2023 
2024  if ( use_sphere )
2025  return distance;
2026  else
2027  return spheroid_distance(&nearest1, &nearest2, s);
2028 }
2029 
2030 
2037 double lwgeom_area_sphere(const LWGEOM *lwgeom, const SPHEROID *spheroid)
2038 {
2039  int type;
2040  double radius2 = spheroid->radius * spheroid->radius;
2041 
2042  assert(lwgeom);
2043 
2044  /* No area in nothing */
2045  if ( lwgeom_is_empty(lwgeom) )
2046  return 0.0;
2047 
2048  /* Read the geometry type number */
2049  type = lwgeom->type;
2050 
2051  /* Anything but polygons and collections returns zero */
2052  if ( ! ( type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE ) )
2053  return 0.0;
2054 
2055  /* Actually calculate area */
2056  if ( type == POLYGONTYPE )
2057  {
2058  LWPOLY *poly = (LWPOLY*)lwgeom;
2059  uint32_t i;
2060  double area = 0.0;
2061 
2062  /* Just in case there's no rings */
2063  if ( poly->nrings < 1 )
2064  return 0.0;
2065 
2066  /* First, the area of the outer ring */
2067  area += radius2 * ptarray_area_sphere(poly->rings[0]);
2068 
2069  /* Subtract areas of inner rings */
2070  for ( i = 1; i < poly->nrings; i++ )
2071  {
2072  area -= radius2 * ptarray_area_sphere(poly->rings[i]);
2073  }
2074  return area;
2075  }
2076 
2077  /* Recurse into sub-geometries to get area */
2078  if ( type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE )
2079  {
2080  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom;
2081  uint32_t i;
2082  double area = 0.0;
2083 
2084  for ( i = 0; i < col->ngeoms; i++ )
2085  {
2086  area += lwgeom_area_sphere(col->geoms[i], spheroid);
2087  }
2088  return area;
2089  }
2090 
2091  /* Shouldn't get here. */
2092  return 0.0;
2093 }
2094 
2095 
2105 LWPOINT* lwgeom_project_spheroid(const LWPOINT *r, const SPHEROID *spheroid, double distance, double azimuth)
2106 {
2107  GEOGRAPHIC_POINT geo_source, geo_dest;
2108  POINT4D pt_dest;
2109  double x, y;
2110  LWPOINT *lwp;
2111  int has_z, has_m;
2112 
2113  /* Normalize distance to be positive*/
2114  if ( distance < 0.0 ) {
2115  distance = -distance;
2116  azimuth += M_PI;
2117  }
2118 
2119  /* Normalize azimuth */
2120  azimuth -= 2.0 * M_PI * floor(azimuth / (2.0 * M_PI));
2121 
2122  /* Check the distance validity */
2123  if ( distance > (M_PI * spheroid->radius) )
2124  {
2125  lwerror("Distance must not be greater than %g", M_PI * spheroid->radius);
2126  return NULL;
2127  }
2128 
2129  /* Convert to ta geodetic point */
2130  x = lwpoint_get_x(r);
2131  y = lwpoint_get_y(r);
2132  has_z = lwgeom_has_z(lwpoint_as_lwgeom(r));
2133  has_m = lwgeom_has_m(lwpoint_as_lwgeom(r));
2134  geographic_point_init(x, y, &geo_source);
2135 
2136  /* Try the projection */
2137  if( spheroid_project(&geo_source, spheroid, distance, azimuth, &geo_dest) == LW_FAILURE )
2138  {
2139  LWDEBUGF(3, "Unable to project from (%g %g) with azimuth %g and distance %g", x, y, azimuth, distance);
2140  lwerror("Unable to project from (%g %g) with azimuth %g and distance %g", x, y, azimuth, distance);
2141  return NULL;
2142  }
2143 
2144  /* Build the output LWPOINT */
2145  pt_dest.x = rad2deg(longitude_radians_normalize(geo_dest.lon));
2146  pt_dest.y = rad2deg(latitude_radians_normalize(geo_dest.lat));
2147  pt_dest.z = has_z ? lwpoint_get_z(r) : 0.0;
2148  pt_dest.m = has_m ? lwpoint_get_m(r) : 0.0;
2149  lwp = lwpoint_make(r->srid, has_z, has_m, &pt_dest);
2151  return lwp;
2152 }
2153 
2154 LWPOINT* lwgeom_project_spheroid_lwpoint(const LWPOINT *from, const LWPOINT *to, const SPHEROID *spheroid, double distance)
2155 {
2156  double azimuth = lwgeom_azumith_spheroid(from, to, spheroid);
2157  LWPOINT *lwp = lwgeom_project_spheroid(to, spheroid, distance, azimuth);
2158  return lwp;
2159 }
2160 
2161 
2170 double lwgeom_azumith_spheroid(const LWPOINT *r, const LWPOINT *s, const SPHEROID *spheroid)
2171 {
2172  GEOGRAPHIC_POINT g1, g2;
2173  double x1, y1, x2, y2, az;
2174 
2175  /* Convert r to a geodetic point */
2176  x1 = lwpoint_get_x(r);
2177  y1 = lwpoint_get_y(r);
2178  geographic_point_init(x1, y1, &g1);
2179 
2180  /* Convert s to a geodetic point */
2181  x2 = lwpoint_get_x(s);
2182  y2 = lwpoint_get_y(s);
2183  geographic_point_init(x2, y2, &g2);
2184 
2185  /* Same point, return NaN */
2186  if ( FP_EQUALS(x1, x2) && FP_EQUALS(y1, y2) )
2187  {
2188  return NAN;
2189  }
2190 
2191  /* Do the direction calculation */
2192  az = spheroid_direction(&g1, &g2, spheroid);
2193  /* Ensure result is positive */
2194  return az < -0 ? 2*M_PI + az : az;
2195  // return az;
2196 }
2197 
2204 double lwgeom_distance_spheroid(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2, const SPHEROID *spheroid, double tolerance)
2205 {
2206  uint8_t type1, type2;
2207  int check_intersection = LW_FALSE;
2208  GBOX gbox1, gbox2;
2209 
2210  gbox_init(&gbox1);
2211  gbox_init(&gbox2);
2212 
2213  assert(lwgeom1);
2214  assert(lwgeom2);
2215 
2216  LWDEBUGF(4, "entered function, tolerance %.8g", tolerance);
2217 
2218  /* What's the distance to an empty geometry? We don't know.
2219  Return a negative number so the caller can catch this case. */
2220  if ( lwgeom_is_empty(lwgeom1) || lwgeom_is_empty(lwgeom2) )
2221  {
2222  return -1.0;
2223  }
2224 
2225  type1 = lwgeom1->type;
2226  type2 = lwgeom2->type;
2227 
2228  /* Make sure we have boxes */
2229  if ( lwgeom1->bbox )
2230  gbox1 = *(lwgeom1->bbox);
2231  else
2232  lwgeom_calculate_gbox_geodetic(lwgeom1, &gbox1);
2233 
2234  /* Make sure we have boxes */
2235  if ( lwgeom2->bbox )
2236  gbox2 = *(lwgeom2->bbox);
2237  else
2238  lwgeom_calculate_gbox_geodetic(lwgeom2, &gbox2);
2239 
2240  /* If the boxes aren't disjoint, we have to check for edge intersections */
2241  if ( gbox_overlaps(&gbox1, &gbox2) )
2242  check_intersection = LW_TRUE;
2243 
2244  /* Point/line combinations can all be handled with simple point array iterations */
2245  if ( ( type1 == POINTTYPE || type1 == LINETYPE ) &&
2246  ( type2 == POINTTYPE || type2 == LINETYPE ) )
2247  {
2248  POINTARRAY *pa1, *pa2;
2249 
2250  if ( type1 == POINTTYPE )
2251  pa1 = ((LWPOINT*)lwgeom1)->point;
2252  else
2253  pa1 = ((LWLINE*)lwgeom1)->points;
2254 
2255  if ( type2 == POINTTYPE )
2256  pa2 = ((LWPOINT*)lwgeom2)->point;
2257  else
2258  pa2 = ((LWLINE*)lwgeom2)->points;
2259 
2260  return ptarray_distance_spheroid(pa1, pa2, spheroid, tolerance, check_intersection);
2261  }
2262 
2263  /* Point/Polygon cases, if point-in-poly, return zero, else return distance. */
2264  if ( ( type1 == POLYGONTYPE && type2 == POINTTYPE ) ||
2265  ( type2 == POLYGONTYPE && type1 == POINTTYPE ) )
2266  {
2267  const POINT2D *p;
2268  LWPOLY *lwpoly;
2269  LWPOINT *lwpt;
2270  double distance = FLT_MAX;
2271  uint32_t i;
2272 
2273  if ( type1 == POINTTYPE )
2274  {
2275  lwpt = (LWPOINT*)lwgeom1;
2276  lwpoly = (LWPOLY*)lwgeom2;
2277  }
2278  else
2279  {
2280  lwpt = (LWPOINT*)lwgeom2;
2281  lwpoly = (LWPOLY*)lwgeom1;
2282  }
2283  p = getPoint2d_cp(lwpt->point, 0);
2284 
2285  /* Point in polygon implies zero distance */
2286  if ( lwpoly_covers_point2d(lwpoly, p) )
2287  {
2288  return 0.0;
2289  }
2290 
2291  /* Not inside, so what's the actual distance? */
2292  for ( i = 0; i < lwpoly->nrings; i++ )
2293  {
2294  double ring_distance = ptarray_distance_spheroid(lwpoly->rings[i], lwpt->point, spheroid, tolerance, check_intersection);
2295  if ( ring_distance < distance )
2296  distance = ring_distance;
2297  if ( distance <= tolerance )
2298  return distance;
2299  }
2300  return distance;
2301  }
2302 
2303  /* Line/polygon case, if start point-in-poly, return zero, else return distance. */
2304  if ( ( type1 == POLYGONTYPE && type2 == LINETYPE ) ||
2305  ( type2 == POLYGONTYPE && type1 == LINETYPE ) )
2306  {
2307  const POINT2D *p;
2308  LWPOLY *lwpoly;
2309  LWLINE *lwline;
2310  double distance = FLT_MAX;
2311  uint32_t i;
2312 
2313  if ( type1 == LINETYPE )
2314  {
2315  lwline = (LWLINE*)lwgeom1;
2316  lwpoly = (LWPOLY*)lwgeom2;
2317  }
2318  else
2319  {
2320  lwline = (LWLINE*)lwgeom2;
2321  lwpoly = (LWPOLY*)lwgeom1;
2322  }
2323  p = getPoint2d_cp(lwline->points, 0);
2324 
2325  LWDEBUG(4, "checking if a point of line is in polygon");
2326 
2327  /* Point in polygon implies zero distance */
2328  if ( lwpoly_covers_point2d(lwpoly, p) )
2329  return 0.0;
2330 
2331  LWDEBUG(4, "checking ring distances");
2332 
2333  /* Not contained, so what's the actual distance? */
2334  for ( i = 0; i < lwpoly->nrings; i++ )
2335  {
2336  double ring_distance = ptarray_distance_spheroid(lwpoly->rings[i], lwline->points, spheroid, tolerance, check_intersection);
2337  LWDEBUGF(4, "ring[%d] ring_distance = %.8g", i, ring_distance);
2338  if ( ring_distance < distance )
2339  distance = ring_distance;
2340  if ( distance <= tolerance )
2341  return distance;
2342  }
2343  LWDEBUGF(4, "all rings checked, returning distance = %.8g", distance);
2344  return distance;
2345 
2346  }
2347 
2348  /* Polygon/polygon case, if start point-in-poly, return zero, else
2349  * return distance. */
2350  if (type1 == POLYGONTYPE && type2 == POLYGONTYPE)
2351  {
2352  const POINT2D* p;
2353  LWPOLY* lwpoly1 = (LWPOLY*)lwgeom1;
2354  LWPOLY* lwpoly2 = (LWPOLY*)lwgeom2;
2355  double distance = FLT_MAX;
2356  uint32_t i, j;
2357 
2358  /* Point of 2 in polygon 1 implies zero distance */
2359  p = getPoint2d_cp(lwpoly1->rings[0], 0);
2360  if (lwpoly_covers_point2d(lwpoly2, p)) return 0.0;
2361 
2362  /* Point of 1 in polygon 2 implies zero distance */
2363  p = getPoint2d_cp(lwpoly2->rings[0], 0);
2364  if (lwpoly_covers_point2d(lwpoly1, p)) return 0.0;
2365 
2366  /* Not contained, so what's the actual distance? */
2367  for (i = 0; i < lwpoly1->nrings; i++)
2368  {
2369  for (j = 0; j < lwpoly2->nrings; j++)
2370  {
2371  double ring_distance =
2373  lwpoly1->rings[i],
2374  lwpoly2->rings[j],
2375  spheroid,
2376  tolerance,
2377  check_intersection);
2378  if (ring_distance < distance)
2379  distance = ring_distance;
2380  if (distance <= tolerance) return distance;
2381  }
2382  }
2383  return distance;
2384  }
2385 
2386  /* Recurse into collections */
2387  if ( lwtype_is_collection(type1) )
2388  {
2389  uint32_t i;
2390  double distance = FLT_MAX;
2391  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom1;
2392 
2393  for ( i = 0; i < col->ngeoms; i++ )
2394  {
2395  double geom_distance = lwgeom_distance_spheroid(
2396  col->geoms[i], lwgeom2, spheroid, tolerance);
2397  if ( geom_distance < distance )
2398  distance = geom_distance;
2399  if ( distance <= tolerance )
2400  return distance;
2401  }
2402  return distance;
2403  }
2404 
2405  /* Recurse into collections */
2406  if ( lwtype_is_collection(type2) )
2407  {
2408  uint32_t i;
2409  double distance = FLT_MAX;
2410  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom2;
2411 
2412  for ( i = 0; i < col->ngeoms; i++ )
2413  {
2414  double geom_distance = lwgeom_distance_spheroid(lwgeom1, col->geoms[i], spheroid, tolerance);
2415  if ( geom_distance < distance )
2416  distance = geom_distance;
2417  if ( distance <= tolerance )
2418  return distance;
2419  }
2420  return distance;
2421  }
2422 
2423 
2424  lwerror("arguments include unsupported geometry type (%s, %s)", lwtype_name(type1), lwtype_name(type1));
2425  return -1.0;
2426 
2427 }
2428 
2429 
2430 int lwgeom_covers_lwgeom_sphere(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
2431 {
2432  int type1, type2;
2433  GBOX gbox1, gbox2;
2434  gbox1.flags = gbox2.flags = 0;
2435 
2436  assert(lwgeom1);
2437  assert(lwgeom2);
2438 
2439  type1 = lwgeom1->type;
2440  type2 = lwgeom2->type;
2441 
2442  /* dim(geom2) > dim(geom1) always returns false (because geom2 is bigger) */
2443  if ( (type1 == POINTTYPE && type2 == LINETYPE)
2444  || (type1 == POINTTYPE && type2 == POLYGONTYPE)
2445  || (type1 == LINETYPE && type2 == POLYGONTYPE) )
2446  {
2447  LWDEBUG(4, "dimension of geom2 is bigger than geom1");
2448  return LW_FALSE;
2449  }
2450 
2451  /* Make sure we have boxes */
2452  if ( lwgeom1->bbox )
2453  gbox1 = *(lwgeom1->bbox);
2454  else
2455  lwgeom_calculate_gbox_geodetic(lwgeom1, &gbox1);
2456 
2457  /* Make sure we have boxes */
2458  if ( lwgeom2->bbox )
2459  gbox2 = *(lwgeom2->bbox);
2460  else
2461  lwgeom_calculate_gbox_geodetic(lwgeom2, &gbox2);
2462 
2463 
2464  /* Handle the polygon/point case */
2465  if ( type1 == POLYGONTYPE && type2 == POINTTYPE )
2466  {
2467  POINT2D pt_to_test;
2468  getPoint2d_p(((LWPOINT*)lwgeom2)->point, 0, &pt_to_test);
2469  return lwpoly_covers_point2d((LWPOLY*)lwgeom1, &pt_to_test);
2470  }
2471  else if ( type1 == POLYGONTYPE && type2 == LINETYPE)
2472  {
2473  return lwpoly_covers_lwline((LWPOLY*)lwgeom1, (LWLINE*)lwgeom2);
2474  }
2475  else if ( type1 == POLYGONTYPE && type2 == POLYGONTYPE)
2476  {
2477  return lwpoly_covers_lwpoly((LWPOLY*)lwgeom1, (LWPOLY*)lwgeom2);
2478  }
2479  else if ( type1 == LINETYPE && type2 == POINTTYPE)
2480  {
2481  return lwline_covers_lwpoint((LWLINE*)lwgeom1, (LWPOINT*)lwgeom2);
2482  }
2483  else if ( type1 == LINETYPE && type2 == LINETYPE)
2484  {
2485  return lwline_covers_lwline((LWLINE*)lwgeom1, (LWLINE*)lwgeom2);
2486  }
2487  else if ( type1 == POINTTYPE && type2 == POINTTYPE)
2488  {
2489  return lwpoint_same((LWPOINT*)lwgeom1, (LWPOINT*)lwgeom2);
2490  }
2491 
2492  /* If any of the first argument parts covers the second argument, it's true */
2493  if ( lwtype_is_collection( type1 ) )
2494  {
2495  uint32_t i;
2496  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom1;
2497 
2498  for ( i = 0; i < col->ngeoms; i++ )
2499  {
2500  if ( lwgeom_covers_lwgeom_sphere(col->geoms[i], lwgeom2) )
2501  {
2502  return LW_TRUE;
2503  }
2504  }
2505  return LW_FALSE;
2506  }
2507 
2508  /* Only if all of the second arguments are covered by the first argument is the condition true */
2509  if ( lwtype_is_collection( type2 ) )
2510  {
2511  uint32_t i;
2512  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom2;
2513 
2514  for ( i = 0; i < col->ngeoms; i++ )
2515  {
2516  if ( ! lwgeom_covers_lwgeom_sphere(lwgeom1, col->geoms[i]) )
2517  {
2518  return LW_FALSE;
2519  }
2520  }
2521  return LW_TRUE;
2522  }
2523 
2524  /* Don't get here */
2525  lwerror("lwgeom_covers_lwgeom_sphere: reached end of function without resolution");
2526  return LW_FALSE;
2527 
2528 }
2529 
2535 int lwpoly_covers_point2d(const LWPOLY *poly, const POINT2D *pt_to_test)
2536 {
2537  uint32_t i;
2538  int in_hole_count = 0;
2539  POINT3D p;
2540  GEOGRAPHIC_POINT gpt_to_test;
2541  POINT2D pt_outside;
2542  GBOX gbox;
2543 #if POSTGIS_DEBUG_LEVEL >= 4
2544  char *geom_ewkt;
2545 #endif
2546  gbox.flags = 0;
2547 
2548  /* Nulls and empties don't contain anything! */
2549  if ( ! poly || lwgeom_is_empty((LWGEOM*)poly) )
2550  {
2551  LWDEBUG(4,"returning false, geometry is empty or null");
2552  return LW_FALSE;
2553  }
2554 
2555  /* Make sure we have boxes */
2556  if ( poly->bbox )
2557  gbox = *(poly->bbox);
2558  else
2559  lwgeom_calculate_gbox_geodetic((LWGEOM*)poly, &gbox);
2560 
2561  /* Point not in box? Done! */
2562  geographic_point_init(pt_to_test->x, pt_to_test->y, &gpt_to_test);
2563  geog2cart(&gpt_to_test, &p);
2564  if ( ! gbox_contains_point3d(&gbox, &p) )
2565  {
2566  LWDEBUG(4, "the point is not in the box!");
2567  return LW_FALSE;
2568  }
2569 
2570  /* Calculate our outside point from the gbox */
2571  lwpoly_pt_outside(poly, &pt_outside);
2572 
2573  LWDEBUGF(4, "pt_outside POINT(%.18g %.18g)", pt_outside.x, pt_outside.y);
2574  LWDEBUGF(4, "pt_to_test POINT(%.18g %.18g)", pt_to_test->x, pt_to_test->y);
2575 #if POSTGIS_DEBUG_LEVEL >= 4
2576  geom_ewkt = lwgeom_to_ewkt((LWGEOM*)poly);
2577  LWDEBUGF(4, "polygon %s", geom_ewkt);
2578  lwfree(geom_ewkt);
2579  geom_ewkt = gbox_to_string(&gbox);
2580  LWDEBUGF(4, "gbox %s", geom_ewkt);
2581  lwfree(geom_ewkt);
2582 #endif
2583 
2584  /* Not in outer ring? We're done! */
2585  if ( ! ptarray_contains_point_sphere(poly->rings[0], &pt_outside, pt_to_test) )
2586  {
2587  LWDEBUG(4,"returning false, point is outside ring");
2588  return LW_FALSE;
2589  }
2590 
2591  LWDEBUGF(4, "testing %d rings", poly->nrings);
2592 
2593  /* But maybe point is in a hole... */
2594  for ( i = 1; i < poly->nrings; i++ )
2595  {
2596  LWDEBUGF(4, "ring test loop %d", i);
2597  /* Count up hole containment. Odd => outside boundary. */
2598  if ( ptarray_contains_point_sphere(poly->rings[i], &pt_outside, pt_to_test) )
2599  in_hole_count++;
2600  }
2601 
2602  LWDEBUGF(4, "in_hole_count == %d", in_hole_count);
2603 
2604  if ( in_hole_count % 2 )
2605  {
2606  LWDEBUG(4,"returning false, inner ring containment count is odd");
2607  return LW_FALSE;
2608  }
2609 
2610  LWDEBUG(4,"returning true, inner ring containment count is even");
2611  return LW_TRUE;
2612 }
2613 
2619 int lwpoly_covers_lwpoly(const LWPOLY *poly1, const LWPOLY *poly2)
2620 {
2621  uint32_t i;
2622 
2623  /* Nulls and empties don't contain anything! */
2624  if ( ! poly1 || lwgeom_is_empty((LWGEOM*)poly1) )
2625  {
2626  LWDEBUG(4,"returning false, geometry1 is empty or null");
2627  return LW_FALSE;
2628  }
2629 
2630  /* Nulls and empties don't contain anything! */
2631  if ( ! poly2 || lwgeom_is_empty((LWGEOM*)poly2) )
2632  {
2633  LWDEBUG(4,"returning false, geometry2 is empty or null");
2634  return LW_FALSE;
2635  }
2636 
2637  /* check if all vertices of poly2 are inside poly1 */
2638  for (i = 0; i < poly2->nrings; i++)
2639  {
2640 
2641  /* every other ring is a hole, check if point is inside the actual polygon */
2642  if ( i % 2 == 0)
2643  {
2644  if (LW_FALSE == lwpoly_covers_pointarray(poly1, poly2->rings[i]))
2645  {
2646  LWDEBUG(4,"returning false, geometry2 has point outside of geometry1");
2647  return LW_FALSE;
2648  }
2649  }
2650  else
2651  {
2652  if (LW_TRUE == lwpoly_covers_pointarray(poly1, poly2->rings[i]))
2653  {
2654  LWDEBUG(4,"returning false, geometry2 has point inside a hole of geometry1");
2655  return LW_FALSE;
2656  }
2657  }
2658  }
2659 
2660  /* check for any edge intersections, so nothing is partially outside of poly1 */
2661  for (i = 0; i < poly2->nrings; i++)
2662  {
2663  if (LW_TRUE == lwpoly_intersects_line(poly1, poly2->rings[i]))
2664  {
2665  LWDEBUG(4,"returning false, geometry2 is partially outside of geometry1");
2666  return LW_FALSE;
2667  }
2668  }
2669 
2670  /* no abort condition found, so the poly2 should be completly inside poly1 */
2671  return LW_TRUE;
2672 }
2673 
2677 int lwpoly_covers_lwline(const LWPOLY *poly, const LWLINE *line)
2678 {
2679  /* Nulls and empties don't contain anything! */
2680  if ( ! poly || lwgeom_is_empty((LWGEOM*)poly) )
2681  {
2682  LWDEBUG(4,"returning false, geometry1 is empty or null");
2683  return LW_FALSE;
2684  }
2685 
2686  /* Nulls and empties don't contain anything! */
2687  if ( ! line || lwgeom_is_empty((LWGEOM*)line) )
2688  {
2689  LWDEBUG(4,"returning false, geometry2 is empty or null");
2690  return LW_FALSE;
2691  }
2692 
2693  if (LW_FALSE == lwpoly_covers_pointarray(poly, line->points))
2694  {
2695  LWDEBUG(4,"returning false, geometry2 has point outside of geometry1");
2696  return LW_FALSE;
2697  }
2698 
2699  /* check for any edge intersections, so nothing is partially outside of poly1 */
2700  if (LW_TRUE == lwpoly_intersects_line(poly, line->points))
2701  {
2702  LWDEBUG(4,"returning false, geometry2 is partially outside of geometry1");
2703  return LW_FALSE;
2704  }
2705 
2706  /* no abort condition found, so the poly2 should be completely inside poly1 */
2707  return LW_TRUE;
2708 }
2709 
2713 int lwpoly_covers_pointarray(const LWPOLY* lwpoly, const POINTARRAY* pta)
2714 {
2715  uint32_t i;
2716  for (i = 0; i < pta->npoints; i++) {
2717  const POINT2D* pt_to_test = getPoint2d_cp(pta, i);
2718 
2719  if ( LW_FALSE == lwpoly_covers_point2d(lwpoly, pt_to_test) ) {
2720  LWDEBUG(4,"returning false, geometry2 has point outside of geometry1");
2721  return LW_FALSE;
2722  }
2723  }
2724 
2725  return LW_TRUE;
2726 }
2727 
2732 int lwpoly_intersects_line(const LWPOLY* lwpoly, const POINTARRAY* line)
2733 {
2734  uint32_t i, j, k;
2735  POINT3D pa1, pa2, pb1, pb2;
2736  for (i = 0; i < lwpoly->nrings; i++)
2737  {
2738  for (j = 0; j < lwpoly->rings[i]->npoints - 1; j++)
2739  {
2740  const POINT2D* a1 = getPoint2d_cp(lwpoly->rings[i], j);
2741  const POINT2D* a2 = getPoint2d_cp(lwpoly->rings[i], j+1);
2742 
2743  /* Set up our stab line */
2744  ll2cart(a1, &pa1);
2745  ll2cart(a2, &pa2);
2746 
2747  for (k = 0; k < line->npoints - 1; k++)
2748  {
2749  const POINT2D* b1 = getPoint2d_cp(line, k);
2750  const POINT2D* b2 = getPoint2d_cp(line, k+1);
2751 
2752  /* Set up our stab line */
2753  ll2cart(b1, &pb1);
2754  ll2cart(b2, &pb2);
2755 
2756  int inter = edge_intersects(&pa1, &pa2, &pb1, &pb2);
2757 
2758  /* ignore same edges */
2759  if (inter & PIR_INTERSECTS
2760  && !(inter & PIR_B_TOUCH_RIGHT || inter & PIR_COLINEAR) )
2761  {
2762  return LW_TRUE;
2763  }
2764  }
2765  }
2766  }
2767 
2768  return LW_FALSE;
2769 }
2770 
2774 int lwline_covers_lwpoint(const LWLINE* lwline, const LWPOINT* lwpoint)
2775 {
2776  uint32_t i;
2777  GEOGRAPHIC_POINT p;
2778  GEOGRAPHIC_EDGE e;
2779 
2780  for ( i = 0; i < lwline->points->npoints - 1; i++)
2781  {
2782  const POINT2D* a1 = getPoint2d_cp(lwline->points, i);
2783  const POINT2D* a2 = getPoint2d_cp(lwline->points, i+1);
2784 
2785  geographic_point_init(a1->x, a1->y, &(e.start));
2786  geographic_point_init(a2->x, a2->y, &(e.end));
2787 
2788  geographic_point_init(lwpoint_get_x(lwpoint), lwpoint_get_y(lwpoint), &p);
2789 
2790  if ( edge_contains_point(&e, &p) ) {
2791  return LW_TRUE;
2792  }
2793  }
2794 
2795  return LW_FALSE;
2796 }
2797 
2803 int lwline_covers_lwline(const LWLINE* lwline1, const LWLINE* lwline2)
2804 {
2805  uint32_t i, j;
2806  GEOGRAPHIC_EDGE e1, e2;
2807  GEOGRAPHIC_POINT p1, p2;
2808  int start = LW_FALSE;
2809  int changed = LW_FALSE;
2810 
2811  /* first point on line */
2812  if ( ! lwline_covers_lwpoint(lwline1, lwline_get_lwpoint(lwline2, 0)))
2813  {
2814  LWDEBUG(4,"returning false, first point of line2 is not covered by line1");
2815  return LW_FALSE;
2816  }
2817 
2818  /* last point on line */
2819  if ( ! lwline_covers_lwpoint(lwline1, lwline_get_lwpoint(lwline2, lwline2->points->npoints - 1)))
2820  {
2821  LWDEBUG(4,"returning false, last point of line2 is not covered by line1");
2822  return LW_FALSE;
2823  }
2824 
2825  j = 0;
2826  i = 0;
2827  while (i < lwline1->points->npoints - 1 && j < lwline2->points->npoints - 1)
2828  {
2829  changed = LW_FALSE;
2830  const POINT2D* a1 = getPoint2d_cp(lwline1->points, i);
2831  const POINT2D* a2 = getPoint2d_cp(lwline1->points, i+1);
2832  const POINT2D* b1 = getPoint2d_cp(lwline2->points, j);
2833  const POINT2D* b2 = getPoint2d_cp(lwline2->points, j+1);
2834 
2835  geographic_point_init(a1->x, a1->y, &(e1.start));
2836  geographic_point_init(a2->x, a2->y, &(e1.end));
2837  geographic_point_init(b1->x, b1->y, &p2);
2838 
2839  /* we already know, that the last point is on line1, so we're done */
2840  if ( j == lwline2->points->npoints - 1)
2841  {
2842  return LW_TRUE;
2843  }
2844  else if (start == LW_TRUE)
2845  {
2846  /* point is on current line1 edge, check next point in line2 */
2847  if ( edge_contains_point(&e1, &p2)) {
2848  j++;
2849  changed = LW_TRUE;
2850  }
2851 
2852  geographic_point_init(a1->x, a1->y, &(e2.start));
2853  geographic_point_init(a2->x, b2->y, &(e2.end));
2854  geographic_point_init(a1->x, a1->y, &p1);
2855 
2856  /* point is on current line2 edge, check next point in line1 */
2857  if ( edge_contains_point(&e2, &p1)) {
2858  i++;
2859  changed = LW_TRUE;
2860  }
2861 
2862  /* no edge progressed -> point left one line */
2863  if ( changed == LW_FALSE )
2864  {
2865  LWDEBUG(4,"returning false, found point not covered by both lines");
2866  return LW_FALSE;
2867  }
2868  else
2869  {
2870  continue;
2871  }
2872  }
2873 
2874  /* find first edge to cover line2 */
2875  if (edge_contains_point(&e1, &p2))
2876  {
2877  start = LW_TRUE;
2878  }
2879 
2880  /* next line1 edge */
2881  i++;
2882  }
2883 
2884  /* no uncovered point found */
2885  return LW_TRUE;
2886 }
2887 
2889 {
2890  uint32_t i;
2891  int first = LW_TRUE;
2892  const POINT2D *p;
2893  POINT3D A1, A2;
2894  GBOX edge_gbox;
2895 
2896  assert(gbox);
2897  assert(pa);
2898 
2899  gbox_init(&edge_gbox);
2900  edge_gbox.flags = gbox->flags;
2901 
2902  if ( pa->npoints == 0 ) return LW_FAILURE;
2903 
2904  if ( pa->npoints == 1 )
2905  {
2906  p = getPoint2d_cp(pa, 0);
2907  ll2cart(p, &A1);
2908  gbox->xmin = gbox->xmax = A1.x;
2909  gbox->ymin = gbox->ymax = A1.y;
2910  gbox->zmin = gbox->zmax = A1.z;
2911  return LW_SUCCESS;
2912  }
2913 
2914  p = getPoint2d_cp(pa, 0);
2915  ll2cart(p, &A1);
2916 
2917  for ( i = 1; i < pa->npoints; i++ )
2918  {
2919 
2920  p = getPoint2d_cp(pa, i);
2921  ll2cart(p, &A2);
2922 
2923  edge_calculate_gbox(&A1, &A2, &edge_gbox);
2924 
2925  /* Initialize the box */
2926  if ( first )
2927  {
2928  gbox_duplicate(&edge_gbox, gbox);
2929  first = LW_FALSE;
2930  }
2931  /* Expand the box where necessary */
2932  else
2933  {
2934  gbox_merge(&edge_gbox, gbox);
2935  }
2936 
2937  A1 = A2;
2938  }
2939 
2940  return LW_SUCCESS;
2941 }
2942 
2943 static int lwpoint_calculate_gbox_geodetic(const LWPOINT *point, GBOX *gbox)
2944 {
2945  assert(point);
2946  return ptarray_calculate_gbox_geodetic(point->point, gbox);
2947 }
2948 
2949 static int lwline_calculate_gbox_geodetic(const LWLINE *line, GBOX *gbox)
2950 {
2951  assert(line);
2952  return ptarray_calculate_gbox_geodetic(line->points, gbox);
2953 }
2954 
2955 static int lwpolygon_calculate_gbox_geodetic(const LWPOLY *poly, GBOX *gbox)
2956 {
2957  GBOX ringbox;
2958  uint32_t i;
2959  int first = LW_TRUE;
2960  assert(poly);
2961  if ( poly->nrings == 0 )
2962  return LW_FAILURE;
2963  ringbox.flags = gbox->flags;
2964  for ( i = 0; i < poly->nrings; i++ )
2965  {
2966  if ( ptarray_calculate_gbox_geodetic(poly->rings[i], &ringbox) == LW_FAILURE )
2967  return LW_FAILURE;
2968  if ( first )
2969  {
2970  gbox_duplicate(&ringbox, gbox);
2971  first = LW_FALSE;
2972  }
2973  else
2974  {
2975  gbox_merge(&ringbox, gbox);
2976  }
2977  }
2978 
2979  /* If the box wraps a poly, push that axis to the absolute min/max as appropriate */
2980  gbox_check_poles(gbox);
2981 
2982  return LW_SUCCESS;
2983 }
2984 
2985 static int lwtriangle_calculate_gbox_geodetic(const LWTRIANGLE *triangle, GBOX *gbox)
2986 {
2987  assert(triangle);
2988  return ptarray_calculate_gbox_geodetic(triangle->points, gbox);
2989 }
2990 
2991 
2993 {
2994  GBOX subbox = {0};
2995  uint32_t i;
2996  int result = LW_FAILURE;
2997  int first = LW_TRUE;
2998  assert(coll);
2999  if ( coll->ngeoms == 0 )
3000  return LW_FAILURE;
3001 
3002  subbox.flags = gbox->flags;
3003 
3004  for ( i = 0; i < coll->ngeoms; i++ )
3005  {
3006  if ( lwgeom_calculate_gbox_geodetic((LWGEOM*)(coll->geoms[i]), &subbox) == LW_SUCCESS )
3007  {
3008  /* Keep a copy of the sub-bounding box for later */
3009  if ( coll->geoms[i]->bbox )
3010  lwfree(coll->geoms[i]->bbox);
3011  coll->geoms[i]->bbox = gbox_copy(&subbox);
3012  if ( first )
3013  {
3014  gbox_duplicate(&subbox, gbox);
3015  first = LW_FALSE;
3016  }
3017  else
3018  {
3019  gbox_merge(&subbox, gbox);
3020  }
3021  result = LW_SUCCESS;
3022  }
3023  }
3024  return result;
3025 }
3026 
3028 {
3029  int result = LW_FAILURE;
3030  LWDEBUGF(4, "got type %d", geom->type);
3031 
3032  /* Add a geodetic flag to the incoming gbox */
3033  gbox->flags = lwflags(FLAGS_GET_Z(geom->flags),FLAGS_GET_M(geom->flags),1);
3034 
3035  switch (geom->type)
3036  {
3037  case POINTTYPE:
3039  break;
3040  case LINETYPE:
3041  result = lwline_calculate_gbox_geodetic((LWLINE *)geom, gbox);
3042  break;
3043  case POLYGONTYPE:
3045  break;
3046  case TRIANGLETYPE:
3048  break;
3049  case MULTIPOINTTYPE:
3050  case MULTILINETYPE:
3051  case MULTIPOLYGONTYPE:
3052  case POLYHEDRALSURFACETYPE:
3053  case TINTYPE:
3054  case COLLECTIONTYPE:
3056  break;
3057  default:
3058  lwerror("lwgeom_calculate_gbox_geodetic: unsupported input geometry type: %d - %s",
3059  geom->type, lwtype_name(geom->type));
3060  break;
3061  }
3062  return result;
3063 }
3064 
3065 
3066 
3067 static int ptarray_check_geodetic(const POINTARRAY *pa)
3068 {
3069  uint32_t t;
3070  POINT2D pt;
3071 
3072  assert(pa);
3073 
3074  for (t=0; t<pa->npoints; t++)
3075  {
3076  getPoint2d_p(pa, t, &pt);
3077  /* printf( "%d (%g, %g)\n", t, pt.x, pt.y); */
3078  if ( pt.x < -180.0 || pt.y < -90.0 || pt.x > 180.0 || pt.y > 90.0 )
3079  return LW_FALSE;
3080  }
3081 
3082  return LW_TRUE;
3083 }
3084 
3085 static int lwpoint_check_geodetic(const LWPOINT *point)
3086 {
3087  assert(point);
3088  return ptarray_check_geodetic(point->point);
3089 }
3090 
3091 static int lwline_check_geodetic(const LWLINE *line)
3092 {
3093  assert(line);
3094  return ptarray_check_geodetic(line->points);
3095 }
3096 
3097 static int lwpoly_check_geodetic(const LWPOLY *poly)
3098 {
3099  uint32_t i = 0;
3100  assert(poly);
3101 
3102  for ( i = 0; i < poly->nrings; i++ )
3103  {
3104  if ( ptarray_check_geodetic(poly->rings[i]) == LW_FALSE )
3105  return LW_FALSE;
3106  }
3107  return LW_TRUE;
3108 }
3109 
3110 static int lwtriangle_check_geodetic(const LWTRIANGLE *triangle)
3111 {
3112  assert(triangle);
3113  return ptarray_check_geodetic(triangle->points);
3114 }
3115 
3116 
3118 {
3119  uint32_t i = 0;
3120  assert(col);
3121 
3122  for ( i = 0; i < col->ngeoms; i++ )
3123  {
3124  if ( lwgeom_check_geodetic(col->geoms[i]) == LW_FALSE )
3125  return LW_FALSE;
3126  }
3127  return LW_TRUE;
3128 }
3129 
3131 {
3132  if ( lwgeom_is_empty(geom) )
3133  return LW_TRUE;
3134 
3135  switch (geom->type)
3136  {
3137  case POINTTYPE:
3138  return lwpoint_check_geodetic((LWPOINT *)geom);
3139  case LINETYPE:
3140  return lwline_check_geodetic((LWLINE *)geom);
3141  case POLYGONTYPE:
3142  return lwpoly_check_geodetic((LWPOLY *)geom);
3143  case TRIANGLETYPE:
3144  return lwtriangle_check_geodetic((LWTRIANGLE *)geom);
3145  case MULTIPOINTTYPE:
3146  case MULTILINETYPE:
3147  case MULTIPOLYGONTYPE:
3148  case POLYHEDRALSURFACETYPE:
3149  case TINTYPE:
3150  case COLLECTIONTYPE:
3151  return lwcollection_check_geodetic((LWCOLLECTION *)geom);
3152  default:
3153  lwerror("lwgeom_check_geodetic: unsupported input geometry type: %d - %s",
3154  geom->type, lwtype_name(geom->type));
3155  }
3156  return LW_FALSE;
3157 }
3158 
3160 {
3161  uint32_t t;
3162  int changed = LW_FALSE;
3163  POINT4D pt;
3164 
3165  assert(pa);
3166 
3167  for ( t=0; t < pa->npoints; t++ )
3168  {
3169  getPoint4d_p(pa, t, &pt);
3170  if ( pt.x < -180.0 || pt.x > 180.0 || pt.y < -90.0 || pt.y > 90.0 )
3171  {
3172  pt.x = longitude_degrees_normalize(pt.x);
3173  pt.y = latitude_degrees_normalize(pt.y);
3174  ptarray_set_point4d(pa, t, &pt);
3175  changed = LW_TRUE;
3176  }
3177  }
3178  return changed;
3179 }
3180 
3182 {
3183  assert(point);
3184  return ptarray_force_geodetic(point->point);
3185 }
3186 
3188 {
3189  assert(line);
3190  return ptarray_force_geodetic(line->points);
3191 }
3192 
3194 {
3195  uint32_t i = 0;
3196  int changed = LW_FALSE;
3197  assert(poly);
3198 
3199  for ( i = 0; i < poly->nrings; i++ )
3200  {
3201  if ( ptarray_force_geodetic(poly->rings[i]) == LW_TRUE )
3202  changed = LW_TRUE;
3203  }
3204  return changed;
3205 }
3206 
3208 {
3209  uint32_t i = 0;
3210  int changed = LW_FALSE;
3211  assert(col);
3212 
3213  for ( i = 0; i < col->ngeoms; i++ )
3214  {
3215  if ( lwgeom_force_geodetic(col->geoms[i]) == LW_TRUE )
3216  changed = LW_TRUE;
3217  }
3218  return changed;
3219 }
3220 
3222 {
3223  switch ( lwgeom_get_type(geom) )
3224  {
3225  case POINTTYPE:
3226  return lwpoint_force_geodetic((LWPOINT *)geom);
3227  case LINETYPE:
3228  return lwline_force_geodetic((LWLINE *)geom);
3229  case POLYGONTYPE:
3230  return lwpoly_force_geodetic((LWPOLY *)geom);
3231  case MULTIPOINTTYPE:
3232  case MULTILINETYPE:
3233  case MULTIPOLYGONTYPE:
3234  case COLLECTIONTYPE:
3235  return lwcollection_force_geodetic((LWCOLLECTION *)geom);
3236  default:
3237  lwerror("unsupported input geometry type: %d", lwgeom_get_type(geom));
3238  }
3239  return LW_FALSE;
3240 }
3241 
3242 
3244 {
3245  GEOGRAPHIC_POINT a, b;
3246  double za = 0.0, zb = 0.0;
3247  POINT4D p;
3248  uint32_t i;
3249  int hasz = LW_FALSE;
3250  double length = 0.0;
3251  double seglength = 0.0;
3252 
3253  /* Return zero on non-sensical inputs */
3254  if ( ! pa || pa->npoints < 2 )
3255  return 0.0;
3256 
3257  /* See if we have a third dimension */
3258  hasz = FLAGS_GET_Z(pa->flags);
3259 
3260  /* Initialize first point */
3261  getPoint4d_p(pa, 0, &p);
3262  geographic_point_init(p.x, p.y, &a);
3263  if ( hasz )
3264  za = p.z;
3265 
3266  /* Loop and sum the length for each segment */
3267  for ( i = 1; i < pa->npoints; i++ )
3268  {
3269  seglength = 0.0;
3270  getPoint4d_p(pa, i, &p);
3271  geographic_point_init(p.x, p.y, &b);
3272  if ( hasz )
3273  zb = p.z;
3274 
3275  /* Special sphere case */
3276  if ( s->a == s->b )
3277  seglength = s->radius * sphere_distance(&a, &b);
3278  /* Spheroid case */
3279  else
3280  seglength = spheroid_distance(&a, &b, s);
3281 
3282  /* Add in the vertical displacement if we're in 3D */
3283  if ( hasz )
3284  seglength = sqrt( (zb-za)*(zb-za) + seglength*seglength );
3285 
3286  /* Add this segment length to the total */
3287  length += seglength;
3288 
3289  /* B gets incremented in the next loop, so we save the value here */
3290  a = b;
3291  za = zb;
3292  }
3293  return length;
3294 }
3295 
3296 double lwgeom_length_spheroid(const LWGEOM *geom, const SPHEROID *s)
3297 {
3298  int type;
3299  uint32_t i = 0;
3300  double length = 0.0;
3301 
3302  assert(geom);
3303 
3304  /* No area in nothing */
3305  if ( lwgeom_is_empty(geom) )
3306  return 0.0;
3307 
3308  type = geom->type;
3309 
3310  if ( type == POINTTYPE || type == MULTIPOINTTYPE )
3311  return 0.0;
3312 
3313  if ( type == LINETYPE )
3314  return ptarray_length_spheroid(((LWLINE*)geom)->points, s);
3315 
3316  if ( type == POLYGONTYPE )
3317  {
3318  LWPOLY *poly = (LWPOLY*)geom;
3319  for ( i = 0; i < poly->nrings; i++ )
3320  {
3321  length += ptarray_length_spheroid(poly->rings[i], s);
3322  }
3323  return length;
3324  }
3325 
3326  if ( type == TRIANGLETYPE )
3327  return ptarray_length_spheroid(((LWTRIANGLE*)geom)->points, s);
3328 
3329  if ( lwtype_is_collection( type ) )
3330  {
3331  LWCOLLECTION *col = (LWCOLLECTION*)geom;
3332 
3333  for ( i = 0; i < col->ngeoms; i++ )
3334  {
3335  length += lwgeom_length_spheroid(col->geoms[i], s);
3336  }
3337  return length;
3338  }
3339 
3340  lwerror("unsupported type passed to lwgeom_length_sphere");
3341  return 0.0;
3342 }
3343 
3350 static int
3352 {
3353 
3354  uint32_t i;
3355  POINT4D p;
3356  int altered = LW_FALSE;
3357  int rv = LW_FALSE;
3358  static double tolerance = 1e-10;
3359 
3360  if ( ! pa )
3361  lwerror("ptarray_nudge_geodetic called with null input");
3362 
3363  for(i = 0; i < pa->npoints; i++ )
3364  {
3365  getPoint4d_p(pa, i, &p);
3366  if ( p.x < -180.0 && (-180.0 - p.x <= tolerance) )
3367  {
3368  p.x = -180.0;
3369  altered = LW_TRUE;
3370  }
3371  if ( p.x > 180.0 && (p.x - 180.0 <= tolerance) )
3372  {
3373  p.x = 180.0;
3374  altered = LW_TRUE;
3375  }
3376  if ( p.y < -90.0 && (-90.0 - p.y <= tolerance) )
3377  {
3378  p.y = -90.0;
3379  altered = LW_TRUE;
3380  }
3381  if ( p.y > 90.0 && (p.y - 90.0 <= tolerance) )
3382  {
3383  p.y = 90.0;
3384  altered = LW_TRUE;
3385  }
3386  if ( altered == LW_TRUE )
3387  {
3388  ptarray_set_point4d(pa, i, &p);
3389  altered = LW_FALSE;
3390  rv = LW_TRUE;
3391  }
3392  }
3393  return rv;
3394 }
3395 
3402 int
3404 {
3405  int type;
3406  uint32_t i = 0;
3407  int rv = LW_FALSE;
3408 
3409  assert(geom);
3410 
3411  /* No points in nothing */
3412  if ( lwgeom_is_empty(geom) )
3413  return LW_FALSE;
3414 
3415  type = geom->type;
3416 
3417  if ( type == POINTTYPE )
3418  return ptarray_nudge_geodetic(((LWPOINT*)geom)->point);
3419 
3420  if ( type == LINETYPE )
3421  return ptarray_nudge_geodetic(((LWLINE*)geom)->points);
3422 
3423  if ( type == POLYGONTYPE )
3424  {
3425  LWPOLY *poly = (LWPOLY*)geom;
3426  for ( i = 0; i < poly->nrings; i++ )
3427  {
3428  int n = ptarray_nudge_geodetic(poly->rings[i]);
3429  rv = (rv == LW_TRUE ? rv : n);
3430  }
3431  return rv;
3432  }
3433 
3434  if ( type == TRIANGLETYPE )
3435  return ptarray_nudge_geodetic(((LWTRIANGLE*)geom)->points);
3436 
3437  if ( lwtype_is_collection( type ) )
3438  {
3439  LWCOLLECTION *col = (LWCOLLECTION*)geom;
3440 
3441  for ( i = 0; i < col->ngeoms; i++ )
3442  {
3443  int n = lwgeom_nudge_geodetic(col->geoms[i]);
3444  rv = (rv == LW_TRUE ? rv : n);
3445  }
3446  return rv;
3447  }
3448 
3449  lwerror("unsupported type (%s) passed to lwgeom_nudge_geodetic", lwtype_name(type));
3450  return rv;
3451 }
3452 
3453 
3457 static int
3458 point_in_cone(const POINT3D *A1, const POINT3D *A2, const POINT3D *P)
3459 {
3460  POINT3D AC; /* Center point of A1/A2 */
3461  double min_similarity, similarity;
3462 
3463  /* Boundary case */
3464  if (point3d_equals(A1, P) || point3d_equals(A2, P))
3465  return LW_TRUE;
3466 
3467  /* The normalized sum bisects the angle between start and end. */
3468  vector_sum(A1, A2, &AC);
3469  normalize(&AC);
3470 
3471  /* The projection of start onto the center defines the minimum similarity */
3472  min_similarity = dot_product(A1, &AC);
3473 
3474  /* If the edge is sufficiently curved, use the dot product test */
3475  if (fabs(1.0 - min_similarity) > 1e-10)
3476  {
3477  /* The projection of candidate p onto the center */
3478  similarity = dot_product(P, &AC);
3479 
3480  /* If the projection of the candidate is larger than */
3481  /* the projection of the start point, the candidate */
3482  /* must be closer to the center than the start, so */
3483  /* therefor inside the cone */
3484  if (similarity > min_similarity)
3485  {
3486  return LW_TRUE;
3487  }
3488  else
3489  {
3490  return LW_FALSE;
3491  }
3492  }
3493  else
3494  {
3495  /* Where the edge is very narrow, the dot product test */
3496  /* fails, but we can use the almost-planar nature of the */
3497  /* problem space then to test if the vector from the */
3498  /* candidate to the start point in a different direction */
3499  /* to the vector from candidate to end point */
3500  /* If so, then candidate is between start and end */
3501  POINT3D PA1, PA2;
3502  vector_difference(P, A1, &PA1);
3503  vector_difference(P, A2, &PA2);
3504  normalize(&PA1);
3505  normalize(&PA2);
3506  if (dot_product(&PA1, &PA2) < 0.0)
3507  {
3508  return LW_TRUE;
3509  }
3510  else
3511  {
3512  return LW_FALSE;
3513  }
3514  }
3515  return LW_FALSE;
3516 }
3517 
3518 
3519 
3524 static int
3525 dot_product_side(const POINT3D *p, const POINT3D *q)
3526 {
3527  double dp = dot_product(p, q);
3528 
3529  if ( FP_IS_ZERO(dp) )
3530  return 0;
3531 
3532  return dp < 0.0 ? -1 : 1;
3533 }
3534 
3539 uint32_t
3540 edge_intersects(const POINT3D *A1, const POINT3D *A2, const POINT3D *B1, const POINT3D *B2)
3541 {
3542  POINT3D AN, BN, VN; /* Normals to plane A and plane B */
3543  double ab_dot;
3544  int a1_side, a2_side, b1_side, b2_side;
3545  int rv = PIR_NO_INTERACT;
3546 
3547  /* Normals to the A-plane and B-plane */
3548  unit_normal(A1, A2, &AN);
3549  unit_normal(B1, B2, &BN);
3550 
3551  /* Are A-plane and B-plane basically the same? */
3552  ab_dot = dot_product(&AN, &BN);
3553 
3554  if ( FP_EQUALS(fabs(ab_dot), 1.0) )
3555  {
3556  /* Co-linear case */
3557  if ( point_in_cone(A1, A2, B1) || point_in_cone(A1, A2, B2) ||
3558  point_in_cone(B1, B2, A1) || point_in_cone(B1, B2, A2) )
3559  {
3560  rv |= PIR_INTERSECTS;
3561  rv |= PIR_COLINEAR;
3562  }
3563  return rv;
3564  }
3565 
3566  /* What side of plane-A and plane-B do the end points */
3567  /* of A and B fall? */
3568  a1_side = dot_product_side(&BN, A1);
3569  a2_side = dot_product_side(&BN, A2);
3570  b1_side = dot_product_side(&AN, B1);
3571  b2_side = dot_product_side(&AN, B2);
3572 
3573  /* Both ends of A on the same side of plane B. */
3574  if ( a1_side == a2_side && a1_side != 0 )
3575  {
3576  /* No intersection. */
3577  return PIR_NO_INTERACT;
3578  }
3579 
3580  /* Both ends of B on the same side of plane A. */
3581  if ( b1_side == b2_side && b1_side != 0 )
3582  {
3583  /* No intersection. */
3584  return PIR_NO_INTERACT;
3585  }
3586 
3587  /* A straddles B and B straddles A, so... */
3588  if ( a1_side != a2_side && (a1_side + a2_side) == 0 &&
3589  b1_side != b2_side && (b1_side + b2_side) == 0 )
3590  {
3591  /* Have to check if intersection point is inside both arcs */
3592  unit_normal(&AN, &BN, &VN);
3593  if ( point_in_cone(A1, A2, &VN) && point_in_cone(B1, B2, &VN) )
3594  {
3595  return PIR_INTERSECTS;
3596  }
3597 
3598  /* Have to check if intersection point is inside both arcs */
3599  vector_scale(&VN, -1);
3600  if ( point_in_cone(A1, A2, &VN) && point_in_cone(B1, B2, &VN) )
3601  {
3602  return PIR_INTERSECTS;
3603  }
3604 
3605  return PIR_NO_INTERACT;
3606  }
3607 
3608  /* The rest are all intersects variants... */
3609  rv |= PIR_INTERSECTS;
3610 
3611  /* A touches B */
3612  if ( a1_side == 0 )
3613  {
3614  /* Touches at A1, A2 is on what side? */
3615  rv |= (a2_side < 0 ? PIR_A_TOUCH_RIGHT : PIR_A_TOUCH_LEFT);
3616  }
3617  else if ( a2_side == 0 )
3618  {
3619  /* Touches at A2, A1 is on what side? */
3620  rv |= (a1_side < 0 ? PIR_A_TOUCH_RIGHT : PIR_A_TOUCH_LEFT);
3621  }
3622 
3623  /* B touches A */
3624  if ( b1_side == 0 )
3625  {
3626  /* Touches at B1, B2 is on what side? */
3627  rv |= (b2_side < 0 ? PIR_B_TOUCH_RIGHT : PIR_B_TOUCH_LEFT);
3628  }
3629  else if ( b2_side == 0 )
3630  {
3631  /* Touches at B2, B1 is on what side? */
3632  rv |= (b1_side < 0 ? PIR_B_TOUCH_RIGHT : PIR_B_TOUCH_LEFT);
3633  }
3634 
3635  return rv;
3636 }
3637 
3646 int ptarray_contains_point_sphere(const POINTARRAY *pa, const POINT2D *pt_outside, const POINT2D *pt_to_test)
3647 {
3648  POINT3D S1, S2; /* Stab line end points */
3649  POINT3D E1, E2; /* Edge end points (3-space) */
3650  POINT2D p; /* Edge end points (lon/lat) */
3651  uint32_t count = 0, i, inter;
3652 
3653  /* Null input, not enough points for a ring? You ain't closed! */
3654  if ( ! pa || pa->npoints < 4 )
3655  return LW_FALSE;
3656 
3657  /* Set up our stab line */
3658  ll2cart(pt_to_test, &S1);
3659  ll2cart(pt_outside, &S2);
3660 
3661  /* Initialize first point */
3662  getPoint2d_p(pa, 0, &p);
3663  ll2cart(&p, &E1);
3664 
3665  /* Walk every edge and see if the stab line hits it */
3666  for ( i = 1; i < pa->npoints; i++ )
3667  {
3668  LWDEBUGF(4, "testing edge (%d)", i);
3669  LWDEBUGF(4, " start point == POINT(%.12g %.12g)", p.x, p.y);
3670 
3671  /* Read next point. */
3672  getPoint2d_p(pa, i, &p);
3673  ll2cart(&p, &E2);
3674 
3675  /* Skip over too-short edges. */
3676  if ( point3d_equals(&E1, &E2) )
3677  {
3678  continue;
3679  }
3680 
3681  /* Our test point is on an edge end! Point is "in ring" by our definition */
3682  if ( point3d_equals(&S1, &E1) )
3683  {
3684  return LW_TRUE;
3685  }
3686 
3687  /* Calculate relationship between stab line and edge */
3688  inter = edge_intersects(&S1, &S2, &E1, &E2);
3689 
3690  /* We have some kind of interaction... */
3691  if ( inter & PIR_INTERSECTS )
3692  {
3693  /* If the stabline is touching the edge, that implies the test point */
3694  /* is on the edge, so we're done, the point is in (on) the ring. */
3695  if ( (inter & PIR_A_TOUCH_RIGHT) || (inter & PIR_A_TOUCH_LEFT) )
3696  {
3697  return LW_TRUE;
3698  }
3699 
3700  /* It's a touching interaction, disregard all the left-side ones. */
3701  /* It's a co-linear intersection, ignore those. */
3702  if ( inter & PIR_B_TOUCH_RIGHT || inter & PIR_COLINEAR )
3703  {
3704  /* Do nothing, to avoid double counts. */
3705  LWDEBUGF(4," edge (%d) crossed, disregarding to avoid double count", i, count);
3706  }
3707  else
3708  {
3709  /* Increment crossingn count. */
3710  count++;
3711  LWDEBUGF(4," edge (%d) crossed, count == %d", i, count);
3712  }
3713  }
3714  else
3715  {
3716  LWDEBUGF(4," edge (%d) did not cross", i);
3717  }
3718 
3719  /* Increment to next edge */
3720  E1 = E2;
3721  }
3722 
3723  LWDEBUGF(4,"final count == %d", count);
3724 
3725  /* An odd number of crossings implies containment! */
3726  if ( count % 2 )
3727  {
3728  return LW_TRUE;
3729  }
3730 
3731  return LW_FALSE;
3732 }
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:262
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:179
void lwgeom_set_geodetic(LWGEOM *geom, int value)
Set the FLAGS geodetic bit on geometry an all sub-geometries and pointlists.
Definition: lwgeom.c:964
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:339
#define LW_FALSE
Definition: liblwgeom.h:94
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:309
#define COLLECTIONTYPE
Definition: liblwgeom.h:108
double lwpoint_get_m(const LWPOINT *point)
Definition: lwpoint.c:107
#define LW_FAILURE
Definition: liblwgeom.h:96
#define MULTILINETYPE
Definition: liblwgeom.h:106
#define LINETYPE
Definition: liblwgeom.h:103
#define LW_SUCCESS
Definition: liblwgeom.h:97
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:329
#define MULTIPOINTTYPE
Definition: liblwgeom.h:105
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:529
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:342
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:934
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition: lwgeom.c:1105
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:102
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:565
#define FLAGS_GET_Z(flags)
Definition: liblwgeom.h:165
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
#define TINTYPE
Definition: liblwgeom.h:116
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:107
void lwfree(void *mem)
Definition: lwutil.c:242
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:344
#define POLYGONTYPE
Definition: liblwgeom.h:104
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:114
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:166
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:125
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:491
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:115
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:233
lwflags_t lwflags(int hasz, int hasm, int geodetic)
Construct a new flags bitmask.
Definition: lwutil.c:471
double lwpoint_get_z(const LWPOINT *point)
Definition: lwpoint.c:89
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:215
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:941
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:369
double lwpoint_get_y(const LWPOINT *point)
Definition: lwpoint.c:76
LWPOINT * lwpoint_make(int32_t srid, int hasz, int hasm, const POINT4D *p)
Definition: lwpoint.c:206
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:62
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:1105
static int lwline_check_geodetic(const LWLINE *line)
Definition: lwgeodetic.c:3091
static int lwcollection_check_geodetic(const LWCOLLECTION *col)
Definition: lwgeodetic.c:3117
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:2535
void normalize(POINT3D *p)
Normalize to a unit vector.
Definition: lwgeodetic.c:615
static int lwpoly_check_geodetic(const LWPOLY *poly)
Definition: lwgeodetic.c:3097
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:2774
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:2732
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:1080
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:2105
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:3193
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:1749
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:2204
static int lwcollection_force_geodetic(LWCOLLECTION *col)
Definition: lwgeodetic.c:3207
static int lwtriangle_check_geodetic(const LWTRIANGLE *triangle)
Definition: lwgeodetic.c:3110
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:1558
double sphere_distance_cartesian(const POINT3D *s, const POINT3D *e)
Given two unit vectors, calculate their distance apart in radians.
Definition: lwgeodetic.c:971
static int ptarray_force_geodetic(POINTARRAY *pa)
Definition: lwgeodetic.c:3159
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:3187
static int lwcollection_calculate_gbox_geodetic(const LWCOLLECTION *coll, GBOX *gbox)
Definition: lwgeodetic.c:2992
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:1813
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:3458
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:2619
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:2713
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:3646
int lwgeom_calculate_gbox_geodetic(const LWGEOM *geom, GBOX *gbox)
Calculate the geodetic bounding box for an LWGEOM.
Definition: lwgeodetic.c:3027
static int ptarray_check_geodetic(const POINTARRAY *pa)
Definition: lwgeodetic.c:3067
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:1690
static int lwpoint_check_geodetic(const LWPOINT *point)
Definition: lwgeodetic.c:3085
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:1841
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:3130
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:2677
int lwgeom_covers_lwgeom_sphere(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
Calculate covers predicate for two lwgeoms on the sphere.
Definition: lwgeodetic.c:2430
double gbox_angular_width(const GBOX *gbox)
Returns the angular width (longitudinal span) of the box in radians.
Definition: lwgeodetic.c:215
LWPOINT * lwgeom_project_spheroid_lwpoint(const LWPOINT *from, const LWPOINT *to, const SPHEROID *spheroid, double distance)
Calculate the location of a point on a spheroid, give a start point, end point and distance.
Definition: lwgeodetic.c:2154
static int lwpoint_force_geodetic(LWPOINT *point)
Definition: lwgeodetic.c:3181
int lwpoly_pt_outside(const LWPOLY *poly, POINT2D *pt_outside)
Definition: lwgeodetic.c:1533
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:1320
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:2949
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:1053
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:1493
int ptarray_calculate_gbox_geodetic(const POINTARRAY *pa, GBOX *gbox)
Calculate geodetic (x/y/z) box and add values to gbox.
Definition: lwgeodetic.c:2888
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:3525
double ptarray_length_spheroid(const POINTARRAY *pa, const SPHEROID *s)
Definition: lwgeodetic.c:3243
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:3221
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:1639
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:3403
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:979
int edge_calculate_gbox_slow(const GEOGRAPHIC_EDGE *e, GBOX *gbox)
Definition: lwgeodetic.c:1350
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:1131
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:2985
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:3540
double lwgeom_length_spheroid(const LWGEOM *geom, const SPHEROID *s)
Calculate the geodetic length of a lwgeom on the unit sphere.
Definition: lwgeodetic.c:3296
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:1414
double edge_distance_to_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *gp, GEOGRAPHIC_POINT *closest)
Definition: lwgeodetic.c:1222
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:2803
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:1038
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:1275
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:3351
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:2170
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:2943
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:2037
static int lwpolygon_calculate_gbox_geodetic(const LWPOLY *poly, GBOX *gbox)
Definition: lwgeodetic.c:2955
#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:357
double zmax
Definition: liblwgeom.h:359
double xmax
Definition: liblwgeom.h:355
double zmin
Definition: liblwgeom.h:358
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354
lwflags_t flags
Definition: liblwgeom.h:353
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:580
LWGEOM ** geoms
Definition: liblwgeom.h:575
uint8_t type
Definition: liblwgeom.h:462
GBOX * bbox
Definition: liblwgeom.h:458
int32_t srid
Definition: liblwgeom.h:460
lwflags_t flags
Definition: liblwgeom.h:461
POINTARRAY * points
Definition: liblwgeom.h:483
POINTARRAY * point
Definition: liblwgeom.h:471
uint8_t type
Definition: liblwgeom.h:474
POINTARRAY ** rings
Definition: liblwgeom.h:519
uint32_t nrings
Definition: liblwgeom.h:524
GBOX * bbox
Definition: liblwgeom.h:518
POINTARRAY * points
Definition: liblwgeom.h:495
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
double z
Definition: liblwgeom.h:402
double x
Definition: liblwgeom.h:402
double y
Definition: liblwgeom.h:402
double m
Definition: liblwgeom.h:414
double x
Definition: liblwgeom.h:414
double z
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414
lwflags_t flags
Definition: liblwgeom.h:431
uint32_t npoints
Definition: liblwgeom.h:427
double radius
Definition: liblwgeom.h:380