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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
// 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 transform functor and function.
// boost/geometry/srs/projections/impl/dms_parser.hpp uses "SEC" as a template
// parameter name, but SEC is a macro in sys/time.h on Solaris.
#include "my_config.h" // HAVE_SYS_TIME_H
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#undef SEC
#endif
#include "sql/gis/transform.h"
#include "sql/gis/transform_functor.h"
#include <boost/geometry.hpp>
#include <memory> // std::unique_ptr
#include "my_inttypes.h" // MYF
#include "my_sys.h" // my_error
#include "mysqld_error.h" // Error codes
#include "sql/dd/string_type.h" // dd::String_type
#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 {
template <typename CartesianGeometry, typename GeographicGeometry,
typename InputGeometry>
auto transform_helper(
const InputGeometry &g, const Coordinate_system &m_output_cs,
const boost::geometry::srs::transformation<> &m_transformation) {
switch (m_output_cs) {
case Coordinate_system::kCartesian: {
// Workaround for limitation in Developer Studio 12.5 on Solaris that
// doesn't allow returning std::unique_ptr<subtype of Geometry>.
CartesianGeometry *pt_result = new CartesianGeometry();
std::unique_ptr<Geometry> result(pt_result);
m_transformation.forward(g, *pt_result);
return result;
}
case Coordinate_system::kGeographic: {
// Workaround for limitation in Developer Studio 12.5 on Solaris that
// doesn't allow returning std::unique_ptr<subtype of Geometry>.
GeographicGeometry *pt_result = new GeographicGeometry();
std::unique_ptr<Geometry> result(pt_result);
m_transformation.forward(g, *pt_result);
return result;
}
}
/* purceov: begin deadcode */
assert(false);
throw std::exception();
/* purceov: end */
}
template <typename InputGeometryCollection>
auto transform_gc_helper(const InputGeometryCollection &g,
const Coordinate_system &m_output_cs,
const Transform &transform) {
switch (m_output_cs) {
case Coordinate_system::kCartesian: {
// Workaround for limitation in Developer Studio 12.5 on Solaris that
// doesn't allow returning std::unique_ptr<subtype of Geometry>.
Cartesian_geometrycollection *gc_result =
new Cartesian_geometrycollection();
std::unique_ptr<Geometry> result(gc_result);
for (std::size_t i = 0; i < g.size(); i++) {
std::unique_ptr<Geometry> geom_res(transform(g[i]));
gc_result->push_back(*geom_res);
}
return result;
}
case Coordinate_system::kGeographic: {
// Workaround for limitation in Developer Studio 12.5 on Solaris that
// doesn't allow returning std::unique_ptr<subtype of Geometry>.
Geographic_geometrycollection *gc_result =
new Geographic_geometrycollection();
std::unique_ptr<Geometry> result(gc_result);
for (std::size_t i = 0; i < g.size(); i++) {
std::unique_ptr<Geometry> geom_res(transform(g[i]));
gc_result->push_back(*geom_res);
}
return result;
}
}
/* purceov: begin deadcode */
assert(false);
throw std::exception();
/* purceov: end */
}
Transform::Transform(const std::string &old_srs_params,
const std::string &new_srs_params,
Coordinate_system output_cs)
: m_transformation(bg::srs::proj4(old_srs_params),
bg::srs::proj4(new_srs_params)),
m_output_cs(output_cs) {}
std::unique_ptr<Geometry> Transform::operator()(const Geometry &g) const {
return apply(*this, g);
}
std::unique_ptr<Geometry> Transform::eval(const Geometry &g) const {
/* purecov: begin deadcode */
assert(false);
throw not_implemented_exception::for_non_projected(g);
/* purecov: end */
}
std::unique_ptr<Geometry> Transform::eval(const Cartesian_point &g) const {
return transform_helper<Cartesian_point, Geographic_point>(g, m_output_cs,
m_transformation);
}
std::unique_ptr<Geometry> Transform::eval(const Geographic_point &g) const {
return transform_helper<Cartesian_point, Geographic_point>(g, m_output_cs,
m_transformation);
}
std::unique_ptr<Geometry> Transform::eval(const Cartesian_linestring &g) const {
return transform_helper<Cartesian_linestring, Geographic_linestring>(
g, m_output_cs, m_transformation);
}
std::unique_ptr<Geometry> Transform::eval(
const Geographic_linestring &g) const {
return transform_helper<Cartesian_linestring, Geographic_linestring>(
g, m_output_cs, m_transformation);
}
std::unique_ptr<Geometry> Transform::eval(const Cartesian_polygon &g) const {
return transform_helper<Cartesian_polygon, Geographic_polygon>(
g, m_output_cs, m_transformation);
}
std::unique_ptr<Geometry> Transform::eval(const Geographic_polygon &g) const {
return transform_helper<Cartesian_polygon, Geographic_polygon>(
g, m_output_cs, m_transformation);
}
std::unique_ptr<Geometry> Transform::eval(
const Cartesian_geometrycollection &g) const {
return transform_gc_helper(g, m_output_cs, *this);
}
std::unique_ptr<Geometry> Transform::eval(
const Geographic_geometrycollection &g) const {
return transform_gc_helper(g, m_output_cs, *this);
}
std::unique_ptr<Geometry> Transform::eval(const Cartesian_multipoint &g) const {
return transform_helper<Cartesian_multipoint, Geographic_multipoint>(
g, m_output_cs, m_transformation);
}
std::unique_ptr<Geometry> Transform::eval(
const Geographic_multipoint &g) const {
return transform_helper<Cartesian_multipoint, Geographic_multipoint>(
g, m_output_cs, m_transformation);
}
std::unique_ptr<Geometry> Transform::eval(
const Cartesian_multilinestring &g) const {
return transform_helper<Cartesian_multilinestring,
Geographic_multilinestring>(g, m_output_cs,
m_transformation);
}
std::unique_ptr<Geometry> Transform::eval(
const Geographic_multilinestring &g) const {
return transform_helper<Cartesian_multilinestring,
Geographic_multilinestring>(g, m_output_cs,
m_transformation);
}
std::unique_ptr<Geometry> Transform::eval(
const Cartesian_multipolygon &g) const {
return transform_helper<Cartesian_multipolygon, Geographic_multipolygon>(
g, m_output_cs, m_transformation);
}
std::unique_ptr<Geometry> Transform::eval(
const Geographic_multipolygon &g) const {
return transform_helper<Cartesian_multipolygon, Geographic_multipolygon>(
g, m_output_cs, m_transformation);
}
bool transform(const dd::Spatial_reference_system *source_srs,
const Geometry &in,
const dd::Spatial_reference_system *target_srs,
const char *func_name, std::unique_ptr<Geometry> *out) noexcept {
try {
if (source_srs->missing_towgs84()) {
my_error(ER_TRANSFORM_SOURCE_SRS_MISSING_TOWGS84, MYF(0),
source_srs->id());
return true;
}
if (target_srs->missing_towgs84()) {
my_error(ER_TRANSFORM_TARGET_SRS_MISSING_TOWGS84, MYF(0),
target_srs->id());
return true;
}
dd::String_type source_proj = source_srs->proj4_parameters();
dd::String_type target_proj = target_srs->proj4_parameters();
if (source_proj.size() == 0) {
assert(source_srs->is_projected());
my_error(ER_TRANSFORM_SOURCE_SRS_NOT_SUPPORTED, MYF(0), source_srs->id());
return true;
}
if (target_proj.size() == 0) {
assert(target_srs->is_projected());
my_error(ER_TRANSFORM_TARGET_SRS_NOT_SUPPORTED, MYF(0), target_srs->id());
return true;
}
Transform transform(source_proj.c_str(), target_proj.c_str(),
target_srs->cs_type());
*out = transform(in);
return false;
} catch (...) {
/* purecov: begin inspected */
handle_gis_exception(func_name);
return true;
/* purecov: end */
}
}
} // namespace gis
|