mirror of
https://github.com/CIMEngine/cimengine-build-tools.git
synced 2024-11-05 20:53:58 +03:00
feat: collection from countriesdata struct
This commit is contained in:
parent
2a9268fae8
commit
817f117ac4
4 changed files with 47 additions and 24 deletions
|
@ -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"
|
||||
|
|
16
src/build.rs
16
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<CountryData>
|
||||
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();
|
||||
|
|
51
src/types.rs
51
src/types.rs
|
@ -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> {
|
||||
fn to_collection(self) -> 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 {
|
||||
fn to_mp(&self) -> MultiPolygon {
|
||||
MultiPolygon::new(vec![self.clone()])
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue