-
Notifications
You must be signed in to change notification settings - Fork 199
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
Validation::is_valid #1279
Merged
Merged
Validation::is_valid #1279
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4a5dde6
extract test runner fn
michaelkirk f4a7ec5
add validation tests from JTS test suite
michaelkirk 45a3eb3
cp -r ../geo-validity-check/src/* geo/src/algorithm/validation/
michaelkirk 8af9864
integrate geo-validity-check into geo
michaelkirk bf14df5
NBD: naming nits
michaelkirk f64a873
Baseline: Test Report: 5 failures / 750 successes in JTS test suite:
michaelkirk 107ed5f
All JTS tests passing: EMPTY geometries are considered valid.
michaelkirk 5027cdb
DRY up validation logic.
michaelkirk 641853b
Warn rather than debug_assert with invalid geometries
michaelkirk 1559ded
DRY up GEOS integration
michaelkirk fdbd4b2
changelog entry
michaelkirk 361ebc1
use latest ci images
michaelkirk 3651525
Update geo/CHANGES.md
michaelkirk c58d61a
use std::error::Error
michaelkirk 3d30ba7
add top level doc
michaelkirk fd7c4b5
remove geos tests over concerns about licensing. it wasn't a critical…
michaelkirk e35499b
Update geo/src/algorithm/validation/geometry.rs
michaelkirk 2b4ded7
make inner value pub for index types
michaelkirk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use super::{utils, Validation}; | ||
use crate::{Coord, GeoFloat}; | ||
|
||
use std::fmt; | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub enum InvalidCoord { | ||
/// A valid [`Coord`] must be finite. | ||
NonFinite, | ||
} | ||
|
||
impl fmt::Display for InvalidCoord { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
InvalidCoord::NonFinite => write!(f, "coordinite was non-finite"), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for InvalidCoord {} | ||
|
||
impl<F: GeoFloat> Validation for Coord<F> { | ||
type Error = InvalidCoord; | ||
|
||
fn visit_validation<T>( | ||
&self, | ||
mut handle_validation_error: Box<dyn FnMut(Self::Error) -> Result<(), T> + '_>, | ||
) -> Result<(), T> { | ||
if utils::check_coord_is_not_finite(self) { | ||
handle_validation_error(InvalidCoord::NonFinite)?; | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
use super::Validation; | ||
use super::{ | ||
InvalidGeometryCollection, InvalidLine, InvalidLineString, InvalidMultiLineString, | ||
InvalidMultiPoint, InvalidMultiPolygon, InvalidPoint, InvalidPolygon, InvalidRect, | ||
InvalidTriangle, | ||
}; | ||
use crate::{GeoFloat, Geometry}; | ||
|
||
use crate::geometry_cow::GeometryCow; | ||
use std::fmt; | ||
|
||
/// A [`Geometry`] is valid if its inner variant is valid. | ||
/// e.g. `Geometry::Polygon(polygon)` is valid if and only if `polygon` is valid. | ||
#[derive(Debug, Clone, PartialEq)] | ||
pub enum InvalidGeometry { | ||
InvalidPoint(InvalidPoint), | ||
InvalidLine(InvalidLine), | ||
InvalidLineString(InvalidLineString), | ||
InvalidPolygon(InvalidPolygon), | ||
InvalidMultiPoint(InvalidMultiPoint), | ||
InvalidMultiLineString(InvalidMultiLineString), | ||
InvalidMultiPolygon(InvalidMultiPolygon), | ||
InvalidGeometryCollection(InvalidGeometryCollection), | ||
InvalidRect(InvalidRect), | ||
InvalidTriangle(InvalidTriangle), | ||
} | ||
|
||
impl fmt::Display for InvalidGeometry { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
InvalidGeometry::InvalidPoint(err) => write!(f, "{}", err), | ||
InvalidGeometry::InvalidLine(err) => write!(f, "{}", err), | ||
InvalidGeometry::InvalidLineString(err) => write!(f, "{}", err), | ||
InvalidGeometry::InvalidPolygon(err) => write!(f, "{}", err), | ||
InvalidGeometry::InvalidMultiPoint(err) => write!(f, "{}", err), | ||
InvalidGeometry::InvalidMultiLineString(err) => write!(f, "{}", err), | ||
InvalidGeometry::InvalidMultiPolygon(err) => write!(f, "{}", err), | ||
InvalidGeometry::InvalidGeometryCollection(err) => write!(f, "{}", err), | ||
InvalidGeometry::InvalidRect(err) => write!(f, "{}", err), | ||
InvalidGeometry::InvalidTriangle(err) => write!(f, "{}", err), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for InvalidGeometry { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
match self { | ||
InvalidGeometry::InvalidPoint(err) => Some(err), | ||
InvalidGeometry::InvalidLine(err) => Some(err), | ||
InvalidGeometry::InvalidLineString(err) => Some(err), | ||
InvalidGeometry::InvalidPolygon(err) => Some(err), | ||
InvalidGeometry::InvalidMultiPoint(err) => Some(err), | ||
InvalidGeometry::InvalidMultiLineString(err) => Some(err), | ||
InvalidGeometry::InvalidMultiPolygon(err) => Some(err), | ||
InvalidGeometry::InvalidGeometryCollection(err) => Some(err), | ||
InvalidGeometry::InvalidRect(err) => Some(err), | ||
InvalidGeometry::InvalidTriangle(err) => Some(err), | ||
} | ||
} | ||
} | ||
|
||
impl<F: GeoFloat> Validation for Geometry<F> { | ||
type Error = InvalidGeometry; | ||
|
||
fn visit_validation<T>( | ||
&self, | ||
mut handle_validation_error: Box<dyn FnMut(Self::Error) -> Result<(), T> + '_>, | ||
) -> Result<(), T> { | ||
match self { | ||
Geometry::Point(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidPoint(err)) | ||
}))?, | ||
Geometry::Line(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidLine(err)) | ||
}))?, | ||
Geometry::LineString(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidLineString(err)) | ||
}))?, | ||
Geometry::Polygon(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidPolygon(err)) | ||
}))?, | ||
Geometry::MultiPoint(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidMultiPoint(err)) | ||
}))?, | ||
Geometry::MultiLineString(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidMultiLineString(err)) | ||
}))?, | ||
Geometry::MultiPolygon(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidMultiPolygon(err)) | ||
}))?, | ||
Geometry::GeometryCollection(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidGeometryCollection(err)) | ||
}))?, | ||
Geometry::Rect(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidRect(err)) | ||
}))?, | ||
Geometry::Triangle(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidTriangle(err)) | ||
}))?, | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<F: GeoFloat> Validation for GeometryCow<'_, F> { | ||
type Error = InvalidGeometry; | ||
|
||
fn visit_validation<T>( | ||
&self, | ||
mut handle_validation_error: Box<dyn FnMut(Self::Error) -> Result<(), T> + '_>, | ||
) -> Result<(), T> { | ||
match self { | ||
GeometryCow::Point(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidPoint(err)) | ||
}))?, | ||
GeometryCow::Line(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidLine(err)) | ||
}))?, | ||
GeometryCow::LineString(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidLineString(err)) | ||
}))?, | ||
GeometryCow::Polygon(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidPolygon(err)) | ||
}))?, | ||
GeometryCow::MultiPoint(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidMultiPoint(err)) | ||
}))?, | ||
GeometryCow::MultiLineString(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidMultiLineString(err)) | ||
}))?, | ||
GeometryCow::MultiPolygon(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidMultiPolygon(err)) | ||
}))?, | ||
GeometryCow::GeometryCollection(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidGeometryCollection(err)) | ||
}))?, | ||
GeometryCow::Rect(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidRect(err)) | ||
}))?, | ||
GeometryCow::Triangle(g) => g.visit_validation(Box::new(|err| { | ||
handle_validation_error(InvalidGeometry::InvalidTriangle(err)) | ||
}))?, | ||
} | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should address #1268
right_position != current_position
should not happen normally, and this debug_assertion was originally intended to find bugs in geo code. However, it's also been shown to happen when given invalid geometry input.We can now distinguish between a hypothetical bug in geo code from a bug in the caller's code, and just log in that latter case.