Skip to content

Commit

Permalink
Plumb through filter types in the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Jan 6, 2024
1 parent a853fd7 commit f5b6dbd
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 16 deletions.
14 changes: 11 additions & 3 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use wasm_bindgen::prelude::*;

use self::cells::Cell;
use self::common::*;
use self::map_model::{Intersection, IntersectionID, MapModel, ModalFilter, Road, RoadID};
use self::map_model::{
FilterKind, Intersection, IntersectionID, MapModel, ModalFilter, Road, RoadID,
};
use self::neighbourhood::Neighbourhood;
use self::render_cells::RenderCells;
use self::route::Router;
Expand Down Expand Up @@ -120,28 +122,34 @@ impl LTN {

/// Takes a LngLat
#[wasm_bindgen(js_name = addModalFilter)]
pub fn add_modal_filter(&mut self, input: JsValue) -> Result<String, JsValue> {
pub fn add_modal_filter(&mut self, input: JsValue, kind: String) -> Result<String, JsValue> {
let pos: LngLat = serde_wasm_bindgen::from_value(input)?;
self.map.add_modal_filter(
self.map.mercator.pt_to_mercator(Coord {
x: pos.lng,
y: pos.lat,
}),
&self.neighbourhood.as_ref().unwrap().interior_roads,
FilterKind::from_string(&kind).unwrap(),
);
self.render_neighbourhood()
}

/// Takes a LineString feature
#[wasm_bindgen(js_name = addManyModalFilters)]
pub fn add_many_modal_filters(&mut self, input: JsValue) -> Result<String, JsValue> {
pub fn add_many_modal_filters(
&mut self,
input: JsValue,
kind: String,
) -> Result<String, JsValue> {
let gj: Feature = serde_wasm_bindgen::from_value(input)?;
let mut linestring: LineString = gj.try_into().map_err(err_to_js)?;
self.map.mercator.to_mercator_in_place(&mut linestring);

self.map.add_many_modal_filters(
linestring,
&self.neighbourhood.as_ref().unwrap().interior_roads,
FilterKind::from_string(&kind).unwrap(),
);
self.render_neighbourhood()
}
Expand Down
77 changes: 66 additions & 11 deletions backend/src/map_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,24 @@ impl MapModel {
panic!("no road from {i1} to {i2} or vice versa");
}

pub fn add_modal_filter(&mut self, click_pt: Coord, candidate_roads: &HashSet<RoadID>) {
let cmd = self.do_edit(self.add_modal_filter_cmd(click_pt, candidate_roads));
pub fn add_modal_filter(
&mut self,
click_pt: Coord,
candidate_roads: &HashSet<RoadID>,
kind: FilterKind,
) {
let cmd = self.do_edit(self.add_modal_filter_cmd(click_pt, candidate_roads, kind));
self.undo_stack.push(cmd);
self.redo_queue.clear();
self.after_edited();
}

fn add_modal_filter_cmd(&self, click_pt: Coord, candidate_roads: &HashSet<RoadID>) -> Command {
fn add_modal_filter_cmd(
&self,
click_pt: Coord,
candidate_roads: &HashSet<RoadID>,
kind: FilterKind,
) -> Command {
// TODO prune with rtree?
let (_, r, percent_along) = candidate_roads
.iter()
Expand All @@ -113,7 +123,13 @@ impl MapModel {
})
.min_by_key(|pair| pair.0)
.unwrap();
Command::SetModalFilter(r, Some(ModalFilter { percent_along }))
Command::SetModalFilter(
r,
Some(ModalFilter {
percent_along,
kind,
}),
)
}

fn after_edited(&mut self) {
Expand All @@ -124,14 +140,18 @@ impl MapModel {
&mut self,
along_line: LineString,
candidate_roads: &HashSet<RoadID>,
kind: FilterKind,
) {
let mut edits = Vec::new();
for r in candidate_roads {
let road = self.get_r(*r);
if let Some(percent_along) = linestring_intersection(&road.linestring, &along_line) {
edits.push(Command::SetModalFilter(
*r,
Some(ModalFilter { percent_along }),
Some(ModalFilter {
percent_along,
kind,
}),
));
}
}
Expand Down Expand Up @@ -203,6 +223,7 @@ impl MapModel {
// TODO Maybe make the WASM API always do the mercator stuff
let mut f = Feature::from(Geometry::from(&self.mercator.to_wgs84(&pt)));
f.set_property("kind", "modal_filter");
f.set_property("filter_kind", modal_filter.kind.to_string());
features.push(f);
}

Expand Down Expand Up @@ -233,13 +254,16 @@ impl MapModel {
for f in gj.features {
match f.property("kind").unwrap().as_str().unwrap() {
"modal_filter" => {
let kind = FilterKind::from_string(
f.property("filter_kind").unwrap().as_str().unwrap(),
)
.unwrap();
let gj_pt: Point = f.geometry.unwrap().try_into()?;
cmds.push(
self.add_modal_filter_cmd(
self.mercator.pt_to_mercator(gj_pt.into()),
&all_roads,
),
);
cmds.push(self.add_modal_filter_cmd(
self.mercator.pt_to_mercator(gj_pt.into()),
&all_roads,
kind,
));
}
"boundary" => {
if boundary.is_some() {
Expand Down Expand Up @@ -299,9 +323,40 @@ impl Road {

#[derive(Clone)]
pub struct ModalFilter {
pub kind: FilterKind,
pub percent_along: f64,
}

#[derive(Clone, Copy)]
pub enum FilterKind {
WalkCycleOnly,
NoEntry,
BusGate,
SchoolStreet,
}

// TODO strum?
impl FilterKind {
pub fn to_string(self) -> &'static str {
match self {
FilterKind::WalkCycleOnly => "walk_cycle_only",
FilterKind::NoEntry => "no_entry",
FilterKind::BusGate => "bus_gate",
FilterKind::SchoolStreet => "school_street",
}
}

pub fn from_string(x: &str) -> Option<Self> {
match x {
"walk_cycle_only" => Some(FilterKind::WalkCycleOnly),
"no_entry" => Some(FilterKind::NoEntry),
"bus_gate" => Some(FilterKind::BusGate),
"school_street" => Some(FilterKind::SchoolStreet),
_ => None,
}
}
}

pub enum Command {
SetModalFilter(RoadID, Option<ModalFilter>),
Multiple(Vec<Command>),
Expand Down
1 change: 1 addition & 0 deletions backend/src/neighbourhood.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl Neighbourhood {
.unwrap();
let mut f = Feature::from(Geometry::from(&map.mercator.to_wgs84(&pt)));
f.set_property("kind", "modal_filter");
f.set_property("filter_kind", filter.kind.to_string());
f.set_property("road", r.0);
features.push(f);
}
Expand Down
4 changes: 2 additions & 2 deletions web/src/NeighbourhoodMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
//$app!.unsetNeighbourhood();
});
function onClick(e: MapMouseEvent) {
render($app!.addModalFilter(e.lngLat));
render($app!.addModalFilter(e.lngLat, filterType));
stopAddingFilter();
}
function stopAddingFilter() {
Expand Down Expand Up @@ -93,7 +93,7 @@
function gotFreehandLine(e: CustomEvent<Feature<LineString> | null>) {
let f = e.detail;
if (f) {
render($app!.addManyModalFilters(f));
render($app!.addManyModalFilters(f, filterType));
}
addingMultipleFilters = false;
Expand Down

0 comments on commit f5b6dbd

Please sign in to comment.