Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid panics in GEOS geometry construction #217

Merged
merged 8 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.DS_Store
.idea

# Generated by Cargo
# will have compiled files and executables
Expand Down
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
13 changes: 8 additions & 5 deletions src/io/geos/scalar/linearring.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::Result;
use crate::error::{GeoArrowError, Result};
use geos::{Geom, GeometryTypes};

pub struct GEOSConstLinearRing<'a, 'b>(pub(crate) geos::ConstGeometry<'a, 'b>);
Expand All @@ -10,10 +10,13 @@ impl<'a, 'b> GEOSConstLinearRing<'a, 'b> {

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

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

pub fn num_coords(&self) -> usize {
Expand Down
23 changes: 14 additions & 9 deletions src/io/geos/scalar/linestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ 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 Expand Up @@ -114,10 +116,13 @@ impl<'a, 'b> GEOSConstLineString<'a, 'b> {

#[allow(dead_code)]
pub fn try_new(geom: geos::ConstGeometry<'a, 'b>) -> 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
14 changes: 7 additions & 7 deletions src/io/geos/scalar/multilinestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ impl<'a> GEOSMultiLineString<'a> {

#[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
11 changes: 7 additions & 4 deletions src/io/geos/scalar/multipoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ impl<'a> GEOSMultiPoint<'a> {

#[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
11 changes: 7 additions & 4 deletions src/io/geos/scalar/multipolygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ impl<'a> GEOSMultiPolygon<'a> {

#[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
22 changes: 14 additions & 8 deletions src/io/geos/scalar/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ 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 Expand Up @@ -96,10 +99,13 @@ impl<'a, 'b> GEOSConstPoint<'a, 'b> {
}

pub fn try_new(geom: &'a geos::ConstGeometry<'a, 'b>) -> 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
22 changes: 14 additions & 8 deletions src/io/geos/scalar/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ impl<'a> GEOSPolygon<'a> {

#[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 Expand Up @@ -88,10 +91,13 @@ impl<'a, 'b> GEOSConstPolygon<'a, 'b> {

#[allow(dead_code)]
pub fn try_new(geom: geos::ConstGeometry<'a, 'b>) -> 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(),
))
}
}

pub fn num_interiors(&self) -> usize {
Expand Down
Loading