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

count.rs 1.1KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Count the number of nodes, ways and relations in a PBF file given as the
  2. // first command line argument.
  3. extern crate error_chain;
  4. extern crate osmpbf;
  5. use osmpbf::*;
  6. use error_chain::ChainedError;
  7. fn main() {
  8. let arg = std::env::args_os().nth(1).expect("need a *.osm.pbf file as argument");
  9. let path = std::path::Path::new(&arg);
  10. let reader = ElementReader::from_path(path).unwrap();
  11. println!("Counting...");
  12. match reader.par_map_reduce(
  13. |element| {
  14. match element {
  15. Element::Node(_) | Element::DenseNode(_) => (1, 0, 0),
  16. Element::Way(_) => (0, 1, 0),
  17. Element::Relation(_) => (0, 0, 1),
  18. }
  19. },
  20. || (0u64, 0u64, 0u64),
  21. |a, b| (a.0 + b.0, a.1 + b.1, a.2 + b.2)
  22. ) {
  23. Ok((nodes, ways, relations)) => {
  24. println!("Nodes: {}", nodes);
  25. println!("Ways: {}", ways);
  26. println!("Relations: {}", relations);
  27. },
  28. Err(e) => {
  29. println!("{}", e.display_chain().to_string());
  30. },
  31. }
  32. }