Skip to content

Commit

Permalink
Merge #205
Browse files Browse the repository at this point in the history
205: Add FeatureWriter to stream writes r=urschrei a=michaelkirk

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---

This allows the user to incrementally serialize a FeatureCollection, rather than requiring them to do it one-shot. This is a pretty logical counterpart to the FeatureReader introduced in #199.

Co-authored-by: Michael Kirk <michael.code@endoftheworl.de>
  • Loading branch information
bors[bot] and michaelkirk authored Sep 2, 2022
2 parents cdf97c3 + 74c5efc commit 0d92bd2
Show file tree
Hide file tree
Showing 6 changed files with 497 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
geojson::ser::to_feature_collection_string(&my_structs).unwrap();
```
* PR: <https://github.com/georust/geojson/pull/199>
* Added `geojson::{FeatureReader, FeatureWriter}` to stream the reading/writing of your custom struct to and from GeoJSON.
* PR: <https://github.com/georust/geojson/pull/199>
* PR: <https://github.com/georust/geojson/pull/205>
* Added IntoIter implementation for FeatureCollection.
* <https://github.com/georust/geojson/pull/196>
* Added `geojson::Result<T>`.
Expand Down
45 changes: 45 additions & 0 deletions examples/stream_reader_writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use geojson::{de::deserialize_geometry, ser::serialize_geometry, FeatureReader, FeatureWriter};

use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fs::File;
use std::io::{BufReader, BufWriter};

#[cfg(not(feature = "geo-types"))]
fn main() -> Result<(), Box<dyn Error>> {
panic!("this example requires geo-types")
}

#[cfg(feature = "geo-types")]
fn main() -> Result<(), Box<dyn Error>> {
#[derive(Serialize, Deserialize)]
struct Country {
#[serde(
serialize_with = "serialize_geometry",
deserialize_with = "deserialize_geometry"
)]
geometry: geo_types::Geometry,
name: String,
}

let reader = {
let file_reader = BufReader::new(File::open("tests/fixtures/countries.geojson")?);
FeatureReader::from_reader(file_reader)
};

let mut writer = {
let file_writer = BufWriter::new(File::create("example-output-contries.geojson")?);
FeatureWriter::from_writer(file_writer)
};

let mut country_count = 0;
for country in reader.deserialize::<Country>()? {
let country = country?;
country_count += 1;

writer.serialize(&country)?;
}

assert_eq!(country_count, 180);
Ok(())
}
5 changes: 3 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ pub(crate) mod tests {

use crate::JsonValue;

use serde::Deserialize;
use serde_json::json;

pub(crate) fn feature_collection() -> JsonValue {
Expand Down Expand Up @@ -491,6 +490,8 @@ pub(crate) mod tests {
mod geo_types_tests {
use super::*;

use serde::Deserialize;

#[test]
fn geometry_field() {
#[derive(Deserialize)]
Expand Down Expand Up @@ -592,7 +593,7 @@ pub(crate) mod tests {
#[test]
fn roundtrip() {
use crate::ser::serialize_geometry;
use serde::Serialize;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct MyStruct {
Expand Down
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum Error {
/// This was previously `GeoJsonUnknownType`, but has been split for clarity
#[error("Expected a Feature, FeatureCollection, or Geometry, but got an empty type")]
EmptyType,
#[error("invalid writer state: {0}")]
InvalidWriterState(&'static str),
#[error("IO Error: {0}")]
Io(std::io::Error),
/// This was previously `GeoJsonUnknownType`, but has been split for clarity
Expand Down
Loading

0 comments on commit 0d92bd2

Please sign in to comment.