1 #ifndef OSMIUM_OSM_LOCATION_HPP 2 #define OSMIUM_OSM_LOCATION_HPP 56 std::range_error(what) {
60 std::range_error(what) {
67 constexpr
const int coordinate_precision = 10000000;
71 inline int32_t string_to_location_coordinate(
const char** data) {
72 const char* str = *data;
73 const char* full = str;
92 if (*str >=
'0' && *str <=
'9') {
100 while (*str >=
'0' && *str <= '9' && max_digits > 0) {
101 result = result * 10 + (*str -
'0');
106 if (max_digits == 0) {
112 if (*(str + 1) <
'0' || *(str + 1) >
'9') {
122 for (; scale > 0 && *str >=
'0' && *str <=
'9'; --scale, ++str) {
123 result = result * 10 + (*str -
'0');
128 while (*str >=
'0' && *str <= '9' && max_digits > 0) {
133 if (max_digits == 0) {
139 if (*str ==
'e' || *str ==
'E') {
152 if (*str >=
'0' && *str <=
'9') {
153 eresult = *str -
'0';
161 while (*str >=
'0' && *str <= '9' && max_digits > 0) {
162 eresult = eresult * 10 + (*str -
'0');
167 if (max_digits == 0) {
171 scale += eresult * esign;
175 for (; scale < 0 && result > 0; ++scale) {
179 for (; scale > 0; --scale) {
184 result = (result + 5) / 10 * sign;
186 if (result > std::numeric_limits<int32_t>::max() ||
187 result < std::numeric_limits<int32_t>::min()) {
192 return static_cast<int32_t>(result);
196 throw invalid_location{std::string{
"wrong format for coordinate: '"} + full +
"'"};
200 template <
typename T>
201 inline T append_location_coordinate_to_string(T iterator, int32_t value) {
203 if (value == std::numeric_limits<int32_t>::min()) {
204 static const char minresult[] =
"-214.7483648";
205 return std::copy_n(minresult,
sizeof(minresult) - 1, iterator);
219 *t++ = char(v % 10) +
'0';
223 while (t - temp < 7) {
228 if (value >= coordinate_precision) {
229 if (value >= 10 * coordinate_precision) {
230 if (value >= 100 * coordinate_precision) {
241 const char* tn = temp;
242 while (tn < t && *tn ==
'0') {
287 return static_cast<int32_t>(std::round(c * detail::coordinate_precision));
291 return static_cast<double>(c) / detail::coordinate_precision;
307 constexpr
Location(
const int32_t
x,
const int32_t
y) noexcept :
317 constexpr
Location(
const int64_t
x,
const int64_t
y) noexcept :
318 m_x(static_cast<int32_t>(
x)),
319 m_y(static_cast<int32_t>(
y)) {
337 explicit constexpr
operator bool() const noexcept {
347 constexpr
bool valid() const noexcept {
348 return m_x >= -180 * detail::coordinate_precision
349 &&
m_x <= 180 * detail::coordinate_precision
350 &&
m_y >= -90 * detail::coordinate_precision
351 &&
m_y <= 90 * detail::coordinate_precision;
372 constexpr int32_t
x() const noexcept {
376 constexpr int32_t
y() const noexcept {
439 const char** data = &str;
440 const auto value = detail::string_to_location_coordinate(data);
441 if (**data !=
'\0') {
442 throw invalid_location{std::string{
"characters after coordinate: '"} + *data +
"'"};
449 const char** data = &str;
450 const auto value = detail::string_to_location_coordinate(data);
451 if (**data !=
'\0') {
452 throw invalid_location{std::string{
"characters after coordinate: '"} + *data +
"'"};
459 m_x = detail::string_to_location_coordinate(str);
464 m_y = detail::string_to_location_coordinate(str);
468 template <
typename T>
470 iterator = detail::append_location_coordinate_to_string(iterator,
x());
471 *iterator++ = separator;
472 return detail::append_location_coordinate_to_string(iterator,
y());
475 template <
typename T>
476 T
as_string(T iterator,
const char separator =
',')
const {
489 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
493 return !(lhs == rhs);
502 return (lhs.x() == rhs.x() && lhs.y() < rhs.y()) || lhs.x() < rhs.x();
520 template <
typename TChar,
typename TTraits>
524 location.
as_string(std::ostream_iterator<char>(out),
',');
527 out <<
"(undefined,undefined)";
536 return static_cast<uint32_t>(location.x()) ^ static_cast<uint32_t>(location.y());
541 uint64_t h = location.x();
543 return static_cast<size_t>(h ^ static_cast<uint64_t>(location.y()));
554 #pragma clang diagnostic push 555 #pragma clang diagnostic ignored "-Wmismatched-tags" 562 return osmium::detail::hash<sizeof(size_t)>(location);
566 #pragma clang diagnostic pop 571 #endif // OSMIUM_OSM_LOCATION_HPP Location & set_lon(const char *str)
Definition: location.hpp:438
static int32_t double_to_fix(const double c) noexcept
Definition: location.hpp:286
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const osmium::Box &box)
Definition: box.hpp:224
double lon() const
Definition: location.hpp:395
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:461
Location & set_lat_partial(const char **str)
Definition: location.hpp:463
double lat_without_check() const
Definition: location.hpp:424
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:212
Location & set_lon(double lon) noexcept
Definition: location.hpp:428
double lat() const
Definition: location.hpp:414
Definition: location.hpp:550
constexpr Location(const int64_t x, const int64_t y) noexcept
Definition: location.hpp:317
constexpr Location(const int32_t x, const int32_t y) noexcept
Definition: location.hpp:307
constexpr bool valid() const noexcept
Definition: location.hpp:347
Location & set_lat(const char *str)
Definition: location.hpp:448
size_t operator()(const osmium::Location &location) const noexcept
Definition: location.hpp:561
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:453
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:53
int32_t m_y
Definition: location.hpp:276
constexpr int32_t y() const noexcept
Definition: location.hpp:376
invalid_location(const std::string &what)
Definition: location.hpp:55
Location & set_lat(double lat) noexcept
Definition: location.hpp:433
Location & set_y(const int32_t y) noexcept
Definition: location.hpp:385
Definition: location.hpp:273
int32_t m_x
Definition: location.hpp:275
Location & set_x(const int32_t x) noexcept
Definition: location.hpp:380
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:465
T as_string(T iterator, const char separator=',') const
Definition: location.hpp:476
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:469
invalid_location(const char *what)
Definition: location.hpp:59
static constexpr int32_t undefined_coordinate
Definition: location.hpp:284
constexpr int32_t x() const noexcept
Definition: location.hpp:372
double lon_without_check() const
Definition: location.hpp:405
static constexpr double fix_to_double(const int32_t c) noexcept
Definition: location.hpp:290
constexpr bool is_defined() const noexcept
Definition: location.hpp:359
Location & set_lon_partial(const char **str)
Definition: location.hpp:458
Location(const double lon, const double lat)
Definition: location.hpp:325
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:446
constexpr bool is_undefined() const noexcept
Definition: location.hpp:368
size_t result_type
Definition: location.hpp:560
constexpr Location() noexcept
Definition: location.hpp:297