Libosmium  2.15.2
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-2019 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  Coordinates(const osmium::Location& location) : // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
78  x(location.lon()),
79  y(location.lat()) {
80  }
81 
85  bool valid() const noexcept {
86  return !std::isnan(x) && !std::isnan(y);
87  }
88 
99  void append_to_string(std::string& s, const char infix, int precision) const {
100  if (valid()) {
101  osmium::double2string(s, x, precision);
102  s += infix;
103  osmium::double2string(s, y, precision);
104  } else {
105  s.append("invalid");
106  }
107  }
108 
121  void append_to_string(std::string& s, const char prefix, const char infix, const char suffix, int precision) const {
122  s += prefix;
123  append_to_string(s, infix, precision);
124  s += suffix;
125  }
126 
127  }; // struct coordinates
128 
138  inline bool operator==(const Coordinates& lhs, const Coordinates& rhs) noexcept {
139  if (!lhs.valid() && !rhs.valid()) {
140  return true;
141  }
142 #pragma GCC diagnostic push
143 #pragma GCC diagnostic ignored "-Wfloat-equal"
144  return lhs.x == rhs.x && lhs.y == rhs.y;
145 #pragma GCC diagnostic pop
146  }
147 
148  inline bool operator!=(const Coordinates& lhs, const Coordinates& rhs) noexcept {
149  return !(lhs == rhs);
150  }
151 
152  template <typename TChar, typename TTraits>
153  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const Coordinates& c) {
154  return out << '(' << c.x << ',' << c.y << ')';
155  }
156 
157  } // namespace geom
158 
159 } // namespace osmium
160 
161 #endif // OSMIUM_GEOM_COORDINATES_HPP
double y
Definition: coordinates.hpp:51
bool valid() const noexcept
Definition: coordinates.hpp:85
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const Coordinates &c)
Definition: coordinates.hpp:153
Definition: location.hpp:550
T double2string(T iterator, double value, int precision)
Definition: double.hpp:56
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
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:121
Definition: location.hpp:271
Coordinates() noexcept
Definition: coordinates.hpp:56
void append_to_string(std::string &s, const char infix, int precision) const
Definition: coordinates.hpp:99
bool operator!=(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition: coordinates.hpp:148
double x
Definition: coordinates.hpp:50
Coordinates(double cx, double cy) noexcept
Definition: coordinates.hpp:65
Coordinates(const osmium::Location &location)
Definition: coordinates.hpp:77
bool operator==(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition: coordinates.hpp:138