fifteen_min/
score_homes.rs

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
use std::collections::BTreeSet;

use crate::App;
use abstutil::{prettyprint_usize, Counter, MultiMap, Timer};
use geom::Percent;
use map_gui::tools::grey_out_map;
use map_model::connectivity::Spot;
use map_model::{AmenityType, BuildingID};
use widgetry::tools::{ColorLegend, PopupMsg, URLManager};
use widgetry::{
    Color, DrawBaselayer, Drawable, EventCtx, GeomBatch, GfxCtx, Key, Line, Outcome, Panel,
    SimpleState, State, Text, TextExt, Toggle, Transition, Widget,
};

use crate::isochrone::Options;
use crate::{common, render};

/// Ask what types of amenities are necessary to be within a walkshed, then rank every house with
/// how many of those needs are satisfied.
pub struct ScoreHomes;

impl ScoreHomes {
    pub fn new_state(
        ctx: &mut EventCtx,
        app: &App,
        amenities: Vec<AmenityType>,
    ) -> Box<dyn State<App>> {
        let amenities_present = app.map.get_available_amenity_types();
        let mut toggles = Vec::new();
        let mut missing = Vec::new();
        for at in AmenityType::all() {
            if amenities_present.contains(&at) {
                toggles.push(Toggle::switch(
                    ctx,
                    &at.to_string(),
                    None,
                    amenities.contains(&at),
                ));
            } else {
                missing.push(at.to_string());
            }
        }

        let panel = Panel::new_builder(Widget::col(vec![
            Widget::row(vec![Line("Calculate acces scores")
                .small_heading()
                .into_widget(ctx)]),
            // TODO Adjust text to say bikeshed, or otherwise reflect the options chosen
            "Select the types of businesses you want within a 15 minute walkshed.".text_widget(ctx),
            Widget::row(vec![
                ctx.style().btn_outline.text("Enable all").build_def(ctx),
                ctx.style().btn_outline.text("Disable all").build_def(ctx),
            ]),
            Widget::custom_row(toggles).flex_wrap(ctx, Percent::int(50)),
            ctx.style()
                .btn_solid_primary
                .text("Calculate")
                .hotkey(Key::Enter)
                .build_def(ctx),
            Text::from(
                Line(format!(
                    "These amenities aren't present in this map: {}",
                    missing.join(", ")
                ))
                .secondary(),
            )
            .wrap_to_pct(ctx, 50)
            .into_widget(ctx),
        ]))
        .build(ctx);

        <dyn SimpleState<_>>::new_state(panel, Box::new(ScoreHomes))
    }
}

impl SimpleState<App> for ScoreHomes {
    fn on_click(
        &mut self,
        ctx: &mut EventCtx,
        app: &mut App,
        x: &str,
        panel: &mut Panel,
    ) -> Transition<App> {
        match x {
            "Enable all" => {
                return Transition::Replace(Self::new_state(
                    ctx,
                    app,
                    app.map.get_available_amenity_types().into_iter().collect(),
                ));
            }
            "Disable all" => {
                return Transition::Replace(Self::new_state(ctx, app, Vec::new()));
            }
            "Calculate" => {
                let amenities: Vec<AmenityType> = AmenityType::all()
                    .into_iter()
                    .filter(|at| panel.maybe_is_checked(&at.to_string()).unwrap_or(false))
                    .collect();
                if amenities.is_empty() {
                    return Transition::Push(PopupMsg::new_state(
                        ctx,
                        "No amenities selected",
                        vec!["Please select at least one amenity that you want in your walkshd"],
                    ));
                }

                return Transition::Multi(vec![
                    Transition::Pop,
                    Transition::Replace(Results::new_state(ctx, app, amenities)),
                ]);
            }
            _ => unreachable!(),
        }
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        grey_out_map(g, app);
    }

    fn draw_baselayer(&self) -> DrawBaselayer {
        DrawBaselayer::PreviousState
    }
}

/// For every house in the map, return the number of amenity types located within a 15min walkshed.
/// A single matching business per category is enough to count as satisfied.
fn score_houses_by_one_match(
    app: &App,
    amenities: Vec<AmenityType>,
    timer: &mut Timer,
) -> (Counter<BuildingID>, MultiMap<AmenityType, BuildingID>) {
    let mut satisfied_per_bldg: Counter<BuildingID> = Counter::new();
    let mut amenities_reachable = MultiMap::new();

    let map = &app.map;
    let movement_opts = &app.session.movement;
    for (category, stores, times) in
        timer.parallelize("find houses close to amenities", amenities, |category| {
            // For each category, find all matching stores
            let mut stores = BTreeSet::new();
            let mut spots = Vec::new();
            for b in map.all_buildings() {
                if b.has_amenity(category) {
                    stores.insert(b.id);
                    spots.push(Spot::Building(b.id));
                }
            }
            (
                category,
                stores,
                movement_opts.clone().times_from(map, spots),
            )
        })
    {
        amenities_reachable.set(category, stores);
        for (b, _) in times {
            satisfied_per_bldg.inc(b);
        }
    }

    (satisfied_per_bldg, amenities_reachable)
}

