|
| 1 | +/** |
| 2 | + * SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + * |
| 4 | + * This file is part of osm2pgsql (https://osm2pgsql.org/). |
| 5 | + * |
| 6 | + * Copyright (C) 2006-2021 by the osm2pgsql developer community. |
| 7 | + * For a full list of authors see the git log. |
| 8 | + */ |
| 9 | + |
| 10 | +#include "node-locations.hpp" |
| 11 | + |
| 12 | +#include <osmium/osm/location.hpp> |
| 13 | +#include <osmium/util/delta.hpp> |
| 14 | + |
| 15 | +// Workaround: This must be included before buffer_string.hpp due to a missing |
| 16 | +// include in the upstream code. https://github.com/mapbox/protozero/pull/104 |
| 17 | +#include <protozero/config.hpp> |
| 18 | + |
| 19 | +#include <protozero/buffer_string.hpp> |
| 20 | +#include <protozero/varint.hpp> |
| 21 | + |
| 22 | +#include <cassert> |
| 23 | +#include <limits> |
| 24 | + |
| 25 | +void node_locations_t::set(osmid_t id, osmium::Location location) |
| 26 | +{ |
| 27 | + assert(block_index() == 0 || m_block[block_index() - 1].first < id); |
| 28 | + |
| 29 | + m_block[block_index()] = {id, location}; |
| 30 | + ++m_count; |
| 31 | + if (block_index() == 0) { |
| 32 | + freeze(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +osmium::Location node_locations_t::get(osmid_t id) const |
| 37 | +{ |
| 38 | + auto const offset = m_index.get_block(id); |
| 39 | + if (offset == ordered_index_t::not_found_value()) { |
| 40 | + return osmium::Location{}; |
| 41 | + } |
| 42 | + |
| 43 | + assert(offset < m_data.size()); |
| 44 | + |
| 45 | + char const *begin = m_data.data() + offset; |
| 46 | + char const *const end = m_data.data() + m_data.size(); |
| 47 | + |
| 48 | + osmium::DeltaDecode<osmid_t> did; |
| 49 | + std::size_t num = block_size; |
| 50 | + for (std::size_t n = 0; n < block_size; ++n) { |
| 51 | + auto bid = did.update(protozero::decode_varint(&begin, end)); |
| 52 | + if (bid == id) { |
| 53 | + num = n; |
| 54 | + } |
| 55 | + if (bid > id && num == block_size) { |
| 56 | + return osmium::Location{}; |
| 57 | + } |
| 58 | + } |
| 59 | + if (num == block_size) { |
| 60 | + return osmium::Location{}; |
| 61 | + } |
| 62 | + |
| 63 | + osmium::DeltaDecode<int64_t> dx; |
| 64 | + osmium::DeltaDecode<int64_t> dy; |
| 65 | + int32_t x = 0; |
| 66 | + int32_t y = 0; |
| 67 | + for (std::size_t n = 0; n <= num; ++n) { |
| 68 | + x = dx.update( |
| 69 | + protozero::decode_zigzag64(protozero::decode_varint(&begin, end))); |
| 70 | + y = dy.update( |
| 71 | + protozero::decode_zigzag64(protozero::decode_varint(&begin, end))); |
| 72 | + } |
| 73 | + |
| 74 | + return osmium::Location{x, y}; |
| 75 | +} |
| 76 | + |
| 77 | +void node_locations_t::freeze() |
| 78 | +{ |
| 79 | + encode_block(); |
| 80 | + clear_block(); |
| 81 | +} |
| 82 | + |
| 83 | +void node_locations_t::clear() |
| 84 | +{ |
| 85 | + m_data.clear(); |
| 86 | + m_data.shrink_to_fit(); |
| 87 | + m_index.clear(); |
| 88 | + clear_block(); |
| 89 | + m_count = 0; |
| 90 | +} |
| 91 | + |
| 92 | +void node_locations_t::encode_block() |
| 93 | +{ |
| 94 | + auto const offset = m_data.size(); |
| 95 | + osmium::DeltaEncode<osmid_t> did; |
| 96 | + osmium::DeltaEncode<int64_t> dx; |
| 97 | + osmium::DeltaEncode<int64_t> dy; |
| 98 | + for (auto const &nl : m_block) { |
| 99 | + protozero::add_varint_to_buffer(&m_data, did.update(nl.first)); |
| 100 | + } |
| 101 | + for (auto const &nl : m_block) { |
| 102 | + protozero::add_varint_to_buffer( |
| 103 | + &m_data, protozero::encode_zigzag64(dx.update(nl.second.x()))); |
| 104 | + protozero::add_varint_to_buffer( |
| 105 | + &m_data, protozero::encode_zigzag64(dy.update(nl.second.y()))); |
| 106 | + } |
| 107 | + m_index.add(m_block[0].first, offset); |
| 108 | +} |
| 109 | + |
| 110 | +void node_locations_t::clear_block() |
| 111 | +{ |
| 112 | + for (auto &nl : m_block) { |
| 113 | + nl.first = std::numeric_limits<osmid_t>::max(); |
| 114 | + nl.second = osmium::Location{}; |
| 115 | + } |
| 116 | +} |
| 117 | + |
0 commit comments