PostGIS  2.4.9dev-r@@SVN_REVISION@@
lwspheroid.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright (C) 2009 Paul Ramsey <pramsey@cleverelephant.ca>
22  * Copyright (C) 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 
31 /* GeographicLib */
32 #if PROJ_GEODESIC
33 #include <geodesic.h>
34 #endif
35 
39 void spheroid_init(SPHEROID *s, double a, double b)
40 {
41  s->a = a;
42  s->b = b;
43  s->f = (a - b) / a;
44  s->e_sq = (a*a - b*b)/(a*a);
45  s->radius = (2.0 * a + b ) / 3.0;
46 }
47 
48 #if ! PROJ_GEODESIC
49 static double spheroid_mu2(double alpha, const SPHEROID *s)
50 {
51  double b2 = POW2(s->b);
52  return POW2(cos(alpha)) * (POW2(s->a) - b2) / b2;
53 }
54 
55 static double spheroid_big_a(double u2)
56 {
57  return 1.0 + (u2 / 16384.0) * (4096.0 + u2 * (-768.0 + u2 * (320.0 - 175.0 * u2)));
58 }
59 
60 static double spheroid_big_b(double u2)
61 {
62  return (u2 / 1024.0) * (256.0 + u2 * (-128.0 + u2 * (74.0 - 47.0 * u2)));
63 }
64 #endif /* ! PROJ_GEODESIC */
65 
66 
67 #if PROJ_GEODESIC
68 
79 double spheroid_distance(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const SPHEROID *spheroid)
80 {
81  struct geod_geodesic gd;
82  geod_init(&gd, spheroid->a, spheroid->f);
83  double lat1 = a->lat * 180.0 / M_PI;
84  double lon1 = a->lon * 180.0 / M_PI;
85  double lat2 = b->lat * 180.0 / M_PI;
86  double lon2 = b->lon * 180.0 / M_PI;
87  double s12; /* return distance */
88  geod_inverse(&gd, lat1, lon1, lat2, lon2, &s12, 0, 0);
89  return s12;
90 }
91 
100 double spheroid_direction(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const SPHEROID *spheroid)
101 {
102  struct geod_geodesic gd;
103  geod_init(&gd, spheroid->a, spheroid->f);
104  double lat1 = a->lat * 180.0 / M_PI;
105  double lon1 = a->lon * 180.0 / M_PI;
106  double lat2 = b->lat * 180.0 / M_PI;
107  double lon2 = b->lon * 180.0 / M_PI;
108  double azi1; /* return azimuth */
109  geod_inverse(&gd, lat1, lon1, lat2, lon2, 0, &azi1, 0);
110  return azi1 * M_PI / 180.0;
111 }
112 
123 int spheroid_project(const GEOGRAPHIC_POINT *r, const SPHEROID *spheroid, double distance, double azimuth, GEOGRAPHIC_POINT *g)
124 {
125  struct geod_geodesic gd;
126  geod_init(&gd, spheroid->a, spheroid->f);
127  double lat1 = r->lat * 180.0 / M_PI;
128  double lon1 = r->lon * 180.0 / M_PI;
129  double lat2, lon2; /* return projected position */
130  geod_direct(&gd, lat1, lon1, azimuth * 180.0 / M_PI, distance, &lat2, &lon2, 0);
131  g->lat = lat2 * M_PI / 180.0;
132  g->lon = lon2 * M_PI / 180.0;
133  return LW_SUCCESS;
134 }
135 
136 
137 static double ptarray_area_spheroid(const POINTARRAY *pa, const SPHEROID *spheroid)
138 {
139  /* Return zero on non-sensical inputs */
140  if ( ! pa || pa->npoints < 4 )
141  return 0.0;
142 
143  struct geod_geodesic gd;
144  geod_init(&gd, spheroid->a, spheroid->f);
145  struct geod_polygon poly;
146  geod_polygon_init(&poly, 0);
147  int i;
148  double area; /* returned polygon area */
149  POINT2D p; /* long/lat units are degrees */
150 
151  /* Pass points from point array; don't close the linearring */
152  for ( i = 0; i < pa->npoints - 1; i++ )
153  {
154  getPoint2d_p(pa, i, &p);
155  geod_polygon_addpoint(&gd, &poly, p.y, p.x);
156  LWDEBUGF(4, "geod_polygon_addpoint %d: %.12g %.12g", i, p.y, p.x);
157  }
158  i = geod_polygon_compute(&gd, &poly, 0, 1, &area, 0);
159  if ( i != pa->npoints - 1 )
160  {
161  lwerror("ptarray_area_spheroid: different number of points %d vs %d",
162  i, pa->npoints - 1);
163  }
164  LWDEBUGF(4, "geod_polygon_compute area: %.12g", area);
165  return fabs(area);
166 }
167 
168 /* Above use GeographicLib */
169 #else /* ! PROJ_GEODESIC */
170 /* Below use pre-version 2.2 geodesic functions */
171 
186 double spheroid_distance(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const SPHEROID *spheroid)
187 {
188  double lambda = (b->lon - a->lon);
189  double f = spheroid->f;
190  double omf = 1 - spheroid->f;
191  double u1, u2;
192  double cos_u1, cos_u2;
193  double sin_u1, sin_u2;
194  double big_a, big_b, delta_sigma;
195  double alpha, sin_alpha, cos_alphasq, c;
196  double sigma, sin_sigma, cos_sigma, cos2_sigma_m, sqrsin_sigma, last_lambda, omega;
197  double cos_lambda, sin_lambda;
198  double distance;
199  int i = 0;
200 
201  /* Same point => zero distance */
202  if ( geographic_point_equals(a, b) )
203  {
204  return 0.0;
205  }
206 
207  u1 = atan(omf * tan(a->lat));
208  cos_u1 = cos(u1);
209  sin_u1 = sin(u1);
210  u2 = atan(omf * tan(b->lat));
211  cos_u2 = cos(u2);
212  sin_u2 = sin(u2);
213 
214  omega = lambda;
215  do
216  {
217  cos_lambda = cos(lambda);
218  sin_lambda = sin(lambda);
219  sqrsin_sigma = POW2(cos_u2 * sin_lambda) +
220  POW2((cos_u1 * sin_u2 - sin_u1 * cos_u2 * cos_lambda));
221  sin_sigma = sqrt(sqrsin_sigma);
222  cos_sigma = sin_u1 * sin_u2 + cos_u1 * cos_u2 * cos_lambda;
223  sigma = atan2(sin_sigma, cos_sigma);
224  sin_alpha = cos_u1 * cos_u2 * sin_lambda / sin(sigma);
225 
226  /* Numerical stability issue, ensure asin is not NaN */
227  if ( sin_alpha > 1.0 )
228  alpha = M_PI_2;
229  else if ( sin_alpha < -1.0 )
230  alpha = -1.0 * M_PI_2;
231  else
232  alpha = asin(sin_alpha);
233 
234  cos_alphasq = POW2(cos(alpha));
235  cos2_sigma_m = cos(sigma) - (2.0 * sin_u1 * sin_u2 / cos_alphasq);
236 
237  /* Numerical stability issue, cos2 is in range */
238  if ( cos2_sigma_m > 1.0 )
239  cos2_sigma_m = 1.0;
240  if ( cos2_sigma_m < -1.0 )
241  cos2_sigma_m = -1.0;
242 
243  c = (f / 16.0) * cos_alphasq * (4.0 + f * (4.0 - 3.0 * cos_alphasq));
244  last_lambda = lambda;
245  lambda = omega + (1.0 - c) * f * sin(alpha) * (sigma + c * sin(sigma) *
246  (cos2_sigma_m + c * cos(sigma) * (-1.0 + 2.0 * POW2(cos2_sigma_m))));
247  i++;
248  }
249  while ( (i < 999) && (lambda != 0.0) && (fabs((last_lambda - lambda)/lambda) > 1.0e-9) );
250 
251  u2 = spheroid_mu2(alpha, spheroid);
252  big_a = spheroid_big_a(u2);
253  big_b = spheroid_big_b(u2);
254  delta_sigma = big_b * sin_sigma * (cos2_sigma_m + (big_b / 4.0) * (cos_sigma * (-1.0 + 2.0 * POW2(cos2_sigma_m)) -
255  (big_b / 6.0) * cos2_sigma_m * (-3.0 + 4.0 * sqrsin_sigma) * (-3.0 + 4.0 * POW2(cos2_sigma_m))));
256 
257  distance = spheroid->b * big_a * (sigma - delta_sigma);
258 
259  /* Algorithm failure, distance == NaN, fallback to sphere */
260  if ( distance != distance )
261  {
262  lwerror("spheroid_distance returned NaN: (%.20g %.20g) (%.20g %.20g) a = %.20g b = %.20g",a->lat, a->lon, b->lat, b->lon, spheroid->a, spheroid->b);
263  return spheroid->radius * sphere_distance(a, b);
264  }
265 
266  return distance;
267 }
268 
282 double spheroid_direction(const GEOGRAPHIC_POINT *r, const GEOGRAPHIC_POINT *s, const SPHEROID *spheroid)
283 {
284  int i = 0;
285  double lambda = s->lon - r->lon;
286  double omf = 1 - spheroid->f;
287  double u1 = atan(omf * tan(r->lat));
288  double cos_u1 = cos(u1);
289  double sin_u1 = sin(u1);
290  double u2 = atan(omf * tan(s->lat));
291  double cos_u2 = cos(u2);
292  double sin_u2 = sin(u2);
293 
294  double omega = lambda;
295  double alpha, sigma, sin_sigma, cos_sigma, cos2_sigma_m, sqr_sin_sigma, last_lambda;
296  double sin_alpha, cos_alphasq, C, alphaFD;
297  do
298  {
299  sqr_sin_sigma = POW2(cos_u2 * sin(lambda)) +
300  POW2((cos_u1 * sin_u2 - sin_u1 * cos_u2 * cos(lambda)));
301  sin_sigma = sqrt(sqr_sin_sigma);
302  cos_sigma = sin_u1 * sin_u2 + cos_u1 * cos_u2 * cos(lambda);
303  sigma = atan2(sin_sigma, cos_sigma);
304  sin_alpha = cos_u1 * cos_u2 * sin(lambda) / sin(sigma);
305 
306  /* Numerical stability issue, ensure asin is not NaN */
307  if ( sin_alpha > 1.0 )
308  alpha = M_PI_2;
309  else if ( sin_alpha < -1.0 )
310  alpha = -1.0 * M_PI_2;
311  else
312  alpha = asin(sin_alpha);
313 
314  cos_alphasq = POW2(cos(alpha));
315  cos2_sigma_m = cos(sigma) - (2.0 * sin_u1 * sin_u2 / cos_alphasq);
316 
317  /* Numerical stability issue, cos2 is in range */
318  if ( cos2_sigma_m > 1.0 )
319  cos2_sigma_m = 1.0;
320  if ( cos2_sigma_m < -1.0 )
321  cos2_sigma_m = -1.0;
322 
323  C = (spheroid->f / 16.0) * cos_alphasq * (4.0 + spheroid->f * (4.0 - 3.0 * cos_alphasq));
324  last_lambda = lambda;
325  lambda = omega + (1.0 - C) * spheroid->f * sin(alpha) * (sigma + C * sin(sigma) *
326  (cos2_sigma_m + C * cos(sigma) * (-1.0 + 2.0 * POW2(cos2_sigma_m))));
327  i++;
328  }
329  while ( (i < 999) && (lambda != 0) && (fabs((last_lambda - lambda) / lambda) > 1.0e-9) );
330 
331  alphaFD = atan2((cos_u2 * sin(lambda)),
332  (cos_u1 * sin_u2 - sin_u1 * cos_u2 * cos(lambda)));
333  if (alphaFD < 0.0)
334  {
335  alphaFD = alphaFD + 2.0 * M_PI;
336  }
337  if (alphaFD > 2.0 * M_PI)
338  {
339  alphaFD = alphaFD - 2.0 * M_PI;
340  }
341  return alphaFD;
342 }
343 
344 
359 int spheroid_project(const GEOGRAPHIC_POINT *r, const SPHEROID *spheroid, double distance, double azimuth, GEOGRAPHIC_POINT *g)
360 {
361  double omf = 1 - spheroid->f;
362  double tan_u1 = omf * tan(r->lat);
363  double u1 = atan(tan_u1);
364  double sigma, last_sigma, delta_sigma, two_sigma_m;
365  double sigma1, sin_alpha, alpha, cos_alphasq;
366  double u2, A, B;
367  double lat2, lambda, lambda2, C, omega;
368  int i = 0;
369 
370  if (azimuth < 0.0)
371  {
372  azimuth = azimuth + M_PI * 2.0;
373  }
374  if (azimuth > (M_PI * 2.0))
375  {
376  azimuth = azimuth - M_PI * 2.0;
377  }
378 
379  sigma1 = atan2(tan_u1, cos(azimuth));
380  sin_alpha = cos(u1) * sin(azimuth);
381  alpha = asin(sin_alpha);
382  cos_alphasq = 1.0 - POW2(sin_alpha);
383 
384  u2 = spheroid_mu2(alpha, spheroid);
385  A = spheroid_big_a(u2);
386  B = spheroid_big_b(u2);
387 
388  sigma = (distance / (spheroid->b * A));
389  do
390  {
391  two_sigma_m = 2.0 * sigma1 + sigma;
392  delta_sigma = B * sin(sigma) * (cos(two_sigma_m) + (B / 4.0) * (cos(sigma) * (-1.0 + 2.0 * POW2(cos(two_sigma_m)) - (B / 6.0) * cos(two_sigma_m) * (-3.0 + 4.0 * POW2(sin(sigma))) * (-3.0 + 4.0 * POW2(cos(two_sigma_m))))));
393  last_sigma = sigma;
394  sigma = (distance / (spheroid->b * A)) + delta_sigma;
395  i++;
396  }
397  while (i < 999 && fabs((last_sigma - sigma) / sigma) > 1.0e-9);
398 
399  lat2 = atan2((sin(u1) * cos(sigma) + cos(u1) * sin(sigma) *
400  cos(azimuth)), (omf * sqrt(POW2(sin_alpha) +
401  POW2(sin(u1) * sin(sigma) - cos(u1) * cos(sigma) *
402  cos(azimuth)))));
403  lambda = atan2((sin(sigma) * sin(azimuth)), (cos(u1) * cos(sigma) -
404  sin(u1) * sin(sigma) * cos(azimuth)));
405  C = (spheroid->f / 16.0) * cos_alphasq * (4.0 + spheroid->f * (4.0 - 3.0 * cos_alphasq));
406  omega = lambda - (1.0 - C) * spheroid->f * sin_alpha * (sigma + C * sin(sigma) *
407  (cos(two_sigma_m) + C * cos(sigma) * (-1.0 + 2.0 * POW2(cos(two_sigma_m)))));
408  lambda2 = r->lon + omega;
409  g->lat = lat2;
410  g->lon = lambda2;
411  return LW_SUCCESS;
412 }
413 
414 
415 static inline double spheroid_prime_vertical_radius_of_curvature(double latitude, const SPHEROID *spheroid)
416 {
417  return spheroid->a / (sqrt(1.0 - spheroid->e_sq * POW2(sin(latitude))));
418 }
419 
420 static inline double spheroid_parallel_arc_length(double latitude, double deltaLongitude, const SPHEROID *spheroid)
421 {
422  return spheroid_prime_vertical_radius_of_curvature(latitude, spheroid)
423  * cos(latitude)
424  * deltaLongitude;
425 }
426 
427 
437 static double spheroid_boundingbox_area(const GEOGRAPHIC_POINT *southWestCorner, const GEOGRAPHIC_POINT *northEastCorner, const SPHEROID *spheroid)
438 {
439  double z0 = (northEastCorner->lon - southWestCorner->lon) * POW2(spheroid->b) / 2.0;
440  double e = sqrt(spheroid->e_sq);
441  double sinPhi1 = sin(southWestCorner->lat);
442  double sinPhi2 = sin(northEastCorner->lat);
443  double t1p1 = sinPhi1 / (1.0 - spheroid->e_sq * sinPhi1 * sinPhi1);
444  double t1p2 = sinPhi2 / (1.0 - spheroid->e_sq * sinPhi2 * sinPhi2);
445  double oneOver2e = 1.0 / (2.0 * e);
446  double t2p1 = oneOver2e * log((1.0 + e * sinPhi1) / (1.0 - e * sinPhi1));
447  double t2p2 = oneOver2e * log((1.0 + e * sinPhi2) / (1.0 - e * sinPhi2));
448  return z0 * (t1p2 + t2p2) - z0 * (t1p1 + t2p1);
449 }
450 
455 static double spheroid_striparea(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, double latitude_min, const SPHEROID *spheroid)
456 {
457  GEOGRAPHIC_POINT A, B, mL, nR;
458  double deltaLng, baseArea, topArea;
459  double bE, tE, ratio, sign;
460 
461  A = *a;
462  B = *b;
463 
464  mL.lat = latitude_min;
465  mL.lon = FP_MIN(A.lon, B.lon);
466  nR.lat = FP_MIN(A.lat, B.lat);
467  nR.lon = FP_MAX(A.lon, B.lon);
468  LWDEBUGF(4, "mL (%.12g %.12g)", mL.lat, mL.lon);
469  LWDEBUGF(4, "nR (%.12g %.12g)", nR.lat, nR.lon);
470  baseArea = spheroid_boundingbox_area(&mL, &nR, spheroid);
471  LWDEBUGF(4, "baseArea %.12g", baseArea);
472 
473  mL.lat = FP_MIN(A.lat, B.lat);
474  mL.lon = FP_MIN(A.lon, B.lon);
475  nR.lat = FP_MAX(A.lat, B.lat);
476  nR.lon = FP_MAX(A.lon, B.lon);
477  LWDEBUGF(4, "mL (%.12g %.12g)", mL.lat, mL.lon);
478  LWDEBUGF(4, "nR (%.12g %.12g)", nR.lat, nR.lon);
479  topArea = spheroid_boundingbox_area(&mL, &nR, spheroid);
480  LWDEBUGF(4, "topArea %.12g", topArea);
481 
482  deltaLng = B.lon - A.lon;
483  LWDEBUGF(4, "deltaLng %.12g", deltaLng);
484  bE = spheroid_parallel_arc_length(A.lat, deltaLng, spheroid);
485  tE = spheroid_parallel_arc_length(B.lat, deltaLng, spheroid);
486  LWDEBUGF(4, "bE %.12g", bE);
487  LWDEBUGF(4, "tE %.12g", tE);
488 
489  ratio = (bE + tE)/tE;
490  sign = SIGNUM(B.lon - A.lon);
491  return (baseArea + topArea / ratio) * sign;
492 }
493 
494 static double ptarray_area_spheroid(const POINTARRAY *pa, const SPHEROID *spheroid)
495 {
496  GEOGRAPHIC_POINT a, b;
497  POINT2D p;
498  int i;
499  double area = 0.0;
500  GBOX gbox2d;
501  int in_south = LW_FALSE;
502  double delta_lon_tolerance;
503  double latitude_min;
504 
505  gbox2d.flags = gflags(0, 0, 0);
506 
507  /* Return zero on non-sensical inputs */
508  if ( ! pa || pa->npoints < 4 )
509  return 0.0;
510 
511  /* Get the raw min/max values for the latitudes */
513 
514  if ( SIGNUM(gbox2d.ymin) != SIGNUM(gbox2d.ymax) )
515  lwerror("ptarray_area_spheroid: cannot handle ptarray that crosses equator");
516 
517  /* Geodetic bbox < 0.0 implies geometry is entirely in southern hemisphere */
518  if ( gbox2d.ymax < 0.0 )
519  in_south = LW_TRUE;
520 
521  LWDEBUGF(4, "gbox2d.ymax %.12g", gbox2d.ymax);
522 
523  /* Tolerance for strip area calculation */
524  if ( in_south )
525  {
526  delta_lon_tolerance = (90.0 / (fabs(gbox2d.ymin) / 8.0) - 2.0) / 10000.0;
527  latitude_min = deg2rad(fabs(gbox2d.ymax));
528  }
529  else
530  {
531  delta_lon_tolerance = (90.0 / (fabs(gbox2d.ymax) / 8.0) - 2.0) / 10000.0;
532  latitude_min = deg2rad(gbox2d.ymin);
533  }
534 
535  /* Initialize first point */
536  getPoint2d_p(pa, 0, &p);
537  geographic_point_init(p.x, p.y, &a);
538 
539  for ( i = 1; i < pa->npoints; i++ )
540  {
541  GEOGRAPHIC_POINT a1, b1;
542  double strip_area = 0.0;
543  double delta_lon = 0.0;
544  LWDEBUGF(4, "edge #%d", i);
545 
546  getPoint2d_p(pa, i, &p);
547  geographic_point_init(p.x, p.y, &b);
548 
549  a1 = a;
550  b1 = b;
551 
552  /* Flip into north if in south */
553  if ( in_south )
554  {
555  a1.lat = -1.0 * a1.lat;
556  b1.lat = -1.0 * b1.lat;
557  }
558 
559  LWDEBUGF(4, "in_south %d", in_south);
560 
561  LWDEBUGF(4, "crosses_dateline(a, b) %d", crosses_dateline(&a, &b) );
562 
563  if ( crosses_dateline(&a, &b) )
564  {
565  double shift;
566 
567  if ( a1.lon > 0.0 )
568  shift = (M_PI - a1.lon) + 0.088; /* About 5deg more */
569  else
570  shift = (M_PI - b1.lon) + 0.088; /* About 5deg more */
571 
572  LWDEBUGF(4, "shift: %.8g", shift);
573  LWDEBUGF(4, "before shift a1(%.8g %.8g) b1(%.8g %.8g)", a1.lat, a1.lon, b1.lat, b1.lon);
574  point_shift(&a1, shift);
575  point_shift(&b1, shift);
576  LWDEBUGF(4, "after shift a1(%.8g %.8g) b1(%.8g %.8g)", a1.lat, a1.lon, b1.lat, b1.lon);
577 
578  }
579 
580 
581  delta_lon = fabs(b1.lon - a1.lon);
582 
583  LWDEBUGF(4, "a1(%.18g %.18g) b1(%.18g %.18g)", a1.lat, a1.lon, b1.lat, b1.lon);
584  LWDEBUGF(4, "delta_lon %.18g", delta_lon);
585  LWDEBUGF(4, "delta_lon_tolerance %.18g", delta_lon_tolerance);
586 
587  if ( delta_lon > 0.0 )
588  {
589  if ( delta_lon < delta_lon_tolerance )
590  {
591  strip_area = spheroid_striparea(&a1, &b1, latitude_min, spheroid);
592  LWDEBUGF(4, "strip_area %.12g", strip_area);
593  area += strip_area;
594  }
595  else
596  {
597  GEOGRAPHIC_POINT p, q;
598  double step = floor(delta_lon / delta_lon_tolerance);
599  double distance = spheroid_distance(&a1, &b1, spheroid);
600  double pDistance = 0.0;
601  int j = 0;
602  LWDEBUGF(4, "step %.18g", step);
603  LWDEBUGF(4, "distance %.18g", distance);
604  step = distance / step;
605  LWDEBUGF(4, "step %.18g", step);
606  p = a1;
607  while (pDistance < (distance - step * 1.01))
608  {
609  double azimuth = spheroid_direction(&p, &b1, spheroid);
610  j++;
611  LWDEBUGF(4, " iteration %d", j);
612  LWDEBUGF(4, " azimuth %.12g", azimuth);
613  pDistance = pDistance + step;
614  LWDEBUGF(4, " pDistance %.12g", pDistance);
615  spheroid_project(&p, spheroid, step, azimuth, &q);
616  strip_area = spheroid_striparea(&p, &q, latitude_min, spheroid);
617  LWDEBUGF(4, " strip_area %.12g", strip_area);
618  area += strip_area;
619  LWDEBUGF(4, " area %.12g", area);
620  p.lat = q.lat;
621  p.lon = q.lon;
622  }
623  strip_area = spheroid_striparea(&p, &b1, latitude_min, spheroid);
624  area += strip_area;
625  }
626  }
627 
628  /* B gets incremented in the next loop, so we save the value here */
629  a = b;
630  }
631  return fabs(area);
632 }
633 #endif /* else ! PROJ_GEODESIC */
634 
642 double lwgeom_area_spheroid(const LWGEOM *lwgeom, const SPHEROID *spheroid)
643 {
644  int type;
645 
646  assert(lwgeom);
647 
648  /* No area in nothing */
649  if ( lwgeom_is_empty(lwgeom) )
650  return 0.0;
651 
652  /* Read the geometry type number */
653  type = lwgeom->type;
654 
655  /* Anything but polygons and collections returns zero */
656  if ( ! ( type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE ) )
657  return 0.0;
658 
659  /* Actually calculate area */
660  if ( type == POLYGONTYPE )
661  {
662  LWPOLY *poly = (LWPOLY*)lwgeom;
663  int i;
664  double area = 0.0;
665 
666  /* Just in case there's no rings */
667  if ( poly->nrings < 1 )
668  return 0.0;
669 
670  /* First, the area of the outer ring */
671  area += ptarray_area_spheroid(poly->rings[0], spheroid);
672 
673  /* Subtract areas of inner rings */
674  for ( i = 1; i < poly->nrings; i++ )
675  {
676  area -= ptarray_area_spheroid(poly->rings[i], spheroid);
677  }
678  return area;
679  }
680 
681  /* Recurse into sub-geometries to get area */
682  if ( type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE )
683  {
684  LWCOLLECTION *col = (LWCOLLECTION*)lwgeom;
685  int i;
686  double area = 0.0;
687 
688  for ( i = 0; i < col->ngeoms; i++ )
689  {
690  area += lwgeom_area_spheroid(col->geoms[i], spheroid);
691  }
692  return area;
693  }
694 
695  /* Shouldn't get here. */
696  return 0.0;
697 }
698 
699 
700 
int ptarray_calculate_gbox_cartesian(const POINTARRAY *pa, GBOX *gbox)
Calculate box (x/y) and add values to gbox.
Definition: g_box.c:547
static double spheroid_big_b(double u2)
Definition: lwspheroid.c:60
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:186
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:944
char * r
Definition: cu_in_wkt.c:24
int npoints
Definition: liblwgeom.h:371
static double spheroid_big_a(double u2)
Definition: lwspheroid.c:55
int crosses_dateline(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e)
Definition: lwgeodetic.c:662
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:359
#define POLYGONTYPE
Definition: liblwgeom.h:87
Datum area(PG_FUNCTION_ARGS)
static double spheroid_parallel_arc_length(double latitude, double deltaLongitude, const SPHEROID *spheroid)
Definition: lwspheroid.c:420
double b
Definition: liblwgeom.h:314
static double spheroid_striparea(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, double latitude_min, const SPHEROID *spheroid)
This function doesn&#39;t work for edges crossing the dateline or in the southern hemisphere.
Definition: lwspheroid.c:455
#define LW_SUCCESS
Definition: liblwgeom.h:80
double radius
Definition: liblwgeom.h:318
static double spheroid_mu2(double alpha, const SPHEROID *s)
Definition: lwspheroid.c:49
void point_shift(GEOGRAPHIC_POINT *p, double shift)
Shift a point around by a number of radians.
Definition: lwgeodetic.c:160
#define FP_MIN(A, B)
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:52
double x
Definition: liblwgeom.h:328
void spheroid_init(SPHEROID *s, double a, double b)
Initialize spheroid object based on major and minor axis.
Definition: lwspheroid.c:39
double ymin
Definition: liblwgeom.h:294
double f
Definition: liblwgeom.h:315
#define LW_FALSE
Definition: liblwgeom.h:77
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
LWGEOM ** geoms
Definition: liblwgeom.h:509
POINTARRAY ** rings
Definition: liblwgeom.h:457
double lwgeom_area_spheroid(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the area of an LWGEOM.
Definition: lwspheroid.c:642
int nrings
Definition: liblwgeom.h:455
double ymax
Definition: liblwgeom.h:295
double y
Definition: liblwgeom.h:328
char * s
Definition: cu_in_wkt.c:23
int getPoint2d_p(const POINTARRAY *pa, int n, POINT2D *point)
Definition: lwgeom_api.c:347
static double ptarray_area_spheroid(const POINTARRAY *pa, const SPHEROID *spheroid)
Definition: lwspheroid.c:494
double spheroid_direction(const GEOGRAPHIC_POINT *r, const GEOGRAPHIC_POINT *s, const SPHEROID *spheroid)
Computes the direction of the geodesic joining two points on the spheroid.
Definition: lwspheroid.c:282
double e_sq
Definition: liblwgeom.h:317
#define POW2(x)
Definition: lwgeodetic.h:47
Datum distance(PG_FUNCTION_ARGS)
uint8_t flags
Definition: liblwgeom.h:291
uint8_t gflags(int hasz, int hasm, int geodetic)
Construct a new flags char.
Definition: g_util.c:145
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
static double spheroid_boundingbox_area(const GEOGRAPHIC_POINT *southWestCorner, const GEOGRAPHIC_POINT *northEastCorner, const SPHEROID *spheroid)
Computes the area on the spheroid of a box bounded by meridians and parallels.
Definition: lwspheroid.c:437
int geographic_point_equals(const GEOGRAPHIC_POINT *g1, const GEOGRAPHIC_POINT *g2)
Definition: lwgeodetic.c:170
double a
Definition: liblwgeom.h:313
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition: lwgeodetic.c:180
double deltaLongitude(double azimuth, double sigma, double tsm, SPHEROID *sphere)
#define deg2rad(d)
Conversion functions.
Definition: lwgeodetic.h:79
uint8_t type
Definition: liblwgeom.h:396
type
Definition: ovdump.py:41
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1346
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
static double spheroid_prime_vertical_radius_of_curvature(double latitude, const SPHEROID *spheroid)
Definition: lwspheroid.c:415
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
#define SIGNUM(n)
Macro that returns: -1 if n < 0, 1 if n > 0, 0 if n == 0.
#define FP_MAX(A, B)