Skip to main content

mln_engine/tile/
feature.rs

1use feature::{Identifier, Value};
2use mln_style::string::to_string_double;
3
4#[must_use]
5pub fn numeric_value_f64(value: &Value) -> Option<f64> {
6    match value {
7        Value::Uint(t) => Some(*t as f64),
8        Value::Int(t) => Some(*t as f64),
9        Value::Double(t) => Some(*t),
10        _ => None,
11    }
12}
13
14/// The text that names the feature with this ID, or `None` for no ID. A
15/// number's text is its shortest decimal form, with no `+` in an exponent
16/// (`1e21`), as the C API's feature IDs are compared.
17#[must_use]
18pub fn feature_id_to_string(id: &Identifier) -> Option<String> {
19    match id {
20        Identifier::Null => None,
21        Identifier::String(value) => Some(value.clone()),
22        Identifier::Uint(value) => Some(value.to_string()),
23        Identifier::Int(value) => Some(value.to_string()),
24        Identifier::Double(value) => Some(to_string_double(*value).replacen("e+", "e", 1)),
25    }
26}
27
28/// A `GeoJSONFeature` plus the source, source layer, style layer and feature
29/// state a query fills in. `geometry` is `None` for a null geometry.
30#[derive(Clone, Debug, Default, PartialEq)]
31pub struct Feature {
32    pub geometry: Option<geo_types::Geometry<f64>>,
33    pub properties: feature::PropertyMap,
34    pub id: Identifier,
35    pub source: String,
36    /// The source layer a vector tile feature came from. Empty for sources
37    /// without source layers, such as GeoJSON.
38    pub source_layer: String,
39    /// The style layer a rendered-feature query matched. A feature that
40    /// several layers render is returned once per layer. Empty for features
41    /// from a source-feature query.
42    pub layer: String,
43    pub state: feature::PropertyMap,
44}
45
46impl From<feature::Feature<f64>> for Feature {
47    fn from(f: feature::Feature<f64>) -> Self {
48        Feature { geometry: f.geometry, properties: f.properties, id: f.id, ..Default::default() }
49    }
50}
51
52impl Feature {
53    /// The `GeoJSONFeature` base.
54    #[must_use]
55    pub fn to_geojson_feature(&self) -> feature::Feature<f64> {
56        feature::Feature { geometry: self.geometry.clone(), properties: self.properties.clone(), id: self.id.clone() }
57    }
58}