georust/wkt

Better way to serialize to wkt?

michaelkirk opened this issue · 5 comments

I was trying to write a geo-type as WKT to file, and this is what I came up with:

    let points = include!("../src/algorithm/test_fixtures/louisiana.rs");
    let geo_linestring: geo::LineString<f32> = points.into();
    use wkt::ToWkt;
    let wkt: wkt::Wkt<_> = geo::Geometry::from(geo_linestring).to_wkt();
    let wkt_linestring = wkt.items.first().unwrap();
    let serialized = format!("{}", wkt_linestring);
    println!("{}", &serialized);

Is there already better way?

I was hoping for something like:

    let points = include!("../src/algorithm/test_fixtures/louisiana.rs");
    let geo_linestring: geo::LineString<f32> = points.into();
    use wkt::ToWkt;
    let wkt: wkt::Wkt<_> = geo::Geometry::from(geo_linestring).to_wkt();
-   let wkt_linestring = wkt.items.first().unwrap();
-   let serialized = format!("{}", wkt_linestring);
+   let serialized = wkt.to_string()
    println!("{}", &serialized);

Or like serde_jsons::to_writer

    let points = include!("../src/algorithm/test_fixtures/louisiana.rs");
    let geo_linestring: geo::LineString<f32> = points.into();
    use wkt::ToWkt;
    let wkt: wkt::Wkt<_> = geo::Geometry::from(geo_linestring).to_wkt();
-   let wkt_linestring = wkt.items.first().unwrap();
-   let serialized = format!("{}", wkt_linestring);
+   let file = File::create("/tmp/foo").unwrap();
+   let mut writer = BufWriter::new(file);
+   wkt::to_writer(&mut writer, wkt);
    println!("{}", &serialized);

Any thoughts?

Somewhat related: proposal to get rid of items array here: #72

I believe this will be closed by #86

I would get even better with #88

With #89 this becomes:

    let points = include!("../src/algorithm/test_fixtures/louisiana.rs");
    let geo_linestring: geo::LineString<f32> = points.into();
    use wkt::ToWkt;
-   let wkt: wkt::Wkt<_> = geo::Geometry::from(geo_linestring).to_wkt();
-   let wkt_linestring = wkt.items.first().unwrap();
-   let serialized = format!("{}", wkt_linestring);
+   let serialized = geo_linestring.wkt_string();
    println!("{}", &serialized);

edited: Thanks @lnicola

I guess you mean:

-   let wkt: wkt::Wkt<_> = geo::Geometry::from(geo_linestring).to_wkt();
-   let wkt_linestring = wkt.items.first().unwrap();
-   let serialized = format!("{}", wkt_linestring);
+   let serialized = geo_linestring.wkt_string();