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;
90 if (*str >=
'0' && *str <=
'9') {
98 while (*str >=
'0' && *str <= '9' && max_digits > 0) {
99 result = result * 10 + (*str -
'0');
104 if (max_digits == 0) {
113 for (; scale > 0 && *str >=
'0' && *str <=
'9'; --scale, ++str) {
114 result = result * 10 + (*str -
'0');
119 while (*str >=
'0' && *str <= '9' && max_digits > 0) {
124 if (max_digits == 0) {
130 if (*str ==
'e' || *str ==
'E') {
143 if (*str >=
'0' && *str <=
'9') {
144 eresult = *str -
'0';
152 while (*str >=
'0' && *str <= '9' && max_digits > 0) {
153 eresult = eresult * 10 + (*str -
'0');
158 if (max_digits == 0) {
162 scale += eresult * esign;
168 for (; scale > 0; --scale) {
172 result = (result + 5) / 10 * sign;
174 if (result > std::numeric_limits<int32_t>::max() ||
175 result < std::numeric_limits<int32_t>::min()) {
181 return static_cast<int32_t
>(result);
185 throw invalid_location{std::string{
"wrong format for coordinate: '"} + full +
"'"};
189 template <
typename T>
190 inline T append_location_coordinate_to_string(T iterator, int32_t value) {
202 *t++ = char(v % 10) +
'0';
211 if (value >= coordinate_precision) {
212 if (value >= 10 * coordinate_precision) {
213 if (value >= 100 * coordinate_precision) {
224 const char* tn = temp;
225 while (tn < t && *tn ==
'0') {
270 return static_cast<int32_t
>(std::round(c * detail::coordinate_precision));
274 return static_cast<double>(c) / detail::coordinate_precision;
281 m_x(undefined_coordinate),
282 m_y(undefined_coordinate) {
290 constexpr
Location(
const int32_t
x,
const int32_t
y) noexcept :
300 constexpr
Location(
const int64_t
x,
const int64_t
y) noexcept :
301 m_x(static_cast<int32_t>(
x)),
302 m_y(static_cast<int32_t>(
y)) {
323 explicit constexpr
operator bool() const noexcept {
331 constexpr
bool valid() const noexcept {
332 return m_x >= -180 * detail::coordinate_precision
333 && m_x <= 180 * detail::coordinate_precision
334 && m_y >= -90 * detail::coordinate_precision
335 && m_y <= 90 * detail::coordinate_precision;
338 constexpr int32_t
x() const noexcept {
342 constexpr int32_t
y() const noexcept {
405 const char** data = &str;
406 m_x = detail::string_to_location_coordinate(data);
407 if (**data !=
'\0') {
408 throw invalid_location{std::string{
"characters after coordinate: '"} + *data +
"'"};
414 const char** data = &str;
415 m_y = detail::string_to_location_coordinate(data);
416 if (**data !=
'\0') {
417 throw invalid_location{std::string{
"characters after coordinate: '"} + *data +
"'"};
423 m_x = detail::string_to_location_coordinate(str);
428 m_y = detail::string_to_location_coordinate(str);
432 template <
typename T>
434 iterator = detail::append_location_coordinate_to_string(iterator,
x());
435 *iterator++ = separator;
436 return detail::append_location_coordinate_to_string(iterator,
y());
439 template <
typename T>
440 T
as_string(T iterator,
const char separator =
',')
const {
453 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
457 return ! (lhs == rhs);
466 return (lhs.x() == rhs.x() && lhs.y() < rhs.y()) || lhs.x() < rhs.x();
474 return ! (rhs < lhs);
478 return ! (lhs < rhs);
484 template <
typename TChar,
typename TTraits>
485 inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out,
const osmium::Location& location) {
488 location.
as_string(std::ostream_iterator<char>(out),
',');
491 out <<
"(undefined,undefined)";
500 return location.x() ^ location.y();
505 size_t h = location.x();
507 return h ^ location.y();
518 #pragma clang diagnostic push
519 #pragma clang diagnostic ignored "-Wmismatched-tags"
526 return osmium::detail::hash<sizeof(size_t)>(location);
530 #pragma clang diagnostic pop
535 #endif // OSMIUM_OSM_LOCATION_HPP
Location & set_lon(const char *str)
Definition: location.hpp:404
double lat_without_check() const
Definition: location.hpp:390
static int32_t double_to_fix(const double c) noexcept
Definition: location.hpp:269
Location & operator=(const Location &)=default
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:453
Location & set_lat_partial(const char **str)
Definition: location.hpp:427
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:221
Location & set_lon(double lon) noexcept
Definition: location.hpp:394
Definition: reader_iterator.hpp:39
constexpr Location(const int64_t x, const int64_t y) noexcept
Definition: location.hpp:300
constexpr Location(const int32_t x, const int32_t y) noexcept
Definition: location.hpp:290
constexpr bool valid() const noexcept
Definition: location.hpp:331
Location & set_lat(const char *str)
Definition: location.hpp:413
double lat() const
Definition: location.hpp:380
T as_string(T iterator, const char separator= ',') const
Definition: location.hpp:440
size_t operator()(const osmium::Location &location) const noexcept
Definition: location.hpp:525
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:445
double lon_without_check() const
Definition: location.hpp:371
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
Definition: location.hpp:52
int32_t m_y
Definition: location.hpp:259
constexpr int32_t y() const noexcept
Definition: location.hpp:342
invalid_location(const std::string &what)
Definition: location.hpp:54
Location & set_lat(double lat) noexcept
Definition: location.hpp:399
Location & set_y(const int32_t y) noexcept
Definition: location.hpp:351
Definition: location.hpp:256
int32_t m_x
Definition: location.hpp:258
Location & set_x(const int32_t x) noexcept
Definition: location.hpp:346
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:457
T as_string_without_check(T iterator, const char separator= ',') const
Definition: location.hpp:433
double lon() const
Definition: location.hpp:361
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:449
invalid_location(const char *what)
Definition: location.hpp:58
static constexpr int32_t undefined_coordinate
Definition: location.hpp:267
constexpr int32_t x() const noexcept
Definition: location.hpp:338
static constexpr double fix_to_double(const int32_t c) noexcept
Definition: location.hpp:273
Location & set_lon_partial(const char **str)
Definition: location.hpp:422
Location(const double lon, const double lat)
Definition: location.hpp:308
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:438
size_t result_type
Definition: location.hpp:524
constexpr Location() noexcept
Definition: location.hpp:280