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