diff --git a/Cargo.toml b/Cargo.toml index 165281b8..c538ba99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,7 +70,6 @@ actix_block_ai_crawling = "0.2.8" actix = "0.13.1" actix-web-actors = "4.2.0" amtk = "0.1.0" -geojson = "0.24.1" travelling_salesman = "1.1.22" ordered-float = "4.2.0" dotenvy = "0.15.7" @@ -93,6 +92,7 @@ diesel-derive-newtype = "2.1.1" async-std = {version = "1.5.3"} geo-repair-polygon = "0.1.2" gtfs-translations = "0.1.0" +geojson = { version = "0.24.1", features = ["geo-types"] } [[bin]] name = "maple" diff --git a/src/birch/server.rs b/src/birch/server.rs index ddf240ad..e57a4134 100644 --- a/src/birch/server.rs +++ b/src/birch/server.rs @@ -8,6 +8,7 @@ use bb8::Pool; use qstring::QString; use serde_json::to_string; use serde_json::{json, to_string_pretty}; +use geojson::{Feature, GeoJson, Geometry, JsonValue, Value}; use std::collections::HashMap; use diesel_async::RunQueryDsl; use std::time::UNIX_EPOCH; @@ -138,16 +139,15 @@ pub async fn amtrakproxy(req: HttpRequest) -> impl Responder { } } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug)] struct ChateauToSend { chateau: String, - hull: geo::MultiPolygon + hull: geo::MultiPolygon, + realtime_feeds: Vec, + schedule_feeds: Vec } -#[derive(Clone, Debug, Deserialize, Serialize)] -struct ChateauList { - chateaus: Vec -} + #[actix_web::get("/getchateaus")] async fn chateaus(pool: web::Data>, req: HttpRequest) -> impl Responder { @@ -167,19 +167,47 @@ async fn chateaus(pool: web::Data>, req: HttpRequest) .map(|pg_chateau| ChateauToSend { chateau: pg_chateau.chateau, + realtime_feeds: pg_chateau.realtime_feeds.into_iter().filter(|opt_string| opt_string.is_some()).map(|string| string.unwrap()).collect(), + schedule_feeds: pg_chateau.static_feeds.into_iter().filter(|opt_string| opt_string.is_some()).map(|string| string.unwrap()).collect(), hull: diesel_multi_polygon_to_geo(pg_chateau.hull.unwrap()) } ).collect::>(); - let struct_to_send = ChateauList { - chateaus: formatted_chateaus + let features = formatted_chateaus.iter().map(|chateau| { + let value = geojson::Value::from(&chateau.hull); + + let mut properties:serde_json::map::Map = serde_json::map::Map::new(); + + properties.insert(String::from("chateau"),serde_json::Value::String(chateau.chateau.clone())); + properties.insert(String::from("realtime_feeds"),serde_json::Value::Array(chateau.realtime_feeds.clone().into_iter().map(|x| serde_json::Value::String(x)).collect())); + properties.insert(String::from("schedule_feeds"),serde_json::Value::Array(chateau.schedule_feeds.clone().into_iter().map(|x| serde_json::Value::String(x)).collect())); + + let feature = geojson::Feature { + bbox: None, + geometry: Some(geojson::Geometry { + bbox: None, + value: value, + foreign_members: None + }), + id: Some(geojson::feature::Id::String(chateau.chateau.clone())), + properties: Some(properties), + foreign_members: None + }; + + feature + }).collect::>(); + + let feature_collection = geojson::FeatureCollection { + bbox: None, + features: features, + foreign_members: None }; - let response = serde_json::to_string(&struct_to_send).unwrap(); + let serialized = GeoJson::from(feature_collection).to_string(); HttpResponse::Ok() .insert_header(("Content-Type", "application/json")) - .body(response) + .body(serialized) } #[actix_web::main]