You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Not sure an issue is the best way to ask this, so let me know if it should be somewhere else.
Basically for some context I've been using this library quite a bit and I do want to give something back. I've made small additions to the API but I think the most interesting part is adding some serde deserialization support.
How it works is I can have an object like this
#[derive(Deserialize,Debug)]structStronglyTypedObject{#[serde(rename="FieldNameInOGR")]global_id:String,some_count:u16,some_code:String,// This means the OGR layer has a field named Shape_Area#[serde(rename="Shape_Area")]shape_area:f32,}
And I can deserialize like so
let mut ds = Dataset::open_from_str("/path/to/an/esri_yuck.gdb")?;
//let layer = ds.layer_by_sql("SELECT field1, field2, field4 FROM my_layer_name")?;
let layer = ds.layer_by_name("my_layer_name")?;
let gd = GdalStructDeserializerContext::new::<StronglyTypedObject>(layer.defn());
let my_obj: StronglyTypedObject = gd.to_struct(&layer.features().next().unwrap())?;
The reason for the context is that it will read the fields in the generic object and store the corresponding field indexes in order. It works similiar to bincode.
I also made a 2nd deserializer that can deserialize to Vec<String>, Vec<FieldValue>, HashMap<String, String>, and HashMap<String, FieldValue>. The maps keys are the field name, and the vector indexes are the field indexes.
FYI FieldValue is in this repo that is an enum that acts as a union to store strongly typed data.
So basically the question is -- Is there interest for a PR?
It would work like the other libraries where you can add a feature serde to the package and it will add this support.
I attached the raw code in case you would prefer looking at source. It has not been cleaned up, but should be more or less working. I'm currently working on making sure Option works as well (check Gdal for null fields) as being able to deserialize the FID. Another idea is integrating with the rust geo library https://github.com/georust/geo to be able to deserialize directly into a rust shape object.
I'm not sure I did things the best way. I hardcoded the FieldValue variant indexes for example, which isn't super clean. I'm not sure if this idea of a context was good. And how I kept state in the deserializer. Said another way, design comments welcome !
Anyway, it is still cool to get typed objects directly from Gdal.
Also I used this trick to get the field names of the struct from serde-rs/serde#1110.
An updated version that doesn't use deserialize_any and supports Option + FID + error handling when types don't fit, note this depends on some extra methods in feature
Hello guys,
Not sure an issue is the best way to ask this, so let me know if it should be somewhere else.
Basically for some context I've been using this library quite a bit and I do want to give something back. I've made small additions to the API but I think the most interesting part is adding some serde deserialization support.
How it works is I can have an object like this
And I can deserialize like so
The reason for the context is that it will read the fields in the generic object and store the corresponding field indexes in order. It works similiar to bincode.
I also made a 2nd deserializer that can deserialize to
Vec<String>
,Vec<FieldValue>
,HashMap<String, String>
, andHashMap<String, FieldValue>
. The maps keys are the field name, and the vector indexes are the field indexes.FYI FieldValue is in this repo that is an enum that acts as a union to store strongly typed data.
So basically the question is -- Is there interest for a PR?
It would work like the other libraries where you can add a feature serde to the package and it will add this support.
I attached the raw code in case you would prefer looking at source. It has not been cleaned up, but should be more or less working. I'm currently working on making sure Option works as well (check Gdal for null fields) as being able to deserialize the FID. Another idea is integrating with the rust geo library https://github.com/georust/geo to be able to deserialize directly into a rust shape object.
I'm not sure I did things the best way. I hardcoded the FieldValue variant indexes for example, which isn't super clean. I'm not sure if this idea of a context was good. And how I kept state in the deserializer. Said another way, design comments welcome !
Anyway, it is still cool to get typed objects directly from Gdal.
Also I used this trick to get the field names of the struct from serde-rs/serde#1110.
gdal_serde.zip
The text was updated successfully, but these errors were encountered: