/* EXAMPLE osmium_location_cache_use This reads ways from an OSM file and writes out the way node locations it got from a location cache generated with osmium_location_cache_create. Warning: The locations cache file will get huge (>32GB) if you are using the DenseFileArray index even if the input file is small, because it depends on the *largest* node ID, not the number of nodes. DEMONSTRATES USE OF: * file input * location indexes and the NodeLocationsForWays handler * location indexes on disk SIMPLER EXAMPLES you might want to understand first: * osmium_read * osmium_count * osmium_road_length LICENSE The code in this example file is released into the Public Domain. */ #include // for errno #include // for std::exit #include // for strerror #include // for open #include // for std::cout, std::cerr #include // for std::string #include // for open #include // for open #ifdef _WIN32 # include // for _setmode #endif // Allow any format of input files (XML, PBF, ...) #include // For the location index. There are different types of index implementation // available. These implementations put the index on disk. See below. #include #include // For the NodeLocationForWays handler #include // For osmium::apply() #include // Chose one of these two. "sparse" is best used for small and medium extracts, // the "dense" index for large extracts or the whole planet. using index_type = osmium::index::map::SparseFileArray; //using index_type = osmium::index::map::DenseFileArray; // The location handler always depends on the index type using location_handler_type = osmium::handler::NodeLocationsForWays; // This handler only implements the way() function which prints out the way // ID and all nodes IDs and locations in those ways. struct MyHandler : public osmium::handler::Handler { void way(const osmium::Way& way) { std::cout << "way " << way.id() << "\n"; for (const auto& nr : way.nodes()) { std::cout << " node " << nr.ref() << " " << nr.location() << "\n"; } } }; // struct MyHandler int main(int argc, char* argv[]) { if (argc != 3) { std::cerr << "Usage: " << argv[0] << " OSM_FILE CACHE_FILE\n"; std::exit(1); } const std::string input_filename{argv[1]}; const std::string cache_filename{argv[2]}; // Construct Reader reading only ways osmium::io::Reader reader{input_filename, osmium::osm_entity_bits::way}; // Initialize location index on disk using an existing file const int fd = ::open(cache_filename.c_str(), O_RDWR); if (fd == -1) { std::cerr << "Can not open location cache file '" << cache_filename << "': " << std::strerror(errno) << "\n"; return 1; } #ifdef _WIN32 _setmode(fd, _O_BINARY); #endif index_type index{fd}; // The handler that adds node locations from the index to the ways. location_handler_type location_handler{index}; // Feed all ways through the location handler and then our own handler. MyHandler handler; osmium::apply(reader, location_handler, handler); // Explicitly close input so we get notified of any errors. reader.close(); }