use crate::{base::BaseVectorFeature, mapbox::MapboxVectorLayer, open::Extent};
use alloc::{string::String, vec::Vec};
use s2json::Shape;
#[derive(Debug)]
pub struct BaseVectorLayer {
pub version: u8,
pub name: String,
pub extent: Extent,
pub shape_defined: bool,
pub m_shape_defined: bool,
pub shape: Shape,
pub m_shape: Option<Shape>,
pub features: Vec<BaseVectorFeature>,
}
impl BaseVectorLayer {
pub fn new(
name: String,
extent: Extent,
features: Vec<BaseVectorFeature>,
shape: Option<Shape>,
m_shape: Option<Shape>,
) -> Self {
Self {
version: 1,
name,
extent,
shape_defined: shape.is_some(),
m_shape_defined: m_shape.is_some(),
shape: shape.unwrap_or_default(),
m_shape,
features,
}
}
pub fn add_feature(&mut self, feature: BaseVectorFeature) {
if !self.shape_defined {
let prop_shape: Shape = feature.properties().into();
self.shape.merge(&prop_shape);
}
if !self.m_shape_defined
&& let Some(m_values) = feature.m_values()
{
let feature_shape: Shape = (&m_values[..]).into();
match self.m_shape {
Some(ref mut m_shape) => m_shape.merge(&feature_shape),
None => self.m_shape = Some(feature_shape),
}
}
self.features.push(feature);
}
pub fn feature(&self, i: usize) -> &BaseVectorFeature {
&self.features[i]
}
pub fn len(&self) -> usize {
self.features.len()
}
pub fn is_empty(&self) -> bool {
self.features.is_empty()
}
}
impl From<&mut MapboxVectorLayer> for BaseVectorLayer {
fn from(mvt: &mut MapboxVectorLayer) -> Self {
let mut bvt = Self {
version: 1,
name: mvt.name.clone(),
extent: mvt.extent.into(),
shape_defined: false,
m_shape_defined: false,
shape: Shape::default(),
m_shape: None,
features: Vec::new(),
};
for feature in mvt.features.values_mut() {
bvt.add_feature(feature.into());
}
bvt
}
}