georust / geos

Rust bindings for GEOS
https://docs.rs/geos/
MIT License
122 stars 42 forks source link

Could not convert to geojson::Geometry #81

Closed JorgeMartinezG closed 3 years ago

JorgeMartinezG commented 3 years ago

First of all, thanks for this library.

I am experimenting an issue converting from geos::Geometry to geojson::Geometry type. It looks like the readme has not been updated since this commit https://github.com/georust/geos/commit/1614a49deedc01b2f0cd4de24852105ba9cabe2f makes use of the std TryFrom trait.

I have this sample code:

    let pt = "POINT(1.0 1.0)";
    let pt = geos::Geometry::new_from_wkt(pt).unwrap();

    let geom: geojson::Geometry = pt.try_into().unwrap();

and the compiler shows this error:

error[E0277]: the trait bound `Value: From<geos::Geometry<'_>>` is not satisfied
  --> src/main.rs:79:38
   |
79 |     let geom: geojson::Geometry = pt.try_into().unwrap();
   |                                      ^^^^^^^^ the trait `From<geos::Geometry<'_>>` is not implemented for `Value`
   |
   = note: required because of the requirements on the impl of `Into<Value>` for `geos::Geometry<'_>`
   = note: required because of the requirements on the impl of `From<geos::Geometry<'_>>` for `geojson::Geometry`
   = note: required because of the requirements on the impl of `Into<geojson::Geometry>` for `geos::Geometry<'_>`
   = note: required because of the requirements on the impl of `TryFrom<geos::Geometry<'_>>` for `geojson::Geometry`
   = note: required because of the requirements on the impl of `TryInto<geojson::Geometry>` for `geos::Geometry<'_>`

I have the geos library with json feature enabled.

[dependencies]
geos = {version = "7.0.0", features = ["json"]}
geojson= {version = "0.20.1"}

Curiously, the code compiles by first doing a conversion to geo_types and then to geojson

    let geotypes_obj: geo_types::Geometry<f64> = pt.try_into().unwrap();
    let geometry = geojson::Geometry::new(geojson::Value::from(&geotypes_obj));

I found here https://github.com/georust/geos/blob/0a4862d722b593e7dbb60311e8a58e0e724ae13a/src/enums.rs#L5 that the crate has its own TryFrom trait, but I am not sure if it is related.

It looks like the geojson::Geometry type has not implemented the TryFrom crate within to_geojson.rs file.

Let me know your opinion

frewsxcv commented 3 years ago

Thanks for the bug report! This is likely because of mismatched versions between dependencies. Can you cargo-tree to ensure all your geo-types and geojson dependency versions match?

JorgeMartinezG commented 3 years ago

@frewsxcv Thank you! Running cargo tree saw a mismatch between geojson (0.20.1) and the geojson crate used in geos (0.19.0). changing the version in Cargo.toml file worked perfectly

frewsxcv commented 3 years ago

Glad that worked! I created a new issue for tracking the version mismatch: https://github.com/georust/geos/issues/82

JorgeMartinezG commented 3 years ago

For new users coming into this issue, please check https://github.com/rust-lang/rust/issues/22750