Display a simple count of amenities reachable from a building. #393

This commit is contained in:
Dustin Carlino 2020-11-21 11:11:57 -08:00
parent bf9d34d1a3
commit 29da713903
4 changed files with 110 additions and 82 deletions

View File

@ -54,7 +54,7 @@ where
self.map.len()
}
pub(crate) fn raw_map(&self) -> &BTreeMap<K, BTreeSet<V>> {
pub fn borrow(&self) -> &BTreeMap<K, BTreeSet<V>> {
&self.map
}

View File

@ -105,8 +105,7 @@ pub fn serialize_multimap<
map: &MultiMap<K, V>,
s: S,
) -> Result<S::Ok, S::Error> {
// TODO maybe need to sort to have deterministic output
map.raw_map().iter().collect::<Vec<(_, _)>>().serialize(s)
map.borrow().iter().collect::<Vec<(_, _)>>().serialize(s)
}
/// Deserializes a MultiMap.

View File

@ -1,20 +1,49 @@
use std::collections::HashMap;
use abstutil::MultiMap;
use geom::{Duration, Polygon};
use map_model::{connectivity, BuildingID};
use widgetry::{Color, Drawable, EventCtx, GeomBatch};
use crate::app::App;
use crate::common::Grid;
use crate::helpers::amenity_type;
/// Represents the area reachable from a single building.
pub struct Isochrone {
/// Colored polygon contours, uploaded to the GPU and ready for drawing
pub draw: Drawable,
/* TODO This is a good place to store the buildings within 5 mins, 10 mins, etc
* and maybe some summary of the types of amenities within these ranges */
/// How far away is each building from the start?
pub time_to_reach_building: HashMap<BuildingID, Duration>,
/// Per category of amenity (defined by helpers::amenity_type), what buildings have that?
pub amenities_reachable: MultiMap<&'static str, BuildingID>,
}
impl Isochrone {
pub fn new(ctx: &mut EventCtx, app: &App, start: BuildingID) -> Isochrone {
let time_to_reach_building =
connectivity::all_costs_from(&app.primary.map, start, Duration::minutes(15));
let draw = draw_isochrone(app, &time_to_reach_building).upload(ctx);
let mut amenities_reachable = MultiMap::new();
for b in time_to_reach_building.keys() {
let bldg = app.primary.map.get_b(*b);
for amenity in &bldg.amenities {
if let Some(category) = amenity_type(&amenity.amenity_type) {
amenities_reachable.insert(category, bldg.id);
}
}
}
Isochrone {
draw,
time_to_reach_building,
amenities_reachable,
}
}
}
fn draw_isochrone(app: &App, time_to_reach_building: &HashMap<BuildingID, Duration>) -> GeomBatch {
// To generate the polygons covering areas between 0-5 mins, 5-10 mins, etc, we have to feed
// in a 2D grid of costs. Use a 100x100 meter resolution.
let bounds = app.primary.map.get_bounds();
@ -28,11 +57,9 @@ impl Isochrone {
);
// Calculate the cost from the start building to every other building in the map
for (b, cost) in
connectivity::all_costs_from(&app.primary.map, start, Duration::minutes(15))
{
for (b, cost) in time_to_reach_building {
// What grid cell does the building belong to?
let pt = app.primary.map.get_b(b).polygon.center();
let pt = app.primary.map.get_b(*b).polygon.center();
let idx = grid.idx(
((pt.x() - bounds.min_x) / resolution_m) as usize,
((pt.y() - bounds.min_y) / resolution_m) as usize,
@ -80,8 +107,5 @@ impl Isochrone {
}
}
Isochrone {
draw: batch.upload(ctx),
}
}
batch
}

View File

@ -35,20 +35,25 @@ impl Viewer {
pub fn new(ctx: &mut EventCtx, app: &App, start: BuildingID) -> Box<dyn State<App>> {
let start = app.primary.map.get_b(start);
let isochrone = Isochrone::new(ctx, app, start.id);
let panel = Panel::new(Widget::col(vec![
Widget::row(vec![
let mut rows = Vec::new();
rows.push(Widget::row(vec![
Line("15-minute neighborhood explorer")
.small_heading()
.draw(ctx),
Btn::close(ctx),
]),
Text::from_all(vec![
]));
let mut txt = Text::from_all(vec![
Line("Starting from: ").secondary(),
Line(&start.address),
])
.draw(ctx),
]))
]);
for (amenity, buildings) in isochrone.amenities_reachable.borrow() {
txt.add(Line(format!("{}: {}", amenity, buildings.len())));
}
rows.push(txt.draw(ctx));
let panel = Panel::new(Widget::col(rows))
.aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
.build(ctx);
@ -60,7 +65,7 @@ impl Viewer {
Box::new(Viewer {
panel,
highlight_start: ctx.upload(highlight_start),
isochrone: Isochrone::new(ctx, app, start.id),
isochrone,
})
}
}