Skip to content

Commit

Permalink
Get routing to work!!
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Dec 2, 2023
1 parent 0a72140 commit 5fcf221
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
16 changes: 14 additions & 2 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate anyhow;
extern crate log;

use std::collections::HashMap;
use std::fmt;
use std::sync::Once;

use fast_paths::{FastGraph, PathCalculator};
Expand Down Expand Up @@ -36,6 +37,18 @@ pub struct RoadID(pub usize);
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize)]
pub struct IntersectionID(pub usize);

impl fmt::Display for RoadID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Road #{}", self.0)
}
}

impl fmt::Display for IntersectionID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Intersection #{}", self.0)
}
}

pub struct Road {
id: RoadID,
src_i: IntersectionID,
Expand Down Expand Up @@ -101,8 +114,7 @@ impl MapModel {
return road;
}
}
// TODO why broken...
panic!("no road from {} to {} or vice versa", i1.0, i2.0);
panic!("no road from {i1} to {i2} or vice versa");
}
}

Expand Down
7 changes: 3 additions & 4 deletions backend/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ pub fn do_route(map: &mut MapModel, req: CompareRouteRequest) -> Result<GeoJson>
if start == end {
bail!("start = end");
}

if let Some(path) = map.path_calc.calc_path(&map.ch, start, end) {
let mut features = Vec::new();
for pair in path.get_nodes().windows(2) {
let src_i = map.node_map.translate_id(pair[0]);
let dst_i = map.node_map.translate_id(pair[1]);
let road = map.find_edge(src_i, dst_i);
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());
}
return Ok(GeoJson::from(features));
Expand Down
32 changes: 19 additions & 13 deletions backend/src/scrape.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, HashMap};
use std::collections::HashMap;

use anyhow::Result;
use geo::{Coord, Geometry, GeometryCollection, LineString, MapCoordsInPlace, Point, Polygon};
Expand Down Expand Up @@ -61,7 +61,8 @@ fn split_edges(
}

// Split each way into edges
let mut intersections = BTreeMap::new();
let mut node_to_intersection: HashMap<NodeID, IntersectionID> = HashMap::new();
let mut intersections = Vec::new();
let mut roads = Vec::new();
for way in ways {
let mut node1 = way.node_ids[0];
Expand All @@ -79,15 +80,22 @@ fn split_edges(

let mut i_ids = Vec::new();
for (n, point) in [(node1, pts[0]), (node, *pts.last().unwrap())] {
let next_id = IntersectionID(intersections.len());
let i = intersections.entry(n).or_insert_with(|| Intersection {
id: next_id,
node: n,
point: Point(point),
roads: Vec::new(),
});
i.roads.push(road_id);
i_ids.push(i.id);
let intersection = if let Some(i) = node_to_intersection.get(&n) {
&mut intersections[i.0]
} else {
let i = IntersectionID(intersections.len());
intersections.push(Intersection {
id: i,
node: n,
point: Point(point),
roads: Vec::new(),
});
node_to_intersection.insert(n, i);
&mut intersections[i.0]
};

intersection.roads.push(road_id);
i_ids.push(intersection.id);
}

roads.push(Road {
Expand All @@ -108,8 +116,6 @@ fn split_edges(
}
}

let intersections = intersections.into_values().collect();

(roads, intersections)
}

Expand Down

0 comments on commit 5fcf221

Please sign in to comment.