-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Just start brute-force taking measurements. Move frontend stuff around a
bit first...
- Loading branch information
1 parent
b515a1d
commit 95bf7d3
Showing
7 changed files
with
177 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |