Skip to content

Commit

Permalink
Send a feature collection of chateaus
Browse files Browse the repository at this point in the history
  • Loading branch information
kylerchin committed Apr 1, 2024
1 parent 7835e8f commit ec68eff
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
48 changes: 38 additions & 10 deletions src/birch/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String>,
schedule_feeds: Vec<String>
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct ChateauList {
chateaus: Vec<ChateauToSend>
}


#[actix_web::get("/getchateaus")]
async fn chateaus(pool: web::Data<Arc<CatenaryPostgresPool>>, req: HttpRequest) -> impl Responder {
Expand All @@ -167,19 +167,47 @@ async fn chateaus(pool: web::Data<Arc<CatenaryPostgresPool>>, 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::<Vec<ChateauToSend>>();

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<String, JsonValue> = 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::<Vec<Feature>>();

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]
Expand Down

0 comments on commit ec68eff

Please sign in to comment.