-
Notifications
You must be signed in to change notification settings - Fork 0
/
borrowed.rs
43 lines (40 loc) · 1.52 KB
/
borrowed.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
use geojson::{GeoJson, Geometry, Value};
use rayon::prelude::*;
/// Process GeoJSON geometries
fn match_geometry(geom: &Geometry) {
match geom.value {
Value::Polygon(_) => println!("Matched a Polygon"),
Value::MultiPolygon(_) => println!("Matched a MultiPolygon"),
Value::GeometryCollection(ref collection) => {
println!("Matched a GeometryCollection");
// GeometryCollections contain other Geometry types, and can nest
// we deal with this by recursively processing each geometry
collection.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(ref collection) => collection
.features
// Iterate in parallel when appropriate
.par_iter()
// Only pass on non-empty geometries, doing so by reference
.filter_map(|feature| feature.geometry.as_ref())
.for_each(match_geometry),
GeoJson::Feature(ref feature) => {
if let Some(ref geometry) = feature.geometry {
match_geometry(geometry)
}
}
GeoJson::Geometry(ref geometry) => match_geometry(geometry),
}
}
fn main() {
let geojson_str = include!("test.geojson");
let geojson = geojson_str.parse::<GeoJson>().unwrap();
process_geojson(&geojson);
}