1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#[macro_use]
extern crate log;
use std::convert::TryInto;
use anyhow::Result;
use geo::algorithm::area::Area;
use geo::algorithm::contains::Contains;
use geojson::GeoJson;
use abstutil::{CmdArgs, Timer};
use geom::LonLat;
#[tokio::main]
async fn main() -> Result<()> {
let mut args = CmdArgs::new();
let input = args.required_free();
args.done();
let boundary_pts = LonLat::read_osmosis_polygon(&input)?;
let center = LonLat::center(&boundary_pts);
let geofabrik_idx = load_remote_geojson(
abstio::path_shared_input("geofabrik-index.json"),
"https://download.geofabrik.de/index-v1.json",
)
.await?;
let matches = find_matching_regions(geofabrik_idx, center);
info!(
"{} regions contain boundary center {}",
matches.len(),
center
);
let (_, url) = matches
.into_iter()
.min_by_key(|(mp, _)| mp.unsigned_area() as usize)
.unwrap();
println!("{}", url);
Ok(())
}
async fn load_remote_geojson(path: String, url: &str) -> Result<GeoJson> {
if !abstio::file_exists(&path) {
info!("Downloading {}", url);
abstio::download_to_file(url, &path).await?;
}
abstio::maybe_read_json(path, &mut Timer::throwaway())
}
fn find_matching_regions(
geojson: GeoJson,
center: LonLat,
) -> Vec<(geo::MultiPolygon<f64>, String)> {
let center: geo::Point<f64> = center.into();
let mut matches = Vec::new();
if let GeoJson::FeatureCollection(fc) = geojson {
info!("Searching {} regions", fc.features.len());
for mut feature in fc.features {
let mp: geo::MultiPolygon<f64> =
feature.geometry.take().unwrap().value.try_into().unwrap();
if mp.contains(¢er) {
matches.push((
mp,
feature
.property("urls")
.unwrap()
.get("pbf")
.unwrap()
.as_str()
.unwrap()
.to_string(),
));
}
}
}
matches
}