georust / geojson

Library for serializing the GeoJSON vector GIS file format
https://crates.io/crates/geojson
Apache License 2.0
280 stars 60 forks source link

feature flag to interop with `rust::geo` #239

Closed kraktus closed 7 months ago

kraktus commented 7 months ago

Hello, thanks for the great crate.

A nice-to-have feature-flag would be to allow directly converting geojson::PolygonType to geo::Polygon. I don't know in which create it should live. I assume it's reasonably frequent to use those two together

for now doing that:

fn vec_to_line(vec: &Vec<Vec<f64>>) -> LineString<f64> {
    vec.iter()
        .map(|coord| (coord[0], coord[1]))
        .collect::<Vec<_>>()
        .into()
}

fn polygon_to_polygon(polygon: &PolygonType) -> geo::Polygon {
    let mut copy_vec = polygon.clone();
    let holes = copy_vec.split_off(1);
    let line = vec_to_line(&copy_vec[0]);

    geo::Polygon::new(line, holes.iter().map(vec_to_line).collect())
}
lnicola commented 7 months ago

https://docs.rs/geojson/latest/geojson/#use-geojson-with-other-crates-by-converting-to-geo-types

kraktus commented 7 months ago

Thanks 👍