1 #ifndef OSMIUM_OSM_LOCATION_HPP 2 #define OSMIUM_OSM_LOCATION_HPP 60 std::range_error(what) {
64 std::range_error(what) {
71 constexpr
const int coordinate_precision = 10000000;
77 inline int32_t string_to_location_coordinate_fallback(
const char* str) {
79 std::istringstream ss{str};
80 ss.imbue(std::locale(
"C"));
81 ss >> std::noskipws >> value;
83 if (ss.fail() || !ss.eof() || ss.bad() || value > 215.0 || value < -215.0) {
84 throw invalid_location{std::string{
"wrong format for coordinate: '"} + str +
"'"};
87 return std::round(value * coordinate_precision);
92 inline int32_t string_to_location_coordinate(
const char* str) {
93 const char* full = str;
97 if (*str ==
'e' || *str ==
'E') {
98 return string_to_location_coordinate_fallback(full);
116 if (*str >=
'0' && *str <=
'9') {
124 if (*str >=
'0' && *str <=
'9') {
125 result = result * 10 + *str -
'0';
129 if (*str >=
'0' && *str <=
'9') {
130 result = result * 10 + *str -
'0';
145 for (; scale > 0 && *str >=
'0' && *str <=
'9'; --scale, ++str) {
146 result = result * 10 + (*str -
'0');
150 if (scale == 0 && *str >=
'5' && *str <=
'9') {
156 while (*str >=
'0' && *str <=
'9') {
167 for (; scale > 0; --scale) {
171 return result * sign;
175 throw invalid_location{std::string{
"wrong format for coordinate: '"} + full +
"'"};
179 template <
typename T>
180 inline T append_location_coordinate_to_string(T iterator, int32_t value) {
192 *t++ = char(v % 10) +
'0';
201 if (value >= coordinate_precision) {
202 if (value >= 10 * coordinate_precision) {
203 if (value >= 100 * coordinate_precision) {
214 const char* tn = temp;
215 while (tn < t && *tn ==
'0') {
257 static constexpr int32_t undefined_coordinate = 2147483647;
260 return static_cast<int32_t
>(std::round(c * detail::coordinate_precision));
264 return static_cast<double>(c) / detail::coordinate_precision;
271 m_x(undefined_coordinate),
272 m_y(undefined_coordinate) {
280 constexpr
Location(
const int32_t x,
const int32_t y) noexcept :
290 constexpr
Location(
const int64_t x,
const int64_t y) noexcept :
291 m_x(static_cast<int32_t>(x)),
292 m_y(static_cast<int32_t>(y)) {
299 m_x(double_to_fix(lon)),
300 m_y(double_to_fix(lat)) {
313 explicit constexpr
operator bool() const noexcept {
314 return m_x != undefined_coordinate && m_y != undefined_coordinate;
321 constexpr
bool valid() const noexcept {
322 return m_x >= -180 * detail::coordinate_precision
323 && m_x <= 180 * detail::coordinate_precision
324 && m_y >= -90 * detail::coordinate_precision
325 && m_y <= 90 * detail::coordinate_precision;
328 constexpr int32_t
x() const noexcept {
332 constexpr int32_t
y() const noexcept {
355 return fix_to_double(m_x);
362 return fix_to_double(m_x);
374 return fix_to_double(m_y);
381 return fix_to_double(m_y);
385 m_x = double_to_fix(lon);
390 m_y = double_to_fix(lat);
395 m_x = detail::string_to_location_coordinate(str);
400 m_y = detail::string_to_location_coordinate(str);
404 template <
typename T>
406 iterator = detail::append_location_coordinate_to_string(iterator, x());
407 *iterator++ = separator;
408 return detail::append_location_coordinate_to_string(iterator, y());
411 template <
typename T>
412 T
as_string(T iterator,
const char separator =
',')
const {
416 return as_string_without_check(iterator, separator);
425 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
429 return ! (lhs == rhs);
438 return (lhs.x() == rhs.x() && lhs.y() < rhs.y()) || lhs.x() < rhs.x();
446 return ! (rhs < lhs);
450 return ! (lhs < rhs);
456 template <
typename TChar,
typename TTraits>
457 inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out,
const osmium::Location& location) {
460 location.
as_string(std::ostream_iterator<char>(out),
',');
463 out <<
"(undefined,undefined)";
472 return location.
x() ^ location.
y();
477 size_t h = location.
x();
479 return h ^ location.
y();
490 #pragma clang diagnostic push 491 #pragma clang diagnostic ignored "-Wmismatched-tags" 498 return osmium::detail::hash<sizeof(size_t)>(location);
502 #pragma clang diagnostic pop 507 #endif // OSMIUM_OSM_LOCATION_HPP double lat_without_check() const
Definition: location.hpp:380
static int32_t double_to_fix(const double c) noexcept
Definition: location.hpp:259
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:446
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:222
Location & set_lon(double lon) noexcept
Definition: location.hpp:384
Definition: reader_iterator.hpp:39
constexpr Location(const int64_t x, const int64_t y) noexcept
Definition: location.hpp:290
constexpr Location(const int32_t x, const int32_t y) noexcept
Definition: location.hpp:280
double lat() const
Definition: location.hpp:370
T as_string(T iterator, const char separator= ',') const
Definition: location.hpp:412
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:438
double lon_without_check() const
Definition: location.hpp:361
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
Definition: location.hpp:57
int32_t m_y
Definition: location.hpp:249
invalid_location(const std::string &what)
Definition: location.hpp:59
Location & set_lat(double lat) noexcept
Definition: location.hpp:389
Location & set_y(const int32_t y) noexcept
Definition: location.hpp:341
constexpr bool valid() const noexcept
Definition: location.hpp:321
Location & set_lat(const char *str) noexcept
Definition: location.hpp:399
Definition: location.hpp:246
int32_t m_x
Definition: location.hpp:248
Location & set_x(const int32_t x) noexcept
Definition: location.hpp:336
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:450
T as_string_without_check(T iterator, const char separator= ',') const
Definition: location.hpp:405
double lon() const
Definition: location.hpp:351
Location & set_lon(const char *str) noexcept
Definition: location.hpp:394
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:442
invalid_location(const char *what)
Definition: location.hpp:63
size_t operator()(const osmium::Location &location) const noexcept
Definition: location.hpp:497
constexpr int32_t x() const noexcept
Definition: location.hpp:328
static constexpr double fix_to_double(const int32_t c) noexcept
Definition: location.hpp:263
Location(const double lon, const double lat)
Definition: location.hpp:298
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:431
constexpr int32_t y() const noexcept
Definition: location.hpp:332
size_t result_type
Definition: location.hpp:496
constexpr Location() noexcept
Definition: location.hpp:270