Rust Geo Crate: Convert between wkt::Geometry and geo:Geometry

Solution 1:

I'm in a similar position, so I can't claim to be an expert, but I've found two solutions to this:

  1. Using https://crates.io/crates/wkt
use geo_types::{LineString, Polygon};
use wkt::ToWkt;

let polygon = Polygon::new(
LineString::from(vec![
    (0., 0.),
    (0., 1.),
    (1., 1.),
    (1., 0.),
]),
vec![],);

let geom: Geometry<f64> = polygon.into();
let wkt = geom.to_wkt().items.pop().unwrap();

println!("{}", wkt)

// POLYGON((0 0,0 1,1 1,1 0,0 0))
  1. Using https://github.com/georust/geozero
use geo_types::{LineString, Polygon};
use geozero::ToWkt;

let polygon = Polygon::new(
LineString::from(vec![
    (0., 0.),
    (0., 1.),
    (1., 1.),
    (1., 0.),
]),
vec![],);

let geom: Geometry<f64> = polygon.into();
let wkt = geom.to_wkt().unwrap();

println!("{}", wkt)

// POLYGON((0 0,0 1,1 1,1 0,0 0))