georust / wkt

Rust read/write support for well-known text (WKT)
https://crates.io/crates/wkt
Apache License 2.0
51 stars 25 forks source link

deserialize specific geo types #99

Closed michaelkirk closed 2 years ago

michaelkirk commented 2 years ago

The first two commits are just no-ops to move things around. But the final commit is the interesting one. With the exciting bit at https://github.com/georust/wkt/pull/99/files#diff-95bc9d4f4b531a38268ea56c3b4db680f15a6ab9a2bed1b257ad64217f477e5bR61

The new feature is the introduction of wkt::deserialize_wkt

 let json = r#"[
   { "geometry": "POINT (3.14 42)", "name": "bob's house" },
   { "geometry": "LINESTRING (0 0, 1 1, 3.14 42)", "name": "bob's route home" }
 ]"#;

 #[derive(serde::Deserialize)]
 struct MyGeomRecord {
     #[serde(deserialize_with = "wkt::deserialize_wkt")]
     pub geometry: geo_types::Geometry<f64>,
     pub name: String,
}

In the above case, it's pretty much equivalent to using the existing deserialize_geometry method, but this new method also works for any of the types that impl TryFromWkt, so notably you can deserialize directly to the inner Geometry members when know you're expecting a particular geometry.

 let json = r#"{ "geometry": "LINESTRING (0 0, 1 1, 3.14 42)", "name": "bob's route home" }"#;

 #[derive(serde::Deserialize)]
 struct MyGeomRecord {
     #[serde(deserialize_with = "wkt::deserialize_wkt")]
     pub geometry: geo_types::LineString<f64>,
     pub name: String,
}
michaelkirk commented 2 years ago

bors r=urschrei

bors[bot] commented 2 years ago

Build succeeded: