use std::borrow::Cow;
use super::{GeometryCollection, GeometryTileFeature, convert_geometry_to_tile};
use crate::error::Error;
use crate::feature::{Feature, Identifier, PropertyMap, Value};
use crate::geometry::{FeatureType, to_feature_type};
use crate::tile_id::CanonicalTileID;
pub(crate) struct GeoJSONFeature<'a> {
feature: &'a Feature<f64>,
geometry: GeometryCollection,
}
impl<'a> GeoJSONFeature<'a> {
pub(crate) fn new(feature: &'a Feature<f64>) -> Self {
GeoJSONFeature { feature, geometry: GeometryCollection::default() }
}
pub(crate) fn with_canonical(feature: &'a Feature<f64>, canonical: &CanonicalTileID) -> Self {
let geometry = feature
.geometry
.as_ref()
.map_or_else(GeometryCollection::default, |g| convert_geometry_to_tile(g, canonical));
GeoJSONFeature { feature, geometry }
}
}
impl GeometryTileFeature for GeoJSONFeature<'_> {
fn feature_type(&self) -> FeatureType {
to_feature_type(self.feature.geometry.as_ref())
}
fn properties(&self) -> Cow<'_, PropertyMap> {
Cow::Borrowed(&self.feature.properties)
}
fn id(&self) -> Identifier {
self.feature.id.clone()
}
fn value(&self, key: &str) -> Option<Value> {
self.feature.properties.get(key).cloned()
}
fn geometries(&self) -> Result<Cow<'_, GeometryCollection>, Error> {
Ok(Cow::Borrowed(&self.geometry))
}
}