Skip to content

Commit

Permalink
Changed precision to 10 decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
mayermart committed Oct 17, 2024
1 parent 973abc4 commit 82af430
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ namespace osmium {
}

inline bool y_range_overlap(const NodeRefSegment& s1, const NodeRefSegment& s2) noexcept {
const std::pair<int32_t, int32_t> m1 = std::minmax(s1.first().location().y(), s1.second().location().y());
const std::pair<int32_t, int32_t> m2 = std::minmax(s2.first().location().y(), s2.second().location().y());
const std::pair<int64_t, int64_t> m1 = std::minmax(s1.first().location().y(), s1.second().location().y());
const std::pair<int64_t, int64_t> m2 = std::minmax(s2.first().location().y(), s2.second().location().y());
return !(m1.first > m2.second || m2.first > m1.second);
}

Expand Down Expand Up @@ -329,7 +329,7 @@ namespace osmium {
(d < 0 && na <= 0 && na >= d && nb <= 0 && nb >= d)) {
const double ua = static_cast<double>(na) / static_cast<double>(d);
const vec i = p0 + ua * (p1 - p0);
return osmium::Location{static_cast<int32_t>(i.x), static_cast<int32_t>(i.y)};
return osmium::Location{static_cast<int64_t>(i.x), static_cast<int64_t>(i.y)};
}

