1 // Copyright (c) 2018, 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 simplify functor and function.
26 
27 #include "sql/gis/simplify.h"
28 #include "sql/gis/simplify_functor.h"
29 
30 #include <memory>  // std::unique_ptr
31 
32 #include <boost/geometry.hpp>
33 
34 #include "my_dbug.h"                                // DBUG_ASSERT
35 #include "my_inttypes.h"                            // MYF
36 #include "my_sys.h"                                 // my_error
37 #include "mysqld_error.h"                           // Error codes
38 #include "sql/dd/types/spatial_reference_system.h"  // dd::Spatial_reference_system
39 #include "sql/gis/geometries.h"
40 #include "sql/gis/geometries_traits.h"
41 #include "sql/sql_exception_handler.h"  // handle_gis_exception
42 
43 namespace bg = boost::geometry;
44 
45 namespace gis {
46 
operator ()(const Geometry & g) const47 std::unique_ptr<Geometry> Simplify::operator()(const Geometry &g) const {
48   return apply(*this, g);
49 }
50 
eval(const Geometry & g) const51 std::unique_ptr<Geometry> Simplify::eval(const Geometry &g) const {
52   // All parameter type combinations have been implemented.
53   DBUG_ASSERT(false);
54   throw not_implemented_exception::for_non_projected(g);
55 }
56 
eval(const Cartesian_point & g) const57 std::unique_ptr<Geometry> Simplify::eval(const Cartesian_point &g) const {
58   Cartesian_point *pt_result = new Cartesian_point();
59   std::unique_ptr<Geometry> result(pt_result);
60   bg::simplify(g, *pt_result, m_max_distance);
61   return result;
62 }
63 
eval(const Cartesian_linestring & g) const64 std::unique_ptr<Geometry> Simplify::eval(const Cartesian_linestring &g) const {
65   Cartesian_linestring *ls_result = new Cartesian_linestring();
66   std::unique_ptr<Geometry> result(ls_result);
67   bg::simplify(g, *ls_result, m_max_distance);
68   if (ls_result->size() < 2) ls_result->clear();
69   return result;
70 }
71 
eval(const Cartesian_polygon & g) const72 std::unique_ptr<Geometry> Simplify::eval(const Cartesian_polygon &g) const {
73   Cartesian_polygon *py_result = new Cartesian_polygon();
74   std::unique_ptr<Geometry> result(py_result);
75   bg::simplify(g, *py_result, m_max_distance);
76   if (py_result->exterior_ring().size() < 4)
77     result.reset(new Cartesian_polygon());
78   return result;
79 }
80 
eval(const Cartesian_geometrycollection & g) const81 std::unique_ptr<Geometry> Simplify::eval(
82     const Cartesian_geometrycollection &g) const {
83   Cartesian_geometrycollection *gc_result = new Cartesian_geometrycollection();
84   std::unique_ptr<Geometry> result(gc_result);
85   for (Geometry *geom : g) {
86     std::unique_ptr<Geometry> simplified_geom = (*this)(*geom);
87     if (!simplified_geom->is_empty()) gc_result->push_back(*simplified_geom);
88   }
89   return result;
90 }
91 
eval(const Cartesian_multipoint & g) const92 std::unique_ptr<Geometry> Simplify::eval(const Cartesian_multipoint &g) const {
93   Cartesian_multipoint *mpt_result = new Cartesian_multipoint();
94   std::unique_ptr<Geometry> result(mpt_result);
95   bg::simplify(g, *mpt_result, m_max_distance);
96   return result;
97 }
98 
eval(const Cartesian_multilinestring & g) const99 std::unique_ptr<Geometry> Simplify::eval(
100     const Cartesian_multilinestring &g) const {
101   std::unique_ptr<Cartesian_multilinestring> unfiltered_result(
102       new Cartesian_multilinestring());
103   bg::simplify(g, *unfiltered_result, m_max_distance);
104 
105   // bg::simplify may create geometries with too few points. Filter out those.
106   Cartesian_multilinestring *mls_result = new Cartesian_multilinestring();
107   std::unique_ptr<Geometry> result(mls_result);
108   for (Cartesian_linestring &ls : *unfiltered_result) {
109     if (ls.size() >= 2) mls_result->push_back(ls);
110   }
111 
112   return result;
113 }
eval(const Cartesian_multipolygon & g) const114 std::unique_ptr<Geometry> Simplify::eval(
115     const Cartesian_multipolygon &g) const {
116   std::unique_ptr<Cartesian_multipolygon> unfiltered_result(
117       new Cartesian_multipolygon());
118   bg::simplify(g, *unfiltered_result, m_max_distance);
119 
120   // bg::simplify may create geometries with too few points. Filter out those.
121   Cartesian_multipolygon *mpy_result = new Cartesian_multipolygon();
122   std::unique_ptr<Geometry> result(mpy_result);
123   for (Cartesian_polygon &py : *unfiltered_result) {
124     if (py.exterior_ring().size() >= 4) mpy_result->push_back(py);
125   }
126 
127   return result;
128 }
129 
simplify(const dd::Spatial_reference_system * srs,const Geometry & g,double max_distance,const char * func_name,std::unique_ptr<Geometry> * result)130 bool simplify(const dd::Spatial_reference_system *srs, const Geometry &g,
131               double max_distance, const char *func_name,
132               std::unique_ptr<Geometry> *result) noexcept {
133   try {
134     DBUG_ASSERT(srs == nullptr ||
135                 ((srs->is_cartesian() &&
136                   g.coordinate_system() == Coordinate_system::kCartesian) ||
137                  (srs->is_geographic() &&
138                   g.coordinate_system() == Coordinate_system::kGeographic)));
139 
140     if (srs != nullptr && !srs->is_cartesian()) {
141       DBUG_ASSERT(srs->is_geographic());
142       std::stringstream types;
143       types << type_to_name(g.type()) << ", ...";
144       my_error(ER_NOT_IMPLEMENTED_FOR_GEOGRAPHIC_SRS, MYF(0), func_name,
145                types.str().c_str());
146       return true;
147     }
148 
149     Simplify simplify_func(max_distance);
150     *result = simplify_func(g);
151     if ((*result)->is_empty()) result->reset();
152   } catch (...) {
153     handle_gis_exception(func_name);
154     return true;
155   }
156 
157   return false;
158 }
159 
160 }  // namespace gis
161