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
mod algorithm;
mod geojson;
use std::collections::BTreeMap;
use anyhow::Result;
use abstutil::Tags;
use geom::{Distance, PolyLine, Polygon};
use crate::{osm, OriginalRoad};
pub use algorithm::intersection_polygon;
#[derive(Clone)]
pub struct InputRoad {
pub id: OriginalRoad,
pub center_pts: PolyLine,
pub half_width: Distance,
pub osm_tags: Tags,
}
#[derive(Clone)]
pub struct Results {
pub intersection_id: osm::NodeID,
pub intersection_polygon: Polygon,
pub trimmed_center_pts: BTreeMap<OriginalRoad, (PolyLine, Distance)>,
pub debug: Vec<(String, Polygon)>,
}
pub fn osm2polygon(input_path: String, output_path: String) -> Result<()> {
let (intersection_id, input_roads, gps_bounds) = geojson::read_osm2polygon_input(input_path)?;
let results = intersection_polygon(intersection_id, input_roads, &BTreeMap::new())?;
let debug_output = false;
results.save_to_geojson(output_path, &gps_bounds, debug_output)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_osm2polygon() {
let mut any = false;
for entry in std::fs::read_dir("src/geometry/tests").unwrap() {
let input = entry.unwrap().path().display().to_string();
println!("Working on {input}");
if input.ends_with("output.json") {
continue;
}
any = true;
let expected_output_path = input.replace("input", "output");
let actual_output_path = "actual_osm2polygon_output.json";
osm2polygon(input.clone(), actual_output_path.to_string()).unwrap();
let expected_output = std::fs::read_to_string(expected_output_path.clone()).unwrap();
let actual_output = std::fs::read_to_string(actual_output_path).unwrap();
if expected_output != actual_output {
panic!("osm2polygon output changed. Manually compare {actual_output_path} and {expected_output_path}");
}
std::fs::remove_file(actual_output_path).unwrap();
}
assert!(any, "Didn't find any tests");
}
}