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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::process::Command;
use anyhow::Result;
use fs_err::File;
use geom::{Distance, PolyLine};
use raw_map::{OriginalRoad, RawMap};
pub fn add_data(map: &mut RawMap) -> Result<()> {
let input = format!("elevation_input_{}", map.name.as_filename());
let output = format!("elevation_output_{}", map.name.as_filename());
let ids = generate_input(&input, map)?;
fs_err::create_dir_all(&output)?;
fs_err::create_dir_all(abstio::path_shared_input("elevation"))?;
let pwd = std::env::current_dir()?.display().to_string();
let status = Command::new("docker")
.arg("run")
.arg("--mount")
.arg(format!(
"type=bind,source={pwd}/{input},target=/elevation/input,readonly"
))
.arg("--mount")
.arg(format!(
"type=bind,source={},target=/elevation/data",
format!("{}/{}", pwd, abstio::path_shared_input("elevation"))
))
.arg("--mount")
.arg(format!(
"type=bind,source={pwd}/{output},target=/elevation/output",
))
.arg("-t")
.arg("abstreet/elevation_lookups")
.arg("python3")
.arg("main.py")
.arg("query")
.arg("--n_threads=1")
.status()?;
if !status.success() {
bail!("Command failed: {}", status);
}
scrape_output(&output, map, ids)?;
fs_err::remove_file(format!("{input}/query"))?;
fs_err::remove_dir(input)?;
fs_err::remove_file(format!("{output}/query"))?;
fs_err::remove_dir(output)?;
Ok(())
}
fn generate_input(input: &str, map: &RawMap) -> Result<Vec<OriginalRoad>> {
fs_err::create_dir_all(input)?;
let mut f = BufWriter::new(File::create(format!("{input}/query"))?);
let mut ids = Vec::new();
for (id, r) in &map.roads {
if let Ok(pl) = PolyLine::new(r.center_points.clone()) {
ids.push(*id);
let mut pts = Vec::new();
for (pt, _) in pl.step_along(Distance::meters(5.0), Distance::ZERO) {
pts.push(pt);
}
if *pts.last().unwrap() != pl.last_pt() {
pts.push(pl.last_pt());
}
for (idx, gps) in map.gps_bounds.convert_back(&pts).into_iter().enumerate() {
write!(f, "{},{}", gps.x(), gps.y())?;
if idx != pts.len() - 1 {
write!(f, " ")?;
}
}
writeln!(f)?;
}
}
Ok(ids)
}
fn scrape_output(output: &str, map: &mut RawMap, ids: Vec<OriginalRoad>) -> Result<()> {
let num_ids = ids.len();
let mut cnt = 0;
for (line, id) in BufReader::new(File::open(format!("{output}/query"))?)
.lines()
.zip(ids)
{
cnt += 1;
let line = line?;
let mut values = Vec::new();
for x in line.split('\t') {
if let Ok(x) = x.parse::<f64>() {
if !x.is_finite() {
continue;
}
if x < 0.0 {
continue;
}
values.push(Distance::meters(x));
} else {
continue;
}
}
if values.len() != 4 {
error!("Elevation output line \"{}\" doesn't have 4 numbers", line);
continue;
}
map.intersections.get_mut(&id.i1).unwrap().elevation = values[0];
map.intersections.get_mut(&id.i2).unwrap().elevation = values[1];
}
if cnt != num_ids {
bail!("Output had {} lines, but we made {} queries", cnt, num_ids);
}
for (id, road) in &mut map.roads {
let rise = map.intersections[&id.i2].elevation - map.intersections[&id.i1].elevation;
let run = road.length();
if !(rise / run).is_finite() {
continue;
}
road.percent_incline = rise / run;
if road.percent_incline.abs() > 0.3 {
error!(
"{} is unexpectedly steep! Incline is {}%",
id,
road.percent_incline * 100.0
);
}
}
Ok(())
}