use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default)]
pub struct LatLng {
pub lat: f64,
pub lng: f64,
}
impl LatLng {
pub fn new(lat: f64, lng: f64) -> Self {
Self { lat, lng }
}
pub fn helsinki() -> Self {
Self::new(60.1699, 24.9384)
}
pub fn to_array(&self) -> [f64; 2] {
[self.lng, self.lat]
}
pub fn from_array(arr: [f64; 2]) -> Self {
Self {
lng: arr[0],
lat: arr[1],
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct MapPosition {
pub center: LatLng,
pub zoom: f64,
}
impl MapPosition {
pub fn new(center: LatLng, zoom: f64) -> Self {
Self { center, zoom }
}
}
impl Default for MapPosition {
fn default() -> Self {
Self {
center: LatLng::helsinki(),
zoom: 10.0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Bounds {
pub sw: LatLng,
pub ne: LatLng,
}
impl Bounds {
pub fn new(sw: LatLng, ne: LatLng) -> Self {
Self { sw, ne }
}
pub fn contains(&self, point: &LatLng) -> bool {
point.lat >= self.sw.lat
&& point.lat <= self.ne.lat
&& point.lng >= self.sw.lng
&& point.lng <= self.ne.lng
}
pub fn center(&self) -> LatLng {
LatLng {
lat: f64::midpoint(self.sw.lat, self.ne.lat),
lng: f64::midpoint(self.sw.lng, self.ne.lng),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QueryFeature {
#[serde(default)]
pub id: Option<i64>,
pub geometry: serde_json::Value,
pub properties: serde_json::Value,
pub source: String,
#[serde(default)]
pub source_layer: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default)]
pub struct Point {
pub x: f64,
pub y: f64,
}
impl Point {
pub fn new(x: f64, y: f64) -> Self {
Self { x, y }
}
}