use std::error::Error;
use itertools::Itertools;
fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = std::env::args().collect();
let file_path = std::path::PathBuf::from(&args[1]);
let db = osmx::Database::open(&file_path)?;
let txn = osmx::Transaction::begin(&db)?;
let ways = txn.ways()?;
let locations = txn.locations()?;
let cell_nodes = txn.cell_nodes()?;
let node_ways = txn.node_ways()?;
let bbox: Vec<f64> = args[2..]
.iter()
.map(|s| s.parse::<f64>().unwrap())
.collect();
let region = osmx::Region::from_bbox(bbox[0], bbox[1], bbox[2], bbox[3]);
let node_ids: roaring::RoaringTreemap = cell_nodes.find_in_region(®ion).collect();
eprintln!("Nodes in region: {}", node_ids.len());
let mut way_ids = roaring::RoaringTreemap::new();
for node_id in node_ids {
way_ids.extend(node_ways.get(node_id));
}
eprintln!("Ways in region: {}", way_ids.len());
for way_id in way_ids {
let way = ways.get(way_id).unwrap();
if let Some(name) = way.tag("name") {
print!("{}", name);
}
let coords = way.nodes().map(|node_id| {
let loc = locations.get(node_id).unwrap();
(loc.lon(), loc.lat())
});
println!(
"\tLINESTRING ({})",
coords
.map(|(lon, lat)| format!("{:.7} {:.7}", lon, lat))
.join(",")
);
}
Ok(())
}