osm_io/osm/pbf/
bounding_box_calculator.rs1use std::path::{Path, PathBuf};
2use std::sync::{Arc, Mutex};
3
4use command_executor::shutdown_mode::ShutdownMode;
5use command_executor::thread_pool_builder::ThreadPoolBuilder;
6
7use crate::osm::model::bounding_box::BoundingBox;
8use crate::osm::pbf::calc_bounding_box_command::CalcBoundingBoxCommand;
9use crate::osm::pbf::reader::Reader;
10
11pub struct BoundingBoxCalculator {
12 path: PathBuf,
13}
14
15impl BoundingBoxCalculator {
16 pub fn new(path: &Path) -> BoundingBoxCalculator {
17 BoundingBoxCalculator {
18 path: path.to_path_buf(),
19 }
20 }
21
22 pub fn calc(&self) -> Result<BoundingBox, anyhow::Error> {
23 let mut tp = ThreadPoolBuilder::new()
24 .with_name_str("bounding-box-calculator")
25 .with_tasks(num_cpus::get())
26 .with_queue_size(1024)
27 .with_shutdown_mode(ShutdownMode::CompletePending)
28 .build()?;
29
30 let result = Arc::new(
31 Mutex::new(
32 None
33 )
34 );
35 let reader = Reader::new(&self.path)?;
36 for blob in reader.blobs()? {
37 tp.submit(
38 Box::new(
39 CalcBoundingBoxCommand::new(
40 blob,
41 result.clone(),
42 )
43 )
44 );
45 }
46
47 tp.shutdown();
48 tp.join()?;
49 let mut result_guard = result.lock().unwrap();
50 Ok(result_guard.take().unwrap())
51 }
52}