Expand description
§Geoserde FGB
geoserde-fgb provides a serde-based API for reading and writing the FlatGeobuf format.
§Write
Write geometries and your own serde structs as features. The geometry can be any geo-traits type, such as geo-types.
use std::fs::File;
use std::io::BufWriter;
use serde::Serialize;
#[derive(Serialize)]
struct City {
name: String,
population: i64,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ser = geoserde_fgb::LayerSerializer::new()?;
let prop = City { name: "Tokyo".into(), population: 14_000_000 };
let geom = geo_types::Point::new(139.7, 35.7);
ser.serialize_feature(&geom, &prop)?;
ser.write(BufWriter::new(File::create("cities.fgb")?))?;
Ok(())
}§Read
Read features into geo-types geometries and your own serde structs.
use std::fs::File;
use std::io::BufReader;
use serde::Deserialize;
#[derive(Deserialize)]
struct City {
name: String,
population: i64,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let file = BufReader::new(File::open("cities.fgb")?);
let mut de = geoserde_fgb::LayerDeserializer::new(file)?;
// Pass the geometry type and the properties type to read into
for feature in de.features::<geo_types::Point, City>() {
let (geom, prop) = feature?;
println!("{} {} {:?}", prop.name, prop.population, geom);
}
Ok(())
}§Cargo features
geo-types— Supports geo-types geometries. Enabled by default.
Re-exports§
pub use de::LayerDeserializer;pub use ser::LayerSerializer;pub use flatgeobuf;