Skip to content

Commit

Permalink
Just start brute-force taking measurements. Move frontend stuff around a
Browse files Browse the repository at this point in the history
bit first...
  • Loading branch information
dabreegster committed Dec 6, 2023
1 parent b515a1d commit 95bf7d3
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 73 deletions.
26 changes: 26 additions & 0 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ anyhow = "1.0.75"
osmpbf = "0.3.2"
rstar = { version = "0.11.0" }
fast_paths = { git = "https://github.com/easbar/fast_paths" }
rand = { version = "0.8.5", default-features = false }
rand_xorshift = "0.3.0"
55 changes: 55 additions & 0 deletions backend/src/heatmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use geo::{Rect, GeometryCollection, BoundingRect, Geometry, LineString};
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;
use geojson::{Feature, FeatureCollection};

use crate::{CompareRouteRequest, MapModel};

pub fn measure_randomly(map: &mut MapModel, n: usize) -> FeatureCollection {
// TODO Expensive
let bbox: Rect<f64> = map
.roads
.iter()
.map(|r| Geometry::LineString(r.linestring.clone()))
.collect::<GeometryCollection>()
.bounding_rect()
.unwrap();
// TODO Do this in the right coordinate space
let dist_away = 0.1;

let mut rng = XorShiftRng::seed_from_u64(42);
let mut samples = Vec::new();
for _ in 0..n {
let x1 = rng.gen_range(bbox.min().x..=bbox.max().x);
let y1 = rng.gen_range(bbox.min().y..=bbox.max().y);
let x2 = x1 + rng.gen_range(-dist_away..=dist_away);
let y2 = y1 + rng.gen_range(-dist_away..=dist_away);
let req = CompareRouteRequest { x1, y1, x2, y2 };
if let Ok(fc) = crate::route::do_route(map, req) {
let direct = fc
.foreign_members
.as_ref()
.unwrap()
.get("direct_length")
.unwrap()
.as_f64()
.unwrap();
let route = fc
.foreign_members
.unwrap()
.get("route_length")
.unwrap()
.as_f64()
.unwrap();
let score = route / direct;
let mut f = Feature::from(geojson::Geometry::from(&LineString::new(vec![(x1, y1).into(), (x2, y2).into()])));
f.set_property("score", score);
samples.push(f);
}
}
FeatureCollection {
features: samples,
bbox: None,
foreign_members: None,
}
}
8 changes: 8 additions & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rstar::{primitives::GeomWithData, RTree};
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;

mod heatmap;
mod node_map;
mod osm;
mod parse_osm;
Expand Down Expand Up @@ -120,6 +121,13 @@ impl MapModel {
Ok(out)
}

#[wasm_bindgen(js_name = makeHeatmap)]
pub fn make_heatmap(&mut self) -> Result<String, JsValue> {
let samples = heatmap::measure_randomly(self, 100);
let out = serde_json::to_string(&samples).map_err(err_to_js)?;
Ok(out)
}

fn find_edge(&self, i1: IntersectionID, i2: IntersectionID) -> &Road {
// TODO Store lookup table
for r in &self.intersections[i1.0].roads {
Expand Down
77 changes: 4 additions & 73 deletions web/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import Layout from "./Layout.svelte";
import Legend from "./Legend.svelte";
import Loading from "./Loading.svelte";
import NetworkLayer from "./NetworkLayer.svelte";
import RouteLayer from "./RouteLayer.svelte";
let model: MapModel | undefined = undefined;
let map;
Expand Down Expand Up @@ -83,20 +85,6 @@
route_err = err.toString();
}
}
export function constructMatchExpression<OutputType>(
getter: any[],
map: { [name: string]: OutputType },
fallback: OutputType
): DataDrivenPropertyValueSpecification<OutputType> {
let x: any[] = ["match", getter];
for (let [key, value] of Object.entries(map)) {
x.push(key);
x.push(value);
}
x.push(fallback);
return x as DataDrivenPropertyValueSpecification<OutputType>;
}
</script>

