To convert 'geo::MultiPolygon' to 'geos'

I want to convert 'geo::MultiPolygon' to 'geos' . compiler says.

error[E0277]: the trait bound `geos::Geometry<'_>: From<&geo::MultiPolygon>` is not satisfied
  --> src/main.rs:47:49
   |
47 |             let geom: geos::Geometry = (&mpoly).try_into::<geos::Geometry<'_>>().unwrap();
   |                                                 ^^^^^^^^ the trait `From<&geo::MultiPolygon>` is not implemented for `geos::Geometry<'_>`
   |
   = note: required for `&geo::MultiPolygon` to implement `Into<geos::Geometry<'_>>`
   = note: required for `geos::Geometry<'_>` to implement `TryFrom<&geo::MultiPolygon>`
   = note: required for `&geo::MultiPolygon` to implement `TryInto<geos::Geometry<'_>>`


here is my code

use std::fs::File;
use std::io::{self, BufReader};
use flatgeobuf::*;
use flatgeobuf::geozero::*;
use geo::{ Geometry};

use std::convert::{TryFrom, TryInto};
use geo::{MultiPolygon, Polygon, LineString};
use geos::{Geometry as OtherGeometry, WKTWriter};

// Rename Result to avoid conflict with flatgeobuf's Result
fn main() -> io::Result<()> {
    // Open the file
    let file_result = File::open("countries_3857.fgb");

    // Handle the Result to get the File
    let file = match file_result {
        Ok(f) => f,
        Err(e) => {
            eprintln!("Error opening file: {}", e);
            return Err(e.into());
        }
    };

    // Create a BufReader
    let mut filein = BufReader::new(file);

    // Open the FgbReader
    let fgb_result = FgbReader::open(&mut filein);

    // Handle the Result to get the FgbReader
    let fgb = match fgb_result {
        Ok(reader) => reader,
        Err(e) => {
            eprintln!("Error creating FgbReader: {}", e);
            return Err(io::Error::new(io::ErrorKind::Other, e));
        }
    };
    let mut feature_iter = fgb.select_all().unwrap();

    while let Some(feature) = feature_iter.next().unwrap() {
        let name = feature.property::<String>("name") ;
        println!("type: {:?}", feature.geometry().unwrap().type_());
        assert_eq!(feature.geometry().unwrap().type_(), GeometryType::MultiPolygon);
        println!("I am printing a feature  {:?} ",name);
        if let Ok(Geometry::MultiPolygon(mpoly)) = feature.to_geo::< '_ >() {
            let geom: geos::Geometry = (&mpoly).try_into::<geos::Geometry<'_>>().unwrap();
        } 

}
 
    Ok(())
}

Could you try this, please?

if let Ok(mpoly) = feature.to_geo::<'_>() {
    let geom: geos::Geometry = mpoly.try_into::<geos::Geometry<'_>>().unwrap();
} 

Thank you ,
I tried but compiler complained as :

error[E0277]: the trait bound `geos::Geometry<'_>: From<geo::Geometry>` is not satisfied
  --> src/main.rs:44:46
   |
44 |             let geom: geos::Geometry = mpoly.try_into::<geos::Geometry<'_>>().unwrap();
   |                                              ^^^^^^^^ the trait `From<geo::Geometry>` is not implemented for `geos::Geometry<'_>`
   |
   = note: required for `geo::Geometry` to implement `Into<geos::Geometry<'_>>`
   = note: required for `geos::Geometry<'_>` to implement `TryFrom<geo::Geometry>`
   = note: required for `geo::Geometry` to implement `TryInto<geos::Geometry<'_>>`

Huh, okay that went in the wrong direction. geos::Geometry does implement TryFrom<&geo_types::MultiPolygon<f64>> though. This maybe?

if let Ok(Geometry::MultiPolygon(mpoly)) = feature.to_geo::< '_ >() {
    let geom = geos::Geometry::try_from(&mpoly).unwrap();
} 

Thank you,

But there is an error :

error[E0277]: the trait bound `geos::Geometry<'_>: From<&geo::MultiPolygon>` is not satisfied
  --> src/main.rs:44:24
   |
44 |             let geom = geos::Geometry::try_from(&mpoly).unwrap();
   |                        ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<&geo::MultiPolygon>` is not implemented for `geos::Geometry<'_>`
   |
   = note: required for `&geo::MultiPolygon` to implement `Into<geos::Geometry<'_>>`
   = note: required for `geos::Geometry<'_>` to implement `TryFrom<&geo::MultiPolygon>`

What are the versions of the geo and geos crates you are using?

Thank you,

[dependencies]
flatgeobuf = "4.0.0"
geo = "0.27.0"
geos = "8.3.0"
geozero = "0.11.0"

Ok no idea why try_into is not working in your case. I'd raise an issue in the geos repo, providing them your snippet and your dependencies.

1 Like

Thank you for your interest.

My main goal is to perform "buffer" on vectors particularly LineStrings. I think "geo" cannot do that. Maybe, Gdal also can .
Or do you have any insight on buffering on LineString.

kind regards

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.