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>

source

pub fn from_reader(reader: R) -> Self

Create a FeatureReader from the given reader.

source

pub fn features(self) -> impl Iterator<Item = Result<Feature>>

Iterate over the individual Features 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);
    }
}
source

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> 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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.