1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// Copyright (c) 2018, 2025, Oracle and/or its affiliates.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0,
// as published by the Free Software Foundation.
//
// This program is designed to work with certain software (including
// but not limited to OpenSSL) that is licensed under separate terms,
// as designated in a particular file or component or in included license
// documentation. The authors of MySQL hereby grant you an additional
// permission to link the program and your derivative works with the
// separately licensed software that they have either included with
// the program or referenced in the documentation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License, version 2.0, for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
/// @file
///
/// This file implements the simplify functor and function.
#include "sql/gis/simplify.h"
#include "sql/gis/simplify_functor.h"
#include <assert.h>
#include <memory> // std::unique_ptr
#include <boost/geometry.hpp>
// assert
#include "my_inttypes.h" // MYF
#include "my_sys.h" // my_error
#include "mysqld_error.h" // Error codes
#include "sql/dd/types/spatial_reference_system.h" // dd::Spatial_reference_system
#include "sql/gis/geometries.h"
#include "sql/gis/geometries_traits.h"
#include "sql/sql_exception_handler.h" // handle_gis_exception
namespace bg = boost::geometry;
namespace gis {
std::unique_ptr<Geometry> Simplify::operator()(const Geometry &g) const {
return apply(*this, g);
}
std::unique_ptr<Geometry> Simplify::eval(const Geometry &g) const {
// All parameter type combinations have been implemented.
assert(false);
throw not_implemented_exception::for_non_projected(g);
}
std::unique_ptr<Geometry> Simplify::eval(const Cartesian_point &g) const {
Cartesian_point *pt_result = new Cartesian_point();
std::unique_ptr<Geometry> result(pt_result);
bg::simplify(g, *pt_result, m_max_distance);
return result;
}
std::unique_ptr<Geometry> Simplify::eval(const Cartesian_linestring &g) const {
Cartesian_linestring *ls_result = new Cartesian_linestring();
std::unique_ptr<Geometry> result(ls_result);
bg::simplify(g, *ls_result, m_max_distance);
if (ls_result->size() < 2) ls_result->clear();
return result;
}
std::unique_ptr<Geometry> Simplify::eval(const Cartesian_polygon &g) const {
Cartesian_polygon *py_result = new Cartesian_polygon();
std::unique_ptr<Geometry> result(py_result);
bg::simplify(g, *py_result, m_max_distance);
if (py_result->exterior_ring().size() < 4)
result.reset(new Cartesian_polygon());
return result;
}
std::unique_ptr<Geometry> Simplify::eval(
const Cartesian_geometrycollection &g) const {
Cartesian_geometrycollection *gc_result = new Cartesian_geometrycollection();
std::unique_ptr<Geometry> result(gc_result);
for (Geometry *geom : g) {
std::unique_ptr<Geometry> simplified_geom = (*this)(*geom);
if (!simplified_geom->is_empty()) gc_result->push_back(*simplified_geom);
}
return result;
}
std::unique_ptr<Geometry> Simplify::eval(const Cartesian_multipoint &g) const {
Cartesian_multipoint *mpt_result = new Cartesian_multipoint();
std::unique_ptr<Geometry> result(mpt_result);
bg::simplify(g, *mpt_result, m_max_distance);
return result;
}
std::unique_ptr<Geometry> Simplify::eval(
const Cartesian_multilinestring &g) const {
std::unique_ptr<Cartesian_multilinestring> unfiltered_result(
new Cartesian_multilinestring());
bg::simplify(g, *unfiltered_result, m_max_distance);
// bg::simplify may create geometries with too few points. Filter out those.
Cartesian_multilinestring *mls_result = new Cartesian_multilinestring();
std::unique_ptr<Geometry> result(mls_result);
for (Cartesian_linestring &ls : *unfiltered_result) {
if (ls.size() >= 2) mls_result->push_back(ls);
}
return result;
}
std::unique_ptr<Geometry> Simplify::eval(
const Cartesian_multipolygon &g) const {
std::unique_ptr<Cartesian_multipolygon> unfiltered_result(
new Cartesian_multipolygon());
bg::simplify(g, *unfiltered_result, m_max_distance);
// bg::simplify may create geometries with too few points. Filter out those.
Cartesian_multipolygon *mpy_result = new Cartesian_multipolygon();
std::unique_ptr<Geometry> result(mpy_result);
for (Cartesian_polygon &py : *unfiltered_result) {
if (py.exterior_ring().size() >= 4) mpy_result->push_back(py);
}
return result;
}
bool simplify(const dd::Spatial_reference_system *srs, const Geometry &g,
double max_distance, const char *func_name,
std::unique_ptr<Geometry> *result) noexcept {
try {
assert(srs == nullptr ||
((srs->is_cartesian() &&
g.coordinate_system() == Coordinate_system::kCartesian) ||
(srs->is_geographic() &&
g.coordinate_system() == Coordinate_system::kGeographic)));
if (srs != nullptr && !srs->is_cartesian()) {
assert(srs->is_geographic());
std::stringstream types;
types << type_to_name(g.type()) << ", ...";
my_error(ER_NOT_IMPLEMENTED_FOR_GEOGRAPHIC_SRS, MYF(0), func_name,
types.str().c_str());
return true;
}
Simplify simplify_func(max_distance);
*result = simplify_func(g);
if ((*result)->is_empty()) result->reset();
} catch (...) {
handle_gis_exception(func_name);
return true;
}
return false;
}
} // namespace gis
|