Johann Woelper 8 rokov pred
rodič
commit
b4fed9a7b3
3 zmenil súbory, kde vykonal 146 pridanie a 16 odobranie
  1. 72 0
      Cargo.lock
  2. 2 1
      Cargo.toml
  3. 72 15
      src/main.rs

+ 72 - 0
Cargo.lock

@@ -1,14 +1,86 @@
+[[package]]
+name = "chrono"
+version = "0.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
+ "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.42"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "num-integer"
+version = "0.1.39"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "num-traits"
+version = "0.2.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "pothole"
 version = "0.1.0"
 dependencies = [
+ "chrono 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
  "xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "redox_syscall"
+version = "0.1.40"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "time"
+version = "0.1.40"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
+ "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "winapi"
+version = "0.3.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "xml-rs"
 version = "0.8.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [metadata]
+"checksum chrono 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "6962c635d530328acc53ac6a955e83093fedc91c5809dfac1fa60fa470830a37"
+"checksum libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)" = "b685088df2b950fccadf07a7187c8ef846a959c142338a48f9dc0b94517eb5f1"
+"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
+"checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe"
+"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1"
+"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b"
+"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd"
+"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
 "checksum xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "541b12c998c5b56aa2b4e6f18f03664eef9a4fd0a246a55594efae6cc2d964b5"

+ 2 - 1
Cargo.toml

@@ -4,4 +4,5 @@ version = "0.1.0"
 authors = ["Johann Woelper <johann.woelper@king.com>"]
 
 [dependencies]
-xml-rs = "0.8"
+xml-rs = "0.8"
+chrono = "0.4"

+ 72 - 15
src/main.rs

@@ -1,44 +1,102 @@
+extern crate chrono;
 extern crate xml;
 
+use chrono::DateTime;
+use chrono::TimeZone;
+use chrono::Utc;
 use std::fs::File;
 use std::io::BufReader;
+use std::time::Duration;
 
 use xml::reader::{EventReader, XmlEvent};
 
-
-
-
+#[derive(Debug)]
 struct Point {
     lat: f64,
-    long: f64, 
-    ele: f64
+    long: f64,
+    ele: f64,
+    time: chrono::DateTime<Utc>,
 }
 
 struct Tour {
-    points: Vec<Point>
+    points: Vec<Point>,
 }
 
+fn dist(p1: Point, p2: Point) -> f64 {
+    let r = 6371.0;
+    let d_lat = (p1.lat - p2.lat).to_radians();
+    let d_long = (p1.long - p2.long).to_radians();
 
-fn dist(p1: Point, p2: Point) -> f64{
-    let R = 6371;
-    let dLat: f64 = p1.lat - p2.lat;
-    return 1.0
+    let a = (d_lat / 2.0).sin() * (d_lat / 2.0).sin()
+        + p1.lat.to_radians().cos()
+            * p2.lat.to_radians().cos()
+            * (d_long / 2.0).sin()
+            * (d_long / 2.0).sin();
+    let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());
+    return r * c;
 }
 
 fn main() {
+    let mut tours: Vec<Tour>;
+
     let file = File::open("data/Day1-1.gpx").unwrap();
     let file = BufReader::new(file);
 
     let parser = EventReader::new(file);
     let mut depth = 0;
+    let mut current_point = Point {
+        lat: 0.0,
+        long: 0.0,
+        ele: 0.0,
+        time: Utc.timestamp(1, 1),
+    };
+    let mut current_data = "".to_string();
+
     for e in parser {
         match e {
-            Ok(XmlEvent::StartElement { name, attributes, namespace }) => {
-                println!("{:?} {:?}", name, attributes);
+            Ok(XmlEvent::StartElement {
+                name,
+                attributes,
+                namespace,
+            }) => {
+                if name.local_name == "trkpt" {
+                    // println!("ADDING POINT --- --- --- ---", )
+                } else {
+                    println!(">>>{:?}", name.local_name);
+                }
+
+                for attr in attributes {
+                    // println!("A\t{:?}={:?}", attr.name.local_name, attr.value);
+                    if attr.name.local_name == "lat" {
+                        current_point.lat = attr.value.parse().unwrap();
+                    }
+                    if attr.name.local_name == "lon" {
+                        current_point.long = attr.value.parse().unwrap();
+                    }
+                }
                 depth += 1;
             }
             Ok(XmlEvent::Characters(text)) => {
-                    println!("{:?}", text);
+                // println!("{:?}", text);
+                current_data = text;
+            }
+            Ok(XmlEvent::EndElement { name, .. }) => {
+                if name.local_name == "trkpt" {
+                    println!("POINT DONE --- --- --- ---",);
+                    println!("{:?}", current_point);
+                }
+
+                if name.local_name == "ele" {
+                    current_point.ele = current_data.parse().unwrap();
+                }
+
+                if name.local_name == "time" {
+                    match Utc.datetime_from_str(current_data.as_str(), "%Y-%m-%dT%H:%M:%S.3f%z") {
+                        Ok(n) => println!("{:?}", n),
+                        Err(err) => println!("=== TIME ERROR === {:?}", err),
+                        // current_point.time = DateTime::parse_from_rfc3339("current_data").unwrap();
+                    }
+                }
             }
             Err(e) => {
                 println!("Error: {}", e);
@@ -46,6 +104,5 @@ fn main() {
             }
             _ => {}
         }
-
     }
-}
+}