Closed jens1o closed 5 years ago
This should work:
extern crate osmpbf;
use std::collections::HashMap;
use std::error::Error;
use std::time::Instant;
fn main() -> Result<(), Box<dyn Error>> {
let reader = osmpbf::ElementReader::from_path("your-file.osm.pbf")?;
let start_instant = Instant::now();
let mut interesting_ways: HashMap<i64, Vec<(_, _)>> = HashMap::new();
reader
.for_each(|element| {
if let osmpbf::Element::Way(way) = element {
if way
.tags()
.find(|(k, _v)| k == &"wikipedia" || k == &"wikidata")
.is_some()
{
for (key, value) in way.tags() {
interesting_ways
.entry(way.id())
.or_default()
.push((key.to_string(), value.to_string()));
}
}
}
})
.unwrap();
println!(
"Processing took {:?} (interesting ways: {})",
start_instant.elapsed(),
interesting_ways.len()
);
Ok(())
}
Note the to_string()
calls. Keys and values are only valid inside the parsing context as they are references into a string table that is shared by many elements. Creating an owned String with to_string
fixes this issue.
(Also: first issue \o/)
Ah, awesome, works fine! Thank you very much for your help. :)
minimal working test case:
error:
Could you help me please how I could achieve this?
Thank you for your work, seems pretty useful. :)