Skip to content

Commit

Permalink
add panic_safe_union to attempt to work around panics coming from g…
Browse files Browse the repository at this point in the history
…eo library

- panix possibly related to georust/geo#1104, georust/geo#1174 or similar
  • Loading branch information
mikemoraned committed Aug 5, 2024
1 parent fe67e61 commit 9ce022a
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions app/service/api/src/union.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashSet;

use geo::{BooleanOps, Geometry, Intersects, MultiPolygon, Polygon};
use tracing::debug;
use tracing::{debug, warn};

#[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
struct PolygonId(usize);
Expand Down Expand Up @@ -112,7 +112,7 @@ pub fn union(
let unioned = multi
.iter()
.skip(1)
.fold(multi[0].clone(), |acc, p| acc.union(p));
.fold(multi[0].clone(), |acc, p| panic_safe_union(acc, &p));

unioned_polygons.append(unioned.into_iter().collect::<Vec<Polygon<f64>>>().as_mut());
}
Expand All @@ -126,6 +126,20 @@ pub fn union(
Ok(unioned)
}

fn panic_safe_union(lhs: MultiPolygon, rhs: &MultiPolygon) -> MultiPolygon {
use std::panic;

let result = panic::catch_unwind(|| lhs.union(rhs));

match result {
Ok(unioned) => unioned,
Err(_) => {
warn!("Panic detected in union, falling back to lhs of attempted union");
lhs
}
}
}

#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
Expand Down

0 comments on commit 9ce022a

Please sign in to comment.