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

use abstutil::prettyprint_usize;
use map_model::{LaneID, PathConstraints};
use widgetry::{Btn, EventCtx, Line, LinePlot, PlotOptions, Series, Text, TextExt, Widget};

use crate::app::App;
use crate::info::{header_btns, make_table, make_tabs, throughput, DataOptions, Details, Tab};

pub fn info(ctx: &EventCtx, app: &App, details: &mut Details, id: LaneID) -> Vec<Widget> {
    let mut rows = header(ctx, app, details, id, Tab::LaneInfo(id));
    let map = &app.primary.map;
    let l = map.get_l(id);
    let r = map.get_r(l.parent);

    let mut kv = Vec::new();

    if !l.is_walkable() {
        kv.push(("Type", l.lane_type.describe().to_string()));
    }
    if r.is_private() {
        let mut ban = Vec::new();
        for p in PathConstraints::all() {
            if !r.access_restrictions.allow_through_traffic.contains(p) {
                ban.push(format!("{:?}", p).to_ascii_lowercase());
            }
        }
        if !ban.is_empty() {
            kv.push(("No through-traffic for", ban.join(", ")));
        }
        if let Some(cap) = r.access_restrictions.cap_vehicles_per_hour {
            kv.push((
                "Cap for vehicles this hour",
                format!(
                    "{} / {}",
                    prettyprint_usize(app.primary.sim.get_cap_counter(l.id)),
                    prettyprint_usize(cap)
                ),
            ));
        }
    }

    if l.is_parking() {
        kv.push((
            "Parking",
            format!(
                "{} / {} spots available",
                app.primary.sim.get_free_onstreet_spots(l.id).len(),
                l.number_parking_spots()
            ),
        ));
    } else {
        kv.push(("Speed limit", r.speed_limit.to_string(&app.opts.units)));
    }

    kv.push(("Length", l.length().to_string(&app.opts.units)));

    rows.extend(make_table(ctx, kv));

    if l.is_parking() {
        let capacity = l.number_parking_spots();
        let mut series = vec![Series {
            label: format!("After \"{}\"", app.primary.map.get_edits().edits_name),
            color: app.cs.after_changes,
            pts: app.primary.sim.get_analytics().parking_lane_availability(
                app.primary.sim.time(),
                l.id,
                capacity,
            ),
        }];
        if app.has_prebaked().is_some() {
            series.push(Series {
                label: format!("Before \"{}\"", app.primary.map.get_edits().edits_name),
                color: app.cs.before_changes.alpha(0.5),
                pts: app.prebaked().parking_lane_availability(
                    app.primary.sim.get_end_of_day(),
                    l.id,
                    capacity,
                ),
            });
        }
        rows.push("Parking spots available".draw_text(ctx));
        rows.push(LinePlot::new(
            ctx,
            series,
            PlotOptions {
                filterable: false,
                max_x: None,
                max_y: Some(capacity),
                disabled: HashSet::new(),
            },
        ));
    }

    rows
}

