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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use geom::{Angle, Distance, FindClosest, PolyLine, Polygon, Pt2D};
use map_gui::tools::{ColorDiscrete, ColorScale, Grid};
use map_gui::ID;
use widgetry::mapspace::ToggleZoomed;
use widgetry::{Color, EventCtx, GeomBatch, GfxCtx, Panel, Text, TextExt, Widget};
use crate::app::App;
use crate::layer::{header, Layer, LayerOutcome, PANEL_PLACEMENT};
pub struct SteepStreets {
tooltip: Option<Text>,
draw: ToggleZoomed,
panel: Panel,
}
impl Layer for SteepStreets {
fn name(&self) -> Option<&'static str> {
Some("steep streets")
}
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Option<LayerOutcome> {
if ctx.redo_mouseover() {
self.tooltip = None;
if let Some(ID::Road(r)) = app.mouseover_unzoomed_roads_and_intersections(ctx) {
self.tooltip = Some(Text::from(format!(
"{:.1}% incline",
app.primary.map.get_r(r).percent_incline.abs() * 100.0
)));
}
}
<dyn Layer>::simple_event(ctx, &mut self.panel)
}
fn draw(&self, g: &mut GfxCtx, _: &App) {
self.panel.draw(g);
self.draw.draw(g);
if let Some(ref txt) = self.tooltip {
g.draw_mouse_tooltip(txt.clone());
}
}
fn draw_minimap(&self, g: &mut GfxCtx) {
g.redraw(&self.draw.unzoomed);
}
}
impl SteepStreets {
pub fn new(ctx: &mut EventCtx, app: &App) -> SteepStreets {
let (colorer, steepest, uphill_legend) = SteepStreets::make_colorer(ctx, app);
let (draw, legend) = colorer.build(ctx);
let panel = Panel::new_builder(Widget::col(vec![
header(ctx, "Steep streets"),
uphill_legend,
legend,
format!("Steepest road: {:.0}% incline", steepest * 100.0).text_widget(ctx),
]))
.aligned_pair(PANEL_PLACEMENT)
.build(ctx);
SteepStreets {
tooltip: None,
draw,
panel,
}
}
pub fn make_colorer<'a>(ctx: &mut EventCtx, app: &'a App) -> (ColorDiscrete<'a>, f64, Widget) {
let (categories, uphill_legend) = SteepStreets::make_legend(ctx);
let mut colorer = ColorDiscrete::new(app, categories);
let arrow_len = Distance::meters(5.0);
let thickness = Distance::meters(2.0);
let mut steepest = 0.0_f64;
let mut arrows = GeomBatch::new();
for r in app.primary.map.all_roads() {
let pct = r.percent_incline.abs();
steepest = steepest.max(pct);
let bucket = if pct < 0.03 {
"0-3% (flat)"
} else if pct < 0.05 {
"3-5%"
} else if pct < 0.08 {
"5-8%"
} else if pct < 0.1 {
"8-10%"
} else if pct < 0.2 {
"10-20%"
} else {
">20% (steep)"
};
colorer.add_r(r.id, bucket);
if pct < 0.03 {
continue;
}
let mut pl = r.center_pts.clone();
if r.percent_incline < 0.0 {
pl = pl.reversed();
}
for (pt, angle) in pl.step_along(Distance::meters(15.0), arrow_len) {
arrows.push(
Color::WHITE,
PolyLine::must_new(vec![
pt.project_away(arrow_len, angle.rotate_degs(-135.0)),
pt,
pt.project_away(arrow_len, angle.rotate_degs(135.0)),
])
.make_polygons(thickness),
);
}
}
colorer.unzoomed.append(arrows);
(colorer, steepest, uphill_legend)
}
pub fn make_legend(ctx: &mut EventCtx) -> (Vec<(&'static str, Color)>, Widget) {
let categories = vec![
("0-3% (flat)", Color::hex("#296B07")),
("3-5%", Color::hex("#689A03")),
("5-8%", Color::hex("#EB9A04")),
("8-10%", Color::hex("#D30800")),
("10-20%", Color::hex("#980104")),
(">20% (steep)", Color::hex("#680605")),
];
let arrow_len = Distance::meters(5.0);
let thickness = Distance::meters(2.0);
let pt = Pt2D::new(0.0, 0.0);
let panel_arrow = PolyLine::must_new(vec![
pt.project_away(arrow_len, Angle::degrees(-135.0)),
pt,
pt.project_away(arrow_len, Angle::degrees(135.0)),
])
.make_polygons(thickness)
.scale(5.0);
let uphill_legend = Widget::row(vec![
GeomBatch::from(vec![(ctx.style().text_primary_color, panel_arrow)])
.autocrop()
.into_widget(ctx),
"points uphill".text_widget(ctx).centered_vert(),
]);
(categories, uphill_legend)
}
}
const INTERSECTION_SEARCH_RADIUS: Distance = Distance::const_meters(300.0);
const CONTOUR_STEP_SIZE: Distance = Distance::const_meters(15.0);
pub struct ElevationContours {
tooltip: Option<Text>,
closest_elevation: FindClosest<Distance>,
draw: ToggleZoomed,
panel: Panel,
}
impl Layer for ElevationContours {
fn name(&self) -> Option<&'static str> {
Some("elevation")
}
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Option<LayerOutcome> {
if ctx.redo_mouseover() {
self.tooltip = None;
if ctx.canvas.is_unzoomed() {
if let Some(pt) = ctx.canvas.get_cursor_in_map_space() {
if let Some((elevation, _)) = self
.closest_elevation
.closest_pt(pt, INTERSECTION_SEARCH_RADIUS)
{
self.tooltip = Some(Text::from(format!(
"Elevation: {}",
elevation.to_string(&app.opts.units)
)));
}
}
}
}
<dyn Layer>::simple_event(ctx, &mut self.panel)
}
fn draw(&self, g: &mut GfxCtx, _: &App) {
self.panel.draw(g);
self.draw.draw(g);
if let Some(ref txt) = self.tooltip {
g.draw_mouse_tooltip(txt.clone());
}
}
fn draw_minimap(&self, g: &mut GfxCtx) {
g.redraw(&self.draw.unzoomed);
}
}
impl ElevationContours {
pub fn new(ctx: &mut EventCtx, app: &App) -> ElevationContours {
let mut low = Distance::ZERO;
let mut high = Distance::ZERO;
for i in app.primary.map.all_intersections() {
low = low.min(i.elevation);
high = high.max(i.elevation);
}
let (closest_elevation, draw) = ElevationContours::make_contours(ctx, app, low, high);
let panel = Panel::new_builder(Widget::col(vec![
header(ctx, "Elevation"),
format!(
"Elevation from {} to {}",
low.to_string(&app.opts.units),
high.to_string(&app.opts.units)
)
.text_widget(ctx),
]))
.aligned_pair(PANEL_PLACEMENT)
.build(ctx);
ElevationContours {
tooltip: None,
closest_elevation,
draw,
panel,
}
}
pub fn make_contours(
ctx: &mut EventCtx,
app: &App,
low: Distance,
high: Distance,
) -> (FindClosest<Distance>, ToggleZoomed) {
let bounds = app.primary.map.get_bounds();
let mut closest = FindClosest::new(bounds);
let mut draw = ToggleZoomed::builder();
ctx.loading_screen("generate contours", |_, timer| {
timer.start("gather input");
let resolution_m = 30.0;
let mut grid: Grid<f64> = Grid::new(
(bounds.width() / resolution_m).ceil() as usize,
(bounds.height() / resolution_m).ceil() as usize,
0.0,
);
for i in app.primary.map.all_intersections() {
closest.add(i.elevation, i.polygon.points());
}
let mut indices = Vec::new();
for x in 0..grid.width {
for y in 0..grid.height {
indices.push((x, y));
}
}
for (idx, elevation) in timer.parallelize("fill out grid", indices, |(x, y)| {
let pt = Pt2D::new((x as f64) * resolution_m, (y as f64) * resolution_m);
let elevation = match closest.closest_pt(pt, INTERSECTION_SEARCH_RADIUS) {
Some((e, _)) => e,
None => Distance::ZERO,
};
(grid.idx(x, y), elevation)
}) {
grid.data[idx] = elevation.inner_meters();
}
timer.stop("gather input");
timer.start("calculate contours");
let mut thresholds: Vec<f64> = Vec::new();
let mut x = low;
while x < high {
thresholds.push(x.inner_meters());
x += CONTOUR_STEP_SIZE;
}
let scale = ColorScale(vec![Color::WHITE, Color::RED]);
let colors: Vec<Color> = (0..thresholds.len())
.map(|i| scale.eval((i as f64) / (thresholds.len() as f64)))
.collect();
let smooth = false;
let c = contour::ContourBuilder::new(grid.width as u32, grid.height as u32, smooth);
let features = c.contours(&grid.data, &thresholds).unwrap();
timer.stop("calculate contours");
timer.start_iter("draw", features.len());
for (feature, color) in features.into_iter().zip(colors) {
timer.next();
match feature.geometry.unwrap().value {
geojson::Value::MultiPolygon(polygons) => {
for p in polygons {
if let Ok(p) = Polygon::from_geojson(&p) {
let poly = p.scale(resolution_m);
if let Ok(x) = poly.to_outline(Distance::meters(5.0)) {
draw.unzoomed.push(Color::BLACK.alpha(0.5), x);
}
draw.unzoomed.push(color.alpha(0.1), poly);
}
}
}
_ => unreachable!(),
}
}
});
(closest, draw.build(ctx))
}
}