Skip to content

Commit

Permalink
Fix score mode crash and clean up route API internally
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Aug 25, 2024
1 parent 260cde1 commit e9d40bd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 54 deletions.
2 changes: 1 addition & 1 deletion backend/Cargo.lock

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

21 changes: 8 additions & 13 deletions backend/src/heatmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use geojson::FeatureCollection;
use graph::IntersectionID;
use rstar::{primitives::GeomWithData, RTree};

use crate::{CompareRouteRequest, MapModel, RoadKind};
use crate::{MapModel, RoadKind};

// Walk along severances. Every X meters, try to cross from one side to the other.
//
Expand All @@ -17,7 +17,7 @@ pub fn along_severances(map: &MapModel) -> FeatureCollection {
for r in &map.graph.roads {
if map.road_kinds[r.id.0] == Some(RoadKind::Severance) {
for line in make_perpendicular_offsets(&r.linestring, 25.0, 15.0) {
requests.push(line.into());
requests.push((line.start, line.end));
}
}
}
Expand Down Expand Up @@ -50,26 +50,21 @@ pub fn nearby_footway_intersections(map: &MapModel, dist_meters: f64) -> Feature
// For every intersection, try to go to every nearby intersection
let mut requests = Vec::new();
for i1 in &footway_intersections {
let i1_pt = map.graph.intersections[i1.0].point;
let i1_pt: Coord = map.graph.intersections[i1.0].point.into();
for i2 in rtree.locate_within_distance(i1_pt.into(), dist_meters) {
// TODO Skip trivial things connected by a road
let i2_pt = map.graph.intersections[i2.data.0].point;
requests.push(CompareRouteRequest {
x1: i1_pt.x(),
y1: i1_pt.y(),
x2: i2_pt.x(),
y2: i2_pt.y(),
});
let i2_pt = map.graph.intersections[i2.data.0].point.into();
requests.push((i1_pt, i2_pt));
}
}
calculate(map, requests)
}

fn calculate(map: &MapModel, requests: Vec<CompareRouteRequest>) -> FeatureCollection {
fn calculate(map: &MapModel, requests: Vec<(Coord, Coord)>) -> FeatureCollection {
let mut samples = Vec::new();
let mut max_score = 0.0_f64;
for req in requests {
if let Ok((mut f, fc)) = crate::route::do_route(map, req) {
for (start, end) in requests {
if let Ok((mut f, fc)) = crate::route::do_route(map, start, end) {
let direct = fc
.foreign_members
.as_ref()
Expand Down
28 changes: 4 additions & 24 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate log;

use std::sync::Once;

use geo::{Coord, Line};
use geo::Coord;
use geojson::GeoJson;
use graph::{Direction, Graph, Mode, Road, Timer};
use serde::Deserialize;
Expand Down Expand Up @@ -95,24 +95,15 @@ impl MapModel {
#[wasm_bindgen(js_name = compareRoute)]
pub fn compare_route(&self, input: JsValue) -> Result<String, JsValue> {
let req: CompareRouteRequest = serde_wasm_bindgen::from_value(input)?;
let pt1 = self.graph.mercator.pt_to_mercator(Coord {
let start = self.graph.mercator.pt_to_mercator(Coord {
x: req.x1,
y: req.y1,
});
let pt2 = self.graph.mercator.pt_to_mercator(Coord {
let end = self.graph.mercator.pt_to_mercator(Coord {
x: req.x2,
y: req.y2,
});
let (_, gj) = route::do_route(
self,
CompareRouteRequest {
x1: pt1.x,
y1: pt1.y,
x2: pt2.x,
y2: pt2.y,
},
)
.map_err(err_to_js)?;
let (_, gj) = route::do_route(self, start, end).map_err(err_to_js)?;
let out = serde_json::to_string(&gj).map_err(err_to_js)?;
Ok(out)
}
Expand Down Expand Up @@ -163,17 +154,6 @@ pub struct CompareRouteRequest {
y2: f64,
}

impl From<Line> for CompareRouteRequest {
fn from(line: Line) -> Self {
Self {
x1: line.start.x,
y1: line.start.y,
x2: line.end.x,
y2: line.end.y,
}
}
}

fn err_to_js<E: std::fmt::Display>(err: E) -> JsValue {
JsValue::from_str(&err.to_string())
}
20 changes: 4 additions & 16 deletions backend/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,12 @@ use geojson::{Feature, FeatureCollection, Geometry};
use graph::{Mode, PathStep};
use serde::Serialize;

use crate::{CompareRouteRequest, MapModel};
use crate::MapModel;

// Also returns the line of the snapped request (in WGS84)
pub fn do_route(map: &MapModel, req: CompareRouteRequest) -> Result<(Feature, FeatureCollection)> {
let start = map.graph.snap_to_road(
Coord {
x: req.x1,
y: req.y1,
},
Mode::Foot,
);
let end = map.graph.snap_to_road(
Coord {
x: req.x2,
y: req.y2,
},
Mode::Foot,
);
pub fn do_route(map: &MapModel, start: Coord, end: Coord) -> Result<(Feature, FeatureCollection)> {
let start = map.graph.snap_to_road(start, Mode::Foot);
let end = map.graph.snap_to_road(end, Mode::Foot);

let route = map.graph.router[Mode::Foot].route(&map.graph, start, end)?;
let route_linestring = route.linestring(&map.graph);
Expand Down

0 comments on commit e9d40bd

Please sign in to comment.