// TODO Show the matching amenities.
// TODO As you hover over a building, show the nearest amenity of each type
struct Results {
    panel: Panel,
    draw_houses: Drawable,
    amenities: Vec<AmenityType>,
    amenities_reachable: MultiMap<AmenityType, BuildingID>,
    draw_unwalkable_roads: Drawable,
    hovering_on_category: common::HoverOnCategory,
}

impl Results {
    fn new_state(
        ctx: &mut EventCtx,
        app: &App,
        amenities: Vec<AmenityType>,
    ) -> Box<dyn State<App>> {
        let draw_unwalkable_roads = render::draw_unwalkable_roads(ctx, app);

        assert!(!amenities.is_empty());
        let (scores, amenities_reachable) = ctx.loading_screen("search for houses", |_, timer| {
            score_houses_by_one_match(app, amenities.clone(), timer)
        });

        let mut batch = GeomBatch::new();
        let mut matches_all = 0;

        for (b, count) in scores.consume() {
            if count == amenities.len() {
                matches_all += 1;
            }
            let color = app
                .cs
                .good_to_bad_red
                .eval((count as f64) / (amenities.len() as f64));
            batch.push(color, app.map.get_b(b).polygon.clone());
        }

        let panel = build_panel(ctx, app, &amenities, &amenities_reachable, matches_all);

        Box::new(Self {
            draw_unwalkable_roads,
            panel,
            draw_houses: ctx.upload(batch),
            amenities,
            amenities_reachable,
            hovering_on_category: common::HoverOnCategory::new(Color::YELLOW),
        })
    }
}

impl State<App> for Results {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition<App> {
        // Allow panning and zooming
        if ctx.canvas_movement() {
            URLManager::update_url_cam(ctx, app.map.get_gps_bounds());
        }

        if ctx.redo_mouseover() {
            self.hovering_on_category.update_on_mouse_move(
                ctx,
                app,
                &self.panel,
                &self.amenities_reachable,
            );
        }

        match self.panel.event(ctx) {
            Outcome::Clicked(x) => {
                if x == "change scoring criteria" {
                    return Transition::Push(ScoreHomes::new_state(
                        ctx,
                        app,
                        self.amenities.clone(),
                    ));
                } else if x.starts_with("businesses: ") {
                    // TODO Use ExploreAmenitiesDetails, but omit duration
                    return Transition::Keep;
                }
                return common::on_click(ctx, app, &x);
            }
            Outcome::Changed(_) => {
                app.session = Options {
                    movement: common::options_from_controls(&self.panel),
                    thresholds: Options::default_thresholds(),
                };
                return Transition::Replace(Self::new_state(ctx, app, self.amenities.clone()));
            }
            _ => {}
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, _: &App) {
        g.redraw(&self.draw_unwalkable_roads);
        g.redraw(&self.draw_houses);
        self.hovering_on_category.draw(g);
        self.panel.draw(g);
    }
}

fn build_panel(
    ctx: &mut EventCtx,
    app: &App,
    amenities: &Vec<AmenityType>,
    amenities_reachable: &MultiMap<AmenityType, BuildingID>,
    matches_all: usize,
) -> Panel {
    let contents = vec![
        "What homes are within 15 minutes away?".text_widget(ctx),
        "Containing at least 1 of each:".text_widget(ctx),
        Widget::custom_row(
            amenities_reachable
                .borrow()
                .iter()
                .map(|(amenity, buildings)| {
                    ctx.style()
                        .btn_outline
                        .text(format!("{}: {}", amenity, buildings.len()))
                        .build_widget(ctx, format!("businesses: {}", amenity))
                        .margin_right(4)
                        .margin_below(4)
                })
                .collect(),
        )
        .flex_wrap(ctx, Percent::int(30)),
        format!(
            "{} houses match all categories",
            prettyprint_usize(matches_all)
        )
        .text_widget(ctx),
        Line("Darker is better; more categories")
            .secondary()
            .into_widget(ctx),
        ColorLegend::gradient_with_width(
            ctx,
            &app.cs.good_to_bad_red,
            vec!["0", &amenities.len().to_string()],
            150.0,
        ),
        ctx.style()
            .btn_outline
            .text("change scoring criteria")
            .build_def(ctx),
    ];

    common::build_panel(ctx, app, common::Mode::ScoreHomes, Widget::col(contents))
}