return osmium::Location{};
Expand Down
2 changes: 1 addition & 1 deletion contrib/libosmium/include/osmium/geom/geojson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace osmium {
using multipolygon_type = std::string;
using ring_type = std::string;

explicit GeoJSONFactoryImpl(int /*srid*/, int precision = 7) :
explicit GeoJSONFactoryImpl(int /*srid*/, int precision = 10) :
m_precision(precision) {
}

Expand Down
2 changes: 1 addition & 1 deletion contrib/libosmium/include/osmium/geom/wkt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace osmium {
using multipolygon_type = std::string;
using ring_type = std::string;

explicit WKTFactoryImpl(int srid, int precision = 7, wkt_type wtype = wkt_type::wkt) :
explicit WKTFactoryImpl(int srid, int precision = 10, wkt_type wtype = wkt_type::wkt) :
m_precision(precision),
m_wkt_type(wtype) {
if (m_wkt_type == wkt_type::ewkt) {
Expand Down
2 changes: 1 addition & 1 deletion contrib/libosmium/include/osmium/io/detail/pbf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace osmium {

// resolution for longitude/latitude used for conversion
// between representation as double and as int
const int64_t lonlat_resolution = 1000L * 1000L * 1000L;
const int64_t lonlat_resolution = 1000L * 1000L * 1000L * 1000L;

const int64_t resolution_convert = lonlat_resolution / osmium::detail::coordinate_precision;

Expand Down
8 changes: 4 additions & 4 deletions contrib/libosmium/include/osmium/io/detail/pbf_decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,12 @@ namespace osmium {
} while (!keys.empty() && !vals.empty());
}

int32_t convert_pbf_lon(const int64_t c) const noexcept {
return static_cast<int32_t>((c * m_granularity + m_lon_offset) / resolution_convert);
int64_t convert_pbf_lon(const int64_t c) const noexcept {
return static_cast<int64_t>((c * m_granularity + m_lon_offset) / resolution_convert);
}

int32_t convert_pbf_lat(const int64_t c) const noexcept {
return static_cast<int32_t>((c * m_granularity + m_lat_offset) / resolution_convert);
int64_t convert_pbf_lat(const int64_t c) const noexcept {
return static_cast<int64_t>((c * m_granularity + m_lat_offset) / resolution_convert);
}

void decode_node(const data_view& data) {
Expand Down
4 changes: 2 additions & 2 deletions contrib/libosmium/include/osmium/osm/crc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ namespace osmium {
}

void update(const osmium::Location& location) noexcept {
update_int32(location.x());
update_int32(location.y());
update_int64(location.x());
update_int64(location.y());
}

void update(const osmium::Box& box) noexcept {
Expand Down
56 changes: 23 additions & 33 deletions contrib/libosmium/include/osmium/osm/location.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ namespace osmium {
namespace detail {

enum {
coordinate_precision = 10000000
coordinate_precision = 10000000000
};

// 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) {
inline int64_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;
int64_t scale = 11;

// paranoia check for maximum number of digits
int max_digits = 10;
int max_digits = 13;

// optional minus sign
if (*str == '-') {
Expand Down Expand Up @@ -185,21 +185,21 @@ namespace osmium {

result = (result + 5) / 10 * sign;

if (result > std::numeric_limits<int32_t>::max() ||
result < std::numeric_limits<int32_t>::min()) {
if (result > std::numeric_limits<int64_t>::max() ||
result < std::numeric_limits<int64_t>::min()) {
throw invalid_location{std::string{"wrong format for coordinate: '"} + full + "'"};
}

*data = str;
return static_cast<int32_t>(result);
return static_cast<int64_t>(result);
}

// 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) {
inline T append_location_coordinate_to_string(T iterator, int64_t value) {
// need to special-case this, because later `value = -value` would overflow.
if (value == std::numeric_limits<int32_t>::min()) {
static const char minresult[] = "-214.7483648";
if (value == std::numeric_limits<int64_t>::min()) {
static const char minresult[] = "-214.7483648123";
return std::copy_n(minresult, sizeof(minresult) - 1, iterator);
}

Expand All @@ -210,7 +210,7 @@ namespace osmium {
}

// write digits into temporary buffer
int32_t v = value;
int64_t v = value;
char temp[10];
char* t = temp;
do {
Expand Down Expand Up @@ -270,8 +270,8 @@ namespace osmium {
*/
class Location {

int32_t m_x; // NOLINT(modernize-use-default-member-init)
int32_t m_y; // NOLINT(modernize-use-default-member-init)
int64_t m_x; // NOLINT(modernize-use-default-member-init)
int64_t m_y; // NOLINT(modernize-use-default-member-init)

constexpr static double precision() noexcept {
return static_cast<double>(detail::coordinate_precision);
Expand All @@ -284,14 +284,14 @@ namespace osmium {
// constexpr, so we hard code this for the time being.
// undefined_coordinate = std::numeric_limits<int32_t>::max();
enum {
undefined_coordinate = 2147483647
undefined_coordinate = 2147483647123
};

static int32_t double_to_fix(const double c) noexcept {
return static_cast<int32_t>(std::round(c * precision()));
static int64_t double_to_fix(const double c) noexcept {
return static_cast<int64_t>(std::round(c * precision()));
}

static constexpr double fix_to_double(const int32_t c) noexcept {
static constexpr double fix_to_double(const int64_t c) noexcept {
return static_cast<double>(c) / precision();
}

Expand All @@ -308,21 +308,11 @@ namespace osmium {
* Note that these coordinates are coordinate_precision
* times larger than the real coordinates.
*/
constexpr Location(const int32_t x, const int32_t y) noexcept :
constexpr Location(const int64_t x, const int64_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 in WGS84
* coordinates.
Expand Down Expand Up @@ -374,20 +364,20 @@ namespace osmium {
return m_x == undefined_coordinate && m_y == undefined_coordinate;
}

constexpr int32_t x() const noexcept {
constexpr int64_t x() const noexcept {
return m_x;
}

constexpr int32_t y() const noexcept {
constexpr int64_t y() const noexcept {
return m_y;
}

Location& set_x(const int32_t x) noexcept {
Location& set_x(const int64_t x) noexcept {
m_x = x;
return *this;
}

Location& set_y(const int32_t y) noexcept {
Location& set_y(const int64_t y) noexcept {
m_y = y;
return *this;
}
Expand Down Expand Up @@ -538,7 +528,7 @@ namespace osmium {

template <int N>
inline size_t hash(const osmium::Location& location) noexcept {
return static_cast<uint32_t>(location.x()) ^ static_cast<uint32_t>(location.y());
return static_cast<uint64_t>(location.x()) ^ static_cast<uint64_t>(location.y());
}

template <>
Expand Down
4 changes: 2 additions & 2 deletions contrib/libosmium/include/osmium/osm/node_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ namespace osmium {
/**
* Get internal x value of the location in this NodeRef.
*/
constexpr int32_t x() const noexcept {
constexpr int64_t x() const noexcept {
return m_location.x();
}

/**
* Get internal y value of the location in this NodeRef.
*/
constexpr int32_t y() const noexcept {
constexpr int64_t y() const noexcept {
return m_location.y();
}

Expand Down
8 changes: 4 additions & 4 deletions src/middle-pgsql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ std::size_t middle_query_pgsql_t::get_way_node_locations_db(
for (int i = 0; i < res.num_tuples(); ++i) {
locs.emplace(osmium::string_to_object_id(res.get_value(i, 0)),
osmium::Location{
(int)std::strtol(res.get_value(i, 1), nullptr, 10),
(int)std::strtol(res.get_value(i, 2), nullptr, 10)});
std::strtol(res.get_value(i, 1), nullptr, 10),
std::strtol(res.get_value(i, 2), nullptr, 10)});
}

for (auto &n : *nodes) {
Expand Down Expand Up @@ -588,8 +588,8 @@ osmium::Location middle_query_pgsql_t::get_node_location_db(osmid_t id) const
return osmium::Location{};
}

return osmium::Location{(int)std::strtol(res.get_value(0, 1), nullptr, 10),
(int)std::strtol(res.get_value(0, 2), nullptr, 10)};
return osmium::Location{std::strtol(res.get_value(0, 1), nullptr, 10),
std::strtol(res.get_value(0, 2), nullptr, 10)};
}

osmium::Location
Expand Down
4 changes: 2 additions & 2 deletions src/node-locations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ osmium::Location node_locations_t::get(osmid_t id) const
for (std::size_t n = 0; n < block_size && begin != end; ++n) {
auto const bid = did.update(
static_cast<int64_t>(protozero::decode_varint(&begin, end)));
auto const x = static_cast<int32_t>(dx.update(
auto const x = static_cast<int64_t>(dx.update(
protozero::decode_zigzag64(protozero::decode_varint(&begin, end))));
auto const y = static_cast<int32_t>(dy.update(
auto const y = static_cast<int64_t>(dy.update(
protozero::decode_zigzag64(protozero::decode_varint(&begin, end))));
if (bid == id) {
return osmium::Location{x, y};
Expand Down

0 comments on commit 82af430

Please sign in to comment.