Better way to serialize to wkt?
michaelkirk opened this issue · 5 comments
michaelkirk commented
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 commented
Somewhat related: proposal to get rid of items array here: #72
callpraths commented
I believe this will be closed by #86
categulario commented
I would get even better with #88
michaelkirk commented
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 commented
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();