Skip to main content

maplibre_expr/
context.rs

1//! The evaluation context: zoom level plus the feature being styled.
2
3use std::collections::BTreeMap;
4
5use crate::value::Value;
6
7/// The feature (and its properties) an expression is evaluated against.
8#[derive(Debug, Clone, Default)]
9pub struct Feature {
10    pub id: Option<Value>,
11    pub properties: BTreeMap<String, Value>,
12    pub geometry_type: Option<String>,
13    /// The interactive feature state read by the `feature-state` operator.
14    pub state: BTreeMap<String, Value>,
15    /// The feature geometry in global tile coordinates, grouped into rings /
16    /// lines (used by the `within` operator).
17    pub geometry: Vec<Vec<(f64, f64)>>,
18}
19
20/// Everything an expression can read while evaluating: global parameters such
21/// as `zoom`, the current [`Feature`], and the shared global-state map read by
22/// the `global-state` operator.
23#[derive(Debug, Clone, Default)]
24pub struct EvaluationContext {
25    pub zoom: Option<f64>,
26    pub feature: Feature,
27    pub global_state: BTreeMap<String, Value>,
28    /// Image names known to be available (used by the `image` operator).
29    pub available_images: Vec<String>,
30    /// The canonical tile `(z, x, y)` the feature belongs to, if any.
31    pub canonical: Option<(u32, u32, u32)>,
32    /// Heatmap density (`heatmap-density`), 0..1.
33    pub heatmap_density: Option<f64>,
34    /// Terrain elevation (`elevation`).
35    pub elevation: Option<f64>,
36    /// Line progress along a symbol/line (`line-progress`), 0..1.
37    pub line_progress: Option<f64>,
38}
39
40impl EvaluationContext {
41    pub fn new() -> EvaluationContext {
42        EvaluationContext::default()
43    }
44
45    pub fn with_zoom(mut self, zoom: f64) -> EvaluationContext {
46        self.zoom = Some(zoom);
47        self
48    }
49
50    pub fn with_feature(mut self, feature: Feature) -> EvaluationContext {
51        self.feature = feature;
52        self
53    }
54
55    pub fn with_global_state(mut self, state: BTreeMap<String, Value>) -> EvaluationContext {
56        self.global_state = state;
57        self
58    }
59}