-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
497 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.