view src/types.rs @ 88:cb4ef5b61aeb

Livemap: Fix last-update display
author Lewin Bormann <lbo@spheniscida.de>
date Mon, 07 Dec 2020 21:21:41 +0100
parents 8ec6df976a19
children df344c293239
line wrap: on
line source

/// Non-JSON plain point representation. Flat and representing a database row.
#[derive(Debug, Clone)]
pub struct GeoPoint {
    pub lat: f64,
    pub long: f64,
    pub spd: Option<f64>,
    pub ele: Option<f64>,
    pub accuracy: Option<f64>,
    pub time: chrono::DateTime<chrono::Utc>,
    pub note: Option<String>,
}

#[derive(serde::Serialize, Debug)]
pub struct LiveUpdate {
    #[serde(rename = "type")]
    typ: String, // always "GeoHubUpdate"
    client: String,
    last: Option<i32>,
    geo: Option<GeoJSON>,
    error: Option<String>,
}

impl LiveUpdate {
    pub fn new(
        client: String,
        last: Option<i32>,
        geo: Option<GeoJSON>,
        err: Option<String>,
    ) -> LiveUpdate {
        LiveUpdate {
            typ: "GeoHubUpdate".into(),
            client: client,
            last: last,
            geo: geo,
            error: err,
        }
    }
}

#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct LogLocations {
    pub locations: Vec<GeoFeature>,
}

/// Fetch geodata as JSON.
///
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct GeoProperties {
    #[serde(alias = "timestamp")]
    time: chrono::DateTime<chrono::Utc>,
    altitude: Option<f64>,
    speed: Option<f64>,
    #[serde(alias = "horizontal_accuracy")]
    accuracy: Option<f64>,
    /// The unique ID of the point.
    id: Option<i32>,
    /// An arbitrary note attached by the logging client.
    note: Option<String>,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct GeoGeometry {
    #[serde(rename = "type")]
    typ: String, // always "Point"
    coordinates: (f64, f64), // always [long, lat]
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct GeoFeature {
    #[serde(rename = "type")]
    typ: String, // always "Feature"
    properties: GeoProperties,
    geometry: GeoGeometry,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct GeoJSON {
    #[serde(rename = "type")]
    typ: String, // always "FeatureCollection"
    features: Vec<GeoFeature>,
}

impl GeoJSON {
    pub fn new() -> GeoJSON {
        GeoJSON {
            typ: "FeatureCollection".into(),
            features: vec![],
        }
    }
    pub fn reserve_features(&mut self, cap: usize) {
        self.features.reserve(cap);
    }
    pub fn push_feature(&mut self, feat: GeoFeature) {
        self.features.push(feat);
    }
}

pub fn geofeature_from_point(id: Option<i32>, point: GeoPoint) -> GeoFeature {
    GeoFeature {
        typ: "Feature".into(),
        properties: GeoProperties {
            id: id,
            time: point.time,
            altitude: point.ele,
            speed: point.spd,
            note: point.note,
            accuracy: point.accuracy,
        },
        geometry: GeoGeometry {
            typ: "Point".into(),
            coordinates: (point.long, point.lat),
        },
    }
}

pub fn geopoint_from_feature(feat: GeoFeature) -> GeoPoint {
    let geo = feat.geometry;
    let prop = feat.properties;
    GeoPoint {
        accuracy: prop.accuracy,
        ele: prop.altitude,
        long: geo.coordinates.0,
        lat: geo.coordinates.1,
        note: None,
        spd: prop.speed,
        time: prop.time,
    }
}