2023-02-25 14:49:09 -05:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2023-03-20 21:09:14 -04:00
|
|
|
use spatial_coordinate_systems::{Coordinate, LatLon};
|
2023-02-25 11:14:25 -05:00
|
|
|
use wasm_bindgen::prelude::*;
|
2023-03-14 23:26:42 -04:00
|
|
|
use xpin::Address;
|
2023-02-25 11:14:25 -05:00
|
|
|
|
2023-03-20 21:09:14 -04:00
|
|
|
#[wasm_bindgen]
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum CoordinateType {
|
|
|
|
DD,
|
|
|
|
DMS,
|
|
|
|
DMM,
|
|
|
|
UTM,
|
|
|
|
// Xpin,
|
|
|
|
Plus,
|
|
|
|
}
|
|
|
|
|
2023-03-24 16:20:56 -04:00
|
|
|
impl From<CoordinateType> for spatial_coordinate_systems::CoordinateType {
|
|
|
|
fn from(val: CoordinateType) -> Self {
|
|
|
|
match val {
|
2023-03-24 18:16:28 -04:00
|
|
|
CoordinateType::DD => Self::DD,
|
|
|
|
CoordinateType::DMS => Self::DMS,
|
|
|
|
CoordinateType::DMM => Self::DMM,
|
|
|
|
CoordinateType::UTM => Self::UTM,
|
|
|
|
// CoordinateType::Xpin => Self::Xpin,
|
|
|
|
CoordinateType::Plus => Self::Plus,
|
2023-03-20 21:09:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<spatial_coordinate_systems::CoordinateType> for CoordinateType {
|
|
|
|
fn from(value: spatial_coordinate_systems::CoordinateType) -> CoordinateType {
|
|
|
|
match value {
|
|
|
|
spatial_coordinate_systems::CoordinateType::DD => Self::DD,
|
|
|
|
spatial_coordinate_systems::CoordinateType::DMS => Self::DMS,
|
|
|
|
spatial_coordinate_systems::CoordinateType::DMM => Self::DMM,
|
|
|
|
spatial_coordinate_systems::CoordinateType::UTM => Self::UTM,
|
2023-03-25 00:41:27 -04:00
|
|
|
// spatial_coordinate_systems::CoordinateType::Xpin => Self::Xpin,
|
2023-03-20 21:09:14 -04:00
|
|
|
spatial_coordinate_systems::CoordinateType::Plus => Self::Plus,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 23:22:29 -04:00
|
|
|
#[wasm_bindgen(getter_with_clone)]
|
2023-03-25 22:35:12 -04:00
|
|
|
#[derive(Debug)]
|
2023-03-14 23:26:42 -04:00
|
|
|
pub struct EncodedAddress {
|
2023-03-21 23:22:29 -04:00
|
|
|
pub address: String,
|
2023-03-20 21:09:14 -04:00
|
|
|
/// The coordinates used to encode this address
|
2023-03-21 23:22:29 -04:00
|
|
|
src_coords: Coordinate,
|
2023-03-26 21:45:05 -04:00
|
|
|
|
|
|
|
#[wasm_bindgen(js_name = coordinateUrls)]
|
|
|
|
pub coordinate_urls: CoordinateUrls,
|
|
|
|
|
2023-03-21 23:22:29 -04:00
|
|
|
#[wasm_bindgen(js_name = latLon)]
|
|
|
|
pub lat_lon: Box<[f64]>,
|
|
|
|
|
|
|
|
#[wasm_bindgen(js_name = decimalDegrees)]
|
|
|
|
pub decimal_degrees: String,
|
2023-03-25 22:08:25 -04:00
|
|
|
|
|
|
|
#[wasm_bindgen(js_name = allCoordinates)]
|
|
|
|
pub all_coordinates: Coordinates,
|
2023-02-25 11:14:25 -05:00
|
|
|
}
|
2023-02-25 14:12:18 -05:00
|
|
|
|
2023-02-25 14:49:09 -05:00
|
|
|
#[wasm_bindgen]
|
2023-03-14 23:26:42 -04:00
|
|
|
impl EncodedAddress {
|
2023-03-26 00:17:41 -04:00
|
|
|
#[wasm_bindgen(js_name = getCoordsType)]
|
|
|
|
pub fn get_coords_type(&self) -> CoordinateType {
|
|
|
|
self.src_coords.get_type().into()
|
|
|
|
}
|
|
|
|
|
2023-03-20 21:09:14 -04:00
|
|
|
/// Get the string representation of the encoded value
|
2023-03-26 00:17:41 -04:00
|
|
|
#[wasm_bindgen(js_name = getCoordsRepr)]
|
2023-03-21 23:22:29 -04:00
|
|
|
// TODO: Do not return option
|
2023-03-26 00:17:41 -04:00
|
|
|
pub fn get_coords_repr(&self) -> String {
|
2023-03-26 00:07:52 -04:00
|
|
|
self.src_coords.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the string representation of the encoded value as a different type
|
2023-03-26 00:17:41 -04:00
|
|
|
#[wasm_bindgen(js_name = getCoordsReprAs)]
|
2023-03-26 00:07:52 -04:00
|
|
|
// TODO: Do not return option
|
2023-03-20 21:09:14 -04:00
|
|
|
pub fn get_coords_repr_as(&self, coordinate_type: CoordinateType) -> Option<String> {
|
|
|
|
self.src_coords
|
|
|
|
// TODO: Remove the clone here
|
2023-03-21 23:22:29 -04:00
|
|
|
.clone()
|
2023-03-25 00:41:48 -04:00
|
|
|
.try_as_type(&coordinate_type.into())
|
2023-03-21 23:22:29 -04:00
|
|
|
.ok()
|
2023-03-20 21:09:14 -04:00
|
|
|
.map(|s| s.to_string())
|
|
|
|
}
|
|
|
|
|
2023-03-19 13:03:19 -04:00
|
|
|
/// Get an encoded address from a latitude/longitude
|
2023-03-14 23:26:42 -04:00
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn from_lat_lon(lat: f64, lon: f64) -> Result<EncodedAddress, String> {
|
2023-03-21 23:22:29 -04:00
|
|
|
Self::try_from(
|
|
|
|
xpin::Address::from_lat_lon(lat, lon)
|
|
|
|
.as_ref()
|
|
|
|
.map_err(|e| e.to_string())?,
|
|
|
|
)
|
2023-03-25 00:41:27 -04:00
|
|
|
.map_err(|e| e.to_string())
|
2023-03-14 23:26:42 -04:00
|
|
|
}
|
|
|
|
|
2023-03-20 21:09:14 -04:00
|
|
|
/// Get an encoded address from a latitude/longitude
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn from_coordinate(i: &str) -> Result<EncodedAddress, String> {
|
2023-03-21 23:22:29 -04:00
|
|
|
console_error_panic_hook::set_once();
|
|
|
|
|
2023-03-20 21:09:14 -04:00
|
|
|
let src_coords = Coordinate::from_str(i)
|
2023-03-25 00:41:27 -04:00
|
|
|
.map_err(|e| format!("Could not parse str {i:?} as a coordinate {e:?}"))?;
|
2023-03-20 21:09:14 -04:00
|
|
|
|
2023-03-26 00:07:52 -04:00
|
|
|
let src_latlon = LatLon::from(&src_coords);
|
2023-03-20 21:09:14 -04:00
|
|
|
|
2023-03-21 23:22:29 -04:00
|
|
|
let mut ret = Self::try_from(
|
2023-03-26 00:07:52 -04:00
|
|
|
xpin::Address::from_lat_lon(src_latlon.get_lat(), src_latlon.get_lon())
|
2023-03-21 23:22:29 -04:00
|
|
|
.as_ref()
|
|
|
|
.map_err(|e| e.to_string())?,
|
|
|
|
)
|
2023-03-25 00:41:27 -04:00
|
|
|
.map_err(|e| e.to_string())?;
|
2023-03-20 21:09:14 -04:00
|
|
|
|
2023-03-26 00:07:52 -04:00
|
|
|
ret.src_coords = ret
|
|
|
|
.src_coords
|
|
|
|
.try_as_type(&src_coords.get_type().into())
|
|
|
|
.map_err(|e| e.to_string())?
|
|
|
|
.into();
|
|
|
|
|
2023-03-20 21:09:14 -04:00
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
|
2023-03-14 23:26:42 -04:00
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn from_address(addr_str: &str) -> Result<EncodedAddress, String> {
|
2023-03-21 23:22:29 -04:00
|
|
|
Self::try_from(
|
|
|
|
xpin::Address::from_str(addr_str)
|
|
|
|
.as_ref()
|
|
|
|
.map_err(|e| e.to_string())?,
|
|
|
|
)
|
2023-03-25 00:41:27 -04:00
|
|
|
.map_err(|e| e.to_string())
|
2023-03-14 23:26:42 -04:00
|
|
|
}
|
2023-03-21 23:22:29 -04:00
|
|
|
}
|
2023-03-14 23:26:42 -04:00
|
|
|
|
2023-03-25 22:08:25 -04:00
|
|
|
#[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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 23:22:29 -04:00
|
|
|
impl TryFrom<&'_ Address<'_>> for EncodedAddress {
|
2023-03-25 00:41:27 -04:00
|
|
|
type Error = String;
|
2023-03-14 23:26:42 -04:00
|
|
|
|
2023-03-21 23:22:29 -04:00
|
|
|
fn try_from(addr: &Address) -> Result<Self, Self::Error> {
|
|
|
|
console_error_panic_hook::set_once();
|
2023-03-14 23:26:42 -04:00
|
|
|
|
|
|
|
let (lat, lon) = addr.as_lat_lon();
|
2023-03-25 00:41:27 -04:00
|
|
|
// TODO: Do not allocate a string here
|
|
|
|
let src_coords =
|
|
|
|
Coordinate::from_str(&format!("{}, {}", lat, lon)).map_err(|e| e.to_string())?;
|
2023-03-21 23:22:29 -04:00
|
|
|
|
2023-03-25 22:08:25 -04:00
|
|
|
let all_coordinates = spatial_coordinate_systems::all::Coordinates::try_from(&src_coords)
|
|
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
|
2023-03-21 23:22:29 -04:00
|
|
|
Ok(Self {
|
2023-03-14 23:26:42 -04:00
|
|
|
address: addr.to_string(),
|
2023-03-21 23:22:29 -04:00
|
|
|
lat_lon: Box::new([lat, lon]),
|
|
|
|
src_coords,
|
2023-03-26 21:45:05 -04:00
|
|
|
coordinate_urls: spatial_coordinate_systems::urls::CoordinateUrls::try_from(
|
|
|
|
all_coordinates.latlon,
|
|
|
|
)
|
|
|
|
.map_err(|e| e.to_string())?
|
|
|
|
.into(),
|
2023-03-21 23:22:29 -04:00
|
|
|
decimal_degrees: format!("{}, {}", lat, lon),
|
2023-03-25 22:08:25 -04:00
|
|
|
all_coordinates: Coordinates::from(&all_coordinates),
|
2023-03-21 23:22:29 -04:00
|
|
|
})
|
2023-03-14 23:26:42 -04:00
|
|
|
}
|
2023-02-25 14:49:09 -05:00
|
|
|
}
|
2023-03-21 23:22:29 -04:00
|
|
|
|
2023-03-26 21:45:05 -04:00
|
|
|
#[wasm_bindgen(getter_with_clone)]
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct CoordinateUrls {
|
|
|
|
// TODO: These should be getters only
|
|
|
|
pub google_maps: String,
|
|
|
|
pub openstreetmap: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<spatial_coordinate_systems::urls::CoordinateUrls> for CoordinateUrls {
|
|
|
|
fn from(value: spatial_coordinate_systems::urls::CoordinateUrls) -> Self {
|
|
|
|
Self {
|
|
|
|
google_maps: value.google_maps,
|
|
|
|
openstreetmap: value.openstreetmap,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 22:35:12 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2023-03-21 23:22:29 -04:00
|
|
|
|
2023-03-25 22:35:12 -04:00
|
|
|
#[test]
|
|
|
|
fn test_general() {
|
|
|
|
// panic!("{:#?}", EncodedAddress::from_lat_lon(50.0, 20.0));
|
|
|
|
}
|
|
|
|
}
|