serde_geozero

Crates.ioserde_geozero
lib.rsserde_geozero
version0.1.2
created_at2025-01-16 10:11:56.792552+00
updated_at2025-02-20 09:39:59.112743+00
descriptionA Rust library for serializing and deserializing geospatial data using serde and geozero.
homepagehttps://github.com/awmath/serde_geozero
repositoryhttps://github.com/awmath/serde_geozero
max_upload_size
id1519141
size308,894
Axel Wegener (awmath)

documentation

README

serde-geozero

A Rust library for serializing and deserializing geospatial data using serde and geozero.

Overview

serde-geozero provides functionality to convert between geospatial data sources and Rust types using serde's serialization framework and geozero's processing capabilities. It enables seamless integration of various geospatial formats like GeoJSON and FlatGeobuf with Rust's type system.

Features

  • Deserialize from various geospatial formats (GeoJSON, FlatGeobuf, etc.) into Rust structs
  • Serialize Rust structs into geospatial formats
  • Support for geometry and property data
  • Type-safe conversion between geospatial and Rust types

Installation

Add this to your Cargo.toml:

[dependencies]
serde-geozero = "0.1.0"

Usage

Deserializing GeoJSON

use serde::Deserialize;
use geo::Geometry;
use serde_geozero::from_datasource;

#[derive(Deserialize)]
struct City {
    geometry: Geometry,
    name: String,
    population: i64,
}

let geojson = r#"{
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [13.4, 52.5]
    },
    "properties": {
        "name": "Berlin",
        "population": 3669495
    }
}"#;

let mut reader = geozero::geojson::GeoJsonReader(geojson.as_bytes());
let cities: Vec<City> = from_datasource(&mut reader).unwrap();

assert_eq!(cities[0].name, "Berlin");

Serializing to GeoJSON

use geo::point;
use geozero::geojson::GeoJsonWriter;
use serde_geozero::to_geozero_datasource;
use hashbrown::HashMap;
use serde_geozero::de::Feature;

// Create a feature
let feature = Feature::new(
    (point! { x: 123.4, y: 345.6 }).into(),
    HashMap::from_iter(vec![
        ("name".to_string(), serde_json::to_value("Location A").unwrap()),
        ("value".to_string(), serde_json::to_value(42).unwrap()),
    ]),
);

// Serialize to GeoJSON
let mut output = Vec::new();
let mut writer = GeoJsonWriter::new(&mut output);
to_geozero_datasource(&[feature], &mut writer).unwrap();

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Commit count: 15

cargo fmt