Skip to content

Commit

Permalink
Change the start time
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed May 26, 2024
1 parent 5a75d69 commit 80be8d4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 14 deletions.
17 changes: 11 additions & 6 deletions backend/src/isochrone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ pub fn calculate(
mode: Mode,
contours: bool,
public_transit: bool,
start_time: NaiveTime,
mut timer: Timer,
) -> Result<String> {
// 15 minutes
let limit = Duration::from_secs(15 * 60);

timer.step("get_costs");
let cost_per_road = get_costs(graph, req, mode, public_transit, limit);
let cost_per_road = get_costs(
graph,
req,
mode,
public_transit,
start_time,
start_time + limit,
);
timer.push("render to GJ");

// Show cost per road
Expand Down Expand Up @@ -58,12 +66,9 @@ fn get_costs(
req: Coord,
mode: Mode,
public_transit: bool,
limit: Duration,
start_time: NaiveTime,
end_time: NaiveTime,
) -> HashMap<RoadID, Duration> {
// TODO plumb in
let start_time = NaiveTime::from_hms_opt(7, 0, 0).unwrap();
let end_time = start_time + limit;

let start = graph.closest_intersection[mode]
.nearest_neighbor(&[req.x, req.y])
.unwrap()
Expand Down
5 changes: 5 additions & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate log;

use std::sync::Once;

use chrono::NaiveTime;
use geo::Coord;
use serde::Deserialize;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -95,6 +96,7 @@ impl MapModel {
mode,
req.contours,
req.mode == "transit",
NaiveTime::parse_from_str(&req.start_time, "%H:%M").map_err(err_to_js)?,
Timer::new("isochrone request", None),
)
.map_err(err_to_js)
Expand Down Expand Up @@ -135,6 +137,7 @@ impl MapModel {
end,
req.debug_search,
req.use_heuristic,
NaiveTime::parse_from_str(&req.start_time, "%H:%M").map_err(err_to_js)?,
Timer::new("route request", None),
)
.map_err(err_to_js)
Expand All @@ -152,6 +155,7 @@ pub struct IsochroneRequest {
y: f64,
mode: String,
contours: bool,
start_time: String,
}

#[derive(Deserialize)]
Expand All @@ -164,6 +168,7 @@ pub struct RouteRequest {
// TODO Only works for transit
debug_search: bool,
use_heuristic: bool,
start_time: String,
}

fn err_to_js<E: std::fmt::Display>(err: E) -> JsValue {
Expand Down
3 changes: 1 addition & 2 deletions backend/src/transit_route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ pub fn route(
// like a good idea yet
debug_search: bool,
use_heuristic: bool,
start_time: NaiveTime,
mut timer: Timer,
) -> Result<String> {
// TODO We'll need a start time too (and a day of the week)
let start_time = NaiveTime::from_hms_opt(7, 0, 0).unwrap();
if start == end {
bail!("start = end");
}
Expand Down
14 changes: 12 additions & 2 deletions web/src/IsochroneMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { onMount } from "svelte";
let travelMode: TravelMode = "foot";
let startTime = "07:00";
let start: { lng: number; lat: number } | null = null;
onMount(async () => {
Expand All @@ -35,13 +36,15 @@
_x: { lng: number; lat: number } | null,
_y: TravelMode,
_z: boolean,
_t: string,
) {
if (start) {
try {
isochroneGj = await $backend!.isochrone({
start,
mode: travelMode,
contours,
startTime,
});
err = "";
} catch (err: any) {
Expand All @@ -50,11 +53,12 @@
}
}
}
$: updateIsochrone(start, travelMode, contours);
$: updateIsochrone(start, travelMode, contours, startTime);
async function updateRoute(
x: { lng: number; lat: number } | null,
_y: Feature<Point> | null,
_t: string,
) {
if (start && hoveredAmenity) {
try {
Expand All @@ -64,6 +68,7 @@
mode: travelMode,
debugSearch: false,
useHeuristic: false,
startTime,
});
err = "";
} catch (err: any) {
Expand All @@ -74,7 +79,7 @@
routeGj = null;
}
}
$: updateRoute(start, hoveredAmenity);
$: updateRoute(start, hoveredAmenity, startTime);
function lerp(pct: number, a: number, b: number): number {
return a + pct * (b - a);
Expand Down Expand Up @@ -102,6 +107,11 @@

<PickTravelMode bind:travelMode />

<label>
Start time (PT only)
<input type="time" bind:value={startTime} />
</label>

<label><input type="checkbox" bind:checked={contours} />Contours</label>

<SequentialLegend {colorScale} limits={limitsMinutes} />
Expand Down
25 changes: 21 additions & 4 deletions web/src/RouteMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
let travelMode: TravelMode = "foot";
let useHeuristic = true;
let startTime = "07:00";
let start: { lng: number; lat: number } | null = null;
let end: { lng: number; lat: number } | null = null;
Expand All @@ -41,6 +42,7 @@
_y: { lng: number; lat: number } | null,
mode: TravelMode,
_z: boolean,
_t: string,
) {
if (start && end) {
try {
Expand All @@ -50,6 +52,7 @@
mode,
debugSearch: false,
useHeuristic,
startTime,
});
err = "";
} catch (error: any) {
Expand All @@ -58,7 +61,7 @@
}
}
}
$: updateRoute(start, end, travelMode, useHeuristic);
$: updateRoute(start, end, travelMode, useHeuristic, startTime);
function onRightClick(e: CustomEvent<MapMouseEvent>) {
// Move the first marker, for convenience
Expand All @@ -77,6 +80,7 @@
mode: travelMode,
debugSearch: true,
useHeuristic,
startTime,
});
$mode = {
kind: "debug-route",
Expand All @@ -102,11 +106,19 @@
>Isochrone mode</button
>
</div>

<PickTravelMode bind:travelMode />

<label>
<input type="checkbox" bind:checked={useHeuristic} />
Use heuristic
Use heuristic (PT only)
</label>

<label>
Start time (PT only)
<input type="time" bind:value={startTime} />
</label>

<p>
Move the <b>A</b> and <b>B</b> pins to find a route. (Hint: right-click to
set the first pin somewhere.)
Expand All @@ -119,10 +131,15 @@
{#each gj.features as f}
{@const props = notNull(f.properties)}
{#if props.kind == "road"}
<li>Walk</li>
{#if props.time1}
<li>[{props.time1} - {props.time2}] Walk</li>
{:else}
<li>Walk / cycle / drive</li>
{/if}
{:else}
<li>
Take {props.route} for {props.num_stops} stops
[{props.time1} - {props.time2}] Take {props.route} for {props.num_stops}
stops
</li>
{/if}
{/each}
Expand Down
4 changes: 4 additions & 0 deletions web/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class Backend {
start: { lng: number; lat: number };
mode: TravelMode;
contours: boolean;
startTime: string;
}): FeatureCollection {
if (!this.inner) {
throw new Error("Backend used without a file loaded");
Expand All @@ -78,6 +79,7 @@ export class Backend {
y: req.start.lat,
mode: req.mode,
contours: req.contours,
start_time: req.startTime,
}),
);
}
Expand All @@ -89,6 +91,7 @@ export class Backend {
mode: TravelMode;
debugSearch: boolean;
useHeuristic: boolean;
startTime: string;
}): FeatureCollection {
if (!this.inner) {
throw new Error("Backend used without a file loaded");
Expand All @@ -103,6 +106,7 @@ export class Backend {
mode: req.mode,
debug_search: req.debugSearch,
use_heuristic: req.useHeuristic,
start_time: req.startTime,
}),
);
}
Expand Down

0 comments on commit 80be8d4

Please sign in to comment.