From 99d111ea1787965c607bc05badf510b2042752a9 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Sat, 25 Mar 2023 22:08:25 -0400 Subject: [PATCH] Convert all coordinates to string for wasm --- spatial-coordinate-systems/src/all.rs | 16 ++++++++++++ spatial-coordinate-systems/src/utm.rs | 1 + web-frontend/src/routes/app/+page.svelte | 1 + xpin-wasm/src/lib.rs | 31 ++++++++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/spatial-coordinate-systems/src/all.rs b/spatial-coordinate-systems/src/all.rs index 4b5e384..a183651 100644 --- a/spatial-coordinate-systems/src/all.rs +++ b/spatial-coordinate-systems/src/all.rs @@ -37,6 +37,22 @@ impl TryFrom for Coordinates { } } +impl TryFrom<&crate::Coordinate> for Coordinates { + type Error = Error; + + fn try_from(value: &crate::Coordinate) -> Result { + Self::try_from(LatLon::from(value)) + } +} + +impl TryFrom for Coordinates { + type Error = Error; + + fn try_from(value: crate::Coordinate) -> Result { + Self::try_from(&value) + } +} + impl FromStr for Coordinates { type Err = Error; diff --git a/spatial-coordinate-systems/src/utm.rs b/spatial-coordinate-systems/src/utm.rs index d8ad614..803605d 100644 --- a/spatial-coordinate-systems/src/utm.rs +++ b/spatial-coordinate-systems/src/utm.rs @@ -152,6 +152,7 @@ mod tests { easting: 666962.588, northing: 0.0, + // Allow nearby comparisons latlon: LatLon::new(1.534972646100779e-9, 100.50027886695585).unwrap() }) ); diff --git a/web-frontend/src/routes/app/+page.svelte b/web-frontend/src/routes/app/+page.svelte index 4614c49..7eb3f31 100644 --- a/web-frontend/src/routes/app/+page.svelte +++ b/web-frontend/src/routes/app/+page.svelte @@ -76,6 +76,7 @@ }) .then(async () => { // Initialize the app + // TODO: This leads to a `map is undefined` error updateAddr(wasm.call.EncodedAddress.from_coordinate(coordinateInputValue), true); }) .then(async () => { diff --git a/xpin-wasm/src/lib.rs b/xpin-wasm/src/lib.rs index fdd7908..61f896f 100644 --- a/xpin-wasm/src/lib.rs +++ b/xpin-wasm/src/lib.rs @@ -55,6 +55,9 @@ pub struct EncodedAddress { #[wasm_bindgen(js_name = decimalDegrees)] pub decimal_degrees: String, + + #[wasm_bindgen(js_name = allCoordinates)] + pub all_coordinates: Coordinates, } #[wasm_bindgen] @@ -118,6 +121,30 @@ impl EncodedAddress { } } +#[wasm_bindgen(getter_with_clone)] +#[derive(Debug, Clone)] +pub struct Coordinates { + // pub latlon: LatLon, + pub dd: String, + pub dms: String, + pub dmm: String, + pub utm: String, + // pub xpin: String, + pub plus: String, +} + +impl From<&spatial_coordinate_systems::all::Coordinates> for Coordinates { + fn from(value: &spatial_coordinate_systems::all::Coordinates) -> Self { + Self { + dd: value.dd.to_string(), + dms: value.dms.to_string(), + dmm: value.dmm.to_string(), + utm: value.utm.to_string(), + plus: value.plus.to_string(), + } + } +} + impl TryFrom<&'_ Address<'_>> for EncodedAddress { type Error = String; @@ -129,6 +156,9 @@ impl TryFrom<&'_ Address<'_>> for EncodedAddress { let src_coords = Coordinate::from_str(&format!("{}, {}", lat, lon)).map_err(|e| e.to_string())?; + let all_coordinates = spatial_coordinate_systems::all::Coordinates::try_from(&src_coords) + .map_err(|e| e.to_string())?; + Ok(Self { address: addr.to_string(), lat_lon: Box::new([lat, lon]), @@ -136,6 +166,7 @@ impl TryFrom<&'_ Address<'_>> for EncodedAddress { src_coords_type: src_coords.get_type().into(), src_coords, decimal_degrees: format!("{}, {}", lat, lon), + all_coordinates: Coordinates::from(&all_coordinates), }) } }