40c4a48 Release v2.10.0 6addb2e Search for protozero also in the place where libosmium was found. 9c0d4bb Consistently use lhs and rhs as parameter names in operators etc. 6179759 Update change log. 894eb7d Explicitly use size_t to get no narrowing conversions. e549e73 Fix CMake config for index_lookup. ccf0bc2 Fix bug where some compilers deduce the wrong type. fc3507d Cleaned and documented index example and renamed to osmium_index_lookup. d3c3036 Rename serdump example to dump_internal and document it. 0e9822e Also forward set_uid_from_signed() function. 29ef95c Rename build_taglist function because it was to similar to build_tag_list. c088dd0 Move static constant out of class so clang will compile it. 606cdc4 Fix CMake build script: Path for finding libosmium. 65f91fe Removed unused typedef. 07174f5 Use condition_variable to tell producer when thread queue is not full. b35e957 Some code cleanup in XML parser. c703dff Fix problem with MemberMeta. 1ccbbef Refactoring CompressionFactory. 6561bd6 Use our own exception type for map factory errors. 56e31fa Throw not_found error directly instead of using helper function. 28230c3 Test empty value for node location store, reorganize tests. 2ba316c Reorganize and fix projection tests. f949485 Don't run quite as many tests with projections because they are slow. 1bad16d Add information about build environment to benchmark output. 99617bb Change proj_geojson benchmark into mercator benchmark. 553b946 Allow optional parameters on Reader in any order. dcc3d8f Factor out some common code. 004d8cd Fix forwarding constructor. 9702978 Fix metadata check. 6cfb6c4 Faster implementation of decode_dense_nodes without reading metdata. 4ba4638 Add additional read_metadata parameter to other Reader constructors. d005937 Optionally ignore metadata when reading file. 63961da Mark all CRC32 update functions as noexcept. cc4ca75 Refactor set_user() function to speed it up. 38d19dc Update comments with file sizes. OSM has grown... f7d0824 Add new benchmark that shows performance when main thread is busy. 25070dc Use const ref params. 88e8d96 Mark add_user() as deprecated. f58d9db Refactor some low-level collection code to clean up code. 4680def Add example showing how to create OSM data out of thin air. d42fd50 Add an example showing how tags in OSM files can be changed. 49bf5bc Add additional constructors to Builders taking a reference to parent. 7b91d63 Change Builder::add_item() to take a reference instead of pointer. 2957e48 Some cleanup of examples. d0b458d Calculate size of object at compile time. 3fbb6e7 Use explicit cast. 1851f3d Remove a test that depends on math details. f6a0802 Various cleanups of example programs. ba4921f Rename add_user() to set_user(). d7637c9 Various cleanups related to builders. 07827bc Fix add_user(). 9a5b395 Also refactor OSMObjectBuilder like ChangesetBuilder before. b1f423c Use call chaining on the builder. e49473d Get rid of ObjectBuilder class. 67d70b9 Refactor ChangesetBuilder::add_user(). 8199c33 Make ChangesetBuilder derive directly from Builder. 61d1b67 Simplify some code. d38467a Change derivation hierarchy of some builder classes. b52f8af Refactor Builder code. d012bfa Refactored some code setting attributes on objects using builder instead. 6a05f60 Also forward set_removed function from builders. 8d63b7d Return *this from Builder setter functions and test it. 72a1266 Update catch.hpp to newest version. 3424a74 Check GEOS version is <= 3.5. aee9f9d Cleanup test code. aef198c Improved asserts in Buffer. a98b9bf Code cleanup in tests. a150466 Use GDAL/OGR instead of GEOS to test our WKB implementation. b04a525 Refactor test. 39aa932 Refactored test_factory_with_projection so it works without GEOS. 648f43a Remove unused dependency on geos from tile test. f1748ae Add setters to Builder classes forwarding to underlying objects. 8166879 In debug mode check that Builders are correctly called and destructed. 1c4257e Call commit() on buffer only after all builders are destructed. 2618636 Add functions to check availability of relation members. b45a4d9 Mark RelationMember::set_ref() as deprecated. 7886771 Move "basic" and "buffer" tests to "osm" and "memory", respectively. b664685 Use functions instead of macros in location test for faster compile. b4929ac Add more tests for number parser. 02662a7 Merge pull request #171 from lonvia/fix-long-exponentials 5344a6c fix parsing of numbers in e-notiation with many post-comma digits 3aeaff3 Add some typedefs to NodeRefList and memory::Collection. e750665 Add iterators to IdSetSmall and add docs and tests to IdSet classes. 50ecb2a Add more features to IdSetDense, including unset and iterator. e3dec78 Make IdSet virtual base class with two implementations. 8ea0153 Use C array instead of std::array in IdSet and clear explicitly. 3ba9905 Deprecate osmium::index::BoolVector in favour of new IdSet. 453d1ca Add osmium::index::IdSet. c78254e Add function to (temporarily) disable the progress bar. 4d88a9f Better document osmium::io::Header class. 320e3af Look for protozero includes in CMake config. 838d25e Allow optional checking for libosmium version number in CMake config. 6ce60c1 Fix entity_bits static_assert() tests. f054731 Update change log. 77ac4c2 Make sleep duration for full queues much smaller. 7e39c01 Make some entity_bits functions constexpr. 69ea72f Fix ~ operator on entity_bits and more tests for entity bits. dafe4cf Update embedded Catch unit test header to version 1.5.7. a41c832 Fixed parsing of location coordinates starting with decimal dot. 6523bae README cosmetics. 229acac Add tests for some examples. f1e753d Merge pull request #163 from sebastic/executable-not-elf-or-script ccea2d5 Remove executable bit from .cpp files. af77fb4 Changelog formatting fixes. git-subtree-dir: third_party/libosmium git-subtree-split: 40c4a48f88d25edace6f0b9e079c306308c7760b
546 lines
16 KiB
C++
546 lines
16 KiB
C++
#ifndef OSMIUM_OSM_LOCATION_HPP
|
|
#define OSMIUM_OSM_LOCATION_HPP
|
|
|
|
/*
|
|
|
|
This file is part of Osmium (http://osmcode.org/libosmium).
|
|
|
|
Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
|
|
|
|
Boost Software License - Version 1.0 - August 17th, 2003
|
|
|
|
Permission is hereby granted, free of charge, to any person or organization
|
|
obtaining a copy of the software and accompanying documentation covered by
|
|
this license (the "Software") to use, reproduce, display, distribute,
|
|
execute, and transmit the Software, and to prepare derivative works of the
|
|
Software, and to permit third-parties to whom the Software is furnished to
|
|
do so, all subject to the following:
|
|
|
|
The copyright notices in the Software and this entire statement, including
|
|
the above license grant, this restriction and the following disclaimer,
|
|
must be included in all copies of the Software, in whole or in part, and
|
|
all derivative works of the Software, unless such copies or derivative
|
|
works are solely in the form of machine-executable object code generated by
|
|
a source language processor.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
|
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
|
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <functional>
|
|
#include <iosfwd>
|
|
#include <iterator>
|
|
#include <limits>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
namespace osmium {
|
|
|
|
/**
|
|
* Exception signaling an invalid location, ie a location
|
|
* outside the -180 to 180 and -90 to 90 degree range.
|
|
*/
|
|
struct invalid_location : public std::range_error {
|
|
|
|
explicit invalid_location(const std::string& what) :
|
|
std::range_error(what) {
|
|
}
|
|
|
|
explicit invalid_location(const char* what) :
|
|
std::range_error(what) {
|
|
}
|
|
|
|
}; // struct invalid_location
|
|
|
|
namespace detail {
|
|
|
|
constexpr const int coordinate_precision = 10000000;
|
|
|
|
// Convert string with a floating point number into integer suitable
|
|
// for use as coordinate in a Location.
|
|
inline int32_t string_to_location_coordinate(const char** data) {
|
|
const char* str = *data;
|
|
const char* full = str;
|
|
|
|
int64_t result = 0;
|
|
int sign = 1;
|
|
|
|
// one more than significant digits to allow rounding
|
|
int64_t scale = 8;
|
|
|
|
// paranoia check for maximum number of digits
|
|
int max_digits = 10;
|
|
|
|
// optional minus sign
|
|
if (*str == '-') {
|
|
sign = -1;
|
|
++str;
|
|
}
|
|
|
|
if (*str != '.') {
|
|
// there has to be at least one digit
|
|
if (*str >= '0' && *str <= '9') {
|
|
result = *str - '0';
|
|
++str;
|
|
} else {
|
|
goto error;
|
|
}
|
|
|
|
// optional additional digits before decimal point
|
|
while (*str >= '0' && *str <= '9' && max_digits > 0) {
|
|
result = result * 10 + (*str - '0');
|
|
++str;
|
|
--max_digits;
|
|
}
|
|
|
|
if (max_digits == 0) {
|
|
goto error;
|
|
}
|
|
} else {
|
|
// need at least one digit after decimal dot if there was no
|
|
// digit before decimal dot
|
|
if (*(str + 1) < '0' || *(str + 1) > '9') {
|
|
goto error;
|
|
}
|
|
}
|
|
|
|
// optional decimal point
|
|
if (*str == '.') {
|
|
++str;
|
|
|
|
// read significant digits
|
|
for (; scale > 0 && *str >= '0' && *str <= '9'; --scale, ++str) {
|
|
result = result * 10 + (*str - '0');
|
|
}
|
|
|
|
// ignore non-significant digits
|
|
max_digits = 20;
|
|
while (*str >= '0' && *str <= '9' && max_digits > 0) {
|
|
++str;
|
|
--max_digits;
|
|
}
|
|
|
|
if (max_digits == 0) {
|
|
goto error;
|
|
}
|
|
}
|
|
|
|
// optional exponent in scientific notation
|
|
if (*str == 'e' || *str == 'E') {
|
|
++str;
|
|
|
|
int esign = 1;
|
|
// optional minus sign
|
|
if (*str == '-') {
|
|
esign = -1;
|
|
++str;
|
|
}
|
|
|
|
int64_t eresult = 0;
|
|
|
|
// there has to be at least one digit in exponent
|
|
if (*str >= '0' && *str <= '9') {
|
|
eresult = *str - '0';
|
|
++str;
|
|
} else {
|
|
goto error;
|
|
}
|
|
|
|
// optional additional digits in exponent
|
|
max_digits = 5;
|
|
while (*str >= '0' && *str <= '9' && max_digits > 0) {
|
|
eresult = eresult * 10 + (*str - '0');
|
|
++str;
|
|
--max_digits;
|
|
}
|
|
|
|
if (max_digits == 0) {
|
|
goto error;
|
|
}
|
|
|
|
scale += eresult * esign;
|
|
}
|
|
|
|
if (scale < 0) {
|
|
for (; scale < 0 && result > 0; ++scale) {
|
|
result /= 10;
|
|
}
|
|
} else {
|
|
for (; scale > 0; --scale) {
|
|
result *= 10;
|
|
}
|
|
}
|
|
|
|
result = (result + 5) / 10 * sign;
|
|
|
|
if (result > std::numeric_limits<int32_t>::max() ||
|
|
result < std::numeric_limits<int32_t>::min()) {
|
|
goto error;
|
|
}
|
|
|
|
*data = str;
|
|
return static_cast<int32_t>(result);
|
|
|
|
error:
|
|
|
|
throw invalid_location{std::string{"wrong format for coordinate: '"} + full + "'"};
|
|
}
|
|
|
|
// Convert integer as used by location for coordinates into a string.
|
|
template <typename T>
|
|
inline T append_location_coordinate_to_string(T iterator, int32_t value) {
|
|
// handle negative values
|
|
if (value < 0) {
|
|
*iterator++ = '-';
|
|
value = -value;
|
|
}
|
|
|
|
// write digits into temporary buffer
|
|
int32_t v = value;
|
|
char temp[10];
|
|
char* t = temp;
|
|
do {
|
|
*t++ = char(v % 10) + '0';
|
|
v /= 10;
|
|
} while (v != 0);
|
|
|
|
while (t-temp < 7) {
|
|
*t++ = '0';
|
|
}
|
|
|
|
// write out digits before decimal point
|
|
if (value >= coordinate_precision) {
|
|
if (value >= 10 * coordinate_precision) {
|
|
if (value >= 100 * coordinate_precision) {
|
|
*iterator++ = *--t;
|
|
}
|
|
*iterator++ = *--t;
|
|
}
|
|
*iterator++ = *--t;
|
|
} else {
|
|
*iterator++ = '0';
|
|
}
|
|
|
|
// remove trailing zeros
|
|
const char* tn = temp;
|
|
while (tn < t && *tn == '0') {
|
|
++tn;
|
|
}
|
|
|
|
// decimal point
|
|
if (t != tn) {
|
|
*iterator++ = '.';
|
|
while (t != tn) {
|
|
*iterator++ = *--t;
|
|
}
|
|
}
|
|
|
|
return iterator;
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
/**
|
|
* Locations define a place on earth.
|
|
*
|
|
* Locations are stored in 32 bit integers for the x and y
|
|
* coordinates, respectively. This gives you an accuracy of a few
|
|
* centimeters, good enough for OSM use. (The main OSM database
|
|
* uses the same scheme.)
|
|
*
|
|
* An undefined Location can be created by calling the constructor
|
|
* without parameters.
|
|
*
|
|
* Coordinates are never checked on whether they are inside bounds.
|
|
* Call valid() to check this.
|
|
*/
|
|
class Location {
|
|
|
|
int32_t m_x;
|
|
int32_t m_y;
|
|
|
|
public:
|
|
|
|
// this value is used for a coordinate to mark it as undefined
|
|
// MSVC doesn't declare std::numeric_limits<int32_t>::max() as
|
|
// constexpr, so we hard code this for the time being.
|
|
// static constexpr int32_t undefined_coordinate = std::numeric_limits<int32_t>::max();
|
|
static constexpr int32_t undefined_coordinate = 2147483647;
|
|
|
|
static int32_t double_to_fix(const double c) noexcept {
|
|
return static_cast<int32_t>(std::round(c * detail::coordinate_precision));
|
|
}
|
|
|
|
static constexpr double fix_to_double(const int32_t c) noexcept {
|
|
return static_cast<double>(c) / detail::coordinate_precision;
|
|
}
|
|
|
|
/**
|
|
* Create undefined Location.
|
|
*/
|
|
explicit constexpr Location() noexcept :
|
|
m_x(undefined_coordinate),
|
|
m_y(undefined_coordinate) {
|
|
}
|
|
|
|
/**
|
|
* Create Location with given x and y coordinates.
|
|
* Note that these coordinates are coordinate_precision
|
|
* times larger than the real coordinates.
|
|
*/
|
|
constexpr Location(const int32_t x, const int32_t y) noexcept :
|
|
m_x(x),
|
|
m_y(y) {
|
|
}
|
|
|
|
/**
|
|
* Create Location with given x and y coordinates.
|
|
* Note that these coordinates are coordinate_precision
|
|
* times larger than the real coordinates.
|
|
*/
|
|
constexpr Location(const int64_t x, const int64_t y) noexcept :
|
|
m_x(static_cast<int32_t>(x)),
|
|
m_y(static_cast<int32_t>(y)) {
|
|
}
|
|
|
|
/**
|
|
* Create Location with given longitude and latitude.
|
|
*/
|
|
Location(const double lon, const double lat) :
|
|
m_x(double_to_fix(lon)),
|
|
m_y(double_to_fix(lat)) {
|
|
}
|
|
|
|
Location(const Location&) = default;
|
|
Location(Location&&) = default;
|
|
Location& operator=(const Location&) = default;
|
|
Location& operator=(Location&&) = default;
|
|
~Location() = default;
|
|
|
|
/**
|
|
* Check whether the coordinates of this location
|
|
* are defined.
|
|
*/
|
|
explicit constexpr operator bool() const noexcept {
|
|
return m_x != undefined_coordinate && m_y != undefined_coordinate;
|
|
}
|
|
|
|
/**
|
|
* Check whether the coordinates are inside the
|
|
* usual bounds (-180<=lon<=180, -90<=lat<=90).
|
|
*/
|
|
constexpr bool valid() const noexcept {
|
|
return m_x >= -180 * detail::coordinate_precision
|
|
&& m_x <= 180 * detail::coordinate_precision
|
|
&& m_y >= -90 * detail::coordinate_precision
|
|
&& m_y <= 90 * detail::coordinate_precision;
|
|
}
|
|
|
|
constexpr int32_t x() const noexcept {
|
|
return m_x;
|
|
}
|
|
|
|
constexpr int32_t y() const noexcept {
|
|
return m_y;
|
|
}
|
|
|
|
Location& set_x(const int32_t x) noexcept {
|
|
m_x = x;
|
|
return *this;
|
|
}
|
|
|
|
Location& set_y(const int32_t y) noexcept {
|
|
m_y = y;
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
* Get longitude.
|
|
*
|
|
* @throws invalid_location if the location is invalid
|
|
*/
|
|
double lon() const {
|
|
if (!valid()) {
|
|
throw osmium::invalid_location("invalid location");
|
|
}
|
|
return fix_to_double(m_x);
|
|
}
|
|
|
|
/**
|
|
* Get longitude without checking the validity.
|
|
*/
|
|
double lon_without_check() const {
|
|
return fix_to_double(m_x);
|
|
}
|
|
|
|
/**
|
|
* Get latitude.
|
|
*
|
|
* @throws invalid_location if the location is invalid
|
|
*/
|
|
double lat() const {
|
|
if (!valid()) {
|
|
throw osmium::invalid_location("invalid location");
|
|
}
|
|
return fix_to_double(m_y);
|
|
}
|
|
|
|
/**
|
|
* Get latitude without checking the validity.
|
|
*/
|
|
double lat_without_check() const {
|
|
return fix_to_double(m_y);
|
|
}
|
|
|
|
Location& set_lon(double lon) noexcept {
|
|
m_x = double_to_fix(lon);
|
|
return *this;
|
|
}
|
|
|
|
Location& set_lat(double lat) noexcept {
|
|
m_y = double_to_fix(lat);
|
|
return *this;
|
|
}
|
|
|
|
Location& set_lon(const char* str) {
|
|
const char** data = &str;
|
|
m_x = detail::string_to_location_coordinate(data);
|
|
if (**data != '\0') {
|
|
throw invalid_location{std::string{"characters after coordinate: '"} + *data + "'"};
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
Location& set_lat(const char* str) {
|
|
const char** data = &str;
|
|
m_y = detail::string_to_location_coordinate(data);
|
|
if (**data != '\0') {
|
|
throw invalid_location{std::string{"characters after coordinate: '"} + *data + "'"};
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
Location& set_lon_partial(const char** str) {
|
|
m_x = detail::string_to_location_coordinate(str);
|
|
return *this;
|
|
}
|
|
|
|
Location& set_lat_partial(const char** str) {
|
|
m_y = detail::string_to_location_coordinate(str);
|
|
return *this;
|
|
}
|
|
|
|
template <typename T>
|
|
T as_string_without_check(T iterator, const char separator = ',') const {
|
|
iterator = detail::append_location_coordinate_to_string(iterator, x());
|
|
*iterator++ = separator;
|
|
return detail::append_location_coordinate_to_string(iterator, y());
|
|
}
|
|
|
|
template <typename T>
|
|
T as_string(T iterator, const char separator = ',') const {
|
|
if (!valid()) {
|
|
throw osmium::invalid_location("invalid location");
|
|
}
|
|
return as_string_without_check(iterator, separator);
|
|
}
|
|
|
|
}; // class Location
|
|
|
|
/**
|
|
* Locations are equal if both coordinates are equal.
|
|
*/
|
|
inline constexpr bool operator==(const Location& lhs, const Location& rhs) noexcept {
|
|
return lhs.x() == rhs.x() && lhs.y() == rhs.y();
|
|
}
|
|
|
|
inline constexpr bool operator!=(const Location& lhs, const Location& rhs) noexcept {
|
|
return ! (lhs == rhs);
|
|
}
|
|
|
|
/**
|
|
* Compare two locations by comparing first the x and then
|
|
* the y coordinate. If either of the locations is
|
|
* undefined the result is undefined.
|
|
*/
|
|
inline constexpr bool operator<(const Location& lhs, const Location& rhs) noexcept {
|
|
return (lhs.x() == rhs.x() && lhs.y() < rhs.y()) || lhs.x() < rhs.x();
|
|
}
|
|
|
|
inline constexpr bool operator>(const Location& lhs, const Location& rhs) noexcept {
|
|
return rhs < lhs;
|
|
}
|
|
|
|
inline constexpr bool operator<=(const Location& lhs, const Location& rhs) noexcept {
|
|
return ! (rhs < lhs);
|
|
}
|
|
|
|
inline constexpr bool operator>=(const Location& lhs, const Location& rhs) noexcept {
|
|
return ! (lhs < rhs);
|
|
}
|
|
|
|
/**
|
|
* Output a location to a stream.
|
|
*/
|
|
template <typename TChar, typename TTraits>
|
|
inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const osmium::Location& location) {
|
|
if (location) {
|
|
out << '(';
|
|
location.as_string(std::ostream_iterator<char>(out), ',');
|
|
out << ')';
|
|
} else {
|
|
out << "(undefined,undefined)";
|
|
}
|
|
return out;
|
|
}
|
|
|
|
namespace detail {
|
|
|
|
template <int N>
|
|
inline size_t hash(const osmium::Location& location) noexcept {
|
|
return location.x() ^ location.y();
|
|
}
|
|
|
|
template <>
|
|
inline size_t hash<8>(const osmium::Location& location) noexcept {
|
|
size_t h = location.x();
|
|
h <<= 32;
|
|
return h ^ location.y();
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace osmium
|
|
|
|
namespace std {
|
|
|
|
// This pragma is a workaround for a bug in an old libc implementation
|
|
#ifdef __clang__
|
|
#pragma clang diagnostic push
|
|
#pragma clang diagnostic ignored "-Wmismatched-tags"
|
|
#endif
|
|
template <>
|
|
struct hash<osmium::Location> {
|
|
using argument_type = osmium::Location;
|
|
using result_type = size_t;
|
|
size_t operator()(const osmium::Location& location) const noexcept {
|
|
return osmium::detail::hash<sizeof(size_t)>(location);
|
|
}
|
|
};
|
|
#ifdef __clang__
|
|
#pragma clang diagnostic pop
|
|
#endif
|
|
|
|
} // namespace std
|
|
|
|
#endif // OSMIUM_OSM_LOCATION_HPP
|