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

update geo and i_overlay #80

Merged
merged 2 commits into from
Jan 14, 2025
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
33 changes: 16 additions & 17 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ contour = "0.12.0"
fast_paths = "1.0.0"
geo = "0.29.1"
geojson = { git = "https://github.com/georust/geojson", features = ["geo-types"] }
i_overlay = { version = "1.7.4", default-features = false }
# Note: i_overlay doesn't follow semver, and only guarantees non-breaking across patch versions.
# https://github.com/iShape-Rust/iOverlay?tab=readme-ov-file#versioning-policy
# So we pin to a minor version.
i_overlay = { version = ">=1.9.4,<1.10.0", default-features = false }
i_float = "1.3.1"
log = "0.4.20"
osm-reader = { git = "https://github.com/a-b-street/osm-reader" }
Expand All @@ -33,3 +36,9 @@ web-time = "1.0.0"
# use dev profile and avoid wasm-opt.
[profile.dev.package."*"]
opt-level = 3

[patch.crates-io]
# use unreleased `geo` and `geo-types` to fix crash: https://github.com/a-b-street/ltn/issues/54
# (awaiting release of https://github.com/georust/geo/pull/1279)
geo = { version = "0.29.1", git = "https://github.com/georust/geo", branch = "release/geo-types-v0.7.15" }
geo-types = { version = "0.7.15", git = "https://github.com/georust/geo", branch = "release/geo-types-v0.7.15" }
35 changes: 13 additions & 22 deletions backend/src/auto_boundaries.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use geo::{Area, Coord, Intersects, LineString, Polygon};
use geojson::FeatureCollection;
use i_float::f64_point::F64Point;
use i_overlay::core::fill_rule::FillRule;
use i_overlay::f64::string::F64StringOverlay;
use i_overlay::string::rule::StringRule;
use i_overlay::float::slice::FloatSlice;

use crate::MapModel;

Expand Down Expand Up @@ -80,42 +78,35 @@ impl MapModel {
}

// TODO Revisit some of this; conversions are now in geo

fn split_polygon(polygon: Polygon, linestrings: Vec<LineString>) -> Vec<Polygon> {
let mut overlay = F64StringOverlay::new();
overlay.add_shape_path(polygon.exterior().coords().map(to_pt).collect());
for ls in linestrings {
overlay.add_string_lines(
ls.lines()
.map(|l| [to_pt(&l.start), to_pt(&l.end)])
.collect(),
);
}
let mut shape = to_i_overlay_contour(polygon.exterior());

let graph = overlay.into_graph(FillRule::NonZero);
let shapes = graph.extract_shapes(StringRule::Slice);
// geo Polygon's are explicitly closed LineStrings, but i_overlay Polygon's are not.
shape.pop();
Copy link
Contributor Author

@michaelkirk michaelkirk Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nail (i_overlay author) told me once (and it's documented) that the i_overlay polygons are implicitly closed, but I got the impression that explicitly closed ones were still acceptable. However with the update to 1.9 in this lib, I was getting bad output when sending an explicitly closed polygon - hence this pop() to remove the redundant first/last coordinate.

i_overlay 1.9 without this pop() line notice almost half of the study area isn't colored:

Screenshot 2025-01-13 at 18 48 12

i_overlay 1.9 with this pop() line:

Screenshot 2025-01-13 at 18 04 48

I opted not to dig further into this, but FYI.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, sounds good.

FYI I was thinking of upstreaming something like this method to geo when there's spare time, as I'm already using this in a second project. I guess similar to the questions about exposing all of the power of spade triangulation in geo, as iOverlay gets more functionality, it'll be interesting to figure out how much effort it is to also expose nicely through geo.


let splitters: Vec<_> = linestrings.iter().map(to_i_overlay_contour).collect();
let shapes = shape.slice_by(&splitters, FillRule::NonZero);
shapes.into_iter().map(to_geo_polygon).collect()
}

fn to_pt(pt: &Coord) -> F64Point {
F64Point::new(pt.x, pt.y)
}

fn to_geo_polygon(rings: Vec<Vec<F64Point>>) -> Polygon {
fn to_geo_polygon(rings: Vec<Vec<[f64; 2]>>) -> Polygon {
let mut interiors: Vec<LineString> = rings.into_iter().map(to_geo_linestring).collect();
let exterior = interiors.remove(0);
Polygon::new(exterior, interiors)
}

fn to_geo_linestring(pts: Vec<F64Point>) -> LineString {
fn to_geo_linestring(pts: Vec<[f64; 2]>) -> LineString {
LineString(
pts.into_iter()
.map(|pt| Coord { x: pt.x, y: pt.y })
.map(|pt| Coord { x: pt[0], y: pt[1] })
.collect(),
)
}

fn to_i_overlay_contour(line_string: &LineString) -> Vec<[f64; 2]> {
line_string.coords().map(|c| [c.x, c.y]).collect()
}

fn boundary_touches_any(polygon: &Polygon, linestrings: &Vec<LineString>) -> bool {
// TODO At least consider an rtree to prune!
linestrings
Expand Down