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