use std::sync::Arc;
use arrow_array::OffsetSizeTrait;
use arrow_buffer::NullBufferBuilder;
use geo_traits::{CoordTrait, GeometryTrait, GeometryType, LineStringTrait, MultiLineStringTrait};
use geoarrow_schema::error::{GeoArrowError, GeoArrowResult};
use geoarrow_schema::type_id::GeometryTypeId;
use geoarrow_schema::{Dimension, LineStringType};
use crate::GeoArrowArray;
use crate::array::{GenericWkbArray, LineStringArray};
use crate::builder::geo_trait_wrappers::LineWrapper;
use crate::builder::{CoordBufferBuilder, OffsetsBuilder};
use crate::capacity::LineStringCapacity;
use crate::trait_::{GeoArrowArrayAccessor, GeoArrowArrayBuilder};
use crate::util::GeometryTypeName;
#[derive(Debug)]
pub struct LineStringBuilder {
data_type: LineStringType,
pub(crate) coords: CoordBufferBuilder,
pub(crate) geom_offsets: OffsetsBuilder<i32>,
pub(crate) validity: NullBufferBuilder,
}
impl LineStringBuilder {
pub fn new(typ: LineStringType) -> Self {
Self::with_capacity(typ, Default::default())
}
pub fn with_capacity(typ: LineStringType, capacity: LineStringCapacity) -> Self {
let coords = CoordBufferBuilder::with_capacity(
capacity.coord_capacity,
typ.coord_type(),
typ.dimension(),
);
Self {
coords,
geom_offsets: OffsetsBuilder::with_capacity(capacity.geom_capacity()),
validity: NullBufferBuilder::new(capacity.geom_capacity()),
data_type: typ,
}
}
pub fn reserve(&mut self, additional: LineStringCapacity) {
self.coords.reserve(additional.coord_capacity());
self.geom_offsets.reserve(additional.geom_capacity());
}
pub fn reserve_exact(&mut self, additional: LineStringCapacity) {
self.coords.reserve_exact(additional.coord_capacity());
self.geom_offsets.reserve_exact(additional.geom_capacity());
}
pub fn shrink_to_fit(&mut self) {
self.coords.shrink_to_fit();
self.geom_offsets.shrink_to_fit();
}
#[inline]
pub(crate) fn try_push_length(&mut self, geom_offsets_length: usize) -> GeoArrowResult<()> {
self.geom_offsets.try_push_usize(geom_offsets_length)?;
self.validity.append(true);
Ok(())
}
#[inline]
pub fn push_empty(&mut self) {
self.geom_offsets.extend_constant(1);
self.validity.append(true);
}
#[inline]
pub(crate) fn push_null(&mut self) {
self.geom_offsets.extend_constant(1);
self.validity.append(false);
}
pub fn finish(mut self) -> LineStringArray {
let validity = self.validity.finish();
LineStringArray::new(
self.coords.finish(),
self.geom_offsets.finish(),
validity,
self.data_type.metadata().clone(),
)
}
pub fn from_line_strings(geoms: &[impl LineStringTrait<T = f64>], typ: LineStringType) -> Self {
let capacity = LineStringCapacity::from_line_strings(geoms.iter().map(Some));
let mut array = Self::with_capacity(typ, capacity);
array.extend_from_iter(geoms.iter().map(Some));
array
}
pub fn from_nullable_line_strings(
geoms: &[Option<impl LineStringTrait<T = f64>>],
typ: LineStringType,
) -> Self {
let capacity = LineStringCapacity::from_line_strings(geoms.iter().map(|x| x.as_ref()));
let mut array = Self::with_capacity(typ, capacity);
array.extend_from_iter(geoms.iter().map(|x| x.as_ref()));
array
}
#[inline]
pub fn push_line_string(
&mut self,
value: Option<&impl LineStringTrait<T = f64>>,
) -> GeoArrowResult<()> {
if let Some(line_string) = value {
let num_coords = line_string.num_coords();
for coord in line_string.coords() {
self.coords.try_push_coord(&coord)?;
}
self.try_push_length(num_coords)?;
} else {
self.push_null();
}
Ok(())
}
pub fn extend_from_iter<'a>(
&mut self,
geoms: impl Iterator<Item = Option<&'a (impl LineStringTrait<T = f64> + 'a)>>,
) {
geoms
.into_iter()
.try_for_each(|maybe_multi_point| self.push_line_string(maybe_multi_point))
.unwrap();
}
pub fn extend_from_geometry_iter<'a>(
&mut self,
geoms: impl Iterator<Item = Option<&'a (impl GeometryTrait<T = f64> + 'a)>>,
) -> GeoArrowResult<()> {
geoms.into_iter().try_for_each(|g| self.push_geometry(g))?;
Ok(())
}
#[inline]
#[allow(dead_code)]
pub(crate) fn push_coord(&mut self, coord: &impl CoordTrait<T = f64>) -> GeoArrowResult<()> {
self.coords.try_push_coord(coord)
}
#[inline]
pub fn push_geometry(
&mut self,
value: Option<&impl GeometryTrait<T = f64>>,
) -> GeoArrowResult<()> {
if let Some(value) = value {
match value.as_type() {
GeometryType::LineString(g) => self.push_line_string(Some(g))?,
GeometryType::MultiLineString(ml) => {
let num_line_strings = ml.num_line_strings();
if num_line_strings == 0 {
self.push_empty();
} else if num_line_strings == 1 {
self.push_line_string(Some(&ml.line_string(0).unwrap()))?
} else {
return Err(GeoArrowError::IncorrectGeometryType(format!(
"Expected MultiLineString with only one LineString in LineStringBuilder, got {num_line_strings} line strings",
)));
}
}
GeometryType::Line(l) => self.push_line_string(Some(&LineWrapper(l)))?,
gt => {
return Err(GeoArrowError::IncorrectGeometryType(format!(
"Expected LineString, got {}",
gt.name()
)));
}
}
} else {
self.push_null();
};
Ok(())
}
pub fn from_nullable_geometries(
geoms: &[Option<impl GeometryTrait<T = f64>>],
typ: LineStringType,
) -> GeoArrowResult<Self> {
let capacity = LineStringCapacity::from_geometries(geoms.iter().map(|x| x.as_ref()))?;
let mut array = Self::with_capacity(typ, capacity);
array.extend_from_geometry_iter(geoms.iter().map(|x| x.as_ref()))?;
Ok(array)
}
}
impl<O: OffsetSizeTrait> TryFrom<(GenericWkbArray<O>, LineStringType)> for LineStringBuilder {
type Error = GeoArrowError;
fn try_from((value, typ): (GenericWkbArray<O>, LineStringType)) -> GeoArrowResult<Self> {
let wkb_objects = value
.iter()
.map(|x| x.transpose())
.collect::<GeoArrowResult<Vec<_>>>()?;
Self::from_nullable_geometries(&wkb_objects, typ)
}
}
impl GeoArrowArrayBuilder for LineStringBuilder {
fn len(&self) -> usize {
self.geom_offsets.len_proxy()
}
fn push_null(&mut self) {
self.push_null();
}
fn push_geometry(
&mut self,
geometry: Option<&impl GeometryTrait<T = f64>>,
) -> GeoArrowResult<()> {
self.push_geometry(geometry)
}
fn finish(self) -> Arc<dyn GeoArrowArray> {
Arc::new(self.finish())
}
}
impl GeometryTypeId for LineStringBuilder {
const GEOMETRY_TYPE_OFFSET: i8 = 2;
fn dimension(&self) -> Dimension {
self.data_type.dimension()
}
}