|
|
@@ -1,14 +1,16 @@
|
|
|
extern crate chrono;
|
|
|
extern crate quick_xml;
|
|
|
|
|
|
-use chrono::DateTime;
|
|
|
-use chrono::Duration;
|
|
|
-use chrono::TimeZone;
|
|
|
-use chrono::Utc;
|
|
|
use std::time::Duration as stdDuration;
|
|
|
use std::io::Cursor;
|
|
|
use std::str;
|
|
|
-
|
|
|
+use std::io::prelude::*;
|
|
|
+use std::fs::File;
|
|
|
+use std::path::Path;
|
|
|
+// use chrono::DateTime;
|
|
|
+use chrono::Duration;
|
|
|
+use chrono::TimeZone;
|
|
|
+use chrono::Utc;
|
|
|
use quick_xml::events::{Event, BytesEnd, BytesStart};
|
|
|
use quick_xml::Reader;
|
|
|
use quick_xml::Writer;
|
|
|
@@ -55,15 +57,40 @@ impl Track {
|
|
|
|
|
|
fn to_xml(&self) {
|
|
|
|
|
|
+
|
|
|
//gpx > trk > trkseg > trkpt
|
|
|
- let mut writer = Writer::new(Cursor::new(Vec::new()));
|
|
|
- let mut elem = BytesStart::owned(b"trkpt".to_vec(), "trkpt".len());
|
|
|
- elem.push_attribute(("lat", "13"));
|
|
|
- assert!(writer.write_event(Event::Start(elem)).is_ok());
|
|
|
- assert!(writer.write_event(Event::End(BytesEnd::borrowed(b"trkpt"))).is_ok());
|
|
|
+ // let mut writer = Writer::new(Cursor::new(Vec::new()));
|
|
|
+ // let indent_char: u8 = " ";
|
|
|
+ let mut writer = Writer::new_with_indent(Cursor::new(Vec::new()), 32, 4);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ let mut gpx_elem = BytesStart::owned(b"gpx".to_vec(), "gpx".len());
|
|
|
+ gpx_elem.push_attribute(("creator", "Pothole"));
|
|
|
+ assert!(writer.write_event(Event::Start(gpx_elem)).is_ok());
|
|
|
+
|
|
|
+ let trk_elem = BytesStart::owned(b"trk".to_vec(), "trk".len());
|
|
|
+ assert!(writer.write_event(Event::Start(trk_elem)).is_ok());
|
|
|
+
|
|
|
+
|
|
|
+ for pt in &self.points {
|
|
|
+ let mut pt_elem = BytesStart::owned(b"trkpt".to_vec(), "trkpt".len());
|
|
|
+ pt_elem.push_attribute(("lat", pt.lat.to_string().as_str()));
|
|
|
+ pt_elem.push_attribute(("lon", pt.long.to_string().as_str()));
|
|
|
+ assert!(writer.write_event(Event::Start(pt_elem)).is_ok());
|
|
|
+ assert!(writer.write_event(Event::End(BytesEnd::borrowed(b"trkpt"))).is_ok());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ assert!(writer.write_event(Event::End(BytesEnd::borrowed(b"trk"))).is_ok());
|
|
|
+ assert!(writer.write_event(Event::End(BytesEnd::borrowed(b"gpx"))).is_ok());
|
|
|
+
|
|
|
|
|
|
let result = writer.into_inner().into_inner();
|
|
|
println!("{:?}", str::from_utf8( &result).unwrap());
|
|
|
+
|
|
|
+ write_bytes(result, "track.gpx".to_string());
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -135,6 +162,34 @@ fn dist(p1: &Point, p2: &Point) -> f64 {
|
|
|
return r * c;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+fn write_bytes(bytes_to_write: Vec<u8>, filename: String) {
|
|
|
+
|
|
|
+ let path = Path::new(filename.as_str());
|
|
|
+ let display = path.display();
|
|
|
+
|
|
|
+ // Open a file in write-only mode, returns `io::Result<File>`
|
|
|
+ let mut file = match File::create(&path) {
|
|
|
+ Err(_why) => panic!("couldn't create {}", display),
|
|
|
+ Ok(file) => file,
|
|
|
+ };
|
|
|
+
|
|
|
+ // Write the `LOREM_IPSUM` string to `file`, returns `io::Result<()>`
|
|
|
+ match file.write_all("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".as_bytes()) {
|
|
|
+ Err(why) => {
|
|
|
+ panic!("couldn't write to {}", display)
|
|
|
+ },
|
|
|
+ Ok(_) => println!("successfully wrote to {}", display),
|
|
|
+ }
|
|
|
+
|
|
|
+ match file.write_all(&bytes_to_write) {
|
|
|
+ Err(why) => {
|
|
|
+ panic!("couldn't write to {}", display)
|
|
|
+ },
|
|
|
+ Ok(_) => println!("successfully wrote to {}", display),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
fn main() {
|
|
|
|
|
|
let mut trk = Track {
|