<Layout>
Expand Down Expand Up @@ -130,67 +118,10 @@
bind:map
>
{#if model}
<GeoJSON data={JSON.parse(model.render())}>
<LineLayer
id="network"
paint={{
"line-width": 5,
"line-color": constructMatchExpression(
["get", "kind"],
{
Footway: "red",
Indoors: "blue",
BridgeOrTunnel: "purple",
Sidewalk: "black",
Crossing: "green",
Severance: "orange",
},
"yellow"
),
}}
on:click={(e) =>
window.open(e.detail.features[0].properties.way, "_blank")}
hoverCursor="pointer"
>
<Popup openOn="hover" let:data
>{@html JSON.stringify(data.properties, null, "<br />")}</Popup
>
</LineLayer>
</GeoJSON>
{#if route_a}
<Marker bind:lngLat={route_a} draggable
><span class="dot">A</span></Marker
>
<Marker bind:lngLat={route_b} draggable
><span class="dot">B</span></Marker
>
{/if}
{#if route_gj}
<GeoJSON data={route_gj}>
<LineLayer
id="route"
beforeId="network"
paint={{
"line-width": 20,
"line-color": "cyan",
"line-opacity": 0.5,
}}
/>
</GeoJSON>
{/if}
<NetworkLayer {model} />
<RouteLayer bind:route_a bind:route_b {route_gj} />
{/if}
</MapLibre>
</div>
</Layout>
<Loading {loading} />

<style>
.dot {
width: 30px;
height: 30px;
border-radius: 50%;
display: inline-block;
background-color: grey;
text-align: center;
}
</style>
46 changes: 46 additions & 0 deletions web/src/NetworkLayer.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script lang="ts">
import { GeoJSON, LineLayer, Popup } from "svelte-maplibre";
export let model;
function constructMatchExpression<OutputType>(
getter: any[],
map: { [name: string]: OutputType },
fallback: OutputType
): DataDrivenPropertyValueSpecification<OutputType> {
let x: any[] = ["match", getter];
for (let [key, value] of Object.entries(map)) {
x.push(key);
x.push(value);
}
x.push(fallback);
return x as DataDrivenPropertyValueSpecification<OutputType>;
}
</script>

<GeoJSON data={JSON.parse(model.render())}>
<LineLayer
id="network"
paint={{
"line-width": 5,
"line-color": constructMatchExpression(
["get", "kind"],
{
Footway: "red",
Indoors: "blue",
BridgeOrTunnel: "purple",
Sidewalk: "black",
Crossing: "green",
Severance: "orange",
},
"yellow"
),
}}
on:click={(e) => window.open(e.detail.features[0].properties.way, "_blank")}
hoverCursor="pointer"
>
<Popup openOn="hover" let:data
>{@html JSON.stringify(data.properties, null, "<br />")}</Popup
>
</LineLayer>
</GeoJSON>
36 changes: 36 additions & 0 deletions web/src/RouteLayer.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<script lang="ts">
import { GeoJSON, LineLayer, Marker } from "svelte-maplibre";
export let route_a;
export let route_b;
export let route_gj;
</script>

{#if route_a}
<Marker bind:lngLat={route_a} draggable><span class="dot">A</span></Marker>
<Marker bind:lngLat={route_b} draggable><span class="dot">B</span></Marker>
{/if}
{#if route_gj}
<GeoJSON data={route_gj}>
<LineLayer
id="route"
beforeId="network"
paint={{
"line-width": 20,
"line-color": "cyan",
"line-opacity": 0.5,
}}
/>
</GeoJSON>
{/if}

<style>
.dot {
width: 30px;
height: 30px;
border-radius: 50%;
display: inline-block;
background-color: grey;
text-align: center;
}
</style>

0 comments on commit 95bf7d3

Please sign in to comment.