diff --git a/rust/geoarrow/src/algorithm/native/downcast.rs b/rust/geoarrow/src/algorithm/native/downcast.rs index 9006f37e..f721f4d6 100644 --- a/rust/geoarrow/src/algorithm/native/downcast.rs +++ b/rust/geoarrow/src/algorithm/native/downcast.rs @@ -21,7 +21,7 @@ pub trait Downcast { type Output; /// The data type that downcasting would result in. - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType; + fn downcasted_data_type(&self) -> NativeType; /// If possible, convert this array to a simpler and/or smaller data type /// @@ -33,19 +33,17 @@ pub trait Downcast { /// - MixedGeometry -> any of the 6 concrete types /// - GeometryCollection -> MixedGeometry or any of the 6 concrete types /// - /// If small_offsets is `true`, it will additionally try to convert `i64` offset buffers to - /// `i32` if the offsets would not overflow. - fn downcast(&self, small_offsets: bool) -> Self::Output; + fn downcast(&self) -> Self::Output; } impl Downcast for PointArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { self.data_type() } - fn downcast(&self, small_offsets: bool) -> Self::Output { + fn downcast(&self) -> Self::Output { Arc::new(self.clone()) } } @@ -93,15 +91,15 @@ pub(crate) fn can_downcast_multi(buffer: &OffsetBuffer) - impl Downcast for LineStringArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { match self.data_type() { NativeType::LineString(ct, dim) => NativeType::LineString(ct, dim), _ => unreachable!(), } } - fn downcast(&self, small_offsets: bool) -> Self::Output { - match (self.data_type(), self.downcasted_data_type(small_offsets)) { + fn downcast(&self) -> Self::Output { + match (self.data_type(), self.downcasted_data_type()) { ( NativeType::LineString(_, Dimension::XY), NativeType::LineString(_, Dimension::XY), @@ -114,14 +112,14 @@ impl Downcast for LineStringArray { impl Downcast for PolygonArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { match self.data_type() { NativeType::Polygon(ct, dim) => NativeType::Polygon(ct, dim), _ => unreachable!(), } } - fn downcast(&self, small_offsets: bool) -> Self::Output { + fn downcast(&self) -> Self::Output { Arc::new(self.clone()) } } @@ -129,7 +127,7 @@ impl Downcast for PolygonArray { impl Downcast for MultiPointArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { match self.data_type() { NativeType::MultiPoint(ct, dim) => { if can_downcast_multi(&self.geom_offsets) { @@ -141,7 +139,7 @@ impl Downcast for MultiPointArray { _ => unreachable!(), } } - fn downcast(&self, small_offsets: bool) -> Self::Output { + fn downcast(&self) -> Self::Output { // Note: this won't allow a downcast for empty MultiPoints if *self.geom_offsets.last() as usize == self.len() { return Arc::new(PointArray::new( @@ -158,7 +156,7 @@ impl Downcast for MultiPointArray { impl Downcast for MultiLineStringArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { match self.data_type() { NativeType::MultiLineString(ct, dim) => { if can_downcast_multi(&self.geom_offsets) { @@ -171,7 +169,7 @@ impl Downcast for MultiLineStringArray { } } - fn downcast(&self, small_offsets: bool) -> Self::Output { + fn downcast(&self) -> Self::Output { if *self.geom_offsets.last() as usize == self.len() { return Arc::new(LineStringArray::new( self.coords.clone(), @@ -188,7 +186,7 @@ impl Downcast for MultiLineStringArray { impl Downcast for MultiPolygonArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { match self.data_type() { NativeType::MultiPolygon(ct, dim) => { if can_downcast_multi(&self.geom_offsets) { @@ -201,7 +199,7 @@ impl Downcast for MultiPolygonArray { } } - fn downcast(&self, small_offsets: bool) -> Self::Output { + fn downcast(&self) -> Self::Output { if *self.geom_offsets.last() as usize == self.len() { return Arc::new(PolygonArray::new( self.coords.clone(), @@ -219,7 +217,7 @@ impl Downcast for MultiPolygonArray { impl Downcast for MixedGeometryArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { let coord_type = self.coord_type(); if self.has_points() @@ -239,7 +237,7 @@ impl Downcast for MixedGeometryArray { && !self.has_multi_line_strings() && !self.has_multi_polygons() { - return self.line_strings.downcasted_data_type(small_offsets); + return self.line_strings.downcasted_data_type(); } if !self.has_points() @@ -249,7 +247,7 @@ impl Downcast for MixedGeometryArray { && !self.has_multi_line_strings() && !self.has_multi_polygons() { - return self.polygons.downcasted_data_type(small_offsets); + return self.polygons.downcasted_data_type(); } if !self.has_points() @@ -259,7 +257,7 @@ impl Downcast for MixedGeometryArray { && !self.has_multi_line_strings() && !self.has_multi_polygons() { - return self.multi_points.downcasted_data_type(small_offsets); + return self.multi_points.downcasted_data_type(); } if !self.has_points() @@ -269,7 +267,7 @@ impl Downcast for MixedGeometryArray { && self.has_multi_line_strings() && !self.has_multi_polygons() { - return self.multi_line_strings.downcasted_data_type(small_offsets); + return self.multi_line_strings.downcasted_data_type(); } if !self.has_points() @@ -279,13 +277,13 @@ impl Downcast for MixedGeometryArray { && !self.has_multi_line_strings() && self.has_multi_polygons() { - return self.multi_polygons.downcasted_data_type(small_offsets); + return self.multi_polygons.downcasted_data_type(); } self.data_type() } - fn downcast(&self, small_offsets: bool) -> Self::Output { + fn downcast(&self) -> Self::Output { // TODO: do I need to handle the slice offset? if self.has_points() && !self.has_line_strings() @@ -304,7 +302,7 @@ impl Downcast for MixedGeometryArray { && !self.has_multi_line_strings() && !self.has_multi_polygons() { - return self.line_strings.downcast(small_offsets); + return self.line_strings.downcast(); } if !self.has_points() @@ -314,7 +312,7 @@ impl Downcast for MixedGeometryArray { && !self.has_multi_line_strings() && !self.has_multi_polygons() { - return self.polygons.downcast(small_offsets); + return self.polygons.downcast(); } if !self.has_points() @@ -324,7 +322,7 @@ impl Downcast for MixedGeometryArray { && !self.has_multi_line_strings() && !self.has_multi_polygons() { - return self.multi_points.downcast(small_offsets); + return self.multi_points.downcast(); } if !self.has_points() @@ -334,7 +332,7 @@ impl Downcast for MixedGeometryArray { && self.has_multi_line_strings() && !self.has_multi_polygons() { - return self.multi_line_strings.downcast(small_offsets); + return self.multi_line_strings.downcast(); } if !self.has_points() @@ -344,7 +342,7 @@ impl Downcast for MixedGeometryArray { && !self.has_multi_line_strings() && self.has_multi_polygons() { - return self.multi_polygons.downcast(small_offsets); + return self.multi_polygons.downcast(); } Arc::new(self.clone()) @@ -354,14 +352,14 @@ impl Downcast for MixedGeometryArray { impl Downcast for GeometryCollectionArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { todo!() } - fn downcast(&self, small_offsets: bool) -> Self::Output { + fn downcast(&self) -> Self::Output { // TODO: support downcasting with null elements if *self.geom_offsets.last() as usize == self.len() && self.null_count() == 0 { // Call downcast on the mixed array - return self.array.downcast(small_offsets); + return self.array.downcast(); } Arc::new(self.clone()) @@ -371,10 +369,10 @@ impl Downcast for GeometryCollectionArray { impl Downcast for RectArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { self.data_type() } - fn downcast(&self, small_offsets: bool) -> Self::Output { + fn downcast(&self) -> Self::Output { Arc::new(self.clone()) } } @@ -382,42 +380,38 @@ impl Downcast for RectArray { impl Downcast for &dyn NativeArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { use Dimension::*; use NativeType::*; match self.data_type() { - Point(_, XY) => self.as_point().downcasted_data_type(small_offsets), - LineString(_, XY) => self.as_line_string().downcasted_data_type(small_offsets), - Polygon(_, XY) => self.as_polygon().downcasted_data_type(small_offsets), - MultiPoint(_, XY) => self.as_multi_point().downcasted_data_type(small_offsets), - MultiLineString(_, XY) => self - .as_multi_line_string() - .downcasted_data_type(small_offsets), - MultiPolygon(_, XY) => self.as_multi_polygon().downcasted_data_type(small_offsets), - Mixed(_, XY) => self.as_mixed().downcasted_data_type(small_offsets), - GeometryCollection(_, XY) => self - .as_geometry_collection() - .downcasted_data_type(small_offsets), - Rect(XY) => self.as_rect().downcasted_data_type(small_offsets), + Point(_, XY) => self.as_point().downcasted_data_type(), + LineString(_, XY) => self.as_line_string().downcasted_data_type(), + Polygon(_, XY) => self.as_polygon().downcasted_data_type(), + MultiPoint(_, XY) => self.as_multi_point().downcasted_data_type(), + MultiLineString(_, XY) => self.as_multi_line_string().downcasted_data_type(), + MultiPolygon(_, XY) => self.as_multi_polygon().downcasted_data_type(), + Mixed(_, XY) => self.as_mixed().downcasted_data_type(), + GeometryCollection(_, XY) => self.as_geometry_collection().downcasted_data_type(), + Rect(XY) => self.as_rect().downcasted_data_type(), _ => todo!("3d support"), } } - fn downcast(&self, small_offsets: bool) -> Self::Output { + fn downcast(&self) -> Self::Output { use Dimension::*; use NativeType::*; match self.data_type() { - Point(_, XY) => self.as_point().downcast(small_offsets), - LineString(_, XY) => self.as_line_string().downcast(small_offsets), - Polygon(_, XY) => self.as_polygon().downcast(small_offsets), - MultiPoint(_, XY) => self.as_multi_point().downcast(small_offsets), - MultiLineString(_, XY) => self.as_multi_line_string().downcast(small_offsets), - MultiPolygon(_, XY) => self.as_multi_polygon().downcast(small_offsets), - Mixed(_, XY) => self.as_mixed().downcast(small_offsets), - GeometryCollection(_, XY) => self.as_geometry_collection().downcast(small_offsets), - Rect(XY) => self.as_rect().downcast(small_offsets), + Point(_, XY) => self.as_point().downcast(), + LineString(_, XY) => self.as_line_string().downcast(), + Polygon(_, XY) => self.as_polygon().downcast(), + MultiPoint(_, XY) => self.as_multi_point().downcast(), + MultiLineString(_, XY) => self.as_multi_line_string().downcast(), + MultiPolygon(_, XY) => self.as_multi_polygon().downcast(), + Mixed(_, XY) => self.as_mixed().downcast(), + GeometryCollection(_, XY) => self.as_geometry_collection().downcast(), + Rect(XY) => self.as_rect().downcast(), _ => todo!("3d support"), } } @@ -460,10 +454,10 @@ fn resolve_types(types: &HashSet) -> NativeType { impl Downcast for ChunkedPointArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { self.data_type() } - fn downcast(&self, small_offsets: bool) -> Self::Output { + fn downcast(&self) -> Self::Output { Arc::new(self.clone()) } } @@ -473,15 +467,15 @@ macro_rules! impl_chunked_downcast { impl Downcast for $chunked_array { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { let mut types = HashSet::new(); self.chunks.iter().for_each(|chunk| { - types.insert(chunk.downcasted_data_type(small_offsets)); + types.insert(chunk.downcasted_data_type()); }); resolve_types(&types) } - fn downcast(&self, small_offsets: bool) -> Self::Output { - let to_data_type = self.downcasted_data_type(small_offsets); + fn downcast(&self) -> Self::Output { + let to_data_type = self.downcasted_data_type(); if to_data_type == self.data_type() { return Arc::new(self.clone()); @@ -504,10 +498,10 @@ impl_chunked_downcast!(ChunkedGeometryCollectionArray); impl Downcast for ChunkedRectArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { self.data_type() } - fn downcast(&self, small_offsets: bool) -> Self::Output { + fn downcast(&self) -> Self::Output { Arc::new(self.clone()) } } @@ -515,42 +509,38 @@ impl Downcast for ChunkedRectArray { impl Downcast for &dyn ChunkedNativeArray { type Output = Arc; - fn downcasted_data_type(&self, small_offsets: bool) -> NativeType { + fn downcasted_data_type(&self) -> NativeType { use Dimension::*; use NativeType::*; match self.data_type() { - Point(_, XY) => self.as_point().downcasted_data_type(small_offsets), - LineString(_, XY) => self.as_line_string().downcasted_data_type(small_offsets), - Polygon(_, XY) => self.as_polygon().downcasted_data_type(small_offsets), - MultiPoint(_, XY) => self.as_multi_point().downcasted_data_type(small_offsets), - MultiLineString(_, XY) => self - .as_multi_line_string() - .downcasted_data_type(small_offsets), - MultiPolygon(_, XY) => self.as_multi_polygon().downcasted_data_type(small_offsets), - Mixed(_, XY) => self.as_mixed().downcasted_data_type(small_offsets), - GeometryCollection(_, XY) => self - .as_geometry_collection() - .downcasted_data_type(small_offsets), - Rect(XY) => self.as_rect().downcasted_data_type(small_offsets), + Point(_, XY) => self.as_point().downcasted_data_type(), + LineString(_, XY) => self.as_line_string().downcasted_data_type(), + Polygon(_, XY) => self.as_polygon().downcasted_data_type(), + MultiPoint(_, XY) => self.as_multi_point().downcasted_data_type(), + MultiLineString(_, XY) => self.as_multi_line_string().downcasted_data_type(), + MultiPolygon(_, XY) => self.as_multi_polygon().downcasted_data_type(), + Mixed(_, XY) => self.as_mixed().downcasted_data_type(), + GeometryCollection(_, XY) => self.as_geometry_collection().downcasted_data_type(), + Rect(XY) => self.as_rect().downcasted_data_type(), _ => todo!("3d support"), } } - fn downcast(&self, small_offsets: bool) -> Self::Output { + fn downcast(&self) -> Self::Output { use Dimension::*; use NativeType::*; match self.data_type() { - Point(_, XY) => self.as_point().downcast(small_offsets), - LineString(_, XY) => self.as_line_string().downcast(small_offsets), - Polygon(_, XY) => self.as_polygon().downcast(small_offsets), - MultiPoint(_, XY) => self.as_multi_point().downcast(small_offsets), - MultiLineString(_, XY) => self.as_multi_line_string().downcast(small_offsets), - MultiPolygon(_, XY) => self.as_multi_polygon().downcast(small_offsets), - Mixed(_, XY) => self.as_mixed().downcast(small_offsets), - GeometryCollection(_, XY) => self.as_geometry_collection().downcast(small_offsets), - Rect(XY) => self.as_rect().downcast(small_offsets), + Point(_, XY) => self.as_point().downcast(), + LineString(_, XY) => self.as_line_string().downcast(), + Polygon(_, XY) => self.as_polygon().downcast(), + MultiPoint(_, XY) => self.as_multi_point().downcast(), + MultiLineString(_, XY) => self.as_multi_line_string().downcast(), + MultiPolygon(_, XY) => self.as_multi_polygon().downcast(), + Mixed(_, XY) => self.as_mixed().downcast(), + GeometryCollection(_, XY) => self.as_geometry_collection().downcast(), + Rect(XY) => self.as_rect().downcast(), _ => todo!("3d support"), } } @@ -569,11 +559,11 @@ pub trait DowncastTable { /// /// If small_offsets is `true`, it will additionally try to convert `i64` offset buffers to /// `i32` if the offsets would not overflow. - fn downcast(&self, small_offsets: bool) -> Result; + fn downcast(&self) -> Result
; } impl DowncastTable for Table { - fn downcast(&self, small_offsets: bool) -> Result
{ + fn downcast(&self) -> Result
{ let downcasted_columns = self .schema() .as_ref() @@ -581,7 +571,7 @@ impl DowncastTable for Table { .iter() .map(|idx| { let geometry = self.geometry_column(Some(*idx))?; - Ok((*idx, geometry.as_ref().downcast(small_offsets))) + Ok((*idx, geometry.as_ref().downcast())) }) .collect::>>()?; diff --git a/rust/geoarrow/src/io/flatgeobuf/reader/async.rs b/rust/geoarrow/src/io/flatgeobuf/reader/async.rs index 0c55370e..92f8eb84 100644 --- a/rust/geoarrow/src/io/flatgeobuf/reader/async.rs +++ b/rust/geoarrow/src/io/flatgeobuf/reader/async.rs @@ -99,7 +99,7 @@ pub async fn read_flatgeobuf_async( ); selection.process_features(&mut builder).await?; let table = builder.finish()?; - table.downcast(true) + table.downcast() } (GeometryType::Point, true) => { impl_read!(PointBuilder, Dimension::XYZ) @@ -122,7 +122,7 @@ pub async fn read_flatgeobuf_async( ); selection.process_features(&mut builder).await?; let table = builder.finish()?; - table.downcast(true) + table.downcast() } // TODO: Parse into a GeometryCollection array and then downcast to a single-typed array if possible. geom_type => Err(GeoArrowError::NotYetImplemented(format!( diff --git a/rust/geoarrow/src/io/flatgeobuf/reader/sync.rs b/rust/geoarrow/src/io/flatgeobuf/reader/sync.rs index f41c82fd..5d5c0376 100644 --- a/rust/geoarrow/src/io/flatgeobuf/reader/sync.rs +++ b/rust/geoarrow/src/io/flatgeobuf/reader/sync.rs @@ -105,7 +105,7 @@ pub fn read_flatgeobuf( ); selection.process_features(&mut builder)?; let table = builder.finish()?; - table.downcast(true) + table.downcast() } (GeometryType::Point, true) => { impl_read!(PointBuilder, Dimension::XYZ) @@ -129,7 +129,7 @@ pub fn read_flatgeobuf( selection.process_features(&mut builder)?; let table = builder.finish()?; // TODO: 3d downcasting not implemented - // table.downcast(true) + // table.downcast() Ok(table) } // TODO: Parse into a GeometryCollection array and then downcast to a single-typed array if possible. diff --git a/rust/geoarrow/src/io/wkb/api.rs b/rust/geoarrow/src/io/wkb/api.rs index 0b733105..70d04bc0 100644 --- a/rust/geoarrow/src/io/wkb/api.rs +++ b/rust/geoarrow/src/io/wkb/api.rs @@ -124,7 +124,7 @@ impl FromWKB for Arc { arr.metadata(), true, )?; - Ok(builder.finish().downcast(true)) + Ok(builder.finish().downcast()) } } @@ -175,7 +175,7 @@ impl FromWKB for Arc { dim: Dimension, ) -> Result { let geom_arr = ChunkedGeometryCollectionArray::from_wkb(arr, coord_type, dim)?; - Ok(geom_arr.downcast(true)) + Ok(geom_arr.downcast()) } } @@ -376,7 +376,7 @@ mod test { .unwrap(); let rt_ref = roundtrip.as_ref(); let rt_mixed_arr = rt_ref.as_mixed(); - let downcasted = rt_mixed_arr.downcast(true); + let downcasted = rt_mixed_arr.downcast(); let downcasted_ref = downcasted.as_ref(); let rt_point_arr = downcasted_ref.as_point(); assert_eq!(&arr, rt_point_arr); diff --git a/rust/geoarrow/src/table.rs b/rust/geoarrow/src/table.rs index 30de22c8..9bc87bb6 100644 --- a/rust/geoarrow/src/table.rs +++ b/rust/geoarrow/src/table.rs @@ -236,7 +236,7 @@ impl Table { ChunkedNativeArrayDyn::from_geoarrow_chunks(parsed_chunks_refs.as_slice())? .into_inner() .as_ref() - .downcast(true) + .downcast() } SerializedType::LargeWKB => { let wkb_chunks = array_slices @@ -254,7 +254,7 @@ impl Table { ChunkedNativeArrayDyn::from_geoarrow_chunks(parsed_chunks_refs.as_slice())? .into_inner() .as_ref() - .downcast(true) + .downcast() } _ => panic!("WKT input not supported yet"), };