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

Validation::is_valid #1279

Merged
merged 18 commits into from
Dec 19, 2024
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
22 changes: 11 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ jobs:
# Minimum supported rust version (MSRV)
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.75"
# Two most recent releases - we omit older ones for expedient CI
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.78"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.79"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.82"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.83"
container:
image: ${{ matrix.container_image }}
steps:
Expand All @@ -108,8 +108,8 @@ jobs:
# Minimum supported rust version (MSRV)
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.75"
# Two most recent releases - we omit older ones for expedient CI
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.78"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.79"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.82"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.83"
container:
image: ${{ matrix.container_image }}
steps:
Expand All @@ -135,8 +135,8 @@ jobs:
# Minimum supported rust version (MSRV)
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.75"
# Two most recent releases - we omit older ones for expedient CI
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.78"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.79"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.82"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.83"
container:
image: ${{ matrix.container_image }}
steps:
Expand All @@ -161,8 +161,8 @@ jobs:
# Minimum supported rust version (MSRV)
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.75"
# Two most recent releases - we omit older ones for expedient CI
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.78"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.79"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.82"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.83"
container:
image: ${{ matrix.container_image }}
steps:
Expand All @@ -182,7 +182,7 @@ jobs:
matrix:
container_image:
# Fuzz only on latest
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.79"
- "ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.83"
container:
image: ${{ matrix.container_image }}
steps:
Expand All @@ -195,7 +195,7 @@ jobs:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip ci]')"
container:
image: ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.79
image: ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.83
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand All @@ -206,7 +206,7 @@ jobs:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip ci]')"
container:
image: ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.79
image: ghcr.io/georust/geo-ci:proj-9.4.0-rust-1.83
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand Down
14 changes: 14 additions & 0 deletions geo/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
- <https://github.com/georust/geo/pull/1246>
- Loosen bounds on `RemoveRepeatedPoints` trait (`num_traits::FromPrimitive` isn't required)
- <https://github.com/georust/geo/pull/1278>
- Added: `Validation` trait to check validity of `Geometry`.
- https://github.com/georust/geo/pull/1279
```rust
// use in control flow
if polygon.is_valid() { foo() }

// raise an error if invalid
polygon.check_validation()?;

// get all validation errors
let errors = polygon.validation_errors();
// error implements Display for human readable explanations
println!("{}", errors[0]);
```

## 0.29.3 - 2024.12.03

Expand Down
2 changes: 0 additions & 2 deletions geo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ jts-test-runner = { path = "../jts-test-runner" }
pretty_env_logger = "0.4"
rand = "0.8.0"
rand_distr = "0.4.3"

### boolean-ops test deps
wkt = "0.10.1"

[[bench]]
Expand Down
5 changes: 5 additions & 0 deletions geo/src/algorithm/bool_ops/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ fn test_unary_union_winding() {
assert_eq!(default_winding_union, reversed_winding_union);
}

#[test]
fn jts_overlay_tests() {
jts_test_runner::assert_jts_tests_succeed("*Overlay*.xml");
}

#[test]
fn jts_test_overlay_la_1() {
// From TestOverlayLA.xml test case with description "mLmA - A and B complex, overlapping and touching #1"
Expand Down
3 changes: 3 additions & 0 deletions geo/src/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,6 @@ pub use monotone::{monotone_subdivision, MonoPoly, MonotonicPolygons};
pub mod rhumb;
#[allow(deprecated)]
pub use rhumb::{RhumbBearing, RhumbDestination, RhumbDistance, RhumbIntermediate, RhumbLength};

pub mod validation;
pub use validation::Validation;
16 changes: 12 additions & 4 deletions geo/src/algorithm/relate/geomgraph/edge_end_bundle_star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ impl<F: GeoFloat> LabeledEdgeEndBundleStar<F> {

/// Compute a label for the star based on the labels of its EdgeEndBundles.
fn compute_labeling(&mut self, graph_a: &GeometryGraph<F>, graph_b: &GeometryGraph<F>) {
self.propagate_side_labels(0);
self.propagate_side_labels(1);
self.propagate_side_labels(0, graph_a);
self.propagate_side_labels(1, graph_b);
let mut has_dimensional_collapse_edge = [false, false];
for edge_end in self.edges.iter() {
let label = edge_end.label();
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<F: GeoFloat> LabeledEdgeEndBundleStar<F> {
debug!("edge_end_bundle_star: {:?}", self);
}

fn propagate_side_labels(&mut self, geom_index: usize) {
fn propagate_side_labels(&mut self, geom_index: usize, geometry_graph: &GeometryGraph<F>) {
let mut start_position = None;

for edge_ends in self.edge_end_bundles_iter() {
Expand All @@ -109,7 +109,15 @@ impl<F: GeoFloat> LabeledEdgeEndBundleStar<F> {
let right_position = label.position(geom_index, Direction::Right);

if let Some(right_position) = right_position {
debug_assert!(right_position == current_position, "side_location conflict with coordinate: {:?}, right_location: {:?}, current_location: {:?}", edge_ends.coordinate(), right_position, current_position);
#[cfg(debug_assertions)]
if right_position != current_position {
use crate::algorithm::Validation;
if geometry_graph.geometry().is_valid() {
Copy link
Member Author

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.

debug_assert!(false, "topology position conflict with coordinate — this can happen with invalid geometries. coordinate: {:?}, right_location: {:?}, current_location: {:?}", edge_ends.coordinate(), right_position, current_position);
} else {
warn!("topology position conflict with coordinate — this can happen with invalid geometries. coordinate: {:?}, right_location: {:?}, current_location: {:?}", edge_ends.coordinate(), right_position, current_position);
}
}
assert!(left_position.is_some(), "found single null side");
current_position = left_position.unwrap();
} else {
Expand Down
8 changes: 8 additions & 0 deletions geo/src/algorithm/relate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,11 @@ relate_impl![
GeometryCollection<F>,
Geometry<F>,
];

#[cfg(test)]
mod tests {
#[test]
fn run_jts_relate_tests() {
jts_test_runner::assert_jts_tests_succeed("*Relate*.xml");
}
}
34 changes: 34 additions & 0 deletions geo/src/algorithm/validation/coord.rs
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(())
}
}
146 changes: 146 additions & 0 deletions geo/src/algorithm/validation/geometry.rs
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(())
}
}
Loading
Loading