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

Fetch Geometry #236

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions osm2lanes-web/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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())
Expand Down
23 changes: 19 additions & 4 deletions osm2lanes-web/src/lib.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 @@ -104,7 +105,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 @@ -116,14 +119,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 @@ -133,6 +139,7 @@ pub enum Msg {
}

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

Expand All @@ -151,6 +158,7 @@ impl Component for App {
road: None,
message: None,
way_ref: NodeRef::default(),
way_geometry: None,
}));
Self { state }
}
Expand All @@ -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
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -289,7 +304,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::{Circle, 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 @@ -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(),
Expand All @@ -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) => {
Expand Down