Skip to content
Draft
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
209 changes: 209 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ anyhow = "1.0.98"
clap = { version = "4.5.37", features = ["derive"] }
crossbeam-channel = "0.5.15"
flatgeobuf = "4.6.0"
geo = "0.30.0"
geo-types = "0.7.16"
geozero = "0.14.0"
indicatif = "0.17.11"
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub enum Error {
Projection(#[from] proj4rs::errors::Error),
#[error("IO error: {0}")]
FS(#[from] std::io::Error),
#[error("Parsing integer failed: {0}")]
ParseInt(#[from] std::num::ParseIntError),
}

pub type Result<T> = std::result::Result<T, Error>;
28 changes: 28 additions & 0 deletions src/geo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use geo_types::{MultiPolygon, Point};

pub fn point_on_surface(mp: &MultiPolygon<f64>) -> Point<f64> {
use geo::{
Triangle,
algorithm::{Area, Centroid, TriangulateEarcut},
};

// get the biggest polygon
let polygon = mp
.into_iter()
.max_by(|a, b| a.unsigned_area().partial_cmp(&b.unsigned_area()).unwrap())
.expect("MultiPolygon must have at least one Polygon");

// (1) Triangulate into a Vec<Triangle<f64>>
let triangles: Vec<Triangle<f64>> = polygon.earcut_triangles();

// (2) Pick the triangle with the max area
let largest = triangles
.into_iter()
.max_by(|a, b| a.unsigned_area().partial_cmp(&b.unsigned_area()).unwrap())
.expect("polygon must have at least one triangle");

// (3) Its centroid is interior


largest.centroid()
}
Loading