Skip to content

Commit

Permalink
Check geo type
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Nov 2, 2023
1 parent f5dc53b commit 6eb91cc
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 66 deletions.
1 change: 0 additions & 1 deletion src/array/mixed/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ pub struct MixedGeometryArray<O: OffsetSizeTrait> {
slice_offset: usize,
}

// TODO: rename to "GeometryType"?
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GeometryType {
Point = 0,
Expand Down
3 changes: 1 addition & 2 deletions src/io/geos/array/linestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ impl<'a, O: OffsetSizeTrait> TryFrom<Vec<Option<geos::Geometry<'a>>>>

fn try_from(value: Vec<Option<geos::Geometry<'a>>>) -> std::result::Result<Self, Self::Error> {
let length = value.len();
// TODO: don't use new_unchecked
let geos_linestring_objects: Vec<Option<GEOSLineString>> = value
.into_iter()
.map(|geom| geom.map(GEOSLineString::new_unchecked))
.map(|geom| geom.map(|geom| GEOSLineString::try_new(geom)?))
.collect();
let (coord_capacity, geom_capacity) = first_pass(
geos_linestring_objects.iter().map(|item| item.as_ref()),
Expand Down
3 changes: 1 addition & 2 deletions src/io/geos/array/multilinestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ impl<O: OffsetSizeTrait> TryFrom<Vec<Option<geos::Geometry<'_>>>>

fn try_from(value: Vec<Option<geos::Geometry<'_>>>) -> Result<Self> {
let length = value.len();
// TODO: don't use new_unchecked
let geos_objects: Vec<Option<GEOSMultiLineString>> = value
.into_iter()
.map(|geom| geom.map(GEOSMultiLineString::new_unchecked))
.map(|geom| geom.map(|geom| GEOSMultiLineString::try_new(geom)?))
.collect();

let (coord_capacity, ring_capacity, geom_capacity) = first_pass(&geos_objects, length);
Expand Down
3 changes: 1 addition & 2 deletions src/io/geos/array/multipoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ impl<'a, O: OffsetSizeTrait> TryFrom<Vec<Option<geos::Geometry<'a>>>>

fn try_from(value: Vec<Option<geos::Geometry<'a>>>) -> std::result::Result<Self, Self::Error> {
let length = value.len();
// TODO: don't use new_unchecked
let geos_objects: Vec<Option<GEOSMultiPoint>> = value
.into_iter()
.map(|geom| geom.map(GEOSMultiPoint::new_unchecked))
.map(|geom| geom.map(|geom| GEOSMultiPoint::try_new(geom)?))
.collect();
let (coord_capacity, geom_capacity) = first_pass(&geos_objects, length);
Ok(second_pass(
Expand Down
3 changes: 1 addition & 2 deletions src/io/geos/array/multipolygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,9 @@ impl<O: OffsetSizeTrait> TryFrom<Vec<Option<geos::Geometry<'_>>>> for MutableMul

fn try_from(value: Vec<Option<geos::Geometry<'_>>>) -> Result<Self> {
let length = value.len();
// TODO: don't use new_unchecked
let geos_objects: Vec<Option<GEOSMultiPolygon>> = value
.into_iter()
.map(|geom| geom.map(GEOSMultiPolygon::new_unchecked))
.map(|geom| geom.map(|geom| GEOSMultiPolygon::try_new(geom)?))
.collect();

let (coord_capacity, ring_capacity, polygon_capacity, geom_capacity) =
Expand Down
3 changes: 1 addition & 2 deletions src/io/geos/array/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ impl<'a> TryFrom<Vec<Option<geos::Geometry<'a>>>> for MutablePointArray {

fn try_from(value: Vec<Option<geos::Geometry<'a>>>) -> std::result::Result<Self, Self::Error> {
let length = value.len();
// TODO: don't use new_unchecked
let geos_linestring_objects: Vec<Option<GEOSPoint>> = value
.into_iter()
.map(|geom| geom.map(GEOSPoint::new_unchecked))
.map(|geom| geom.map(|geom| GEOSPoint::try_new(geom)?))
.collect();
Ok(from_nullable_coords(
geos_linestring_objects.iter().map(|item| item.as_ref()),
Expand Down
6 changes: 2 additions & 4 deletions src/io/geos/array/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ impl<O: OffsetSizeTrait> TryFrom<Vec<Option<geos::Geometry<'_>>>> for MutablePol

fn try_from(value: Vec<Option<geos::Geometry<'_>>>) -> Result<Self> {
let length = value.len();
// TODO: don't use new_unchecked
let geos_objects: Vec<Option<GEOSPolygon>> = value
.into_iter()
.map(|geom| geom.map(GEOSPolygon::new_unchecked))
.map(|geom| geom.map(|geom| GEOSPolygon::try_new(geom)?))
.collect();

let (coord_capacity, ring_capacity, geom_capacity) = first_pass(&geos_objects, length);
Expand Down Expand Up @@ -151,10 +150,9 @@ impl<'a, O: OffsetSizeTrait> TryFrom<bumpalo::collections::Vec<'a, Option<geos::

// TODO: avoid creating GEOSPolygon objects at all?
let length = value.len();
// TODO: don't use new_unchecked
let geos_objects: bumpalo::collections::Vec<'_, Option<GEOSPolygon>> = value
.into_iter()
.map(|geom| geom.map(GEOSPolygon::new_unchecked))
.map(|geom| geom.map(|geom| GEOSPolygon::try_new(geom)?))
.collect_in(&bump);

let (coord_capacity, ring_capacity, geom_capacity) = first_pass(&geos_objects, length);
Expand Down
13 changes: 5 additions & 8 deletions src/io/geos/scalar/linestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,12 @@ impl<'b, O: OffsetSizeTrait> LineString<'_, O> {
pub struct GEOSLineString<'a>(geos::Geometry<'a>);

impl<'a> GEOSLineString<'a> {
pub fn new_unchecked(geom: geos::Geometry<'a>) -> Self {
Self(geom)
}

pub fn try_new(geom: geos::Geometry<'a>) -> Result<Self> {
// TODO: make Err
assert!(matches!(geom.geometry_type(), GeometryTypes::LineString));

Ok(Self(geom))
if matches!(geom.geometry_type(), GeometryTypes::LineString) {
Ok(Self(geom))
} else {
Err(GeoArrowError::General("Geometry type must be line string".to_string()))
}
}
}

Expand Down
17 changes: 5 additions & 12 deletions src/io/geos/scalar/multilinestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,12 @@ impl<'a, 'b, O: OffsetSizeTrait> TryFrom<&'a MultiLineString<'_, O>> for geos::G
pub struct GEOSMultiLineString<'a>(pub(crate) geos::Geometry<'a>);

impl<'a> GEOSMultiLineString<'a> {
pub fn new_unchecked(geom: geos::Geometry<'a>) -> Self {
Self(geom)
}

#[allow(dead_code)]
pub fn try_new(geom: geos::Geometry<'a>) -> Result<Self> {
// TODO: make Err
assert!(matches!(
geom.geometry_type(),
GeometryTypes::MultiLineString
));

Ok(Self(geom))
if matches!(geom.geometry_type(), GeometryTypes::MultiLineString) {
Ok(Self(geom))
} else {
Err(GeoArrowError::General("Geometry type must be multi line string".to_string()))
}
}

pub fn num_lines(&self) -> usize {
Expand Down
14 changes: 5 additions & 9 deletions src/io/geos/scalar/multipoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,12 @@ impl<'a, 'b, O: OffsetSizeTrait> TryFrom<&'a MultiPoint<'_, O>> for geos::Geomet
pub struct GEOSMultiPoint<'a>(pub(crate) geos::Geometry<'a>);

impl<'a> GEOSMultiPoint<'a> {
pub fn new_unchecked(geom: geos::Geometry<'a>) -> Self {
Self(geom)
}

#[allow(dead_code)]
pub fn try_new(geom: geos::Geometry<'a>) -> Result<Self> {
// TODO: make Err
assert!(matches!(geom.geometry_type(), GeometryTypes::MultiPoint));

Ok(Self(geom))
if matches!(geom.geometry_type(), GeometryTypes::MultiPoint) {
Ok(Self(geom))
} else {
Err(GeoArrowError::General("Geometry type must be multi point".to_string()))
}
}

pub fn num_points(&self) -> usize {
Expand Down
14 changes: 5 additions & 9 deletions src/io/geos/scalar/multipolygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,12 @@ impl<'a, 'b, O: OffsetSizeTrait> TryFrom<&'a MultiPolygon<'_, O>> for geos::Geom
pub struct GEOSMultiPolygon<'a>(pub(crate) geos::Geometry<'a>);

impl<'a> GEOSMultiPolygon<'a> {
pub fn new_unchecked(geom: geos::Geometry<'a>) -> Self {
Self(geom)
}

#[allow(dead_code)]
pub fn try_new(geom: geos::Geometry<'a>) -> Result<Self> {
// TODO: make Err
assert!(matches!(geom.geometry_type(), GeometryTypes::MultiPolygon));

Ok(Self(geom))
if matches!(geom.geometry_type(), GeometryTypes::MultiPolygon) {
Ok(Self(geom))
} else {
Err(GeoArrowError::General("Geometry type must be multi polygon".to_string()))
}
}

pub fn num_polygons(&self) -> usize {
Expand Down
9 changes: 5 additions & 4 deletions src/io/geos/scalar/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ impl<'a> GEOSPoint<'a> {
}

pub fn try_new(geom: geos::Geometry<'a>) -> Result<Self> {
// TODO: make Err
assert!(matches!(geom.geometry_type(), GeometryTypes::Point));

Ok(Self(geom))
if matches!(geom.geometry_type(), GeometryTypes::Point) {
Ok(Self(geom))
} else {
Err(GeoArrowError::General("Geometry type must be point".to_string()))
}
}
}

Expand Down
14 changes: 5 additions & 9 deletions src/io/geos/scalar/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,12 @@ impl<'a, 'b, O: OffsetSizeTrait> TryFrom<&'a Polygon<'_, O>> for geos::Geometry<
pub struct GEOSPolygon<'a>(pub(crate) geos::Geometry<'a>);

impl<'a> GEOSPolygon<'a> {
pub fn new_unchecked(geom: geos::Geometry<'a>) -> Self {
Self(geom)
}

#[allow(dead_code)]
pub fn try_new(geom: geos::Geometry<'a>) -> Result<Self> {
// TODO: make Err
assert!(matches!(geom.geometry_type(), GeometryTypes::LineString));

Ok(Self(geom))
if matches!(geom.geometry_type(), GeometryTypes::Polygon) {
Ok(Self(geom))
} else {
Err(GeoArrowError::General("Geometry type must be polygon".to_string()))
}
}

#[allow(dead_code)]
Expand Down

0 comments on commit 6eb91cc

Please sign in to comment.