/src/postgis/liblwgeom/lwspheroid.c
Line | Count | Source |
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 | | #include "../postgis_config.h" |
27 | | /*#define POSTGIS_DEBUG_LEVEL 4*/ |
28 | | |
29 | | #include "liblwgeom_internal.h" |
30 | | #include "lwgeodetic.h" |
31 | | |
32 | | #include "lwgeom_log.h" |
33 | | |
34 | | /* In proj4.9, GeographicLib is in special header */ |
35 | | #ifdef PROJ_GEODESIC |
36 | | #include <geodesic.h> |
37 | | #endif |
38 | | |
39 | | /** |
40 | | * Initialize spheroid object based on major and minor axis |
41 | | */ |
42 | | void |
43 | | spheroid_init(SPHEROID *s, double a, double b) |
44 | 0 | { |
45 | 0 | s->a = a; |
46 | 0 | s->b = b; |
47 | 0 | s->f = (a - b) / a; |
48 | 0 | s->e_sq = (a * a - b * b) / (a * a); |
49 | 0 | s->radius = (2.0 * a + b) / 3.0; |
50 | 0 | } |
51 | | |
52 | | #ifndef PROJ_GEODESIC |
53 | | static double |
54 | | spheroid_mu2(double alpha, const SPHEROID *s) |
55 | | { |
56 | | double b2 = POW2(s->b); |
57 | | return POW2(cos(alpha)) * (POW2(s->a) - b2) / b2; |
58 | | } |
59 | | |
60 | | static double |
61 | | spheroid_big_a(double u2) |
62 | | { |
63 | | return 1.0 + (u2 / 16384.0) * (4096.0 + u2 * (-768.0 + u2 * (320.0 - 175.0 * u2))); |
64 | | } |
65 | | |
66 | | static double |
67 | | spheroid_big_b(double u2) |
68 | | { |
69 | | return (u2 / 1024.0) * (256.0 + u2 * (-128.0 + u2 * (74.0 - 47.0 * u2))); |
70 | | } |
71 | | #endif /* ! PROJ_GEODESIC */ |
72 | | |
73 | | #ifdef PROJ_GEODESIC |
74 | | |
75 | | /** |
76 | | * Computes the shortest distance along the surface of the spheroid |
77 | | * between two points, using the inverse geodesic problem from |
78 | | * GeographicLib (Karney 2013). |
79 | | * |
80 | | * @param a - location of first point |
81 | | * @param b - location of second point |
82 | | * @param s - spheroid to calculate on |
83 | | * @return spheroidal distance between a and b in spheroid units |
84 | | */ |
85 | | double |
86 | | spheroid_distance(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const SPHEROID *spheroid) |
87 | 0 | { |
88 | 0 | struct geod_geodesic gd; |
89 | | |
90 | | /* Same point => zero distance */ |
91 | 0 | if (geographic_point_equals(a, b)) |
92 | 0 | return 0.0; |
93 | | |
94 | 0 | geod_init(&gd, spheroid->a, spheroid->f); |
95 | 0 | double lat1 = a->lat * 180.0 / M_PI; |
96 | 0 | double lon1 = a->lon * 180.0 / M_PI; |
97 | 0 | double lat2 = b->lat * 180.0 / M_PI; |
98 | 0 | double lon2 = b->lon * 180.0 / M_PI; |
99 | 0 | double s12 = 0.0; /* return distance */ |
100 | 0 | geod_inverse(&gd, lat1, lon1, lat2, lon2, &s12, 0, 0); |
101 | 0 | return s12; |
102 | 0 | } |
103 | | |
104 | | /** |
105 | | * Computes the forward azimuth of the geodesic joining two points on |
106 | | * the spheroid, using the inverse geodesic problem (Karney 2013). |
107 | | * |
108 | | * @param r - location of first point |
109 | | * @param s - location of second point |
110 | | * @return azimuth of line joining r to s (but not reverse) |
111 | | */ |
112 | | double |
113 | | spheroid_direction(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const SPHEROID *spheroid) |
114 | 0 | { |
115 | 0 | struct geod_geodesic gd; |
116 | 0 | geod_init(&gd, spheroid->a, spheroid->f); |
117 | 0 | double lat1 = a->lat * 180.0 / M_PI; |
118 | 0 | double lon1 = a->lon * 180.0 / M_PI; |
119 | 0 | double lat2 = b->lat * 180.0 / M_PI; |
120 | 0 | double lon2 = b->lon * 180.0 / M_PI; |
121 | 0 | double azi1; /* return azimuth */ |
122 | 0 | geod_inverse(&gd, lat1, lon1, lat2, lon2, 0, &azi1, 0); |
123 | 0 | return azi1 * M_PI / 180.0; |
124 | 0 | } |
125 | | |
126 | | /** |
127 | | * Given a location, an azimuth and a distance, computes the location of |
128 | | * the projected point. Using the direct geodesic problem from |
129 | | * GeographicLib (Karney 2013). |
130 | | * |
131 | | * @param r - location of first point |
132 | | * @param distance - distance in meters |
133 | | * @param azimuth - azimuth in radians |
134 | | * @return g - location of projected point |
135 | | */ |
136 | | int |
137 | | spheroid_project(const GEOGRAPHIC_POINT *r, |
138 | | const SPHEROID *spheroid, |
139 | | double distance, |
140 | | double azimuth, |
141 | | GEOGRAPHIC_POINT *g) |
142 | 0 | { |
143 | 0 | struct geod_geodesic gd; |
144 | 0 | geod_init(&gd, spheroid->a, spheroid->f); |
145 | 0 | double lat1 = r->lat * 180.0 / M_PI; |
146 | 0 | double lon1 = r->lon * 180.0 / M_PI; |
147 | 0 | double lat2, lon2; /* return projected position */ |
148 | 0 | geod_direct(&gd, lat1, lon1, azimuth * 180.0 / M_PI, distance, &lat2, &lon2, 0); |
149 | 0 | g->lat = lat2 * M_PI / 180.0; |
150 | 0 | g->lon = lon2 * M_PI / 180.0; |
151 | 0 | return LW_SUCCESS; |
152 | 0 | } |
153 | | |
154 | | static double |
155 | | ptarray_area_spheroid(const POINTARRAY *pa, const SPHEROID *spheroid) |
156 | 0 | { |
157 | | /* Return zero on non-sensical inputs */ |
158 | 0 | if (!pa || pa->npoints < 4) |
159 | 0 | return 0.0; |
160 | | |
161 | 0 | struct geod_geodesic gd; |
162 | 0 | geod_init(&gd, spheroid->a, spheroid->f); |
163 | 0 | struct geod_polygon poly; |
164 | 0 | geod_polygon_init(&poly, 0); |
165 | 0 | uint32_t i; |
166 | 0 | double area; /* returned polygon area */ |
167 | 0 | POINT2D p; /* long/lat units are degrees */ |
168 | | |
169 | | /* Pass points from point array; don't close the linearring */ |
170 | 0 | for (i = 0; i < pa->npoints - 1; i++) |
171 | 0 | { |
172 | 0 | getPoint2d_p(pa, i, &p); |
173 | 0 | geod_polygon_addpoint(&gd, &poly, p.y, p.x); |
174 | 0 | LWDEBUGF(4, "geod_polygon_addpoint %d: %.12g %.12g", i, p.y, p.x); |
175 | 0 | } |
176 | 0 | i = geod_polygon_compute(&gd, &poly, 0, 1, &area, 0); |
177 | 0 | if (i != pa->npoints - 1) |
178 | 0 | { |
179 | 0 | lwerror("ptarray_area_spheroid: different number of points %d vs %d", i, pa->npoints - 1); |
180 | 0 | } |
181 | 0 | LWDEBUGF(4, "geod_polygon_compute area: %.12g", area); |
182 | 0 | return fabs(area); |
183 | 0 | } |
184 | | |
185 | | /* Above use Proj GeographicLib */ |
186 | | #else /* ! PROJ_GEODESIC */ |
187 | | /* Below use pre-version 2.2 geodesic functions */ |
188 | | |
189 | | /** |
190 | | * Computes the shortest distance along the surface of the spheroid |
191 | | * between two points. Based on Vincenty's formula for the geodetic |
192 | | * inverse problem as described in "Geocentric Datum of Australia |
193 | | * Technical Manual", Chapter 4. Tested against: |
194 | | * http://mascot.gdbc.gov.bc.ca/mascot/util1a.html |
195 | | * and |
196 | | * http://www.ga.gov.au/nmd/geodesy/datums/vincenty_inverse.jsp |
197 | | * |
198 | | * @param a - location of first point. |
199 | | * @param b - location of second point. |
200 | | * @param s - spheroid to calculate on |
201 | | * @return spheroidal distance between a and b in spheroid units. |
202 | | */ |
203 | | double |
204 | | spheroid_distance(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const SPHEROID *spheroid) |
205 | | { |
206 | | double lambda = (b->lon - a->lon); |
207 | | double f = spheroid->f; |
208 | | double omf = 1 - spheroid->f; |
209 | | double u1, u2; |
210 | | double cos_u1, cos_u2; |
211 | | double sin_u1, sin_u2; |
212 | | double big_a, big_b, delta_sigma; |
213 | | double alpha, sin_alpha, cos_alphasq, c; |
214 | | double sigma, sin_sigma, cos_sigma, cos2_sigma_m, sqrsin_sigma, last_lambda, omega; |
215 | | double cos_lambda, sin_lambda; |
216 | | double distance; |
217 | | int i = 0; |
218 | | |
219 | | /* Same point => zero distance */ |
220 | | if (geographic_point_equals(a, b)) |
221 | | { |
222 | | return 0.0; |
223 | | } |
224 | | |
225 | | u1 = atan(omf * tan(a->lat)); |
226 | | cos_u1 = cos(u1); |
227 | | sin_u1 = sin(u1); |
228 | | u2 = atan(omf * tan(b->lat)); |
229 | | cos_u2 = cos(u2); |
230 | | sin_u2 = sin(u2); |
231 | | |
232 | | omega = lambda; |
233 | | do |
234 | | { |
235 | | cos_lambda = cos(lambda); |
236 | | sin_lambda = sin(lambda); |
237 | | sqrsin_sigma = POW2(cos_u2 * sin_lambda) + POW2((cos_u1 * sin_u2 - sin_u1 * cos_u2 * cos_lambda)); |
238 | | sin_sigma = sqrt(sqrsin_sigma); |
239 | | cos_sigma = sin_u1 * sin_u2 + cos_u1 * cos_u2 * cos_lambda; |
240 | | sigma = atan2(sin_sigma, cos_sigma); |
241 | | sin_alpha = cos_u1 * cos_u2 * sin_lambda / sin(sigma); |
242 | | |
243 | | /* Numerical stability issue, ensure asin is not NaN */ |
244 | | if (sin_alpha > 1.0) |
245 | | alpha = M_PI_2; |
246 | | else if (sin_alpha < -1.0) |
247 | | alpha = -1.0 * M_PI_2; |
248 | | else |
249 | | alpha = asin(sin_alpha); |
250 | | |
251 | | cos_alphasq = POW2(cos(alpha)); |
252 | | cos2_sigma_m = cos(sigma) - (2.0 * sin_u1 * sin_u2 / cos_alphasq); |
253 | | |
254 | | /* Numerical stability issue, cos2 is in range */ |
255 | | if (cos2_sigma_m > 1.0) |
256 | | cos2_sigma_m = 1.0; |
257 | | if (cos2_sigma_m < -1.0) |
258 | | cos2_sigma_m = -1.0; |
259 | | |
260 | | c = (f / 16.0) * cos_alphasq * (4.0 + f * (4.0 - 3.0 * cos_alphasq)); |
261 | | last_lambda = lambda; |
262 | | lambda = omega + (1.0 - c) * f * sin(alpha) * |
263 | | (sigma + c * sin(sigma) * |
264 | | (cos2_sigma_m + c * cos(sigma) * (-1.0 + 2.0 * POW2(cos2_sigma_m)))); |
265 | | i++; |
266 | | } while ((i < 999) && (lambda != 0.0) && (fabs((last_lambda - lambda) / lambda) > 1.0e-9)); |
267 | | |
268 | | u2 = spheroid_mu2(alpha, spheroid); |
269 | | big_a = spheroid_big_a(u2); |
270 | | big_b = spheroid_big_b(u2); |
271 | | delta_sigma = big_b * sin_sigma * |
272 | | (cos2_sigma_m + (big_b / 4.0) * (cos_sigma * (-1.0 + 2.0 * POW2(cos2_sigma_m)) - |
273 | | (big_b / 6.0) * cos2_sigma_m * (-3.0 + 4.0 * sqrsin_sigma) * |
274 | | (-3.0 + 4.0 * POW2(cos2_sigma_m)))); |
275 | | |
276 | | distance = spheroid->b * big_a * (sigma - delta_sigma); |
277 | | |
278 | | /* Algorithm failure, distance == NaN, fallback to sphere */ |
279 | | if (distance != distance) |
280 | | { |
281 | | lwerror("spheroid_distance returned NaN: (%.20g %.20g) (%.20g %.20g) a = %.20g b = %.20g", |
282 | | a->lat, |
283 | | a->lon, |
284 | | b->lat, |
285 | | b->lon, |
286 | | spheroid->a, |
287 | | spheroid->b); |
288 | | return spheroid->radius * sphere_distance(a, b); |
289 | | } |
290 | | |
291 | | return distance; |
292 | | } |
293 | | |
294 | | /** |
295 | | * Computes the direction of the geodesic joining two points on |
296 | | * the spheroid. Based on Vincenty's formula for the geodetic |
297 | | * inverse problem as described in "Geocentric Datum of Australia |
298 | | * Technical Manual", Chapter 4. Tested against: |
299 | | * http://mascot.gdbc.gov.bc.ca/mascot/util1a.html |
300 | | * and |
301 | | * http://www.ga.gov.au/nmd/geodesy/datums/vincenty_inverse.jsp |
302 | | * |
303 | | * @param r - location of first point |
304 | | * @param s - location of second point |
305 | | * @return azimuth of line joining r and s |
306 | | */ |
307 | | double |
308 | | spheroid_direction(const GEOGRAPHIC_POINT *r, const GEOGRAPHIC_POINT *s, const SPHEROID *spheroid) |
309 | | { |
310 | | int i = 0; |
311 | | double lambda = s->lon - r->lon; |
312 | | double omf = 1 - spheroid->f; |
313 | | double u1 = atan(omf * tan(r->lat)); |
314 | | double cos_u1 = cos(u1); |
315 | | double sin_u1 = sin(u1); |
316 | | double u2 = atan(omf * tan(s->lat)); |
317 | | double cos_u2 = cos(u2); |
318 | | double sin_u2 = sin(u2); |
319 | | |
320 | | double omega = lambda; |
321 | | double alpha, sigma, sin_sigma, cos_sigma, cos2_sigma_m, sqr_sin_sigma, last_lambda; |
322 | | double sin_alpha, cos_alphasq, C, alphaFD; |
323 | | do |
324 | | { |
325 | | sqr_sin_sigma = POW2(cos_u2 * sin(lambda)) + POW2((cos_u1 * sin_u2 - sin_u1 * cos_u2 * cos(lambda))); |
326 | | sin_sigma = sqrt(sqr_sin_sigma); |
327 | | cos_sigma = sin_u1 * sin_u2 + cos_u1 * cos_u2 * cos(lambda); |
328 | | sigma = atan2(sin_sigma, cos_sigma); |
329 | | sin_alpha = cos_u1 * cos_u2 * sin(lambda) / sin(sigma); |
330 | | |
331 | | /* Numerical stability issue, ensure asin is not NaN */ |
332 | | if (sin_alpha > 1.0) |
333 | | alpha = M_PI_2; |
334 | | else if (sin_alpha < -1.0) |
335 | | alpha = -1.0 * M_PI_2; |
336 | | else |
337 | | alpha = asin(sin_alpha); |
338 | | |
339 | | cos_alphasq = POW2(cos(alpha)); |
340 | | cos2_sigma_m = cos(sigma) - (2.0 * sin_u1 * sin_u2 / cos_alphasq); |
341 | | |
342 | | /* Numerical stability issue, cos2 is in range */ |
343 | | if (cos2_sigma_m > 1.0) |
344 | | cos2_sigma_m = 1.0; |
345 | | if (cos2_sigma_m < -1.0) |
346 | | cos2_sigma_m = -1.0; |
347 | | |
348 | | C = (spheroid->f / 16.0) * cos_alphasq * (4.0 + spheroid->f * (4.0 - 3.0 * cos_alphasq)); |
349 | | last_lambda = lambda; |
350 | | lambda = omega + (1.0 - C) * spheroid->f * sin(alpha) * |
351 | | (sigma + C * sin(sigma) * |
352 | | (cos2_sigma_m + C * cos(sigma) * (-1.0 + 2.0 * POW2(cos2_sigma_m)))); |
353 | | i++; |
354 | | } while ((i < 999) && (lambda != 0) && (fabs((last_lambda - lambda) / lambda) > 1.0e-9)); |
355 | | |
356 | | alphaFD = atan2((cos_u2 * sin(lambda)), (cos_u1 * sin_u2 - sin_u1 * cos_u2 * cos(lambda))); |
357 | | if (alphaFD < 0.0) |
358 | | { |
359 | | alphaFD = alphaFD + 2.0 * M_PI; |
360 | | } |
361 | | if (alphaFD > 2.0 * M_PI) |
362 | | { |
363 | | alphaFD = alphaFD - 2.0 * M_PI; |
364 | | } |
365 | | return alphaFD; |
366 | | } |
367 | | |
368 | | /** |
369 | | * Given a location, an azimuth and a distance, computes the |
370 | | * location of the projected point. Based on Vincenty's formula |
371 | | * for the geodetic direct problem as described in "Geocentric |
372 | | * Datum of Australia Technical Manual", Chapter 4. Tested against: |
373 | | * http://mascot.gdbc.gov.bc.ca/mascot/util1b.html |
374 | | * and |
375 | | * http://www.ga.gov.au/nmd/geodesy/datums/vincenty_direct.jsp |
376 | | * |
377 | | * @param r - location of first point. |
378 | | * @param distance - distance in meters. |
379 | | * @param azimuth - azimuth in radians. |
380 | | * @return s - location of projected point. |
381 | | */ |
382 | | int |
383 | | spheroid_project(const GEOGRAPHIC_POINT *r, |
384 | | const SPHEROID *spheroid, |
385 | | double distance, |
386 | | double azimuth, |
387 | | GEOGRAPHIC_POINT *g) |
388 | | { |
389 | | double omf = 1 - spheroid->f; |
390 | | double tan_u1 = omf * tan(r->lat); |
391 | | double u1 = atan(tan_u1); |
392 | | double sigma, last_sigma, delta_sigma, two_sigma_m; |
393 | | double sigma1, sin_alpha, alpha, cos_alphasq; |
394 | | double u2, A, B; |
395 | | double lat2, lambda, lambda2, C, omega; |
396 | | int i = 0; |
397 | | |
398 | | if (azimuth < 0.0) |
399 | | { |
400 | | azimuth = azimuth + M_PI * 2.0; |
401 | | } |
402 | | if (azimuth > (M_PI * 2.0)) |
403 | | { |
404 | | azimuth = azimuth - M_PI * 2.0; |
405 | | } |
406 | | |
407 | | sigma1 = atan2(tan_u1, cos(azimuth)); |
408 | | sin_alpha = cos(u1) * sin(azimuth); |
409 | | alpha = asin(sin_alpha); |
410 | | cos_alphasq = 1.0 - POW2(sin_alpha); |
411 | | |
412 | | u2 = spheroid_mu2(alpha, spheroid); |
413 | | A = spheroid_big_a(u2); |
414 | | B = spheroid_big_b(u2); |
415 | | |
416 | | sigma = (distance / (spheroid->b * A)); |
417 | | do |
418 | | { |
419 | | two_sigma_m = 2.0 * sigma1 + sigma; |
420 | | delta_sigma = |
421 | | B * sin(sigma) * |
422 | | (cos(two_sigma_m) + |
423 | | (B / 4.0) * (cos(sigma) * (-1.0 + 2.0 * POW2(cos(two_sigma_m)) - |
424 | | (B / 6.0) * cos(two_sigma_m) * (-3.0 + 4.0 * POW2(sin(sigma))) * |
425 | | (-3.0 + 4.0 * POW2(cos(two_sigma_m)))))); |
426 | | last_sigma = sigma; |
427 | | sigma = (distance / (spheroid->b * A)) + delta_sigma; |
428 | | i++; |
429 | | } while (i < 999 && fabs((last_sigma - sigma) / sigma) > 1.0e-9); |
430 | | |
431 | | lat2 = atan2((sin(u1) * cos(sigma) + cos(u1) * sin(sigma) * cos(azimuth)), |
432 | | (omf * sqrt(POW2(sin_alpha) + POW2(sin(u1) * sin(sigma) - cos(u1) * cos(sigma) * cos(azimuth))))); |
433 | | lambda = atan2((sin(sigma) * sin(azimuth)), (cos(u1) * cos(sigma) - sin(u1) * sin(sigma) * cos(azimuth))); |
434 | | C = (spheroid->f / 16.0) * cos_alphasq * (4.0 + spheroid->f * (4.0 - 3.0 * cos_alphasq)); |
435 | | omega = lambda - (1.0 - C) * spheroid->f * sin_alpha * |
436 | | (sigma + C * sin(sigma) * |
437 | | (cos(two_sigma_m) + C * cos(sigma) * (-1.0 + 2.0 * POW2(cos(two_sigma_m))))); |
438 | | lambda2 = r->lon + omega; |
439 | | g->lat = lat2; |
440 | | g->lon = lambda2; |
441 | | return LW_SUCCESS; |
442 | | } |
443 | | |
444 | | static inline double |
445 | | spheroid_prime_vertical_radius_of_curvature(double latitude, const SPHEROID *spheroid) |
446 | | { |
447 | | return spheroid->a / (sqrt(1.0 - spheroid->e_sq * POW2(sin(latitude)))); |
448 | | } |
449 | | |
450 | | static inline double |
451 | | spheroid_parallel_arc_length(double latitude, double deltaLongitude, const SPHEROID *spheroid) |
452 | | { |
453 | | return spheroid_prime_vertical_radius_of_curvature(latitude, spheroid) * cos(latitude) * deltaLongitude; |
454 | | } |
455 | | |
456 | | /** |
457 | | * Computes the area on the spheroid of a box bounded by meridians and |
458 | | * parallels. The box is defined by two points, the South West corner |
459 | | * and the North East corner. Formula based on Bagratuni 1967. |
460 | | * |
461 | | * @param southWestCorner - lower left corner of bounding box. |
462 | | * @param northEastCorner - upper right corner of bounding box. |
463 | | * @return area in square meters. |
464 | | */ |
465 | | static double |
466 | | spheroid_boundingbox_area(const GEOGRAPHIC_POINT *southWestCorner, |
467 | | const GEOGRAPHIC_POINT *northEastCorner, |
468 | | const SPHEROID *spheroid) |
469 | | { |
470 | | double z0 = (northEastCorner->lon - southWestCorner->lon) * POW2(spheroid->b) / 2.0; |
471 | | double e = sqrt(spheroid->e_sq); |
472 | | double sinPhi1 = sin(southWestCorner->lat); |
473 | | double sinPhi2 = sin(northEastCorner->lat); |
474 | | double t1p1 = sinPhi1 / (1.0 - spheroid->e_sq * sinPhi1 * sinPhi1); |
475 | | double t1p2 = sinPhi2 / (1.0 - spheroid->e_sq * sinPhi2 * sinPhi2); |
476 | | double oneOver2e = 1.0 / (2.0 * e); |
477 | | double t2p1 = oneOver2e * log((1.0 + e * sinPhi1) / (1.0 - e * sinPhi1)); |
478 | | double t2p2 = oneOver2e * log((1.0 + e * sinPhi2) / (1.0 - e * sinPhi2)); |
479 | | return z0 * (t1p2 + t2p2) - z0 * (t1p1 + t2p1); |
480 | | } |
481 | | |
482 | | /** |
483 | | * This function doesn't work for edges crossing the dateline or in the southern |
484 | | * hemisphere. Points are pre-conditioned in ptarray_area_spheroid. |
485 | | */ |
486 | | static double |
487 | | spheroid_striparea(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, double latitude_min, const SPHEROID *spheroid) |
488 | | { |
489 | | GEOGRAPHIC_POINT A, B, mL, nR; |
490 | | double deltaLng, baseArea, topArea; |
491 | | double bE, tE, ratio, sign; |
492 | | |
493 | | A = *a; |
494 | | B = *b; |
495 | | |
496 | | mL.lat = latitude_min; |
497 | | mL.lon = FP_MIN(A.lon, B.lon); |
498 | | nR.lat = FP_MIN(A.lat, B.lat); |
499 | | nR.lon = FP_MAX(A.lon, B.lon); |
500 | | LWDEBUGF(4, "mL (%.12g %.12g)", mL.lat, mL.lon); |
501 | | LWDEBUGF(4, "nR (%.12g %.12g)", nR.lat, nR.lon); |
502 | | baseArea = spheroid_boundingbox_area(&mL, &nR, spheroid); |
503 | | LWDEBUGF(4, "baseArea %.12g", baseArea); |
504 | | |
505 | | mL.lat = FP_MIN(A.lat, B.lat); |
506 | | mL.lon = FP_MIN(A.lon, B.lon); |
507 | | nR.lat = FP_MAX(A.lat, B.lat); |
508 | | nR.lon = FP_MAX(A.lon, B.lon); |
509 | | LWDEBUGF(4, "mL (%.12g %.12g)", mL.lat, mL.lon); |
510 | | LWDEBUGF(4, "nR (%.12g %.12g)", nR.lat, nR.lon); |
511 | | topArea = spheroid_boundingbox_area(&mL, &nR, spheroid); |
512 | | LWDEBUGF(4, "topArea %.12g", topArea); |
513 | | |
514 | | deltaLng = B.lon - A.lon; |
515 | | LWDEBUGF(4, "deltaLng %.12g", deltaLng); |
516 | | bE = spheroid_parallel_arc_length(A.lat, deltaLng, spheroid); |
517 | | tE = spheroid_parallel_arc_length(B.lat, deltaLng, spheroid); |
518 | | LWDEBUGF(4, "bE %.12g", bE); |
519 | | LWDEBUGF(4, "tE %.12g", tE); |
520 | | |
521 | | ratio = (bE + tE) / tE; |
522 | | sign = SIGNUM(B.lon - A.lon); |
523 | | return (baseArea + topArea / ratio) * sign; |
524 | | } |
525 | | |
526 | | static double |
527 | | ptarray_area_spheroid(const POINTARRAY *pa, const SPHEROID *spheroid) |
528 | | { |
529 | | GEOGRAPHIC_POINT a, b; |
530 | | POINT2D p; |
531 | | uint32_t i; |
532 | | double area = 0.0; |
533 | | GBOX gbox2d; |
534 | | int in_south = LW_FALSE; |
535 | | double delta_lon_tolerance; |
536 | | double latitude_min; |
537 | | |
538 | | gbox2d.flags = lwflags(0, 0, 0); |
539 | | |
540 | | /* Return zero on non-sensical inputs */ |
541 | | if (!pa || pa->npoints < 4) |
542 | | return 0.0; |
543 | | |
544 | | /* Get the raw min/max values for the latitudes */ |
545 | | ptarray_calculate_gbox_cartesian(pa, &gbox2d); |
546 | | |
547 | | if (SIGNUM(gbox2d.ymin) != SIGNUM(gbox2d.ymax)) |
548 | | lwerror("ptarray_area_spheroid: cannot handle ptarray that crosses equator"); |
549 | | |
550 | | /* Geodetic bbox < 0.0 implies geometry is entirely in southern hemisphere */ |
551 | | if (gbox2d.ymax < 0.0) |
552 | | in_south = LW_TRUE; |
553 | | |
554 | | LWDEBUGF(4, "gbox2d.ymax %.12g", gbox2d.ymax); |
555 | | |
556 | | /* Tolerance for strip area calculation */ |
557 | | if (in_south) |
558 | | { |
559 | | delta_lon_tolerance = (90.0 / (fabs(gbox2d.ymin) / 8.0) - 2.0) / 10000.0; |
560 | | latitude_min = deg2rad(fabs(gbox2d.ymax)); |
561 | | } |
562 | | else |
563 | | { |
564 | | delta_lon_tolerance = (90.0 / (fabs(gbox2d.ymax) / 8.0) - 2.0) / 10000.0; |
565 | | latitude_min = deg2rad(gbox2d.ymin); |
566 | | } |
567 | | |
568 | | /* Initialize first point */ |
569 | | getPoint2d_p(pa, 0, &p); |
570 | | geographic_point_init(p.x, p.y, &a); |
571 | | |
572 | | for (i = 1; i < pa->npoints; i++) |
573 | | { |
574 | | GEOGRAPHIC_POINT a1, b1; |
575 | | double strip_area = 0.0; |
576 | | double delta_lon = 0.0; |
577 | | LWDEBUGF(4, "edge #%d", i); |
578 | | |
579 | | getPoint2d_p(pa, i, &p); |
580 | | geographic_point_init(p.x, p.y, &b); |
581 | | |
582 | | a1 = a; |
583 | | b1 = b; |
584 | | |
585 | | /* Flip into north if in south */ |
586 | | if (in_south) |
587 | | { |
588 | | a1.lat = -1.0 * a1.lat; |
589 | | b1.lat = -1.0 * b1.lat; |
590 | | } |
591 | | |
592 | | LWDEBUGF(4, "in_south %d", in_south); |
593 | | |
594 | | LWDEBUGF(4, "crosses_dateline(a, b) %d", crosses_dateline(&a, &b)); |
595 | | |
596 | | if (crosses_dateline(&a, &b)) |
597 | | { |
598 | | double shift; |
599 | | |
600 | | if (a1.lon > 0.0) |
601 | | shift = (M_PI - a1.lon) + 0.088; /* About 5deg more */ |
602 | | else |
603 | | shift = (M_PI - b1.lon) + 0.088; /* About 5deg more */ |
604 | | |
605 | | LWDEBUGF(4, "shift: %.8g", shift); |
606 | | LWDEBUGF(4, "before shift a1(%.8g %.8g) b1(%.8g %.8g)", a1.lat, a1.lon, b1.lat, b1.lon); |
607 | | point_shift(&a1, shift); |
608 | | point_shift(&b1, shift); |
609 | | LWDEBUGF(4, "after shift a1(%.8g %.8g) b1(%.8g %.8g)", a1.lat, a1.lon, b1.lat, b1.lon); |
610 | | } |
611 | | |
612 | | delta_lon = fabs(b1.lon - a1.lon); |
613 | | |
614 | | LWDEBUGF(4, "a1(%.18g %.18g) b1(%.18g %.18g)", a1.lat, a1.lon, b1.lat, b1.lon); |
615 | | LWDEBUGF(4, "delta_lon %.18g", delta_lon); |
616 | | LWDEBUGF(4, "delta_lon_tolerance %.18g", delta_lon_tolerance); |
617 | | |
618 | | if (delta_lon > 0.0) |
619 | | { |
620 | | if (delta_lon < delta_lon_tolerance) |
621 | | { |
622 | | strip_area = spheroid_striparea(&a1, &b1, latitude_min, spheroid); |
623 | | LWDEBUGF(4, "strip_area %.12g", strip_area); |
624 | | area += strip_area; |
625 | | } |
626 | | else |
627 | | { |
628 | | GEOGRAPHIC_POINT p, q; |
629 | | double step = floor(delta_lon / delta_lon_tolerance); |
630 | | double distance = spheroid_distance(&a1, &b1, spheroid); |
631 | | double pDistance = 0.0; |
632 | | int j = 0; |
633 | | LWDEBUGF(4, "step %.18g", step); |
634 | | LWDEBUGF(4, "distance %.18g", distance); |
635 | | step = distance / step; |
636 | | LWDEBUGF(4, "step %.18g", step); |
637 | | p = a1; |
638 | | while (pDistance < (distance - step * 1.01)) |
639 | | { |
640 | | double azimuth = spheroid_direction(&p, &b1, spheroid); |
641 | | j++; |
642 | | LWDEBUGF(4, " iteration %d", j); |
643 | | LWDEBUGF(4, " azimuth %.12g", azimuth); |
644 | | pDistance = pDistance + step; |
645 | | LWDEBUGF(4, " pDistance %.12g", pDistance); |
646 | | spheroid_project(&p, spheroid, step, azimuth, &q); |
647 | | strip_area = spheroid_striparea(&p, &q, latitude_min, spheroid); |
648 | | LWDEBUGF(4, " strip_area %.12g", strip_area); |
649 | | area += strip_area; |
650 | | LWDEBUGF(4, " area %.12g", area); |
651 | | p.lat = q.lat; |
652 | | p.lon = q.lon; |
653 | | } |
654 | | strip_area = spheroid_striparea(&p, &b1, latitude_min, spheroid); |
655 | | area += strip_area; |
656 | | } |
657 | | } |
658 | | |
659 | | /* B gets incremented in the next loop, so we save the value here */ |
660 | | a = b; |
661 | | } |
662 | | return fabs(area); |
663 | | } |
664 | | #endif /* else ! PROJ_GEODESIC */ |
665 | | |
666 | | /** |
667 | | * Calculate the area of an LWGEOM. Anything except POLYGON, MULTIPOLYGON |
668 | | * and GEOMETRYCOLLECTION return zero immediately. Multi's recurse, polygons |
669 | | * calculate external ring area and subtract internal ring area. A GBOX is |
670 | | * required to check relationship to equator an outside point. |
671 | | * WARNING: Does NOT WORK for polygons over equator or pole. |
672 | | */ |
673 | | double |
674 | | lwgeom_area_spheroid(const LWGEOM *lwgeom, const SPHEROID *spheroid) |
675 | 0 | { |
676 | 0 | int type; |
677 | |
|
678 | 0 | assert(lwgeom); |
679 | | |
680 | | /* No area in nothing */ |
681 | 0 | if (lwgeom_is_empty(lwgeom)) |
682 | 0 | return 0.0; |
683 | | |
684 | | /* Read the geometry type number */ |
685 | 0 | type = lwgeom->type; |
686 | | |
687 | | /* Anything but polygons and collections returns zero */ |
688 | 0 | if (!(type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE)) |
689 | 0 | return 0.0; |
690 | | |
691 | | /* Actually calculate area */ |
692 | 0 | if (type == POLYGONTYPE) |
693 | 0 | { |
694 | 0 | LWPOLY *poly = (LWPOLY *)lwgeom; |
695 | 0 | uint32_t i; |
696 | 0 | double area = 0.0; |
697 | | |
698 | | /* Just in case there's no rings */ |
699 | 0 | if (poly->nrings < 1) |
700 | 0 | return 0.0; |
701 | | |
702 | | /* First, the area of the outer ring */ |
703 | 0 | area += ptarray_area_spheroid(poly->rings[0], spheroid); |
704 | |
|
705 | | #if 0 |
706 | | /* |
707 | | * According to https://geographiclib.sourceforge.io/scripts/geod-calc.html |
708 | | * we can expect a 0.1 m2 per vertex accuracty for areas |
709 | | */ |
710 | | double accuracy = poly->rings[0]->npoints * 0.1; |
711 | | if ( area < accuracy ) |
712 | | { |
713 | | lwnotice("Spheroid area of outer ring is below %g accuracy, skipping hole area computation"); |
714 | | return area; |
715 | | } |
716 | | #endif |
717 | | |
718 | | /* Subtract areas of inner rings */ |
719 | 0 | for (i = 1; i < poly->nrings; i++) |
720 | 0 | { |
721 | 0 | area -= ptarray_area_spheroid(poly->rings[i], spheroid); |
722 | 0 | if (area < 0) |
723 | 0 | { |
724 | 0 | lwnotice( |
725 | 0 | "subtracting area of inner ring %i made the total area negative: %.12g, returning 0", |
726 | 0 | i, |
727 | 0 | area); |
728 | 0 | return 0.0; |
729 | 0 | } |
730 | 0 | } |
731 | 0 | return area; |
732 | 0 | } |
733 | | |
734 | | /* Recurse into sub-geometries to get area */ |
735 | 0 | if (type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE) |
736 | 0 | { |
737 | 0 | LWCOLLECTION *col = (LWCOLLECTION *)lwgeom; |
738 | 0 | uint32_t i; |
739 | 0 | double area = 0.0; |
740 | |
|
741 | 0 | for (i = 0; i < col->ngeoms; i++) |
742 | 0 | { |
743 | 0 | area += lwgeom_area_spheroid(col->geoms[i], spheroid); |
744 | 0 | } |
745 | 0 | return area; |
746 | 0 | } |
747 | | |
748 | | /* Shouldn't get here. */ |
749 | 0 | return 0.0; |
750 | 0 | } |