Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better way to serialize to wkt? #55

Closed
michaelkirk opened this issue Jan 29, 2021 · 5 comments · Fixed by #89
Closed

Better way to serialize to wkt? #55

michaelkirk opened this issue Jan 29, 2021 · 5 comments · Fixed by #89

Comments

@michaelkirk
Copy link
Member

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?

@michaelkirk
Copy link
Member Author

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

@callpraths
Copy link

I believe this will be closed by #86

@categulario
Copy link
Contributor

I would get even better with #88

@michaelkirk
Copy link
Member Author

michaelkirk commented Apr 11, 2022

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

@lnicola
Copy link
Member

lnicola commented Apr 11, 2022

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();

bors bot added a commit that referenced this issue Apr 14, 2022
89: Easier serialization of wkt with ToWkt::wkt_string/write_wkt r=urschrei a=michaelkirk

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
- [x] I ran cargo fmt
---
Fixes #55 - adds an easier API to serialize a geo-type.

~~Depends on #86, so merge that first.~~ Merged!

~~partially conflicts with #88, because I'm relying on the `ToWkt` trait.~~ superseded!


Co-authored-by: Michael Kirk <michael.code@endoftheworl.de>
@bors bors bot closed this as completed in #89 Apr 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants