Skip to content

Commit

Permalink
Show two routes, before & after
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Jan 5, 2024
1 parent 165eb27 commit 9f8ce0a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
22 changes: 19 additions & 3 deletions backend/src/map_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ pub struct MapModel {
pub intersections: Vec<Intersection>,
// All geometry stored in worldspace, including rtrees
pub mercator: Mercator,
pub router: Router,
// TODO Wasteful, can share some
pub router_original: Router,
pub router_current: Router,

// TODO Keep edits / state here or not?
pub modal_filters: BTreeMap<RoadID, ModalFilter>,
Expand Down Expand Up @@ -111,6 +113,11 @@ impl MapModel {
));
self.undo_stack.push(cmd);
self.redo_queue.clear();
self.after_edited();
}

fn after_edited(&mut self) {
self.router_current = Router::new(&self.roads, &self.intersections, &self.modal_filters);
}

pub fn add_many_modal_filters(
Expand All @@ -131,12 +138,14 @@ impl MapModel {
let cmd = self.do_edit(Command::Multiple(edits));
self.undo_stack.push(cmd);
self.redo_queue.clear();
self.after_edited();
}

pub fn delete_modal_filter(&mut self, r: RoadID) {
let cmd = self.do_edit(Command::SetModalFilter(r, None));
self.undo_stack.push(cmd);
self.redo_queue.clear();
self.after_edited();
}

// Returns the command to undo this one
Expand Down Expand Up @@ -166,6 +175,7 @@ impl MapModel {
if let Some(cmd) = self.undo_stack.pop() {
let cmd = self.do_edit(cmd);
self.redo_queue.push(cmd);
self.after_edited();
}
}

Expand All @@ -176,6 +186,7 @@ impl MapModel {
let cmd = self.redo_queue.remove(0);
let cmd = self.do_edit(cmd);
self.undo_stack.push(cmd);
self.after_edited();
}

pub fn to_savefile(&self, neighbourhood: Option<&Neighbourhood>) -> GeoJson {
Expand Down Expand Up @@ -236,14 +247,19 @@ impl MapModel {
x => bail!("Unknown kind in savefile {x}"),
}
}
self.router_current = Router::new(&self.roads, &self.intersections, &self.modal_filters);

Ok(boundary)
}

pub fn compare_route(&self, pt1: Coord, pt2: Coord) -> GeoJson {
let mut features = Vec::new();
// TODO before, after
if let Some(linestring) = self.router.route(self, pt1, pt2) {
if let Some(linestring) = self.router_original.route(self, pt1, pt2) {
let mut f = Feature::from(Geometry::from(&self.mercator.to_wgs84(&linestring)));
f.set_property("kind", "before");
features.push(f);
}
if let Some(linestring) = self.router_current.route(self, pt1, pt2) {
let mut f = Feature::from(Geometry::from(&self.mercator.to_wgs84(&linestring)));
f.set_property("kind", "after");
features.push(f);
Expand Down
1 change: 1 addition & 0 deletions backend/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rstar::RTree;

use crate::{Intersection, IntersectionID, MapModel, ModalFilter, NodeMap, Road, RoadID};

#[derive(Clone)]
pub struct Router {
ch: FastGraph,
node_map: NodeMap<IntersectionID>,
Expand Down
6 changes: 4 additions & 2 deletions backend/src/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ pub fn scrape_osm(input_bytes: &[u8]) -> Result<MapModel> {
}

let modal_filters = BTreeMap::new();
let router = Router::new(&roads, &intersections, &modal_filters);
let router_original = Router::new(&roads, &intersections, &modal_filters);
let router_current = router_original.clone();

Ok(MapModel {
roads,
intersections,
mercator,
router,
router_original,
router_current,

modal_filters,
undo_stack: Vec::new(),
Expand Down
2 changes: 1 addition & 1 deletion web/src/RenderNeighbourhood.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
manageHoverState
paint={{
"fill-color": ["get", "color"],
"fill-opacity": hoverStateFilter(0.3, 0.5),
"fill-opacity": interactive ? hoverStateFilter(0.3, 0.5) : 0.3,
}}
/>

Expand Down
17 changes: 14 additions & 3 deletions web/src/RouteMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { LngLat } from "maplibre-gl";
import { onDestroy, onMount } from "svelte";
import { GeoJSON, LineLayer, Marker } from "svelte-maplibre";
import { bbox } from "./common";
import { bbox, constructMatchExpression } from "./common";
import RenderNeighbourhood from "./RenderNeighbourhood.svelte";
import SplitComponent from "./SplitComponent.svelte";
import { app, map, mode } from "./stores";
Expand Down Expand Up @@ -48,6 +48,10 @@
<div><button on:click={back}>Back to editing</button></div>

<p>Drag markers for a route</p>
<p>
<span style="color: red">Route before</span>,
<span style="color: blue">route after</span>
</p>
</div>

<div slot="map">
Expand All @@ -58,8 +62,15 @@
<GeoJSON data={gj}>
<LineLayer
paint={{
"line-width": 5,
"line-color": "red",
"line-width": 10,
"line-color": constructMatchExpression(
["get", "kind"],
{
before: "red",
after: "blue",
},
"red"
),
}}
/>
</GeoJSON>
Expand Down

0 comments on commit 9f8ce0a

Please sign in to comment.