MySQL  8.0.26
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, 2021, 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 also distributed 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 included with MySQL.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License, version 2.0, for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25 
26 #include <cmath> // M_PI, M_PI_2
27 
28 #include "sql/dd/types/spatial_reference_system.h" // dd::Spatial_reference_system
30 
31 namespace gis {
32 
33 /// A visitor that checks if coordinates are within range for a spatial
34 /// reference system.
35 ///
36 /// If a coordinate value is found to be out of range, the visitor returns
37 /// true. Otherwise, it retruns false.
38 ///
39 /// Checking stops on the first value found to be out of range. Cartesian
40 /// coordinates are always within range.
42  private:
43  /// Spatial reference system of the geometry
45  /// Whether an out of range longitude value has been encountered
47  /// Whether an out of range latitude value has been encountered
49  /// The coordinate value that is out of range, in SRS unit
50  double m_coordinate;
51 
52  public:
53  /// Construct a new coordinate range visitor.
54  ///
55  /// @param srs The spatial reference system of the geometry.
57  : m_srs(srs),
58  m_detected_longitude(false),
59  m_detected_latitude(false),
60  m_coordinate(0.0) {}
61 
62  /// Check if the visitor has detected any out of range longitude values.
63  ///
64  /// @retval true At least one out of range longitude value.
65  /// @retval false All longitude values are within range.
67 
68  /// Check if the visitor has detected any out of range latitude values.
69  ///
70  /// @retval true At least one out of range latitude value.
71  /// @retval false All latitude values are within range.
72  bool latitude_out_of_range() const { return m_detected_latitude; }
73 
74  /// Get the coordinate value that is out of range.
75  ///
76  /// @return The coordinate value in the SRS unit.
77  double coordinate_value() const { return m_coordinate; }
78 
80  bool visit_enter(Geometry *) override {
81  if (m_srs == nullptr || m_srs->is_cartesian())
82  return true; // Don't descend into each child.
83 
84  return false;
85  }
86 
87  using Nop_visitor::visit;
88  bool visit(Point *pt) override {
89  if (m_srs == nullptr || m_srs->is_cartesian())
90  return false; // Everything OK.
91 
92  double lon = pt->x() - m_srs->prime_meridian() * m_srs->angular_unit();
93  if (!m_srs->positive_east()) lon *= -1.0;
94  if (lon <= -M_PI || lon > M_PI) {
95  m_detected_longitude = true;
97  return true; // Out of range coordinate detected.
98  }
99 
100  double lat = pt->y();
101  if (!m_srs->positive_north()) lat *= -1.0;
102  if (lat < -M_PI_2 || lat > M_PI_2) {
103  m_detected_latitude = true;
105  return true; // Out of range coordinate detected.
106  }
107 
108  return false; // Everything OK.
109  }
110 };
111 
112 } // namespace gis
113 
114 #endif // SQL_GIS_COORDINATE_RANGE_VISITOR_H_INCLUDED
Definition: spatial_reference_system.h:52
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:41
Coordinate_range_visitor(const dd::Spatial_reference_system *srs)
Construct a new coordinate range visitor.
Definition: coordinate_range_visitor.h:56
bool m_detected_latitude
Whether an out of range latitude value has been encountered.
Definition: coordinate_range_visitor.h:48
bool m_detected_longitude
Whether an out of range longitude value has been encountered.
Definition: coordinate_range_visitor.h:46
bool visit_enter(Geometry *) override
Enters a compound geometry.
Definition: coordinate_range_visitor.h:80
double coordinate_value() const
Get the coordinate value that is out of range.
Definition: coordinate_range_visitor.h:77
bool latitude_out_of_range() const
Check if the visitor has detected any out of range latitude values.
Definition: coordinate_range_visitor.h:72
double m_coordinate
The coordinate value that is out of range, in SRS unit.
Definition: coordinate_range_visitor.h:50
const dd::Spatial_reference_system * m_srs
Spatial reference system of the geometry.
Definition: coordinate_range_visitor.h:44
bool visit(Point *pt) override
Definition: coordinate_range_visitor.h:88
bool longitude_out_of_range() const
Check if the visitor has detected any out of range longitude values.
Definition: coordinate_range_visitor.h:66
Abstract superclass for all geometric objects.
Definition: geometries.h:99
A visitor that implements the entire interface and does nothing.
Definition: geometry_visitor.h:121
bool visit_enter(Geometry *) override
Enters a compound geometry.
Definition: geometry_visitor.h:123
bool visit(Geometry *) override
Visits a geometry.
Definition: geometry_visitor.h:158
A 2d point.
Definition: geometries.h:149
double x() const
Gets the first coordinate value.
Definition: geometries.cc:45
double y() const
Gets the second coordinate value.
Definition: geometries.cc:47
The geometries implement a hierarchical visitor pattern.
Definition: area.cc:46