//! Region states: a place on a board coloured by a state with a time — threat //! now, soon, later, passed; a weather code; a flood warning; a band of a //! statistic (v1-engine). //! //! A layer of region states either **lists** them (a published alert, a //! study's table, a CBS figure per municipality: place, time, state) or //! **derives** them from the scenario's tracks: each area takes the state of //! the soonest track to enter it. That second kind is what an air-alert map is, //! and it is a model, so a Live scenario may not have one (v1-drones: "it must //! never colour a region in Live"). Or it **reads a figure** the areas carry //! (CBS's share of people aged 65 and over, per buurt) and gives each area the //! state of the band its value falls in. //! //! A layer can stand on several boards, one per rung of the ladder (buurt, //! wijk, gemeente, provincie, country): the one read follows the camera. //! //! A figure can be a **series**, one value per year (`AreaSet::field_at`): on //! a clock of years the year the clock stands in picks the value, so a //! forecast plays to 2050 and a measured series back to 2014. Such a layer can //! read its figure as the change since a year (`change_since`), and name the //! two fields that bound it (`range`: a forecast's interval, a score's 95 %). use crate::board::{AreaSet, Board}; use crate::time::{Span, Stamp, Time}; use crate::track::Track; use serde::Deserialize; /// Tracks are sampled this often along their path to find where they enter and /// leave an area, km: ~40 s at a drone's 170 km/h, and under the width of the /// smallest area a continental set holds. const SAMPLE_KM: f64 = 2.0; /// When a derived state applies. #[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)] #[serde(rename_all = "lowercase")] pub enum When { /// A track is in the area now. Inside, /// A track has been and left. Passed, } /// One state a layer can give a place. #[derive(Clone, Debug, Deserialize)] #[serde(deny_unknown_fields)] pub struct StateSpec { pub id: String, /// What it means, in words a person reads ("Expected within an hour"). pub label: String, /// `#rrggbb`. pub colour: String, /// For a derived layer: `inside`, `passed`, or … #[serde(default)] pub when: Option, /// … a track enters within this long (`"1:00"`). #[serde(default)] pub within: Option, /// For a layer reading a figure: the value is under this. The last state /// has none and takes the rest. #[serde(default)] pub below: Option, } /// One listed state: from `t` on, `place` is in `state` (empty clears it). #[derive(Clone, Debug, Deserialize)] #[serde(deny_unknown_fields)] pub struct SetSpec { pub place: String, pub t: Stamp, pub state: String, } /// What a layer's states come from. #[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)] #[serde(rename_all = "lowercase")] pub enum Derive { /// The soonest track to enter an area. Tracks, /// The band a figure of the area falls in. Value, } /// A layer of region states as the file writes it. #[derive(Clone, Debug, Deserialize)] #[serde(deny_unknown_fields)] pub struct RegionStateSpec { pub id: String, /// What the layer shows, in words a person reads ("Aged 65 and over"). #[serde(default)] pub label: Option, /// Which heading the lens lists it under ("People", "Homes"); the /// figures without one come first, under none. #[serde(default)] pub group: Option, /// One board, or … #[serde(default)] pub board: Option, /// … the same layer on several rungs, any order. #[serde(default)] pub boards: Vec, /// For `derive = "value"`: the area field read. #[serde(default)] pub field: Option, /// Read the figure as its change, in %, since this year's value of it /// (`"2023"`): growth and decline rather than size. #[serde(default)] pub change_since: Option, /// The fields that bound the figure, low then high: how sure it is. Shown /// beside the value wherever a set carries them; never coloured. #[serde(default)] pub range: Option<[String; 2]>, /// Only areas in these countries (ISO alpha-3) take part; all when empty. #[serde(default)] pub within: Vec, #[serde(default)] pub derive: Option, #[serde(rename = "state")] pub states: Vec, #[serde(default)] pub set: Vec, } /// A state's colour, parsed. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Rgb(pub [u8; 3]); impl Rgb { pub fn parse(s: &str) -> Result { let h = s .strip_prefix('#') .filter(|h| h.len() == 6) .ok_or_else(|| format!("colour {s:?}: expected #rrggbb"))?; let c = |i: usize| u8::from_str_radix(&h[i..i + 2], 16).map_err(|_| format!("colour {s:?}")); Ok(Rgb([c(0)?, c(2)?, c(4)?])) } } /// One state of a layer, ready to draw. #[derive(Clone, Debug)] pub struct State { pub id: String, pub label: String, pub colour: Rgb, pub when: Option, pub within: Option