1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
/*
EXAMPLE osmium_index
Example program to look at Osmium indexes on disk.
You can use the osmium_dump_internal example program to create the offset
indexes or osmium_location_cache_create to create a node location index.
DEMONSTRATES USE OF:
* access to indexes on disk
SIMPLER EXAMPLES you might want to understand first:
* osmium_read
* osmium_count
* osmium_road_length
* osmium_location_cache_create
* osmium_location_cache_use
LICENSE
The code in this example file is released into the Public Domain.
*/
#include <algorithm> // for std::all_of, std::equal_range
#include <cstdlib> // for std::exit
#include <cstring> // for std::strcmp
#include <fcntl.h> // for open
#include <iostream> // for std::cout, std::cerr
#include <memory> // for std::unique_ptr
#include <string> // for std::string
#include <sys/stat.h> // for open
#include <sys/types.h> // for open
#include <vector> // for std::vector
#ifdef _WIN32
# include <io.h> // for _setmode
#endif
// Disk-based indexes
#include <osmium/index/map/dense_file_array.hpp>
#include <osmium/index/map/sparse_file_array.hpp>
// osmium::Location
#include <osmium/osm/location.hpp>
// Basic Osmium types
#include <osmium/osm/types.hpp>
// Virtual class for disk index access. If offers functions to dump the
// indexes and to search for ids in the index.
template <typename TValue>
class IndexAccess {
int m_fd;
public:
explicit IndexAccess(int fd) :
m_fd(fd) {
}
int fd() const noexcept {
return m_fd;
}
IndexAccess(const IndexAccess&) = delete;
IndexAccess& operator=(const IndexAccess&) = delete;
IndexAccess(IndexAccess&&) = delete;
IndexAccess& operator=(IndexAccess&&) = delete;
virtual ~IndexAccess() = default;
virtual void dump() const = 0;
virtual bool search(const osmium::unsigned_object_id_type& key) const = 0;
bool search(const std::vector<osmium::unsigned_object_id_type>& keys) const {
return std::all_of(keys.cbegin(), keys.cend(), [this](const osmium::unsigned_object_id_type& key) {
return search(key);
});
}
}; // class IndexAccess
// Implementation of IndexAccess for dense indexes usually used for very large
// extracts or the planet.
template <typename TValue>
class IndexAccessDense : public IndexAccess<TValue> {
using index_type = typename osmium::index::map::DenseFileArray<osmium::unsigned_object_id_type, TValue>;
public:
explicit IndexAccessDense(int fd) :
IndexAccess<TValue>(fd) {
}
void dump() const override {
index_type index{this->fd()};
for (std::size_t i = 0; i < index.size(); ++i) {
if (index.get(i) != TValue{}) {
std::cout << i << " " << index.get(i) << "\n";
}
}
}
bool search(const osmium::unsigned_object_id_type& key) const override {
index_type index{this->fd()};
try {
TValue value = index.get(key);
std::cout << key << " " << value << "\n";
} catch (...) {
std::cout << key << " not found\n";
return false;
}
return true;
}
}; // class IndexAccessDense
// Implementation of IndexAccess for sparse indexes usually used for small or
// medium sized extracts or for "multimap" type indexes.
template <typename TValue>
class IndexAccessSparse : public IndexAccess<TValue> {
using index_type = typename osmium::index::map::SparseFileArray<osmium::unsigned_object_id_type, TValue>;
public:
explicit IndexAccessSparse(int fd) :
IndexAccess<TValue>(fd) {
}
void dump() const override {
index_type index{this->fd()};
for (const auto& element : index) {
std::cout << element.first << " " << element.second << "\n";
}
}
bool search(const osmium::unsigned_object_id_type& key) const override {
using element_type = typename index_type::element_type;
index_type index{this->fd()};
element_type elem{key, TValue{}};
const auto positions = std::equal_range(index.begin(),
index.end(),
elem,
[](const element_type& lhs,
const element_type& rhs) {
return lhs.first < rhs.first;
});
if (positions.first == positions.second) {
std::cout << key << " not found\n";
return false;
}
for (auto it = positions.first; it != positions.second; ++it) {
std::cout << it->first << " " << it->second << "\n";
}
return true;
}
}; // class IndexAccessSparse
// This class contains the code to parse the command line arguments, check
// them and present the results to the rest of the program in an easy-to-use
// way.
class Options {
std::vector<osmium::unsigned_object_id_type> m_ids;
std::string m_type;
std::string m_filename;
bool m_dump = false;
bool m_array_format = false;
bool m_list_format = false;
void print_help() {
std::cout << "Usage: osmium_index_lookup [OPTIONS]\n\n"
<< "-h, --help Print this help message\n"
<< "-a, --array=FILE Read given index file in array format\n"
<< "-l, --list=FILE Read given index file in list format\n"
<< "-d, --dump Dump contents of index file to STDOUT\n"
<< "-s, --search=ID Search for given id (Option can appear multiple times)\n"
<< "-t, --type=TYPE Type of value ('location', 'id', or 'offset')\n"
;
}
void print_usage(const char* prgname) {
std::cout << "Usage: " << prgname << " [OPTIONS]\n\n";
std::exit(0);
}
public:
Options(int argc, char* argv[]) {
if (argc == 1) {
print_usage(argv[0]);
}
if (argc > 1 && (!std::strcmp(argv[1], "-h") ||
!std::strcmp(argv[1], "--help"))) {
print_help();
std::exit(0);
}
for (int i = 1; i < argc; ++i) {
if (!std::strcmp(argv[i], "-a") || !std::strcmp(argv[i], "--array")) {
++i;
if (i < argc) {
m_array_format = true;
m_filename = argv[i];
} else {
print_usage(argv[0]);
}
} else if (!std::strncmp(argv[i], "--array=", 8)) {
m_array_format = true;
m_filename = argv[i] + 8;
} else if (!std::strcmp(argv[i], "-l") || !std::strcmp(argv[i], "--list")) {
++i;
if (i < argc) {
m_list_format = true;
m_filename = argv[i];
} else {
print_usage(argv[0]);
}
} else if (!std::strncmp(argv[i], "--list=", 7)) {
m_list_format = true;
m_filename = argv[i] + 7;
} else if (!std::strcmp(argv[i], "-d") || !std::strcmp(argv[i], "--dump")) {
m_dump = true;
} else if (!std::strcmp(argv[i], "-s") || !std::strcmp(argv[i], "--search")) {
++i;
if (i < argc) {
m_ids.push_back(std::atoll(argv[i])); // NOLINT(cert-err34-c)
} else {
print_usage(argv[0]);
}
} else if (!std::strncmp(argv[i], "--search=", 9)) {
m_ids.push_back(std::atoll(argv[i] + 9)); // NOLINT(cert-err34-c)
} else if (!std::strcmp(argv[i], "-t") || !std::strcmp(argv[i], "--type")) {
++i;
if (i < argc) {
m_type = argv[i];
} else {
print_usage(argv[0]);
}
} else if (!std::strncmp(argv[i], "--type=", 7)) {
m_type = argv[i] + 7;
} else {
std::cerr << "Unknown command line options or arguments\n";
print_usage(argv[0]);
}
}
if (m_array_format == m_list_format) {
std::cerr << "Need option --array or --list, but not both\n";
std::exit(2);
}
if (m_dump == !m_ids.empty()) {
std::cerr << "Need option --dump or --search, but not both\n";
std::exit(2);
}
if (m_type.empty()) {
std::cerr << "Need --type argument.\n";
std::exit(2);
}
if (m_type != "location" && m_type != "id" && m_type != "offset") {
std::cerr << "Unknown type '" << m_type
<< "'. Must be 'location', 'id', or 'offset'.\n";
std::exit(2);
}
}
const char* filename() const noexcept {
return m_filename.c_str();
}
bool dense_format() const noexcept {
return m_array_format;
}
bool do_dump() const noexcept {
return m_dump;
}
const std::vector<osmium::unsigned_object_id_type>& search_keys() const noexcept {
return m_ids;
}
bool type_is(const char* type) const noexcept {
return m_type == type;
}
}; // class Options
// Factory function to create the right IndexAccess-derived class.
template <typename TValue>
std::unique_ptr<IndexAccess<TValue>> create(bool dense, int fd) {
std::unique_ptr<IndexAccess<TValue>> ptr;
if (dense) {
ptr.reset(new IndexAccessDense<TValue>{fd});
} else {
ptr.reset(new IndexAccessSparse<TValue>{fd});
}
return ptr;
}
// Do the actual work: Either dump the index or search in the index.
template <typename TValue>
int run(const IndexAccess<TValue>& index, const Options& options) {
if (options.do_dump()) {
index.dump();
return 0;
}
return index.search(options.search_keys()) ? 0 : 1;
}
int main(int argc, char* argv[]) {
// Parse command line options.
Options options{argc, argv};
// Open the index file.
const int fd = ::open(options.filename(), O_RDWR);
if (fd < 0) {
std::cerr << "Can not open file '" << options.filename()
<< "': " << std::strerror(errno) << '\n';
std::exit(2);
}
#ifdef _WIN32
_setmode(fd, _O_BINARY);
#endif
try {
// Depending on the type of index, we have different implementations.
if (options.type_is("location")) {
// index id -> location
const auto index = create<osmium::Location>(options.dense_format(), fd);
return run(*index, options);
}
if (options.type_is("id")) {
// index id -> id
const auto index = create<osmium::unsigned_object_id_type>(options.dense_format(), fd);
return run(*index, options);
}
// index id -> offset
const auto index = create<std::size_t>(options.dense_format(), fd);
return run(*index, options);
} catch (const std::exception& e) {
// All exceptions used by the Osmium library derive from std::exception.
std::cerr << e.what() << '\n';
std::exit(1);
}
}
|