MySQL 9.1.0
Source Code Documentation
coordinate_range_visitor.h
Go to the documentation of this file.
1#ifndef SQL_GIS_COORDINATE_RANGE_VISITOR_H_INCLUDED
2#define SQL_GIS_COORDINATE_RANGE_VISITOR_H_INCLUDED
3
4// Copyright (c) 2017, 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#include <cmath> // M_PI, M_PI_2
28
29#include "sql/dd/types/spatial_reference_system.h" // dd::Spatial_reference_system
31
32namespace gis {
33
34/// A visitor that checks if coordinates are within range for a spatial
35/// reference system.
36///
37/// If a coordinate value is found to be out of range, the visitor returns
38/// true. Otherwise, it returns false.
39///
40/// Checking stops on the first value found to be out of range. Cartesian
41/// coordinates are always within range.
43 private:
44 /// Spatial reference system of the geometry
46 /// Whether an out of range longitude value has been encountered
48 /// Whether an out of range latitude value has been encountered
50 /// The coordinate value that is out of range, in SRS unit
52
53 public:
54 /// Construct a new coordinate range visitor.
55 ///
56 /// @param srs The spatial reference system of the geometry.
58 : m_srs(srs),
61 m_coordinate(0.0) {}
62
63 /// Check if the visitor has detected any out of range longitude values.
64 ///
65 /// @retval true At least one out of range longitude value.
66 /// @retval false All longitude values are within range.
68
69 /// Check if the visitor has detected any out of range latitude values.
70 ///
71 /// @retval true At least one out of range latitude value.
72 /// @retval false All latitude values are within range.
74
75 /// Get the coordinate value that is out of range.
76 ///
77 /// @return The coordinate value in the SRS unit.
78 double coordinate_value() const { return m_coordinate; }
79
81 bool visit_enter(Geometry *) override {
82 if (m_srs == nullptr || m_srs->is_cartesian())
83 return true; // Don't descend into each child.
84
85 return false;
86 }
87
89 bool visit(Point *pt) override {
90 if (m_srs == nullptr || m_srs->is_cartesian())
91 return false; // Everything OK.
92
93 double lon = pt->x() - m_srs->prime_meridian() * m_srs->angular_unit();
94 if (!m_srs->positive_east()) lon *= -1.0;
95 if (lon <= -M_PI || lon > M_PI) {
98 return true; // Out of range coordinate detected.
99 }
100
101 double lat = pt->y();
102 if (!m_srs->positive_north()) lat *= -1.0;
103 if (lat < -M_PI_2 || lat > M_PI_2) {
104 m_detected_latitude = true;
106 return true; // Out of range coordinate detected.
107 }
108
109 return false; // Everything OK.
110 }
111};
112
113} // namespace gis
114
115#endif // SQL_GIS_COORDINATE_RANGE_VISITOR_H_INCLUDED
Definition: spatial_reference_system.h:53
virtual double angular_unit() const =0
virtual bool positive_north() const =0
virtual double from_radians(double d) const =0
Converts a coordinate value from radians to the SRS unit.
virtual bool is_cartesian() const =0
virtual bool positive_east() const =0
virtual double prime_meridian() const =0
A visitor that checks if coordinates are within range for a spatial reference system.
Definition: coordinate_range_visitor.h:42
Coordinate_range_visitor(const dd::Spatial_reference_system *srs)
Construct a new coordinate range visitor.
Definition: coordinate_range_visitor.h:57
bool m_detected_latitude
Whether an out of range latitude value has been encountered.
Definition: coordinate_range_visitor.h:49
bool m_detected_longitude
Whether an out of range longitude value has been encountered.
Definition: coordinate_range_visitor.h:47
bool visit_enter(Geometry *) override
Enters a compound geometry.
Definition: coordinate_range_visitor.h:81
double coordinate_value() const
Get the coordinate value that is out of range.
Definition: coordinate_range_visitor.h:78
bool latitude_out_of_range() const
Check if the visitor has detected any out of range latitude values.
Definition: coordinate_range_visitor.h:73
double m_coordinate
The coordinate value that is out of range, in SRS unit.
Definition: coordinate_range_visitor.h:51
const dd::Spatial_reference_system * m_srs
Spatial reference system of the geometry.
Definition: coordinate_range_visitor.h:45
bool visit(Point *pt) override
Definition: coordinate_range_visitor.h:89
bool longitude_out_of_range() const
Check if the visitor has detected any out of range longitude values.
Definition: coordinate_range_visitor.h:67
Abstract superclass for all geometric objects.
Definition: geometries.h:100
A visitor that implements the entire interface and does nothing.
Definition: geometry_visitor.h:122
bool visit_enter(Geometry *) override
Enters a compound geometry.
Definition: geometry_visitor.h:124
bool visit(Geometry *) override
Visits a geometry.
Definition: geometry_visitor.h:159
A 2d point.
Definition: geometries.h:150
double x() const
Gets the first coordinate value.
Definition: geometries.cc:46
double y() const
Gets the second coordinate value.
Definition: geometries.cc:48
The geometries implement a hierarchical visitor pattern.
Definition: area.cc:47