Struct geojson::FeatureWriter

source ·
pub struct FeatureWriter<W: Write> { /* private fields */ }
Expand description

Write Features to a FeatureCollection

Implementations§

source§

impl<W: Write> FeatureWriter<W>

source

pub fn from_writer(writer: W) -> Self

Create a FeatureWriter from the given writer.

To append features from your custom structs, use FeatureWriter::serialize.

To append features from Feature use FeatureWriter::write_feature.

source

pub fn write_feature(&mut self, feature: &Feature) -> Result<()>

Write a crate::Feature struct to the output stream. If you’d like to serialize your own custom structs, see FeatureWriter::serialize instead.

source

pub fn serialize<S: Serialize>(&mut self, value: &S) -> Result<()>

Serialize your own custom struct to the features of a FeatureCollection using the serde crate.

Examples

Your struct must implement or derive serde::Serialize.

If you have enabled the geo-types feature, which is enabled by default, you can serialize directly from a useful geometry type.

use geojson::{FeatureWriter, ser::serialize_geometry};

#[derive(serde::Serialize)]
struct MyStruct {
    #[serde(serialize_with = "serialize_geometry")]
    geometry: geo_types::Point<f64>,
    name: String,
    age: u64,
}

Then you can serialize the FeatureCollection directly from your type.


let dinagat = MyStruct {
    geometry: geo_types::point!(x: 125.6, y: 10.1),
    name: "Dinagat Islands".to_string(),
    age: 123
};

let neverland = MyStruct {
    geometry: geo_types::point!(x: 2.3, y: 4.5),
    name: "Neverland".to_string(),
    age: 456
};

let mut output: Vec<u8> = vec![];
{
    let io_writer = std::io::BufWriter::new(&mut output);
    let mut feature_writer = FeatureWriter::from_writer(io_writer);
    feature_writer.serialize(&dinagat).unwrap();
    feature_writer.serialize(&neverland).unwrap();
}

let expected_output = 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();

fn assert_eq_json(bytes_1: &[u8], bytes_2: &[u8]) {
    // check for semantic equality, discarding any formatting/whitespace differences
    let json_1: serde_json::Value = serde_json::from_slice(bytes_1).unwrap();
    let json_2: serde_json::Value = serde_json::from_slice(bytes_2).unwrap();
    assert_eq!(json_1, json_2);
}

assert_eq_json(expected_output, &output);

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,
}
source

pub fn finish(&mut self) -> Result<()>

Writes the closing syntax for the FeatureCollection.

You shouldn’t normally need to call this manually, as the writer will close itself upon being dropped.

source

pub fn flush(&mut self) -> Result<()>

Flush the underlying writer buffer.

You shouldn’t normally need to call this manually, as the writer will flush itself upon being dropped.

Trait Implementations§

source§

impl<W: Write> Drop for FeatureWriter<W>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<W> RefUnwindSafe for FeatureWriter<W>where W: RefUnwindSafe,

§

impl<W> Send for FeatureWriter<W>where W: Send,

§

impl<W> Sync for FeatureWriter<W>where W: Sync,

§

impl<W> Unpin for FeatureWriter<W>where W: Unpin,

§

impl<W> UnwindSafe for FeatureWriter<W>where W: 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.