Libosmium  2.19.0
Fast and flexible C++ library for working with OpenStreetMap data
coordinates.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_GEOM_COORDINATES_HPP
2 #define OSMIUM_GEOM_COORDINATES_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <osmium/osm/location.hpp>
37 #include <osmium/util/double.hpp>
38 
39 #include <cmath>
40 #include <iosfwd>
41 #include <limits>
42 #include <string>
43 
44 namespace osmium {
45 
46  namespace geom {
47 
48  struct Coordinates {
49 
50  double x;
51  double y;
52 
56  Coordinates() noexcept :
57  x(std::numeric_limits<double>::quiet_NaN()),
58  y(std::numeric_limits<double>::quiet_NaN()) {
59  }
60 
65  explicit Coordinates(double cx, double cy) noexcept :
66  x(cx),
67  y(cy) {
68  }
69 
77  // cppcheck-suppress noExplicitConstructor
78  Coordinates(const osmium::Location& location) : // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
79  x(location.lon()),
80  y(location.lat()) {
81  }
82 
86  bool valid() const noexcept {
87  return !std::isnan(x) && !std::isnan(y);
88  }
89 
100  void append_to_string(std::string& s, const char infix, int precision) const {
101  if (valid()) {
102  osmium::double2string(s, x, precision);
103  s += infix;
104  osmium::double2string(s, y, precision);
105  } else {
106  s.append("invalid");
107  }
108  }
109 
122  void append_to_string(std::string& s, const char prefix, const char infix, const char suffix, int precision) const {
123  s += prefix;
124  append_to_string(s, infix, precision);
125  s += suffix;
126  }
127 
128  }; // struct coordinates
129 
139  inline bool operator==(const Coordinates& lhs, const Coordinates& rhs) noexcept {
140  if (!lhs.valid() && !rhs.valid()) {
141  return true;
142  }
143 #pragma GCC diagnostic push
144 #pragma GCC diagnostic ignored "-Wfloat-equal"
145  return lhs.x == rhs.x && lhs.y == rhs.y;
146 #pragma GCC diagnostic pop
147  }
148 
149  inline bool operator!=(const Coordinates& lhs, const Coordinates& rhs) noexcept {
150  return !(lhs == rhs);
151  }
152 
153  template <typename TChar, typename TTraits>
154  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const Coordinates& c) {
155  return out << '(' << c.x << ',' << c.y << ')';
156  }
157 
158  } // namespace geom
159 
160 } // namespace osmium
161 
162 #endif // OSMIUM_GEOM_COORDINATES_HPP
Definition: location.hpp:271
bool operator!=(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition: coordinates.hpp:149
bool operator==(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition: coordinates.hpp:139
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const Coordinates &c)
Definition: coordinates.hpp:154
T double2string(T iterator, double value, int precision)
Definition: double.hpp:56
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:555
Definition: coordinates.hpp:48
void append_to_string(std::string &s, const char prefix, const char infix, const char suffix, int precision) const
Definition: coordinates.hpp:122
bool valid() const noexcept
Definition: coordinates.hpp:86
double y
Definition: coordinates.hpp:51
Coordinates(double cx, double cy) noexcept
Definition: coordinates.hpp:65
void append_to_string(std::string &s, const char infix, int precision) const
Definition: coordinates.hpp:100
double x
Definition: coordinates.hpp:50
Coordinates(const osmium::Location &location)
Definition: coordinates.hpp:78
Coordinates() noexcept
Definition: coordinates.hpp:56