A Rust library for reading the OpenStreetMap PBF file format (*.osm.pbf).

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Count the number of buildings and their nodes from a PBF file
  2. // given as the first command line argument.
  3. extern crate osmpbf;
  4. use osmpbf::{IndexedReader, Element};
  5. use std::error::Error;
  6. fn main() -> Result<(), Box<dyn Error>> {
  7. // Read command line argument and create IndexedReader
  8. let arg = std::env::args_os()
  9. .nth(1)
  10. .ok_or("need a *.osm.pbf file as argument")?;
  11. let mut reader = IndexedReader::from_path(&arg)?;
  12. println!("Counting...");
  13. let mut ways = 0;
  14. let mut nodes = 0;
  15. reader.read_ways_and_deps(
  16. |way| {
  17. // Filter ways. Return true if tags contain "building": "yes".
  18. way.tags().any(|key_value| key_value == ("building", "yes"))
  19. },
  20. |element| {
  21. // Increment counter for ways and nodes
  22. match element {
  23. Element::Way(_way) => ways += 1,
  24. Element::Node(_node) => nodes += 1,
  25. Element::DenseNode(_dense_node) => nodes += 1,
  26. Element::Relation(_) => {}, // should not occur
  27. }
  28. },
  29. )?;
  30. // Print result
  31. println!("ways: {}\nnodes: {}", ways, nodes);
  32. Ok(())
  33. }