-
Notifications
You must be signed in to change notification settings - Fork 0
/
owned.rs
54 lines (51 loc) · 1.85 KB
/
owned.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use geo::algorithm::centroid::Centroid;
use geo_types::Polygon;
use geojson::{GeoJson, Geometry, Value};
use rayon::prelude::*;
use std::convert::TryInto;
/// Process GeoJSON geometries
fn match_geometry(geom: Geometry) {
match geom.value {
Value::Polygon(_) => {
let poly: Polygon<f64> = geom.value.try_into().expect("Unable to convert Polygon");
let centroid = poly.centroid().unwrap();
println!(
"Matched a Polygon with centroid ({}, {})",
centroid.x(),
centroid.y()
);
}
Value::MultiPolygon(_) => println!("Matched a MultiPolygon"),
Value::GeometryCollection(collection) => {
println!("Matched a GeometryCollection");
// GeometryCollections contain other Geometry types, and can nest
// we deal with this by recursively processing each geometry
collection.into_par_iter().for_each(match_geometry)
}
// Point, LineString, and their Multi– counterparts
_ => println!("Matched some other geometry"),
}
}
/// Process top-level GeoJSON items
fn process_geojson(gj: GeoJson) {
match gj {
GeoJson::FeatureCollection(collection) => collection
.features
// Iterate in parallel where appropriate
.into_par_iter()
// Only pass on non-empty geometries
.filter_map(|feature| feature.geometry)
.for_each(match_geometry),
GeoJson::Feature(feature) => {
if let Some(geometry) = feature.geometry {
match_geometry(geometry)
}
}
GeoJson::Geometry(geometry) => match_geometry(geometry),
}
}
fn main() {
let geojson_str = include!("test.geojson");
let geojson = geojson_str.parse::<GeoJson>().unwrap();
process_geojson(geojson);
}