Libosmium
2.16.0
Fast and flexible C++ library for working with OpenStreetMap data
|
Go to the documentation of this file. 1 #ifndef OSMIUM_OSM_LOCATION_HPP
2 #define OSMIUM_OSM_LOCATION_HPP
56 std::range_error(what) {
60 std::range_error(what) {
68 coordinate_precision = 10000000
73 inline int32_t string_to_location_coordinate(
const char** data) {
74 const char* str = *data;
75 const char* full = str;
94 if (*str >=
'0' && *str <=
'9') {
98 throw invalid_location{std::string{
"wrong format for coordinate: '"} + full +
"'"};
102 while (*str >=
'0' && *str <= '9' && max_digits > 0) {
103 result = result * 10 + (*str -
'0');
108 if (max_digits == 0) {
109 throw invalid_location{std::string{
"wrong format for coordinate: '"} + full +
"'"};
114 if (*(str + 1) <
'0' || *(str + 1) >
'9') {
115 throw invalid_location{std::string{
"wrong format for coordinate: '"} + full +
"'"};
124 for (; scale > 0 && *str >=
'0' && *str <=
'9'; --scale, ++str) {
125 result = result * 10 + (*str -
'0');
130 while (*str >=
'0' && *str <= '9' && max_digits > 0) {
135 if (max_digits == 0) {
136 throw invalid_location{std::string{
"wrong format for coordinate: '"} + full +
"'"};
141 if (*str ==
'e' || *str ==
'E') {
154 if (*str >=
'0' && *str <=
'9') {
155 eresult = *str -
'0';
158 throw invalid_location{std::string{
"wrong format for coordinate: '"} + full +
"'"};
163 while (*str >=
'0' && *str <= '9' && max_digits > 0) {
164 eresult = eresult * 10 + (*str -
'0');
169 if (max_digits == 0) {
170 throw invalid_location{std::string{
"wrong format for coordinate: '"} + full +
"'"};
173 scale += eresult * esign;
177 for (; scale < 0 && result > 0; ++scale) {
181 for (; scale > 0; --scale) {
186 result = (result + 5) / 10 * sign;
188 if (result > std::numeric_limits<int32_t>::max() ||
189 result < std::numeric_limits<int32_t>::min()) {
190 throw invalid_location{std::string{
"wrong format for coordinate: '"} + full +
"'"};
194 return static_cast<int32_t
>(result);
198 template <
typename T>
199 inline T append_location_coordinate_to_string(T iterator, int32_t value) {
201 if (value == std::numeric_limits<int32_t>::min()) {
202 static const char minresult[] =
"-214.7483648";
203 return std::copy_n(minresult,
sizeof(minresult) - 1, iterator);
217 *t++ =
static_cast<char>(v % 10) +
'0';
221 while (t - temp < 7) {
226 if (value >= coordinate_precision) {
227 if (value >= 10 * coordinate_precision) {
228 if (value >= 100 * coordinate_precision) {
239 const char* tn = temp;
240 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)) {
338 explicit constexpr
operator bool() const noexcept {
348 constexpr
bool valid() const noexcept {
349 return m_x >= -180 * detail::coordinate_precision
350 &&
m_x <= 180 * detail::coordinate_precision
351 &&
m_y >= -90 * detail::coordinate_precision
352 &&
m_y <= 90 * detail::coordinate_precision;
373 constexpr int32_t
x() const noexcept {
377 constexpr int32_t
y() const noexcept {
440 const char** data = &str;
441 const auto value = detail::string_to_location_coordinate(data);
442 if (**data !=
'\0') {
443 throw invalid_location{std::string{
"characters after coordinate: '"} + *data +
"'"};
450 const char** data = &str;
451 const auto value = detail::string_to_location_coordinate(data);
452 if (**data !=
'\0') {
453 throw invalid_location{std::string{
"characters after coordinate: '"} + *data +
"'"};
460 m_x = detail::string_to_location_coordinate(str);
465 m_y = detail::string_to_location_coordinate(str);
469 template <
typename T>
471 iterator = detail::append_location_coordinate_to_string(iterator,
x());
472 *iterator++ = separator;
473 return detail::append_location_coordinate_to_string(iterator,
y());
476 template <
typename T>
477 T
as_string(T iterator,
const char separator =
',')
const {
490 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
494 return !(lhs == rhs);
503 return (lhs.x() == rhs.x() && lhs.y() < rhs.y()) || lhs.x() < rhs.x();
521 template <
typename TChar,
typename TTraits>
525 location.
as_string(std::ostream_iterator<char>(out),
',');
528 out <<
"(undefined,undefined)";
537 return static_cast<uint32_t
>(location.x()) ^
static_cast<uint32_t
>(location.y());
542 uint64_t h = location.x();
544 return static_cast<size_t>(h ^
static_cast<uint64_t
>(location.y()));
555 #pragma clang diagnostic push
556 #pragma clang diagnostic ignored "-Wmismatched-tags"
563 return osmium::detail::hash<sizeof(size_t)>(location);
567 #pragma clang diagnostic pop
572 #endif // OSMIUM_OSM_LOCATION_HPP
T as_string(T iterator, const char separator=',') const
Definition: location.hpp:477
static int32_t double_to_fix(const double c) noexcept
Definition: location.hpp:286
int32_t m_x
Definition: location.hpp:273
Location & set_lon(double lon) noexcept
Definition: location.hpp:429
constexpr bool is_defined() const noexcept
Definition: location.hpp:360
constexpr Location() noexcept
Definition: location.hpp:297
Definition: location.hpp:53
Location & set_y(const int32_t y) noexcept
Definition: location.hpp:386
double lon_without_check() const
Definition: location.hpp:406
double lon() const
Definition: location.hpp:396
constexpr int32_t x() const noexcept
Definition: location.hpp:373
Location & set_lat(double lat) noexcept
Definition: location.hpp:434
constexpr int32_t y() const noexcept
Definition: location.hpp:377
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:461
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:465
constexpr bool valid() const noexcept
Definition: location.hpp:348
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:470
Location & set_x(const int32_t x) noexcept
Definition: location.hpp:381
double lat() const
Definition: location.hpp:415
invalid_location(const std::string &what)
Definition: location.hpp:55
@ undefined_coordinate
Definition: location.hpp:283
size_t operator()(const osmium::Location &location) const noexcept
Definition: location.hpp:562
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:212
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:453
Location(const double lon, const double lat)
Definition: location.hpp:326
Location & set_lon_partial(const char **str)
Definition: location.hpp:459
int32_t m_y
Definition: location.hpp:274
constexpr Location(const int64_t x, const int64_t y) noexcept
Definition: location.hpp:317
Definition: location.hpp:271
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const osmium::Box &box)
Definition: box.hpp:224
Definition: location.hpp:551
Location & set_lat(const char *str)
Definition: location.hpp:449
Location & set_lat_partial(const char **str)
Definition: location.hpp:464
Location & set_lon(const char *str)
Definition: location.hpp:439
static constexpr double fix_to_double(const int32_t c) noexcept
Definition: location.hpp:290
constexpr Location(const int32_t x, const int32_t y) noexcept
Definition: location.hpp:307
constexpr bool is_undefined() const noexcept
Definition: location.hpp:369
invalid_location(const char *what)
Definition: location.hpp:59
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:446
size_t result_type
Definition: location.hpp:561
double lat_without_check() const
Definition: location.hpp:425