Struct geojson::FeatureReader
source · pub struct FeatureReader<R> { /* private fields */ }
Expand description
Enumerates individual Features from a GeoJSON FeatureCollection
Implementations§
source§impl<R: Read> FeatureReader<R>
impl<R: Read> FeatureReader<R>
sourcepub fn from_reader(reader: R) -> Self
pub fn from_reader(reader: R) -> Self
Create a FeatureReader from the given reader
.
sourcepub fn features(self) -> impl Iterator<Item = Result<Feature>>
pub fn features(self) -> impl Iterator<Item = Result<Feature>>
Iterate over the individual Feature
s of a FeatureCollection.
If instead you’d like to deserialize directly to your own struct, see FeatureReader::deserialize
.
§Examples
let feature_collection_string = r#"{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [125.6, 10.1] },
"properties": {
"name": "Dinagat Islands",
"age": 123
}
},
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [2.3, 4.5] },
"properties": {
"name": "Neverland",
"age": 456
}
}
]
}"#
.as_bytes();
let io_reader = std::io::BufReader::new(feature_collection_string);
use geojson::FeatureReader;
let feature_reader = FeatureReader::from_reader(io_reader);
for feature in feature_reader.features() {
let feature = feature.expect("valid geojson feature");
let name = feature.property("name").unwrap().as_str().unwrap();
let age = feature.property("age").unwrap().as_u64().unwrap();
if name == "Dinagat Islands" {
assert_eq!(123, age);
} else if name == "Neverland" {
assert_eq!(456, age);
} else {
panic!("unexpected name: {}", name);
}
}
sourcepub fn deserialize<D: DeserializeOwned>(
self,
) -> Result<impl Iterator<Item = Result<D>>>
pub fn deserialize<D: DeserializeOwned>( self, ) -> Result<impl Iterator<Item = Result<D>>>
Deserialize the features of FeatureCollection into your own custom
struct using the serde
crate.
§Examples
Your struct must implement or derive serde::Deserialize
.
If you have enabled the geo-types
feature, which is enabled by default, you can
deserialize directly to a useful geometry type.
ⓘ
use geojson::{FeatureReader, de::deserialize_geometry};
#[derive(serde::Deserialize)]
struct MyStruct {
#[serde(deserialize_with = "deserialize_geometry")]
geometry: geo_types::Point<f64>,
name: String,
age: u64,
}
Then you can deserialize the FeatureCollection directly to your type.
let feature_collection_string = r#"{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [125.6, 10.1] },
"properties": {
"name": "Dinagat Islands",
"age": 123
}
},
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [2.3, 4.5] },
"properties": {
"name": "Neverland",
"age": 456
}
}
]
}"#.as_bytes();
let io_reader = std::io::BufReader::new(feature_collection_string);
let feature_reader = FeatureReader::from_reader(io_reader);
for feature in feature_reader.deserialize::<MyStruct>().unwrap() {
let my_struct = feature.expect("valid geojson feature");
if my_struct.name == "Dinagat Islands" {
assert_eq!(123, my_struct.age);
} else if my_struct.name == "Neverland" {
assert_eq!(456, my_struct.age);
} else {
panic!("unexpected name: {}", my_struct.name);
}
}
If you’re not using geo-types
, you can deserialize to a geojson::Geometry
instead.
ⓘ
use serde::Deserialize;
#[derive(Deserialize)]
struct MyStruct {
geometry: geojson::Geometry,
name: String,
age: u64,
}
Auto Trait Implementations§
impl<R> Freeze for FeatureReader<R>where
R: Freeze,
impl<R> RefUnwindSafe for FeatureReader<R>where
R: RefUnwindSafe,
impl<R> Send for FeatureReader<R>where
R: Send,
impl<R> Sync for FeatureReader<R>where
R: Sync,
impl<R> Unpin for FeatureReader<R>where
R: Unpin,
impl<R> UnwindSafe for FeatureReader<R>where
R: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more