Libosmium  2.4.1
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
location.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_OSM_LOCATION_HPP
2 #define OSMIUM_OSM_LOCATION_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 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 <cmath>
37 #include <cstdint>
38 #include <iosfwd>
39 #include <stdexcept>
40 #include <string>
41 
42 #include <iostream>
43 
45 #include <osmium/util/double.hpp>
46 
47 namespace osmium {
48 
53  struct invalid_location : public std::range_error {
54 
55  invalid_location(const std::string& what) :
56  std::range_error(what) {
57  }
58 
59  invalid_location(const char* what) :
60  std::range_error(what) {
61  }
62 
63  }; // struct invalid_location
64 
79  class Location {
80 
81  int32_t m_x;
82  int32_t m_y;
83 
84  public:
85 
86  // this value is used for a coordinate to mark it as undefined
87  // MSVC doesn't declare std::numeric_limits<int32_t>::max() as
88  // constexpr, so we hard code this for the time being.
89  // static constexpr int32_t undefined_coordinate = std::numeric_limits<int32_t>::max();
90  static constexpr int32_t undefined_coordinate = 2147483647;
91 
92  static constexpr int coordinate_precision = 10000000;
93 
94  static int32_t double_to_fix(const double c) noexcept {
95  return static_cast<int32_t>(std::round(c * coordinate_precision));
96  }
97 
98  static OSMIUM_CONSTEXPR double fix_to_double(const int32_t c) noexcept {
99  return static_cast<double>(c) / coordinate_precision;
100  }
101 
105  explicit constexpr Location() noexcept :
106  m_x(undefined_coordinate),
107  m_y(undefined_coordinate) {
108  }
109 
115  constexpr Location(const int32_t x, const int32_t y) noexcept :
116  m_x(x),
117  m_y(y) {
118  }
119 
125  constexpr Location(const int64_t x, const int64_t y) noexcept :
126  m_x(static_cast<int32_t>(x)),
127  m_y(static_cast<int32_t>(y)) {
128  }
129 
133  Location(const double lon, const double lat) :
134  m_x(double_to_fix(lon)),
135  m_y(double_to_fix(lat)) {
136  }
137 
138  Location(const Location&) = default;
139  Location(Location&&) = default;
140  Location& operator=(const Location&) = default;
141  Location& operator=(Location&&) = default;
142  ~Location() = default;
143 
148  explicit constexpr operator bool() const noexcept {
149  return m_x != undefined_coordinate && m_y != undefined_coordinate;
150  }
151 
156  constexpr bool valid() const noexcept {
157  return m_x >= -180 * coordinate_precision
158  && m_x <= 180 * coordinate_precision
159  && m_y >= -90 * coordinate_precision
160  && m_y <= 90 * coordinate_precision;
161  }
162 
163  constexpr int32_t x() const noexcept {
164  return m_x;
165  }
166 
167  constexpr int32_t y() const noexcept {
168  return m_y;
169  }
170 
171  Location& set_x(const int32_t x) noexcept {
172  m_x = x;
173  return *this;
174  }
175 
176  Location& set_y(const int32_t y) noexcept {
177  m_y = y;
178  return *this;
179  }
180 
186  double lon() const {
187  if (!valid()) {
188  throw osmium::invalid_location("invalid location");
189  }
190  return fix_to_double(m_x);
191  }
192 
196  double lon_without_check() const {
197  return fix_to_double(m_x);
198  }
199 
205  double lat() const {
206  if (!valid()) {
207  throw osmium::invalid_location("invalid location");
208  }
209  return fix_to_double(m_y);
210  }
211 
215  double lat_without_check() const {
216  return fix_to_double(m_y);
217  }
218 
219  Location& set_lon(double lon) noexcept {
220  m_x = double_to_fix(lon);
221  return *this;
222  }
223 
224  Location& set_lat(double lat) noexcept {
225  m_y = double_to_fix(lat);
226  return *this;
227  }
228 
229  template <typename T>
230  T as_string(T iterator, const char separator) const {
231  iterator = osmium::util::double2string(iterator, lon(), 7);
232  *iterator++ = separator;
233  return osmium::util::double2string(iterator, lat(), 7);
234  }
235 
236  }; // class Location
237 
241  inline OSMIUM_CONSTEXPR bool operator==(const Location& lhs, const Location& rhs) noexcept {
242  return lhs.x() == rhs.x() && lhs.y() == rhs.y();
243  }
244 
245  inline OSMIUM_CONSTEXPR bool operator!=(const Location& lhs, const Location& rhs) noexcept {
246  return ! (lhs == rhs);
247  }
248 
254  inline OSMIUM_CONSTEXPR bool operator<(const Location& lhs, const Location& rhs) noexcept {
255  return (lhs.x() == rhs.x() && lhs.y() < rhs.y()) || lhs.x() < rhs.x();
256  }
257 
258  inline OSMIUM_CONSTEXPR bool operator>(const Location& lhs, const Location& rhs) noexcept {
259  return rhs < lhs;
260  }
261 
262  inline OSMIUM_CONSTEXPR bool operator<=(const Location& lhs, const Location& rhs) noexcept {
263  return ! (rhs < lhs);
264  }
265 
266  inline OSMIUM_CONSTEXPR bool operator>=(const Location& lhs, const Location& rhs) noexcept {
267  return ! (lhs < rhs);
268  }
269 
273  template <typename TChar, typename TTraits>
274  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const osmium::Location& location) {
275  if (location) {
276  out << '(' << location.lon() << ',' << location.lat() << ')';
277  } else {
278  out << "(undefined,undefined)";
279  }
280  return out;
281  }
282 
283 } // namespace osmium
284 
285 #endif // OSMIUM_OSM_LOCATION_HPP
double lat_without_check() const
Definition: location.hpp:215
static int32_t double_to_fix(const double c) noexcept
Definition: location.hpp:94
Location & operator=(const Location &)=default
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:324
~Location()=default
Location & set_lon(double lon) noexcept
Definition: location.hpp:219
Definition: reader_iterator.hpp:39
constexpr Location(const int64_t x, const int64_t y) noexcept
Definition: location.hpp:125
constexpr Location(const int32_t x, const int32_t y) noexcept
Definition: location.hpp:115
T double2string(T iterator, double value, int precision)
Definition: double.hpp:59
static OSMIUM_CONSTEXPR double fix_to_double(const int32_t c) noexcept
Definition: location.hpp:98
constexpr bool valid() const noexcept
Definition: location.hpp:156
OSMIUM_CONSTEXPR bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:219
double lat() const
Definition: location.hpp:205
static constexpr int coordinate_precision
Definition: location.hpp:92
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:316
double lon_without_check() const
Definition: location.hpp:196
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
Definition: location.hpp:53
int32_t m_y
Definition: location.hpp:82
constexpr int32_t y() const noexcept
Definition: location.hpp:167
invalid_location(const std::string &what)
Definition: location.hpp:55
T as_string(T iterator, const char separator) const
Definition: location.hpp:230
Location & set_lat(double lat) noexcept
Definition: location.hpp:224
Location & set_y(const int32_t y) noexcept
Definition: location.hpp:176
Definition: location.hpp:79
int32_t m_x
Definition: location.hpp:81
Location & set_x(const int32_t x) noexcept
Definition: location.hpp:171
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:328
double lon() const
Definition: location.hpp:186
#define OSMIUM_CONSTEXPR
Definition: compatibility.hpp:43
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:320
invalid_location(const char *what)
Definition: location.hpp:59
static constexpr int32_t undefined_coordinate
Definition: location.hpp:90
constexpr int32_t x() const noexcept
Definition: location.hpp:163
Location(const double lon, const double lat)
Definition: location.hpp:133
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:309
constexpr Location() noexcept
Definition: location.hpp:105