Skip to content

Commit

Permalink
Show the snapped test lines instead, for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Dec 11, 2023
1 parent 46b6d8f commit 4cc27bb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
14 changes: 2 additions & 12 deletions backend/src/heatmap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashSet;

use geo::{Coord, Densify, Line, LineString};
use geojson::{Feature, FeatureCollection};
use geojson::FeatureCollection;
use rstar::{primitives::GeomWithData, RTree};

use crate::{CompareRouteRequest, IntersectionID, MapModel, RoadKind};
Expand Down Expand Up @@ -65,17 +65,7 @@ fn calculate(map: &mut MapModel, requests: Vec<CompareRouteRequest>) -> FeatureC
let mut samples = Vec::new();
let mut max_score = 0.0_f64;
for req in requests {
let mut f = Feature::from(geojson::Geometry::from(&LineString::new(vec![
map.mercator.to_wgs84(Coord {
x: req.x1,
y: req.y1,
}),
map.mercator.to_wgs84(Coord {
x: req.x2,
y: req.y2,
}),
])));
if let Ok(fc) = crate::route::do_route(map, req) {
if let Ok((mut f, fc)) = crate::route::do_route(map, req) {
let direct = fc
.foreign_members
.as_ref()
Expand Down
2 changes: 1 addition & 1 deletion backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl MapModel {
x: req.x2,
y: req.y2,
});
let gj = route::do_route(
let (_, gj) = route::do_route(
self,
CompareRouteRequest {
x1: pt1.x,
Expand Down
62 changes: 37 additions & 25 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, LineString};
use geojson::FeatureCollection;
use geo::{HaversineLength, LineString, MapCoords};
use geojson::{Feature, FeatureCollection};
use rstar::RTree;

use crate::node_map::NodeMap;
Expand Down Expand Up @@ -52,7 +52,11 @@ fn build_closest_intersection(
RTree::bulk_load(points)
}

pub fn do_route(map: &mut MapModel, req: CompareRouteRequest) -> Result<FeatureCollection> {
// Also returns the line of the snapped request (in WGS84)
pub fn do_route(
map: &mut MapModel,
req: CompareRouteRequest,
) -> Result<(Feature, FeatureCollection)> {
let start = map
.closest_intersection
.nearest_neighbor(&[req.x1, req.y1])
Expand All @@ -66,7 +70,20 @@ pub fn do_route(map: &mut MapModel, req: CompareRouteRequest) -> Result<FeatureC
if start == end {
bail!("start = end");
}

if let Some(path) = map.path_calc.calc_path(&map.ch, start, end) {
let direct_line = 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(),
]);
let direct_feature = Feature::from(geojson::Geometry::from(
&direct_line.map_coords(|c| map.mercator.to_wgs84(c)),
));

let mut features = Vec::new();
let mut route_length = 0.0;
for pair in path.get_nodes().windows(2) {
Expand All @@ -76,28 +93,23 @@ pub fn do_route(map: &mut MapModel, req: CompareRouteRequest) -> Result<FeatureC
features.push(road.to_gj(&map.mercator));
route_length += road.linestring.haversine_length();
}
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(),
),
});
let direct_length = direct_line.haversine_length();
return Ok((
direct_feature,
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");
}

0 comments on commit 4cc27bb

Please sign in to comment.