1 // Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License, version 2.0,
5 // as published by the Free Software Foundation.
6 //
7 // This program is also distributed with certain software (including
8 // but not limited to OpenSSL) that is licensed under separate terms,
9 // as designated in a particular file or component or in included license
10 // documentation. The authors of MySQL hereby grant you an additional
11 // permission to link the program and your derivative works with the
12 // separately licensed software that they have included with MySQL.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License, version 2.0, for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 /// @file
24 ///
25 /// This file implements the overlaps functor and function.
26
27 #include <memory> // std::unique_ptr
28
29 #include <boost/geometry.hpp>
30
31 #include "sql/dd/types/spatial_reference_system.h" // dd::Spatial_reference_system
32 #include "sql/gis/box.h"
33 #include "sql/gis/box_traits.h"
34 #include "sql/gis/gc_utils.h"
35 #include "sql/gis/geometries.h"
36 #include "sql/gis/geometries_traits.h"
37 #include "sql/gis/mbr_utils.h"
38 #include "sql/gis/overlaps_functor.h"
39 #include "sql/gis/relops.h"
40 #include "sql/sql_exception_handler.h" // handle_gis_exception
41
42 namespace bg = boost::geometry;
43
44 namespace gis {
45
46 /// Apply an Overlaps functor to two geometries, which both may be geometry
47 /// collections, and return the booelan result of the functor applied on each
48 /// combination of elements in the collections.
49 ///
50 /// @tparam GC Coordinate specific gometry collection type.
51 ///
52 /// @param f Functor to apply.
53 /// @param g1 First geometry.
54 /// @param g2 Second geometry.
55 ///
56 /// @retval true g1 overlaps g2.
57 /// @retval false g1 doesn't overlap g2.
58 template <typename GC>
geometry_collection_apply_overlaps(const Overlaps & f,const Geometry * g1,const Geometry * g2)59 static bool geometry_collection_apply_overlaps(const Overlaps &f,
60 const Geometry *g1,
61 const Geometry *g2) {
62 if (g1->type() == Geometry_type::kGeometrycollection &&
63 g2->type() == Geometry_type::kGeometrycollection) {
64 std::unique_ptr<Multipoint> g1_mpt;
65 std::unique_ptr<Multilinestring> g1_mls;
66 std::unique_ptr<Multipolygon> g1_mpy;
67 std::unique_ptr<Multipoint> g2_mpt;
68 std::unique_ptr<Multilinestring> g2_mls;
69 std::unique_ptr<Multipolygon> g2_mpy;
70 split_gc(down_cast<const Geometrycollection *>(g1), &g1_mpt, &g1_mls,
71 &g1_mpy);
72 gc_union(f.semi_major(), f.semi_minor(), &g1_mpt, &g1_mls, &g1_mpy);
73 split_gc(down_cast<const Geometrycollection *>(g2), &g2_mpt, &g2_mls,
74 &g2_mpy);
75 gc_union(f.semi_major(), f.semi_minor(), &g2_mpt, &g2_mls, &g2_mpy);
76
77 int g1_dim;
78 if (!g1_mpy->empty())
79 g1_dim = 2;
80 else if (!g1_mls->empty())
81 g1_dim = 1;
82 else if (!g1_mpt->empty())
83 g1_dim = 0;
84 else {
85 DBUG_ASSERT(false); /* purecov: inspected */
86 g1_dim = -1; /* purecov: inspected */
87 }
88
89 int g2_dim;
90 if (!g2_mpy->empty())
91 g2_dim = 2;
92 else if (!g2_mls->empty())
93 g2_dim = 1;
94 else if (!g2_mpt->empty())
95 g2_dim = 0;
96 else {
97 DBUG_ASSERT(false); /* purecov: inspected */
98 g2_dim = -1; /* purecov: inspected */
99 }
100
101 if (g1_dim != g2_dim) throw null_value_exception();
102
103 switch (g1_dim) {
104 case 0:
105 return f(g1_mpt.get(), g2_mpt.get());
106 break;
107 case 1:
108 return f(g1_mpt.get(), g2_mpt.get()) || f(g1_mls.get(), g2_mls.get());
109 break;
110 case 2:
111 return f(g1_mpt.get(), g2_mpt.get()) || f(g1_mls.get(), g2_mls.get()) ||
112 f(g1_mpy.get(), g2_mpy.get());
113 break;
114 default:
115 DBUG_ASSERT(false); /* purecov: inspected */
116 throw null_value_exception(); /* purecov: inspected */
117 break;
118 }
119 } else if (g1->type() == Geometry_type::kGeometrycollection) {
120 return f(g2, g1);
121 } else if (g2->type() == Geometry_type::kGeometrycollection) {
122 std::unique_ptr<Multipoint> g2_mpt;
123 std::unique_ptr<Multilinestring> g2_mls;
124 std::unique_ptr<Multipolygon> g2_mpy;
125 split_gc(down_cast<const Geometrycollection *>(g2), &g2_mpt, &g2_mls,
126 &g2_mpy);
127 gc_union(f.semi_major(), f.semi_minor(), &g2_mpt, &g2_mls, &g2_mpy);
128
129 int g2_dim;
130 if (!g2_mpy->empty())
131 g2_dim = 2;
132 else if (!g2_mls->empty())
133 g2_dim = 1;
134 else if (!g2_mpt->empty())
135 g2_dim = 0;
136 else {
137 DBUG_ASSERT(false); /* purecov: inspected */
138 g2_dim = -1; /* purecov: inspected */
139 }
140
141 switch (g1->type()) {
142 case Geometry_type::kPoint:
143 case Geometry_type::kMultipoint:
144 if (g2_dim != 0) throw null_value_exception();
145 return f(g1, g2_mpt.get());
146 case Geometry_type::kLinestring:
147 case Geometry_type::kMultilinestring:
148 if (g2_dim != 1) throw null_value_exception();
149 return f(g1, g2_mls.get());
150 case Geometry_type::kPolygon:
151 case Geometry_type::kMultipolygon:
152 if (g2_dim != 2) throw null_value_exception();
153 return f(g1, g2_mpy.get());
154 default:
155 // All possible combinations should be covered above.
156 DBUG_ASSERT(false); /* purecov: inspected */
157 return false;
158 }
159 } else {
160 DBUG_ASSERT(false); /* purecov: inspected */
161 return f(g1, g2);
162 }
163 }
164
Overlaps(double semi_major,double semi_minor)165 Overlaps::Overlaps(double semi_major, double semi_minor)
166 : m_semi_major(semi_major),
167 m_semi_minor(semi_minor),
168 m_geographic_ll_aa_strategy(
169 bg::srs::spheroid<double>(semi_major, semi_minor)) {}
170
operator ()(const Geometry * g1,const Geometry * g2) const171 bool Overlaps::operator()(const Geometry *g1, const Geometry *g2) const {
172 return apply(*this, g1, g2);
173 }
174
operator ()(const Box * b1,const Box * b2) const175 bool Overlaps::operator()(const Box *b1, const Box *b2) const {
176 DBUG_ASSERT(b1->coordinate_system() == b2->coordinate_system());
177 switch (b1->coordinate_system()) {
178 case Coordinate_system::kCartesian:
179 return eval(down_cast<const Cartesian_box *>(b1),
180 down_cast<const Cartesian_box *>(b2));
181 case Coordinate_system::kGeographic:
182 return eval(down_cast<const Geographic_box *>(b1),
183 down_cast<const Geographic_box *>(b2));
184 }
185
186 DBUG_ASSERT(false);
187 return false;
188 }
189
eval(const Geometry * g1,const Geometry * g2) const190 bool Overlaps::eval(const Geometry *g1, const Geometry *g2) const {
191 // All parameter type combinations have been implemented.
192 DBUG_ASSERT(false);
193 throw not_implemented_exception::for_non_projected(*g1, *g2);
194 }
195
196 //////////////////////////////////////////////////////////////////////////////
197
198 // overlaps(Cartesian_point, *)
199
eval(const Cartesian_point *,const Cartesian_point *) const200 bool Overlaps::eval(const Cartesian_point *, const Cartesian_point *) const {
201 // The interior of a point can never be within both the interior and exterior
202 // of another geometry.
203 return false;
204 }
205
eval(const Cartesian_point *,const Cartesian_linestring *) const206 bool Overlaps::eval(const Cartesian_point *,
207 const Cartesian_linestring *) const {
208 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
209 throw null_value_exception();
210 }
211
eval(const Cartesian_point *,const Cartesian_polygon *) const212 bool Overlaps::eval(const Cartesian_point *, const Cartesian_polygon *) const {
213 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
214 throw null_value_exception();
215 }
216
eval(const Cartesian_point * g1,const Cartesian_geometrycollection * g2) const217 bool Overlaps::eval(const Cartesian_point *g1,
218 const Cartesian_geometrycollection *g2) const {
219 return geometry_collection_apply_overlaps<Cartesian_geometrycollection>(
220 *this, g1, g2);
221 }
222
eval(const Cartesian_point *,const Cartesian_multipoint *) const223 bool Overlaps::eval(const Cartesian_point *,
224 const Cartesian_multipoint *) const {
225 // The interior of a point can never be within both the interior and exterior
226 // of another geometry.
227 return false;
228 }
229
eval(const Cartesian_point *,const Cartesian_multilinestring *) const230 bool Overlaps::eval(const Cartesian_point *,
231 const Cartesian_multilinestring *) const {
232 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
233 throw null_value_exception();
234 }
235
eval(const Cartesian_point *,const Cartesian_multipolygon *) const236 bool Overlaps::eval(const Cartesian_point *,
237 const Cartesian_multipolygon *) const {
238 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
239 throw null_value_exception();
240 }
241
242 //////////////////////////////////////////////////////////////////////////////
243
244 // overlaps(Cartesian_linestring, *)
245
eval(const Cartesian_linestring *,const Cartesian_point *) const246 bool Overlaps::eval(const Cartesian_linestring *,
247 const Cartesian_point *) const {
248 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
249 throw null_value_exception();
250 }
251
eval(const Cartesian_linestring * g1,const Cartesian_linestring * g2) const252 bool Overlaps::eval(const Cartesian_linestring *g1,
253 const Cartesian_linestring *g2) const {
254 return bg::overlaps(*g1, *g2);
255 }
256
eval(const Cartesian_linestring *,const Cartesian_polygon *) const257 bool Overlaps::eval(const Cartesian_linestring *,
258 const Cartesian_polygon *) const {
259 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
260 throw null_value_exception();
261 }
262
eval(const Cartesian_linestring * g1,const Cartesian_geometrycollection * g2) const263 bool Overlaps::eval(const Cartesian_linestring *g1,
264 const Cartesian_geometrycollection *g2) const {
265 return geometry_collection_apply_overlaps<Cartesian_geometrycollection>(
266 *this, g1, g2);
267 }
268
eval(const Cartesian_linestring *,const Cartesian_multipoint *) const269 bool Overlaps::eval(const Cartesian_linestring *,
270 const Cartesian_multipoint *) const {
271 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
272 throw null_value_exception();
273 }
274
eval(const Cartesian_linestring * g1,const Cartesian_multilinestring * g2) const275 bool Overlaps::eval(const Cartesian_linestring *g1,
276 const Cartesian_multilinestring *g2) const {
277 return bg::overlaps(*g1, *g2);
278 }
279
eval(const Cartesian_linestring *,const Cartesian_multipolygon *) const280 bool Overlaps::eval(const Cartesian_linestring *,
281 const Cartesian_multipolygon *) const {
282 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
283 throw null_value_exception();
284 }
285
286 //////////////////////////////////////////////////////////////////////////////
287
288 // overlaps(Cartesian_polygon, *)
289
eval(const Cartesian_polygon *,const Cartesian_point *) const290 bool Overlaps::eval(const Cartesian_polygon *, const Cartesian_point *) const {
291 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
292 throw null_value_exception();
293 }
294
eval(const Cartesian_polygon *,const Cartesian_linestring *) const295 bool Overlaps::eval(const Cartesian_polygon *,
296 const Cartesian_linestring *) const {
297 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
298 throw null_value_exception();
299 }
300
eval(const Cartesian_polygon * g1,const Cartesian_polygon * g2) const301 bool Overlaps::eval(const Cartesian_polygon *g1,
302 const Cartesian_polygon *g2) const {
303 return bg::overlaps(*g1, *g2);
304 }
305
eval(const Cartesian_polygon * g1,const Cartesian_geometrycollection * g2) const306 bool Overlaps::eval(const Cartesian_polygon *g1,
307 const Cartesian_geometrycollection *g2) const {
308 return geometry_collection_apply_overlaps<Cartesian_geometrycollection>(
309 *this, g1, g2);
310 }
311
eval(const Cartesian_polygon *,const Cartesian_multipoint *) const312 bool Overlaps::eval(const Cartesian_polygon *,
313 const Cartesian_multipoint *) const {
314 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
315 throw null_value_exception();
316 }
317
eval(const Cartesian_polygon *,const Cartesian_multilinestring *) const318 bool Overlaps::eval(const Cartesian_polygon *,
319 const Cartesian_multilinestring *) const {
320 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
321 throw null_value_exception();
322 }
323
eval(const Cartesian_polygon * g1,const Cartesian_multipolygon * g2) const324 bool Overlaps::eval(const Cartesian_polygon *g1,
325 const Cartesian_multipolygon *g2) const {
326 return bg::overlaps(*g1, *g2);
327 }
328
329 //////////////////////////////////////////////////////////////////////////////
330
331 // overlaps(Cartesian_geometrycollection, *)
332
eval(const Cartesian_geometrycollection * g1,const Geometry * g2) const333 bool Overlaps::eval(const Cartesian_geometrycollection *g1,
334 const Geometry *g2) const {
335 return geometry_collection_apply_overlaps<Cartesian_geometrycollection>(
336 *this, g1, g2);
337 }
338
339 //////////////////////////////////////////////////////////////////////////////
340
341 // overlaps(Cartesian_multipoint, *)
342
eval(const Cartesian_multipoint *,const Cartesian_point *) const343 bool Overlaps::eval(const Cartesian_multipoint *,
344 const Cartesian_point *) const {
345 // The interior of a point can never be within both the interior and exterior
346 // of another geometry.
347 return false;
348 }
349
eval(const Cartesian_multipoint *,const Cartesian_linestring *) const350 bool Overlaps::eval(const Cartesian_multipoint *,
351 const Cartesian_linestring *) const {
352 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
353 throw null_value_exception();
354 }
355
eval(const Cartesian_multipoint *,const Cartesian_polygon *) const356 bool Overlaps::eval(const Cartesian_multipoint *,
357 const Cartesian_polygon *) const {
358 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
359 throw null_value_exception();
360 }
361
eval(const Cartesian_multipoint * g1,const Cartesian_geometrycollection * g2) const362 bool Overlaps::eval(const Cartesian_multipoint *g1,
363 const Cartesian_geometrycollection *g2) const {
364 return geometry_collection_apply_overlaps<Cartesian_geometrycollection>(
365 *this, g1, g2);
366 }
367
eval(const Cartesian_multipoint * g1,const Cartesian_multipoint * g2) const368 bool Overlaps::eval(const Cartesian_multipoint *g1,
369 const Cartesian_multipoint *g2) const {
370 return bg::overlaps(*g1, *g2);
371 }
372
eval(const Cartesian_multipoint *,const Cartesian_multilinestring *) const373 bool Overlaps::eval(const Cartesian_multipoint *,
374 const Cartesian_multilinestring *) const {
375 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
376 throw null_value_exception();
377 }
378
eval(const Cartesian_multipoint *,const Cartesian_multipolygon *) const379 bool Overlaps::eval(const Cartesian_multipoint *,
380 const Cartesian_multipolygon *) const {
381 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
382 throw null_value_exception();
383 }
384
385 //////////////////////////////////////////////////////////////////////////////
386
387 // overlaps(Cartesian_multilinestring, *)
388
eval(const Cartesian_multilinestring *,const Cartesian_point *) const389 bool Overlaps::eval(const Cartesian_multilinestring *,
390 const Cartesian_point *) const {
391 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
392 throw null_value_exception();
393 }
394
eval(const Cartesian_multilinestring * g1,const Cartesian_linestring * g2) const395 bool Overlaps::eval(const Cartesian_multilinestring *g1,
396 const Cartesian_linestring *g2) const {
397 return bg::overlaps(*g1, *g2);
398 }
399
eval(const Cartesian_multilinestring *,const Cartesian_polygon *) const400 bool Overlaps::eval(const Cartesian_multilinestring *,
401 const Cartesian_polygon *) const {
402 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
403 throw null_value_exception();
404 }
405
eval(const Cartesian_multilinestring * g1,const Cartesian_geometrycollection * g2) const406 bool Overlaps::eval(const Cartesian_multilinestring *g1,
407 const Cartesian_geometrycollection *g2) const {
408 return geometry_collection_apply_overlaps<Cartesian_geometrycollection>(
409 *this, g1, g2);
410 }
411
eval(const Cartesian_multilinestring *,const Cartesian_multipoint *) const412 bool Overlaps::eval(const Cartesian_multilinestring *,
413 const Cartesian_multipoint *) const {
414 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
415 throw null_value_exception();
416 }
417
eval(const Cartesian_multilinestring * g1,const Cartesian_multilinestring * g2) const418 bool Overlaps::eval(const Cartesian_multilinestring *g1,
419 const Cartesian_multilinestring *g2) const {
420 return bg::overlaps(*g1, *g2);
421 }
422
eval(const Cartesian_multilinestring *,const Cartesian_multipolygon *) const423 bool Overlaps::eval(const Cartesian_multilinestring *,
424 const Cartesian_multipolygon *) const {
425 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
426 throw null_value_exception();
427 }
428
429 //////////////////////////////////////////////////////////////////////////////
430
431 // overlaps(Cartesian_multipolygon, *)
432
eval(const Cartesian_multipolygon *,const Cartesian_point *) const433 bool Overlaps::eval(const Cartesian_multipolygon *,
434 const Cartesian_point *) const {
435 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
436 throw null_value_exception();
437 }
438
eval(const Cartesian_multipolygon *,const Cartesian_linestring *) const439 bool Overlaps::eval(const Cartesian_multipolygon *,
440 const Cartesian_linestring *) const {
441 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
442 throw null_value_exception();
443 }
444
eval(const Cartesian_multipolygon * g1,const Cartesian_polygon * g2) const445 bool Overlaps::eval(const Cartesian_multipolygon *g1,
446 const Cartesian_polygon *g2) const {
447 return bg::overlaps(*g1, *g2);
448 }
449
eval(const Cartesian_multipolygon * g1,const Cartesian_geometrycollection * g2) const450 bool Overlaps::eval(const Cartesian_multipolygon *g1,
451 const Cartesian_geometrycollection *g2) const {
452 return geometry_collection_apply_overlaps<Cartesian_geometrycollection>(
453 *this, g1, g2);
454 }
455
eval(const Cartesian_multipolygon *,const Cartesian_multipoint *) const456 bool Overlaps::eval(const Cartesian_multipolygon *,
457 const Cartesian_multipoint *) const {
458 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
459 throw null_value_exception();
460 }
461
eval(const Cartesian_multipolygon *,const Cartesian_multilinestring *) const462 bool Overlaps::eval(const Cartesian_multipolygon *,
463 const Cartesian_multilinestring *) const {
464 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
465 throw null_value_exception();
466 }
467
eval(const Cartesian_multipolygon * g1,const Cartesian_multipolygon * g2) const468 bool Overlaps::eval(const Cartesian_multipolygon *g1,
469 const Cartesian_multipolygon *g2) const {
470 return bg::overlaps(*g1, *g2);
471 }
472
473 //////////////////////////////////////////////////////////////////////////////
474
475 // overlaps(Geographic_point, *)
476
eval(const Geographic_point *,const Geographic_point *) const477 bool Overlaps::eval(const Geographic_point *, const Geographic_point *) const {
478 // The interior of a point can never be within both the interior and exterior
479 // of another geometry.
480 return false;
481 }
482
eval(const Geographic_point *,const Geographic_linestring *) const483 bool Overlaps::eval(const Geographic_point *,
484 const Geographic_linestring *) const {
485 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
486 throw null_value_exception();
487 }
488
eval(const Geographic_point *,const Geographic_polygon *) const489 bool Overlaps::eval(const Geographic_point *,
490 const Geographic_polygon *) const {
491 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
492 throw null_value_exception();
493 }
494
eval(const Geographic_point * g1,const Geographic_geometrycollection * g2) const495 bool Overlaps::eval(const Geographic_point *g1,
496 const Geographic_geometrycollection *g2) const {
497 return geometry_collection_apply_overlaps<Geographic_geometrycollection>(
498 *this, g1, g2);
499 }
500
eval(const Geographic_point *,const Geographic_multipoint *) const501 bool Overlaps::eval(const Geographic_point *,
502 const Geographic_multipoint *) const {
503 // The interior of a point can never be within both the interior and exterior
504 // of another geometry.
505 return false;
506 }
507
eval(const Geographic_point *,const Geographic_multilinestring *) const508 bool Overlaps::eval(const Geographic_point *,
509 const Geographic_multilinestring *) const {
510 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
511 throw null_value_exception();
512 }
513
eval(const Geographic_point *,const Geographic_multipolygon *) const514 bool Overlaps::eval(const Geographic_point *,
515 const Geographic_multipolygon *) const {
516 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
517 throw null_value_exception();
518 }
519
520 //////////////////////////////////////////////////////////////////////////////
521
522 // overlaps(Geographic_linestring, *)
523
eval(const Geographic_linestring *,const Geographic_point *) const524 bool Overlaps::eval(const Geographic_linestring *,
525 const Geographic_point *) const {
526 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
527 throw null_value_exception();
528 }
529
eval(const Geographic_linestring * g1,const Geographic_linestring * g2) const530 bool Overlaps::eval(const Geographic_linestring *g1,
531 const Geographic_linestring *g2) const {
532 return bg::overlaps(*g1, *g2, m_geographic_ll_aa_strategy);
533 }
534
eval(const Geographic_linestring *,const Geographic_polygon *) const535 bool Overlaps::eval(const Geographic_linestring *,
536 const Geographic_polygon *) const {
537 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
538 throw null_value_exception();
539 }
540
eval(const Geographic_linestring * g1,const Geographic_geometrycollection * g2) const541 bool Overlaps::eval(const Geographic_linestring *g1,
542 const Geographic_geometrycollection *g2) const {
543 return geometry_collection_apply_overlaps<Geographic_geometrycollection>(
544 *this, g1, g2);
545 }
546
eval(const Geographic_linestring *,const Geographic_multipoint *) const547 bool Overlaps::eval(const Geographic_linestring *,
548 const Geographic_multipoint *) const {
549 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
550 throw null_value_exception();
551 }
552
eval(const Geographic_linestring * g1,const Geographic_multilinestring * g2) const553 bool Overlaps::eval(const Geographic_linestring *g1,
554 const Geographic_multilinestring *g2) const {
555 return bg::overlaps(*g1, *g2, m_geographic_ll_aa_strategy);
556 }
557
eval(const Geographic_linestring *,const Geographic_multipolygon *) const558 bool Overlaps::eval(const Geographic_linestring *,
559 const Geographic_multipolygon *) const {
560 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
561 throw null_value_exception();
562 }
563
564 //////////////////////////////////////////////////////////////////////////////
565
566 // overlaps(Geographic_polygon, *)
567
eval(const Geographic_polygon *,const Geographic_point *) const568 bool Overlaps::eval(const Geographic_polygon *,
569 const Geographic_point *) const {
570 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
571 throw null_value_exception();
572 }
573
eval(const Geographic_polygon *,const Geographic_linestring *) const574 bool Overlaps::eval(const Geographic_polygon *,
575 const Geographic_linestring *) const {
576 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
577 throw null_value_exception();
578 }
579
eval(const Geographic_polygon * g1,const Geographic_polygon * g2) const580 bool Overlaps::eval(const Geographic_polygon *g1,
581 const Geographic_polygon *g2) const {
582 return bg::overlaps(*g1, *g2, m_geographic_ll_aa_strategy);
583 }
584
eval(const Geographic_polygon * g1,const Geographic_geometrycollection * g2) const585 bool Overlaps::eval(const Geographic_polygon *g1,
586 const Geographic_geometrycollection *g2) const {
587 return geometry_collection_apply_overlaps<Geographic_geometrycollection>(
588 *this, g1, g2);
589 }
590
eval(const Geographic_polygon *,const Geographic_multipoint *) const591 bool Overlaps::eval(const Geographic_polygon *,
592 const Geographic_multipoint *) const {
593 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
594 throw null_value_exception();
595 }
596
eval(const Geographic_polygon *,const Geographic_multilinestring *) const597 bool Overlaps::eval(const Geographic_polygon *,
598 const Geographic_multilinestring *) const {
599 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
600 throw null_value_exception();
601 }
602
eval(const Geographic_polygon * g1,const Geographic_multipolygon * g2) const603 bool Overlaps::eval(const Geographic_polygon *g1,
604 const Geographic_multipolygon *g2) const {
605 return bg::overlaps(*g1, *g2, m_geographic_ll_aa_strategy);
606 }
607
608 //////////////////////////////////////////////////////////////////////////////
609
610 // overlaps(Geographic_geometrycollection, *)
611
eval(const Geographic_geometrycollection * g1,const Geometry * g2) const612 bool Overlaps::eval(const Geographic_geometrycollection *g1,
613 const Geometry *g2) const {
614 return geometry_collection_apply_overlaps<Geographic_geometrycollection>(
615 *this, g1, g2);
616 }
617
618 //////////////////////////////////////////////////////////////////////////////
619
620 // overlaps(Geographic_multipoint, *)
621
eval(const Geographic_multipoint *,const Geographic_point *) const622 bool Overlaps::eval(const Geographic_multipoint *,
623 const Geographic_point *) const {
624 // The interior of a point can never be within both the interior and exterior
625 // of another geometry.
626 return false;
627 }
628
eval(const Geographic_multipoint *,const Geographic_linestring *) const629 bool Overlaps::eval(const Geographic_multipoint *,
630 const Geographic_linestring *) const {
631 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
632 throw null_value_exception();
633 }
634
eval(const Geographic_multipoint *,const Geographic_polygon *) const635 bool Overlaps::eval(const Geographic_multipoint *,
636 const Geographic_polygon *) const {
637 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
638 throw null_value_exception();
639 }
640
eval(const Geographic_multipoint * g1,const Geographic_geometrycollection * g2) const641 bool Overlaps::eval(const Geographic_multipoint *g1,
642 const Geographic_geometrycollection *g2) const {
643 return geometry_collection_apply_overlaps<Geographic_geometrycollection>(
644 *this, g1, g2);
645 }
646
eval(const Geographic_multipoint * g1,const Geographic_multipoint * g2) const647 bool Overlaps::eval(const Geographic_multipoint *g1,
648 const Geographic_multipoint *g2) const {
649 // Default strategy is OK. P/P computations do not depend on shape of
650 // ellipsoid.
651 return bg::overlaps(*g1, *g2);
652 }
653
eval(const Geographic_multipoint *,const Geographic_multilinestring *) const654 bool Overlaps::eval(const Geographic_multipoint *,
655 const Geographic_multilinestring *) const {
656 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
657 throw null_value_exception();
658 }
659
eval(const Geographic_multipoint *,const Geographic_multipolygon *) const660 bool Overlaps::eval(const Geographic_multipoint *,
661 const Geographic_multipolygon *) const {
662 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
663 throw null_value_exception();
664 }
665
666 //////////////////////////////////////////////////////////////////////////////
667
668 // overlaps(Geographic_multilinestring, *)
669
eval(const Geographic_multilinestring *,const Geographic_point *) const670 bool Overlaps::eval(const Geographic_multilinestring *,
671 const Geographic_point *) const {
672 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
673 throw null_value_exception();
674 }
675
eval(const Geographic_multilinestring * g1,const Geographic_linestring * g2) const676 bool Overlaps::eval(const Geographic_multilinestring *g1,
677 const Geographic_linestring *g2) const {
678 return bg::overlaps(*g1, *g2, m_geographic_ll_aa_strategy);
679 }
680
eval(const Geographic_multilinestring *,const Geographic_polygon *) const681 bool Overlaps::eval(const Geographic_multilinestring *,
682 const Geographic_polygon *) const {
683 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
684 throw null_value_exception();
685 }
686
eval(const Geographic_multilinestring * g1,const Geographic_geometrycollection * g2) const687 bool Overlaps::eval(const Geographic_multilinestring *g1,
688 const Geographic_geometrycollection *g2) const {
689 return geometry_collection_apply_overlaps<Geographic_geometrycollection>(
690 *this, g1, g2);
691 }
692
eval(const Geographic_multilinestring *,const Geographic_multipoint *) const693 bool Overlaps::eval(const Geographic_multilinestring *,
694 const Geographic_multipoint *) const {
695 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
696 throw null_value_exception();
697 }
698
eval(const Geographic_multilinestring * g1,const Geographic_multilinestring * g2) const699 bool Overlaps::eval(const Geographic_multilinestring *g1,
700 const Geographic_multilinestring *g2) const {
701 return bg::overlaps(*g1, *g2, m_geographic_ll_aa_strategy);
702 }
703
eval(const Geographic_multilinestring *,const Geographic_multipolygon *) const704 bool Overlaps::eval(const Geographic_multilinestring *,
705 const Geographic_multipolygon *) const {
706 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
707 throw null_value_exception();
708 }
709
710 //////////////////////////////////////////////////////////////////////////////
711
712 // overlaps(Geographic_multipolygon, *)
713
eval(const Geographic_multipolygon *,const Geographic_point *) const714 bool Overlaps::eval(const Geographic_multipolygon *,
715 const Geographic_point *) const {
716 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
717 throw null_value_exception();
718 }
719
eval(const Geographic_multipolygon *,const Geographic_linestring *) const720 bool Overlaps::eval(const Geographic_multipolygon *,
721 const Geographic_linestring *) const {
722 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
723 throw null_value_exception();
724 }
725
eval(const Geographic_multipolygon * g1,const Geographic_polygon * g2) const726 bool Overlaps::eval(const Geographic_multipolygon *g1,
727 const Geographic_polygon *g2) const {
728 return bg::overlaps(*g1, *g2, m_geographic_ll_aa_strategy);
729 }
730
eval(const Geographic_multipolygon * g1,const Geographic_geometrycollection * g2) const731 bool Overlaps::eval(const Geographic_multipolygon *g1,
732 const Geographic_geometrycollection *g2) const {
733 return geometry_collection_apply_overlaps<Geographic_geometrycollection>(
734 *this, g1, g2);
735 }
736
eval(const Geographic_multipolygon *,const Geographic_multipoint *) const737 bool Overlaps::eval(const Geographic_multipolygon *,
738 const Geographic_multipoint *) const {
739 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
740 throw null_value_exception();
741 }
742
eval(const Geographic_multipolygon *,const Geographic_multilinestring *) const743 bool Overlaps::eval(const Geographic_multipolygon *,
744 const Geographic_multilinestring *) const {
745 // If dim(g1) != dim(g2), return NULL (SQL/MM 2015, Part 3, Sect. 5.1.54).
746 throw null_value_exception();
747 }
748
eval(const Geographic_multipolygon * g1,const Geographic_multipolygon * g2) const749 bool Overlaps::eval(const Geographic_multipolygon *g1,
750 const Geographic_multipolygon *g2) const {
751 return bg::overlaps(*g1, *g2, m_geographic_ll_aa_strategy);
752 }
753
754 //////////////////////////////////////////////////////////////////////////////
755
756 // overlaps(Box, Box)
757
eval(const Cartesian_box * b1,const Cartesian_box * b2) const758 bool Overlaps::eval(const Cartesian_box *b1, const Cartesian_box *b2) const {
759 // Work around bugs in BG for boxes that have zero height and/or width.
760 if (mbr_is_point(*b1) || mbr_is_point(*b2)) {
761 // A bounding box around a point may never overlap another box. The point is
762 // either entirely in the interior, boundary or exterior of the other box.
763 return false;
764 }
765
766 if (mbr_is_line(*b1) && mbr_is_line(*b2)) {
767 Cartesian_point b1_ls_start(b1->min_corner().x(), b1->min_corner().y());
768 Cartesian_point b1_ls_end(b1->max_corner().x(), b1->max_corner().y());
769 Cartesian_linestring b1_ls;
770 b1_ls.push_back(b1_ls_start);
771 b1_ls.push_back(b1_ls_end);
772 Cartesian_point b2_ls_start(b2->min_corner().x(), b2->min_corner().y());
773 Cartesian_point b2_ls_end(b2->max_corner().x(), b2->max_corner().y());
774 Cartesian_linestring b2_ls;
775 b2_ls.push_back(b2_ls_start);
776 b2_ls.push_back(b2_ls_end);
777 return bg::overlaps(b1_ls, b2_ls) || bg::crosses(b1_ls, b2_ls);
778 }
779
780 return bg::overlaps(*b1, *b2);
781 }
782
eval(const Geographic_box * b1,const Geographic_box * b2) const783 bool Overlaps::eval(const Geographic_box *b1, const Geographic_box *b2) const {
784 // Work around bugs in BG for boxes that have zero height and/or width.
785 if (mbr_is_point(*b1) || mbr_is_point(*b2)) {
786 // A bounding box around a point may never overlap another box. The point is
787 // either entirely in the interior, boundary or exterior of the other box.
788 return false;
789 }
790
791 if (mbr_is_line(*b1) && mbr_is_line(*b2)) {
792 Geographic_point b1_ls_start(b1->min_corner().x(), b1->min_corner().y());
793 Geographic_point b1_ls_end(b1->max_corner().x(), b1->max_corner().y());
794 Geographic_linestring b1_ls;
795 b1_ls.push_back(b1_ls_start);
796 b1_ls.push_back(b1_ls_end);
797 Geographic_point b2_ls_start(b2->min_corner().x(), b2->min_corner().y());
798 Geographic_point b2_ls_end(b2->max_corner().x(), b2->max_corner().y());
799 Geographic_linestring b2_ls;
800 b2_ls.push_back(b2_ls_start);
801 b2_ls.push_back(b2_ls_end);
802 return bg::overlaps(b1_ls, b2_ls) || bg::crosses(b1_ls, b2_ls);
803 }
804
805 return bg::overlaps(*b1, *b2);
806 }
807
808 //////////////////////////////////////////////////////////////////////////////
809
overlaps(const dd::Spatial_reference_system * srs,const Geometry * g1,const Geometry * g2,const char * func_name,bool * overlaps,bool * null)810 bool overlaps(const dd::Spatial_reference_system *srs, const Geometry *g1,
811 const Geometry *g2, const char *func_name, bool *overlaps,
812 bool *null) noexcept {
813 try {
814 DBUG_ASSERT(g1->coordinate_system() == g2->coordinate_system());
815 DBUG_ASSERT(srs == nullptr ||
816 ((srs->is_cartesian() &&
817 g1->coordinate_system() == Coordinate_system::kCartesian) ||
818 (srs->is_geographic() &&
819 g1->coordinate_system() == Coordinate_system::kGeographic)));
820
821 if ((*null = (g1->is_empty() || g2->is_empty()))) return false;
822
823 Overlaps overlaps_func(srs ? srs->semi_major_axis() : 0.0,
824 srs ? srs->semi_minor_axis() : 0.0);
825 *overlaps = overlaps_func(g1, g2);
826 } catch (const null_value_exception &) {
827 *null = true;
828 return false;
829 } catch (...) {
830 handle_gis_exception(func_name);
831 return true;
832 }
833
834 return false;
835 }
836
mbr_overlaps(const dd::Spatial_reference_system * srs,const Geometry * g1,const Geometry * g2,const char * func_name,bool * overlaps,bool * null)837 bool mbr_overlaps(const dd::Spatial_reference_system *srs, const Geometry *g1,
838 const Geometry *g2, const char *func_name, bool *overlaps,
839 bool *null) noexcept {
840 try {
841 DBUG_ASSERT(g1->coordinate_system() == g2->coordinate_system());
842 DBUG_ASSERT(srs == nullptr ||
843 ((srs->is_cartesian() &&
844 g1->coordinate_system() == Coordinate_system::kCartesian) ||
845 (srs->is_geographic() &&
846 g1->coordinate_system() == Coordinate_system::kGeographic)));
847
848 if ((*null = (g1->is_empty() || g2->is_empty()))) return false;
849
850 Overlaps overlaps_func(srs ? srs->semi_major_axis() : 0.0,
851 srs ? srs->semi_minor_axis() : 0.0);
852
853 switch (g1->coordinate_system()) {
854 case Coordinate_system::kCartesian: {
855 Cartesian_box mbr1;
856 box_envelope(g1, srs, &mbr1);
857 Cartesian_box mbr2;
858 box_envelope(g2, srs, &mbr2);
859 *overlaps = overlaps_func(&mbr1, &mbr2);
860 break;
861 }
862 case Coordinate_system::kGeographic: {
863 Geographic_box mbr1;
864 box_envelope(g1, srs, &mbr1);
865 Geographic_box mbr2;
866 box_envelope(g2, srs, &mbr2);
867 *overlaps = overlaps_func(&mbr1, &mbr2);
868 break;
869 }
870 }
871 } catch (...) {
872 handle_gis_exception(func_name);
873 return true;
874 }
875
876 return false;
877 }
878
879 } // namespace gis
880