From 166ed95bf739fbe0110aade77f567ec07c1a9e7d Mon Sep 17 00:00:00 2001 From: Michael Droogleever Fortuyn Date: Sun, 19 Jun 2022 09:11:52 +0200 Subject: [PATCH] Fetch Geometry --- osm2lanes-web/src/control.rs | 5 +++-- osm2lanes-web/src/lib.rs | 23 +++++++++++++++++++---- osm2lanes-web/src/map.rs | 16 ++++++++++++---- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/osm2lanes-web/src/control.rs b/osm2lanes-web/src/control.rs index 1ff26098..4660a136 100644 --- a/osm2lanes-web/src/control.rs +++ b/osm2lanes-web/src/control.rs @@ -72,7 +72,7 @@ impl Component for Control { .0 .into_iter() .filter_map(|t| { - let example_name = t.example().map(std::borrow::ToOwned::to_owned); + let example_name = t.example().map(ToOwned::to_owned); example_name.map(|e| (e, t)) }) .collect(); @@ -89,9 +89,10 @@ impl Component for Control { .unwrap() .clone(); self.example = Some(example); - ctx.link().send_message(Msg::from(AppMsg::TagsLocaleSet { + ctx.link().send_message(Msg::from(AppMsg::TagsLocaleGeoSet { id: test_case.way_id.unwrap().to_string(), tags: test_case.tags, + geometry: None, locale: Locale::builder() .driving_side(test_case.driving_side) .iso_3166_option(test_case.iso_3166_2.as_deref()) diff --git a/osm2lanes-web/src/lib.rs b/osm2lanes-web/src/lib.rs index aa51e7f7..bb538082 100644 --- a/osm2lanes-web/src/lib.rs +++ b/osm2lanes-web/src/lib.rs @@ -44,6 +44,7 @@ use std::cell::RefCell; use std::rc::Rc; use std::str::FromStr; +use geo::LineString; use osm2lanes::locale::{Country, Locale}; use osm2lanes::overpass::get_way; use osm2lanes::road::{Lane, Printable, Road}; @@ -104,7 +105,9 @@ type ShouldRender = bool; #[derive(Debug, PartialEq)] pub struct State { + /// Way locale pub locale: Locale, + /// Way id pub id: Option, /// The editable input, line and equal separated tags pub edit_tags: String, @@ -116,14 +119,17 @@ pub struct State { pub message: Option, /// Ref to input for way id pub way_ref: NodeRef, + /// Way geometry + pub way_geometry: Option>, } #[derive(Debug)] pub enum Msg { TagsSet(String), - TagsLocaleSet { + TagsLocaleGeoSet { id: String, tags: Tags, + geometry: Option>, locale: Locale, }, ToggleDrivingSide, @@ -133,6 +139,7 @@ pub enum Msg { } pub struct App { + // TODO: maybe use Context instead of Props? state: Rc>, } @@ -151,6 +158,7 @@ impl Component for App { road: None, message: None, way_ref: NodeRef::default(), + way_geometry: None, })); Self { state } } @@ -166,12 +174,18 @@ impl Component for App { self.update_tags(); true }, - Msg::TagsLocaleSet { id, tags, locale } => { + Msg::TagsLocaleGeoSet { + id, + tags, + geometry, + locale, + } => { { let mut state = self.state.borrow_mut(); state.edit_tags = tags.to_string(); state.locale = locale; state.id = Some(id); + state.way_geometry = geometry; } self.update_tags(); true @@ -208,9 +222,10 @@ impl Component for App { Ok(way_id) => { ctx.link().send_future(async move { match get_way(&way_id).await { - Ok((tags, _geom, locale)) => Msg::TagsLocaleSet { + Ok((tags, geometry, locale)) => Msg::TagsLocaleGeoSet { id: way_id.to_string(), tags, + geometry: Some(geometry), locale, }, Err(e) => Msg::Error(e.to_string()), @@ -289,7 +304,7 @@ impl Component for App { }
- + } } diff --git a/osm2lanes-web/src/map.rs b/osm2lanes-web/src/map.rs index 88abcc3e..a9dca99a 100644 --- a/osm2lanes-web/src/map.rs +++ b/osm2lanes-web/src/map.rs @@ -1,3 +1,6 @@ +use std::cell::RefCell; +use std::rc::Rc; + use geo::{LineString, Point}; use leaflet::{Circle, LatLng, Map, MouseEvent, Path, Polyline, TileLayer}; use osm2lanes::locale::Locale; @@ -10,7 +13,7 @@ use web_sys::{Element, HtmlElement}; use yew::prelude::*; use yew::Html; -use crate::Msg as WebMsg; +use crate::{Msg as WebMsg, State}; #[allow(clippy::large_enum_variant)] pub(crate) enum Msg { @@ -22,6 +25,7 @@ pub(crate) enum Msg { #[derive(Properties, Clone, PartialEq)] pub(crate) struct Props { pub(crate) callback_msg: Callback, + pub(crate) state: Rc>, } pub(crate) struct MapComponent { @@ -137,6 +141,7 @@ impl Component for MapComponent { let polyline = Polyline::new( geometry + .clone() .into_iter() .map(|coordinate| LatLng::new(coordinate.x, coordinate.y).into()) .collect(), @@ -146,9 +151,12 @@ impl Component for MapComponent { path.addTo(&self.map); self.path = Some(path); self.map.fitBounds(&bounds); - ctx.props() - .callback_msg - .emit(WebMsg::TagsLocaleSet { id, tags, locale }); + ctx.props().callback_msg.emit(WebMsg::TagsLocaleGeoSet { + id, + tags, + geometry: Some(geometry), + locale, + }); true }, Msg::Error(e) => {