feat: collection from countriesdata struct

This commit is contained in:
Artemy 2024-05-29 21:29:36 +03:00
parent 2a9268fae8
commit 817f117ac4
4 changed files with 47 additions and 24 deletions

View file

@ -8,7 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.5.4", features = ["derive"] } clap = { version = "4.5.4", features = ["derive"] }
geo = "0.28.0" geo = "0.28.0"
geojson = "0.24.1" geojson = { version = "0.24.1", features = ["geo-types"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
toml = "0.8.12" toml = "0.8.12"

View file

@ -5,7 +5,7 @@ use serde_json::json;
use wax::{Glob, Pattern}; use wax::{Glob, Pattern};
use crate::{ use crate::{
types::{CountryData, ToFeature, ToFeatures}, types::{CountryData, ToCollection, ToFeature, ToFeatures},
utils::{diff_countries, get_country, read_config}, utils::{diff_countries, get_country, read_config},
}; };
@ -47,25 +47,19 @@ pub fn build() {
// TODO: Add nature support // TODO: Add nature support
// TODO: Create from Vec<CountryData> let countries_json = serde_json::to_string_pretty(&serde_json::Map::from_iter(
let feature_collection = FeatureCollection {
bbox: None,
features,
foreign_members: None,
}
.to_string();
let countries = serde_json::to_string_pretty(&serde_json::Map::from_iter(
countries countries
.iter() .iter()
.map(|country| (country.id.clone(), json!(country.config))), .map(|country| (country.id.clone(), json!(country.config))),
)) ))
.unwrap(); .unwrap();
let feature_collection = countries.to_collection().to_string();
fs::create_dir_all(out_folder).unwrap(); fs::create_dir_all(out_folder).unwrap();
fs::write(out_folder.join("geo.geojson"), feature_collection).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 { if let Some(public) = processing_item.public {
let public = serde_json::to_string(&public).unwrap(); let public = serde_json::to_string(&public).unwrap();

View file

@ -203,6 +203,46 @@ impl ToFeatures for Vec<Marker> {
} }
} }
impl ToFeatures for CountryData {
fn to_features(&self) -> Vec<geojson::Feature> {
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<CountryData> {
fn to_collection(self) -> geojson::FeatureCollection {
geojson::FeatureCollection {
features: self
.iter()
.flat_map(|c| c.to_features())
.collect::<Vec<geojson::Feature>>(),
bbox: None,
foreign_members: None,
}
}
}
impl ToCollection for Vec<geojson::Feature> { impl ToCollection for Vec<geojson::Feature> {
fn to_collection(self) -> geojson::FeatureCollection { fn to_collection(self) -> geojson::FeatureCollection {
geojson::FeatureCollection { geojson::FeatureCollection {
@ -264,17 +304,6 @@ impl ToSplitGeo for FeatureCollection {
} }
} }
impl UnsplitGeo for (Vec<Marker>, Feature) {
fn unsplit_geo(self) -> FeatureCollection {
let (markers, territories) = self;
let mut features: Vec<geojson::Feature> = markers.to_features();
features.push(territories);
features.to_collection()
}
}
impl ToMultiPolygon for Polygon { impl ToMultiPolygon for Polygon {
fn to_mp(&self) -> MultiPolygon { fn to_mp(&self) -> MultiPolygon {
MultiPolygon::new(vec![self.clone()]) MultiPolygon::new(vec![self.clone()])

View file

@ -26,7 +26,7 @@ pub fn get_country(id: String) -> CountryData {
let config = match config { let config = match config {
Ok(c) => c, 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(); let geo_str = fs::read_to_string(country_folder.join("country.geojson")).unwrap();