MySQL 8.3.0
Source Code Documentation
item_geofunc_internal.h
Go to the documentation of this file.
1#ifndef GEOFUNC_INTERNAL_INCLUDED
2#define GEOFUNC_INTERNAL_INCLUDED
3
4/* Copyright (c) 2014, 2023, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is also distributed with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26/**
27 @file
28
29 @brief
30 This file defines common build blocks of GIS functions.
31*/
32
33#include <stddef.h>
34#include <boost/concept/usage.hpp>
35#include <boost/geometry/core/cs.hpp>
36#include <boost/geometry/core/tags.hpp>
37#include <boost/geometry/geometries/box.hpp>
38#include <boost/geometry/geometries/point.hpp>
39#include <boost/geometry/geometries/segment.hpp>
40#include <boost/geometry/index/rtree.hpp>
41#include <cmath>
42#include <utility>
43#include <vector>
44
45#include "sql/gis/srid.h"
46#include "sql/gis_bg_traits.h"
47#include "sql/item_geofunc.h"
48#include "sql/spatial.h"
49
50class String;
51
52#define GIS_ZERO 0.00000000001
53
54/// A wrapper and interface for all geometry types used here. Make these
55/// types as localized as possible. It's used as a type interface.
56/// @tparam CoordinateSystemType Coordinate system type, specified using
57// those defined in boost::geometry::cs.
58template <typename CoordinateSystemType>
59class BG_models {
60 public:
62 // An counter-clockwise, closed Polygon type. It can hold open Polygon data,
63 // but not clockwise ones, otherwise things can go wrong, e.g. intersection.
69
70 typedef double Coordinate_type;
71 typedef CoordinateSystemType Coordinate_system;
72};
73
74namespace bg = boost::geometry;
75namespace bgm = boost::geometry::model;
76namespace bgcs = boost::geometry::cs;
77namespace bgi = boost::geometry::index;
78namespace bgm = boost::geometry::model;
79
80typedef bgm::point<double, 2, bgcs::cartesian> BG_point;
81typedef bgm::box<BG_point> BG_box;
82typedef std::pair<BG_box, size_t> BG_rtree_entry;
83typedef std::vector<BG_rtree_entry> BG_rtree_entries;
84typedef bgi::rtree<BG_rtree_entry, bgi::quadratic<64>> Rtree_index;
85typedef std::vector<BG_rtree_entry> Rtree_result;
86
87inline void make_bg_box(const Geometry *g, BG_box *box) {
88 MBR mbr;
89 g->envelope(&mbr);
90 box->min_corner().set<0>(mbr.xmin);
91 box->min_corner().set<1>(mbr.ymin);
92 box->max_corner().set<0>(mbr.xmax);
93 box->max_corner().set<1>(mbr.ymax);
94}
95
96inline bool is_box_valid(const BG_box &box) {
97 return !(!std::isfinite(box.min_corner().get<0>()) ||
98 !std::isfinite(box.min_corner().get<1>()) ||
99 !std::isfinite(box.max_corner().get<0>()) ||
100 !std::isfinite(box.max_corner().get<1>()) ||
101 box.max_corner().get<0>() < box.min_corner().get<0>() ||
102 box.max_corner().get<1>() < box.min_corner().get<1>());
103}
104
105/**
106 Build an rtree set using a geometry collection.
107 @param gl geometry object pointers container.
108 @param [out] rtree entries which can be used to build an rtree.
109 */
111 Rtree_index *rtree);
112
113/**
114 Build an rtree set using array of Boost.Geometry objects, which are
115 components of a multi geometry.
116 @param mg the multi geometry.
117 @param rtree the rtree to build.
118 */
119template <typename MultiGeometry>
120void make_rtree_bggeom(const MultiGeometry &mg, Rtree_index *rtree);
121
123 gis::srid_t srid) {
124 return new Gis_geometry_collection(srid, Geometry::wkb_invalid_type, nullptr,
125 str);
126}
127
128/*
129 Check whether a geometry is an empty geometry collection, i.e. one that
130 doesn't contain any geometry component of [multi]point or [multi]linestring
131 or [multi]polygon type.
132 @param g the geometry to check.
133 @return true if g is such an empty geometry collection;
134 false otherwise.
135*/
136bool is_empty_geocollection(const Geometry *g);
137
138/*
139 Check whether wkbres is the data of an empty geometry collection, i.e. one
140 that doesn't contain any geometry component of [multi]point or
141 [multi]linestring or [multi]polygon type.
142
143 @param wkbres a piece of geometry data of GEOMETRY format, i.e. an SRID
144 prefixing a WKB.
145 @return true if wkbres contains such an empty geometry collection;
146 false otherwise.
147 */
148bool is_empty_geocollection(const String &wkbres);
149
150/**
151 Less than comparator for points used by BG.
152 */
153struct bgpt_lt {
154 template <typename Point>
155 bool operator()(const Point &p1, const Point &p2) const {
156 if (p1.template get<0>() != p2.template get<0>())
157 return p1.template get<0>() < p2.template get<0>();
158 else
159 return p1.template get<1>() < p2.template get<1>();
160 }
161};
162
163/**
164 Equals comparator for points used by BG.
165 */
166struct bgpt_eq {
167 template <typename Point>
168 bool operator()(const Point &p1, const Point &p2) const {
169 return p1.template get<0>() == p2.template get<0>() &&
170 p1.template get<1>() == p2.template get<1>();
171 }
172};
173
174/**
175 For every Geometry object write-accessed by a boost geometry function, i.e.
176 those passed as out parameter into set operation functions, call this
177 function before using the result object's data.
178
179 @param resbuf_mgr Tracks the result buffer
180 @param [in,out] geout Geometry object
181 @param [in,out] res GEOMETRY string.
182
183 @return true if an error occurred or if the geometry is an empty
184 collection; false if no error occurred.
185*/
186template <typename BG_geotype>
187bool post_fix_result(BG_result_buf_mgr *resbuf_mgr, BG_geotype &geout,
188 String *res);
189
190#endif
std::vector< Geometry * > Geometry_list
Definition: item_geofunc.h:149
A wrapper and interface for all geometry types used here.
Definition: item_geofunc_internal.h:59
Gis_multi_polygon Multipolygon
Definition: item_geofunc_internal.h:68
Gis_line_string Linestring
Definition: item_geofunc_internal.h:65
Gis_multi_point Multipoint
Definition: item_geofunc_internal.h:66
Gis_multi_line_string Multilinestring
Definition: item_geofunc_internal.h:67
Gis_polygon Polygon
Definition: item_geofunc_internal.h:64
double Coordinate_type
Definition: item_geofunc_internal.h:70
CoordinateSystemType Coordinate_system
Definition: item_geofunc_internal.h:71
Gis_point Point
Definition: item_geofunc_internal.h:61
We have to hold result buffers in functions that return a GEOMETRY string, because such a function's ...
Definition: item_geofunc.h:81
Definition: spatial.h:212
@ wkb_invalid_type
Definition: spatial.h:290
bool envelope(String *result) const
Definition: spatial.cc:957
Definition: spatial.h:2408
Definition: spatial.h:2098
Definition: spatial.h:2332
Definition: spatial.h:2289
Definition: spatial.h:2372
Definition: spatial.h:1151
Definition: spatial.h:2186
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:166
bgi::rtree< BG_rtree_entry, bgi::quadratic< 64 > > Rtree_index
Definition: item_geofunc_internal.h:84
void make_rtree_bggeom(const MultiGeometry &mg, Rtree_index *rtree)
Build an rtree set using array of Boost.Geometry objects, which are components of a multi geometry.
Definition: geometry_rtree.cc:79
bool post_fix_result(BG_result_buf_mgr *resbuf_mgr, BG_geotype &geout, String *res)
For every Geometry object write-accessed by a boost geometry function, i.e.
Definition: item_geofunc_internal.cc:156
std::vector< BG_rtree_entry > Rtree_result
Definition: item_geofunc_internal.h:85
Gis_geometry_collection * empty_collection(String *str, gis::srid_t srid)
Definition: item_geofunc_internal.h:122
bgm::box< BG_point > BG_box
Definition: item_geofunc_internal.h:81
std::pair< BG_box, size_t > BG_rtree_entry
Definition: item_geofunc_internal.h:82
void make_rtree(const BG_geometry_collection::Geometry_list &gl, Rtree_index *rtree)
Build an rtree set using a geometry collection.
Definition: geometry_rtree.cc:54
bool is_box_valid(const BG_box &box)
Definition: item_geofunc_internal.h:96
std::vector< BG_rtree_entry > BG_rtree_entries
Definition: item_geofunc_internal.h:83
bool is_empty_geocollection(const Geometry *g)
Definition: item_geofunc_internal.cc:229
void make_bg_box(const Geometry *g, BG_box *box)
Definition: item_geofunc_internal.h:87
bgm::point< double, 2, bgcs::cartesian > BG_point
Definition: item_geofunc_internal.h:80
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1065
Definition: item_geofunc.cc:101
Definition: box_traits.h:41
std::uint32_t srid_t
A spatial reference system ID (SRID).
Definition: srid.h:32
Definition: spatial.h:82
double xmin
Definition: spatial.h:83
double ymax
Definition: spatial.h:83
double xmax
Definition: spatial.h:83
double ymin
Definition: spatial.h:83
Equals comparator for points used by BG.
Definition: item_geofunc_internal.h:166
bool operator()(const Point &p1, const Point &p2) const
Definition: item_geofunc_internal.h:168
Less than comparator for points used by BG.
Definition: item_geofunc_internal.h:153
bool operator()(const Point &p1, const Point &p2) const
Definition: item_geofunc_internal.h:155