Skip to content

Commit

Permalink
Show a diversion score
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Dec 4, 2023
1 parent 5ccfeba commit e62892d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
26 changes: 22 additions & 4 deletions backend/src/route.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{bail, Result};
use fast_paths::{FastGraph, InputGraph};
use geo::HaversineLength;
use geojson::GeoJson;
use geo::{HaversineLength, LineString};
use geojson::FeatureCollection;
use rstar::primitives::GeomWithData;
use rstar::RTree;

Expand Down Expand Up @@ -50,7 +50,7 @@ fn build_closest_intersection(
RTree::bulk_load(points)
}

pub fn do_route(map: &mut MapModel, req: CompareRouteRequest) -> Result<GeoJson> {
pub fn do_route(map: &mut MapModel, req: CompareRouteRequest) -> Result<FeatureCollection> {
let start = map
.closest_intersection
.nearest_neighbor(&[req.x1, req.y1])
Expand All @@ -66,13 +66,31 @@ pub fn do_route(map: &mut MapModel, req: CompareRouteRequest) -> Result<GeoJson>
}
if let Some(path) = map.path_calc.calc_path(&map.ch, start, end) {
let mut features = Vec::new();
let mut route_length = 0.0;
for pair in path.get_nodes().windows(2) {
let i1 = map.node_map.translate_id(pair[0]);
let i2 = map.node_map.translate_id(pair[1]);
let road = map.find_edge(i1, i2);
features.push(road.to_gj());
route_length += road.linestring.haversine_length();
}
return Ok(GeoJson::from(features));
let direct_length = LineString::new(vec![
map.intersections[map.node_map.translate_id(start).0]
.point
.into(),
map.intersections[map.node_map.translate_id(end).0]
.point
.into(),
])
.haversine_length();
return Ok(FeatureCollection {
features,
bbox: None,
foreign_members: Some(serde_json::json!({
"direct_length": direct_length,
"route_length": route_length,
}).as_object().unwrap().clone()),
});
}
bail!("No path");
}
18 changes: 18 additions & 0 deletions web/src/Directions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
export let route_gj;
function levelChanges(gj) {
let count = 0;
// No windows(2)?
for (let i = 0; i < gj.features.length - 1; i++) {
let l1 = route_gj.features[i].properties.layer ?? "0";
let l2 = route_gj.features[i + 1].properties.layer ?? "0";
if (l1 != l2) {
count++;
}
}
return count;
}
function step(f) {
classifyStep(f);
let props = f.properties;
Expand All @@ -16,6 +29,11 @@
}
</script>

<p>
Crow-flies {route_gj.direct_length.toFixed(0)} vs actual {route_gj.route_length.toFixed(0)}:
<b>{(route_gj.route_length / route_gj.direct_length).toFixed(1)}</b>
</p>
<p>{levelChanges(route_gj)} changes in level</p>
<ol>
{#each route_gj.features as f}
<li><a href={f.properties.way} target="_blank">{@html step(f)}</a></li>
Expand Down

0 comments on commit e62892d

Please sign in to comment.