osmium_read.cpp 1.28 KB
Newer Older
1 2
/*

3
  EXAMPLE osmium_read
4

5 6 7 8 9 10 11
  Reads and discards the contents of the input file.
  (It can be used for timing.)

  DEMONSTRATES USE OF:
  * file input

  LICENSE
12 13 14 15
  The code in this example file is released into the Public Domain.

*/

16 17
#include <cstdlib>  // for std::exit
#include <iostream> // for std::cerr
18

19
// Allow any format of input files (XML, PBF, ...)
20 21 22 23 24
#include <osmium/io/any_input.hpp>

int main(int argc, char* argv[]) {
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " OSMFILE\n";
25
        std::exit(1);
26 27
    }

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    try {
        // The Reader is initialized here with an osmium::io::File, but could
        // also be directly initialized with a file name.
        osmium::io::File input_file{argv[1]};
        osmium::io::Reader reader{input_file};

        // OSM data comes in buffers, read until there are no more.
        while (osmium::memory::Buffer buffer = reader.read()) {
            // do nothing
        }

        // You do not have to close the Reader explicitly, but because the
        // destructor can't throw, you will not see any errors otherwise.
        reader.close();
    } catch (const std::exception& e) {
        // All exceptions used by the Osmium library derive from std::exception.
        std::cerr << e.what() << '\n';
        std::exit(1);
46 47 48
    }
}