Function geojson::de::deserialize_feature_collection
source · pub fn deserialize_feature_collection<'de, T>(
feature_collection_reader: impl Read,
) -> Result<impl Iterator<Item = Result<T>>>where
T: Deserialize<'de>,
Expand description
Deserialize a GeoJSON FeatureCollection into your custom structs.
Your struct must implement or derive serde::Deserialize
.
You must use the deserialize_geometry
helper if you are using geo_types or some other geometry
representation other than geojson::Geometry.
§Examples
use serde::Deserialize;
use geojson::de::deserialize_geometry;
#[derive(Deserialize)]
struct MyStruct {
// You must use the `deserialize_geometry` helper if you are using geo_types or some other
// geometry representation other than geojson::Geometry
#[serde(deserialize_with = "deserialize_geometry")]
geometry: geo_types::Point<f64>,
name: String,
}
let feature_collection_str = r#"{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [11.1, 22.2] },
"properties": { "name": "Downtown" }
},
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [33.3, 44.4] },
"properties": { "name": "Uptown" }
}
]
}"#;
let reader = feature_collection_str.as_bytes();
// enumerate over the features in the feature collection
for (idx, feature_result) in geojson::de::deserialize_feature_collection::<MyStruct>(reader).unwrap().enumerate() {
let my_struct = feature_result.expect("valid geojson for MyStruct");
if idx == 0 {
assert_eq!(my_struct.name, "Downtown");
assert_eq!(my_struct.geometry.x(), 11.1);
} else if idx == 1 {
assert_eq!(my_struct.name, "Uptown");
assert_eq!(my_struct.geometry.x(), 33.3);
} else {
unreachable!("there are only two features in this collection");
}
}