Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
Fetch Geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
droogmic committed Jun 19, 2022
1 parent 762b74d commit 9a3a747
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
5 changes: 3 additions & 2 deletions osm2lanes-web/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Component for Control {
let examples: BTreeMap<_, _> = tests
.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();
Expand All @@ -69,9 +69,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())
Expand Down
23 changes: 19 additions & 4 deletions osm2lanes-web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -102,7 +103,9 @@ type ShouldRender = bool;

#[derive(Debug, PartialEq)]
pub struct State {
/// Way locale
pub locale: Locale,
/// Way id
pub id: Option<String>,
/// The editable input, line and equal separated tags
pub edit_tags: String,
Expand All @@ -114,14 +117,17 @@ pub struct State {
pub message: Option<String>,
/// Ref to input for way id
pub way_ref: NodeRef,
/// Way geometry
pub way_geometry: Option<LineString<f64>>,
}

#[derive(Debug)]
pub enum Msg {
TagsSet(String),
TagsLocaleSet {
TagsLocaleGeoSet {
id: String,
tags: Tags,
geometry: Option<LineString<f64>>,
locale: Locale,
},
ToggleDrivingSide,
Expand All @@ -131,6 +137,7 @@ pub enum Msg {
}

pub struct App {
// TODO: maybe use Context instead of Props?
state: Rc<RefCell<State>>,
}

Expand All @@ -149,6 +156,7 @@ impl Component for App {
road: None,
message: None,
way_ref: NodeRef::default(),
way_geometry: None,
}));
Self { state }
}
Expand All @@ -164,12 +172,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
Expand Down Expand Up @@ -206,9 +220,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()),
Expand Down Expand Up @@ -287,7 +302,7 @@ impl Component for App {
}
<Canvas callback_error={callback_error} state={Rc::clone(&self.state)}/>
<hr/>
<MapComponent callback_msg={callback_msg.clone()}/>
<MapComponent callback_msg={callback_msg.clone()} state={Rc::clone(&self.state)}/>
</div>
}
}
Expand Down
16 changes: 12 additions & 4 deletions osm2lanes-web/src/map.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::cell::RefCell;
use std::rc::Rc;

use geo::{LineString, Point};
use leaflet::{LatLng, Map, MouseEvent, Path, Polyline, TileLayer};
use osm2lanes::locale::Locale;
Expand All @@ -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 {
Expand All @@ -22,6 +25,7 @@ pub(crate) enum Msg {
#[derive(Properties, Clone, PartialEq)]
pub(crate) struct Props {
pub(crate) callback_msg: Callback<WebMsg>,
pub(crate) state: Rc<RefCell<State>>,
}

pub(crate) struct MapComponent {
Expand Down Expand Up @@ -125,6 +129,7 @@ impl Component for MapComponent {

let polyline = Polyline::new(
geometry
.clone()
.into_iter()
.map(|coordinate| LatLng::new(coordinate.x, coordinate.y).into())
.collect(),
Expand All @@ -134,9 +139,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) => {
Expand Down

0 comments on commit 9a3a747

Please sign in to comment.