feat: country rewrite

This commit is contained in:
Artemy 2024-05-30 12:07:42 +03:00
parent 0acf4ecc5c
commit d028617736
4 changed files with 97 additions and 38 deletions

View file

@ -1,12 +1,14 @@
use std::{fs, path::Path, time};
use serde_json::json;
use wax::{Glob, Pattern};
use crate::{
types::{CountryData, ToCollection},
utils::{diff_countries, get_country, read_config},
utils::{
diff_countries, get_country, is_match, read_config, rewrite_if_some, rewrite_if_some_option,
},
};
use wax::Glob;
pub fn build() {
let config = read_config();
@ -28,50 +30,55 @@ pub fn build() {
{
let dissolved_time = time::Instant::now();
if tags.len() == 0 {
for country_id in &config.main.layers {
countries.push(get_country(country_id.to_owned()));
}
} else {
for country_id in &config.main.layers {
let country = get_country(country_id.to_owned());
match &country.config.tags {
Some(tags) => {
let mut matches = false;
for glob in &globs {
for tag in tags {
if glob.is_match(tag.as_str()) {
matches = true;
}
}
}
if !matches {
continue;
}
if is_match(&country.config.tags, &globs) {
countries.push(country);
}
None => {
continue;
}
}
}
}
println!("Dissolved in {:?}", dissolved_time.elapsed());
}
// TODO: Add country_rewrite support
let countries = {
let countries: Vec<CountryData> = {
let diff_time = time::Instant::now();
let countries = diff_countries(countries);
let mut countries = diff_countries(countries);
println!("Diffed in {:?}", diff_time.elapsed());
countries.iter_mut().for_each(|c| {
if !processing_item.show_markers.unwrap_or(true) {
c.markers = vec![];
}
for country_rewrite in processing_item.countries_rewrite.clone().unwrap_or(vec![]) {
let tags = country_rewrite.tags.unwrap_or(vec![]);
let globs: Vec<Glob> = tags.iter().map(|tag| Glob::new(tag).unwrap()).collect();
if is_match(&c.config.tags, &globs) {
rewrite_if_some(country_rewrite.properties.name, &mut c.config.name);
rewrite_if_some(
country_rewrite.properties.description,
&mut c.config.description,
);
rewrite_if_some(
country_rewrite.properties.foundation_date,
&mut c.config.foundation_date,
);
rewrite_if_some(country_rewrite.properties.flag, &mut c.config.flag);
rewrite_if_some_option(
country_rewrite.properties.about,
&mut c.config.about,
);
rewrite_if_some(country_rewrite.properties.fill, &mut c.config.fill);
rewrite_if_some(country_rewrite.properties.stroke, &mut c.config.stroke);
rewrite_if_some_option(country_rewrite.properties.tags, &mut c.config.tags);
}
}
});
countries
};

View file

@ -20,8 +20,10 @@ output_folder = "./out/map"
# Glob syntax is supported
# tags = ["test-[!2]"]
# Rewrite properties of all countries. All fields are optional.
# [processing.countries_rewrite]
# Rewrite properties of countries by tags. All fields except `tags` are optional.
# [[processing.countries_rewrite]]
# tags = ["test"]
# [processing.countries_rewrite.properties]
# name = "name"
# color = "#000000"
# description = "description"
@ -29,7 +31,7 @@ output_folder = "./out/map"
# flag = "https://example.com/flag.png"
# about = "https://example.com/about.html"
# Nature layers
# Additional nature layers
[[nature]]
id = "water"
color = "#75cff0"

View file

@ -75,17 +75,26 @@ pub struct ProcessingConfig {
pub output_folder: String,
pub tags: Option<Vec<String>>,
pub countries_rewrite: Option<CountryRewriteConfig>,
pub countries_rewrite: Option<Vec<CountryRewriteConfig>>,
pub public: Option<PublicConfig>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CountryRewriteConfig {
pub tags: Option<Vec<String>>,
pub properties: CountryRewriteConfigProps,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CountryRewriteConfigProps {
pub name: Option<String>,
pub description: Option<String>,
pub foundation_date: Option<String>,
pub flag: Option<String>,
pub fill: Option<String>,
pub stroke: Option<String>,
pub about: Option<String>,
pub tags: Option<Vec<String>>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]

View file

@ -2,6 +2,7 @@ use std::{fs, path::Path};
use geo::{BooleanOps, MultiPolygon};
use geojson::GeoJson;
use wax::{Glob, Pattern};
use crate::types::{Config, CountryConfig, CountryData, Territory, ToMultiPolygon, ToSplitGeo};
@ -77,3 +78,43 @@ pub fn hash_hex_color(s: String) -> String {
format!("#{}", hex_str.chars().take(6).collect::<String>())
}
pub fn is_match(tags: &Option<Vec<String>>, globs: &Vec<Glob>) -> bool {
if globs.len() == 0 {
return true;
}
match tags {
Some(tags) => {
let mut matches = false;
for glob in globs {
for tag in tags {
if glob.is_match(tag.as_str()) {
matches = true;
}
}
}
matches
}
None => true,
}
}
pub fn rewrite_if_some<T>(value: Option<T>, rewrite: &mut T) {
match value {
Some(value) => {
*rewrite = value;
}
None => {}
}
}
pub fn rewrite_if_some_option<T>(value: Option<T>, rewrite: &mut Option<T>) {
match value {
Some(value) => {
*rewrite = Some(value);
}
None => {}
}
}