1 #ifndef OSMIUM_OSM_LOCATION_HPP 2 #define OSMIUM_OSM_LOCATION_HPP 55 std::range_error(what) {
59 std::range_error(what) {
66 constexpr
const int coordinate_precision = 10000000;
70 inline int32_t string_to_location_coordinate(
const char** data) {
71 const char* str = *data;
72 const char* full = str;
91 if (*str >=
'0' && *str <=
'9') {
99 while (*str >=
'0' && *str <= '9' && max_digits > 0) {
100 result = result * 10 + (*str -
'0');
105 if (max_digits == 0) {
111 if (*(str + 1) <
'0' || *(str + 1) >
'9') {
121 for (; scale > 0 && *str >=
'0' && *str <=
'9'; --scale, ++str) {
122 result = result * 10 + (*str -
'0');
127 while (*str >=
'0' && *str <= '9' && max_digits > 0) {
132 if (max_digits == 0) {
138 if (*str ==
'e' || *str ==
'E') {
151 if (*str >=
'0' && *str <=
'9') {
152 eresult = *str -
'0';
160 while (*str >=
'0' && *str <= '9' && max_digits > 0) {
161 eresult = eresult * 10 + (*str -
'0');
166 if (max_digits == 0) {
170 scale += eresult * esign;
174 for (; scale < 0 && result > 0; ++scale) {
178 for (; scale > 0; --scale) {
183 result = (result + 5) / 10 * sign;
185 if (result > std::numeric_limits<int32_t>::max() ||
186 result < std::numeric_limits<int32_t>::min()) {
191 return static_cast<int32_t
>(result);
195 throw invalid_location{std::string{
"wrong format for coordinate: '"} + full +
"'"};
199 template <
typename T>
200 inline T append_location_coordinate_to_string(T iterator, int32_t value) {
212 *t++ = char(v % 10) +
'0';
221 if (value >= coordinate_precision) {
222 if (value >= 10 * coordinate_precision) {
223 if (value >= 100 * coordinate_precision) {
234 const char* tn = temp;
235 while (tn < t && *tn ==
'0') {
277 static constexpr int32_t undefined_coordinate = 2147483647;
280 return static_cast<int32_t
>(std::round(c * detail::coordinate_precision));
284 return static_cast<double>(c) / detail::coordinate_precision;
291 m_x(undefined_coordinate),
292 m_y(undefined_coordinate) {
300 constexpr
Location(
const int32_t x,
const int32_t y) noexcept :
310 constexpr
Location(
const int64_t x,
const int64_t y) noexcept :
311 m_x(static_cast<int32_t>(x)),
312 m_y(static_cast<int32_t>(y)) {
319 m_x(double_to_fix(lon)),
320 m_y(double_to_fix(lat)) {
333 explicit constexpr
operator bool() const noexcept {
334 return m_x != undefined_coordinate && m_y != undefined_coordinate;
341 constexpr
bool valid() const noexcept {
342 return m_x >= -180 * detail::coordinate_precision
343 && m_x <= 180 * detail::coordinate_precision
344 && m_y >= -90 * detail::coordinate_precision
345 && m_y <= 90 * detail::coordinate_precision;
348 constexpr int32_t
x() const noexcept {
352 constexpr int32_t
y() const noexcept {
375 return fix_to_double(m_x);
382 return fix_to_double(m_x);
394 return fix_to_double(m_y);
401 return fix_to_double(m_y);
405 m_x = double_to_fix(lon);
410 m_y = double_to_fix(lat);
415 const char** data = &str;
416 m_x = detail::string_to_location_coordinate(data);
417 if (**data !=
'\0') {
418 throw invalid_location{std::string{
"characters after coordinate: '"} + *data +
"'"};
424 const char** data = &str;
425 m_y = detail::string_to_location_coordinate(data);
426 if (**data !=
'\0') {
427 throw invalid_location{std::string{
"characters after coordinate: '"} + *data +
"'"};
433 m_x = detail::string_to_location_coordinate(str);
438 m_y = detail::string_to_location_coordinate(str);
442 template <
typename T>
444 iterator = detail::append_location_coordinate_to_string(iterator, x());
445 *iterator++ = separator;
446 return detail::append_location_coordinate_to_string(iterator, y());
449 template <
typename T>
450 T
as_string(T iterator,
const char separator =
',')
const {
454 return as_string_without_check(iterator, separator);
463 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
467 return ! (lhs == rhs);
476 return (lhs.x() == rhs.x() && lhs.y() < rhs.y()) || lhs.x() < rhs.x();
484 return ! (rhs < lhs);
488 return ! (lhs < rhs);
494 template <
typename TChar,
typename TTraits>
495 inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out,
const osmium::Location& location) {
498 location.
as_string(std::ostream_iterator<char>(out),
',');
501 out <<
"(undefined,undefined)";
510 return location.
x() ^ location.
y();
515 uint64_t h = location.
x();
517 return static_cast<size_t>(h ^ location.
y());
528 #pragma clang diagnostic push 529 #pragma clang diagnostic ignored "-Wmismatched-tags" 536 return osmium::detail::hash<sizeof(size_t)>(location);
540 #pragma clang diagnostic pop 545 #endif // OSMIUM_OSM_LOCATION_HPP Location & set_lon(const char *str)
Definition: location.hpp:414
static int32_t double_to_fix(const double c) noexcept
Definition: location.hpp:279
double lon() const
Definition: location.hpp:371
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:455
Location & set_lat_partial(const char **str)
Definition: location.hpp:437
double lat_without_check() const
Definition: location.hpp:400
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:221
Location & set_lon(double lon) noexcept
Definition: location.hpp:404
double lat() const
Definition: location.hpp:390
Definition: reader_iterator.hpp:39
constexpr Location(const int64_t x, const int64_t y) noexcept
Definition: location.hpp:310
constexpr Location(const int32_t x, const int32_t y) noexcept
Definition: location.hpp:300
constexpr bool valid() const noexcept
Definition: location.hpp:341
Location & set_lat(const char *str)
Definition: location.hpp:423
size_t operator()(const osmium::Location &location) const noexcept
Definition: location.hpp:535
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:447
Namespace for everything in the Osmium library.
Definition: assembler.hpp:63
Definition: location.hpp:52
int32_t m_y
Definition: location.hpp:269
constexpr int32_t y() const noexcept
Definition: location.hpp:352
invalid_location(const std::string &what)
Definition: location.hpp:54
Location & set_lat(double lat) noexcept
Definition: location.hpp:409
Location & set_y(const int32_t y) noexcept
Definition: location.hpp:361
Definition: location.hpp:266
int32_t m_x
Definition: location.hpp:268
Location & set_x(const int32_t x) noexcept
Definition: location.hpp:356
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:459
T as_string(T iterator, const char separator=',') const
Definition: location.hpp:450
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:451
T as_string_without_check(T iterator, const char separator=',') const
Definition: location.hpp:443
invalid_location(const char *what)
Definition: location.hpp:58
constexpr int32_t x() const noexcept
Definition: location.hpp:348
double lon_without_check() const
Definition: location.hpp:381
static constexpr double fix_to_double(const int32_t c) noexcept
Definition: location.hpp:283
Location & set_lon_partial(const char **str)
Definition: location.hpp:432
Location(const double lon, const double lat)
Definition: location.hpp:318
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:440
size_t result_type
Definition: location.hpp:534
constexpr Location() noexcept
Definition: location.hpp:290