pub fn debug(ctx: &EventCtx, app: &App, details: &mut Details, id: LaneID) -> Vec<Widget> {
    let mut rows = header(ctx, app, details, id, Tab::LaneDebug(id));
    let map = &app.primary.map;
    let l = map.get_l(id);
    let r = map.get_r(l.parent);

    let mut kv = Vec::new();

    kv.push(("Parent".to_string(), r.id.to_string()));

    if l.lane_type.is_for_moving_vehicles() {
        kv.push((
            "Driving blackhole".to_string(),
            l.driving_blackhole.to_string(),
        ));
        kv.push((
            "Biking blackhole".to_string(),
            l.biking_blackhole.to_string(),
        ));
    }

    if let Some(types) = l.get_turn_restrictions(r) {
        kv.push((
            "Turn restrictions".to_string(),
            format!("{:?}", types.into_iter().collect::<Vec<_>>()),
        ));
    }
    for (restriction, to) in &r.turn_restrictions {
        kv.push((
            format!("Restriction from this road to {}", to),
            format!("{:?}", restriction),
        ));
    }

    // TODO Simplify and expose everywhere after there's better data
    kv.push((
        "Elevation change".to_string(),
        format!(
            "{} to {}",
            map.get_i(l.src_i).elevation,
            map.get_i(l.dst_i).elevation
        ),
    ));
    kv.push((
        "Incline / grade".to_string(),
        format!("{:.1}%", r.percent_grade(map) * 100.0),
    ));
    kv.push((
        "Elevation details".to_string(),
        format!(
            "{} over {}",
            map.get_i(l.dst_i).elevation - map.get_i(l.src_i).elevation,
            l.length()
        ),
    ));
    kv.push((
        "Dir and offset".to_string(),
        format!("{}, {}", r.dir(l.id), r.offset(l.id)),
    ));
    if let Some((reserved, total)) = app.primary.sim.debug_queue_lengths(l.id) {
        kv.push((
            "Queue (reserved, total) length".to_string(),
            format!("{}, {}", reserved, total),
        ));
    }

    rows.extend(make_table(ctx, kv));

    rows.push(Btn::text_bg1("Open OSM way").build(
        ctx,
        format!("open {}", r.orig_id.osm_way_id),
        None,
    ));

    let mut txt = Text::from(Line(""));
    txt.add(Line("Raw OpenStreetMap data"));
    rows.push(txt.draw(ctx));

    rows.extend(make_table(
        ctx,
        r.osm_tags
            .inner()
            .iter()
            .map(|(k, v)| (k, v.to_string()))
            .collect(),
    ));

    rows
}

pub fn traffic(
    ctx: &mut EventCtx,
    app: &App,
    details: &mut Details,
    id: LaneID,
    opts: &DataOptions,
) -> Vec<Widget> {
    let mut rows = header(ctx, app, details, id, Tab::LaneTraffic(id, opts.clone()));
    let map = &app.primary.map;
    let l = map.get_l(id);
    let r = map.get_r(l.parent);

    // Since this applies to the entire road, ignore lane type.
    let mut txt = Text::from(Line("Traffic over entire road, not just this lane"));
    txt.add(Line(format!(
        "Since midnight: {} commuters and vehicles crossed",
        prettyprint_usize(app.primary.sim.get_analytics().road_thruput.total_for(r.id))
    )));
    rows.push(txt.draw(ctx));

    rows.push(opts.to_controls(ctx, app));

    let r = map.get_l(id).parent;
    let time = if opts.show_end_of_day {
        app.primary.sim.get_end_of_day()
    } else {
        app.primary.sim.time()
    };
    // TODO This conflates commuters and vehicles, so we should maybe split it into different plots.
    rows.push(throughput(
        ctx,
        app,
        "Number of commuters and vehicles per hour",
        move |a| {
            if a.road_thruput.raw.is_empty() {
                a.road_thruput.count_per_hour(r, time)
            } else {
                a.road_thruput.raw_throughput(time, r)
            }
        },
        &opts,
    ));

    rows
}

fn header(ctx: &EventCtx, app: &App, details: &mut Details, id: LaneID, tab: Tab) -> Vec<Widget> {
    let mut rows = vec![];

    let map = &app.primary.map;
    let l = map.get_l(id);
    let r = map.get_r(l.parent);

    let label = if l.is_shoulder() {
        "Shoulder"
    } else if l.is_sidewalk() {
        "Sidewalk"
    } else {
        "Lane"
    };
    rows.push(Widget::row(vec![
        Line(format!("{} #{}", label, id.0))
            .small_heading()
            .draw(ctx),
        header_btns(ctx),
    ]));
    rows.push(format!("@ {}", r.get_name(app.opts.language.as_ref())).draw_text(ctx));

    let mut tabs = vec![("Info", Tab::LaneInfo(id))];
    if !l.is_parking() {
        tabs.push(("Traffic", Tab::LaneTraffic(id, DataOptions::new())));
    }
    if app.opts.dev {
        tabs.push(("Debug", Tab::LaneDebug(id)));
    }
    rows.push(make_tabs(ctx, &mut details.hyperlinks, tab, tabs));

    rows
}