From 817f117ac469771c5de93b5befc1af84e03a9216 Mon Sep 17 00:00:00 2001 From: Artemy Date: Wed, 29 May 2024 21:29:36 +0300 Subject: [PATCH] feat: collection from countriesdata struct --- Cargo.toml | 2 +- src/build.rs | 16 +++++----------- src/types.rs | 51 ++++++++++++++++++++++++++++++++++++++++----------- src/utils.rs | 2 +- 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 48c53b4..533ceb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] clap = { version = "4.5.4", features = ["derive"] } geo = "0.28.0" -geojson = "0.24.1" +geojson = { version = "0.24.1", features = ["geo-types"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" toml = "0.8.12" diff --git a/src/build.rs b/src/build.rs index 127b56f..e16aac6 100644 --- a/src/build.rs +++ b/src/build.rs @@ -5,7 +5,7 @@ use serde_json::json; use wax::{Glob, Pattern}; use crate::{ - types::{CountryData, ToFeature, ToFeatures}, + types::{CountryData, ToCollection, ToFeature, ToFeatures}, utils::{diff_countries, get_country, read_config}, }; @@ -47,25 +47,19 @@ pub fn build() { // TODO: Add nature support - // TODO: Create from Vec - let feature_collection = FeatureCollection { - bbox: None, - features, - foreign_members: None, - } - .to_string(); - - let countries = serde_json::to_string_pretty(&serde_json::Map::from_iter( + let countries_json = serde_json::to_string_pretty(&serde_json::Map::from_iter( countries .iter() .map(|country| (country.id.clone(), json!(country.config))), )) .unwrap(); + let feature_collection = countries.to_collection().to_string(); + fs::create_dir_all(out_folder).unwrap(); fs::write(out_folder.join("geo.geojson"), feature_collection).unwrap(); - fs::write(out_folder.join("countries.json"), countries).unwrap(); + fs::write(out_folder.join("countries.json"), countries_json).unwrap(); if let Some(public) = processing_item.public { let public = serde_json::to_string(&public).unwrap(); diff --git a/src/types.rs b/src/types.rs index e0cef04..0364006 100644 --- a/src/types.rs +++ b/src/types.rs @@ -203,6 +203,46 @@ impl ToFeatures for Vec { } } +impl ToFeatures for CountryData { + fn to_features(&self) -> Vec { + let land = geojson::Feature { + geometry: Some(geojson::Geometry::from(&self.land)), + properties: Some( + serde_json::Map::from_iter([ + ("id".to_owned(), json!(self.id)), + ("type".to_owned(), json!("country")), + ("fill".to_owned(), json!(self.config.fill)), + ("stroke".to_owned(), json!(self.config.stroke)), + ("tags".to_owned(), json!(self.config.tags)), + ]) + .into(), + ), + + bbox: None, + id: None, + foreign_members: None, + }; + + let mut features = vec![land]; + features.extend(self.markers.to_features()); + + features + } +} + +impl ToCollection for Vec { + fn to_collection(self) -> geojson::FeatureCollection { + geojson::FeatureCollection { + features: self + .iter() + .flat_map(|c| c.to_features()) + .collect::>(), + bbox: None, + foreign_members: None, + } + } +} + impl ToCollection for Vec { fn to_collection(self) -> geojson::FeatureCollection { geojson::FeatureCollection { @@ -264,17 +304,6 @@ impl ToSplitGeo for FeatureCollection { } } -impl UnsplitGeo for (Vec, Feature) { - fn unsplit_geo(self) -> FeatureCollection { - let (markers, territories) = self; - - let mut features: Vec = markers.to_features(); - features.push(territories); - - features.to_collection() - } -} - impl ToMultiPolygon for Polygon { fn to_mp(&self) -> MultiPolygon { MultiPolygon::new(vec![self.clone()]) diff --git a/src/utils.rs b/src/utils.rs index 5b47317..798baff 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -26,7 +26,7 @@ pub fn get_country(id: String) -> CountryData { let config = match config { Ok(c) => c, - Err(err) => panic!("Invalid config: {}", err), + Err(err) => panic!("Invalid config: {err}"), }; let geo_str = fs::read_to_string(country_folder.join("country.geojson")).unwrap();