1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#![warn(missing_docs)]
use chrono::NaiveDateTime;
use serde_derive::{Deserialize, Serialize};
pub type Locations = Vec<Location>;
pub trait LocationsExt {
fn average_time(&self) -> i64;
fn find_closest(&self, time: NaiveDateTime) -> Option<Location>;
fn filter_outliers(self) -> Locations;
}
impl LocationsExt for Locations {
fn average_time(&self) -> i64 {
let mut time = 0;
for i in 1..self.len() {
time += self[i - 1].timestamp.timestamp() - self[i].timestamp.timestamp()
}
time / (self.len() as i64)
}
fn find_closest(&self, time: NaiveDateTime) -> Option<Location> {
let result = self.binary_search_by(|x| x.timestamp.cmp(&time));
let index = match result {
Ok(x) => Some(x),
Err(x) => {
if x > 0 && x < self.len() {
Some(x)
} else {
None
}
}
};
if let Some(x) = index {
if x < self.len() {
return Some(self[x]);
}
}
None
}
fn filter_outliers(self) -> Locations {
let mut tmp: Vec<Location> = vec![self[0]];
for location in self.into_iter() {
if location.speed_kmh(&tmp[tmp.len() - 1]) < 300.0 {
tmp.push(location);
}
}
tmp
}
}
pub fn deserialize(from: &str) -> Locations {
#[derive(Deserialize)]
struct LocationList {
locations: Vec<Location>,
}
let mut deserialized: LocationList = serde_json::from_str(from).expect("Failed to deserialize");
deserialized
.locations
.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));
deserialized.locations
}
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
pub struct Location {
#[serde(rename = "timestampMs", deserialize_with = "parse_date")]
pub timestamp: NaiveDateTime,
#[serde(rename = "latitudeE7", deserialize_with = "parse_location")]
pub latitude: f32,
#[serde(rename = "longitudeE7", deserialize_with = "parse_location")]
pub longitude: f32,
pub accuracy: i32,
pub altitude: Option<i32>,
}
impl Location {
pub fn haversine_distance(&self, other: &Location) -> f32 {
let long1 = self.longitude.to_radians();
let long2 = other.longitude.to_radians();
let lat1 = self.latitude.to_radians();
let lat2 = other.latitude.to_radians();
let dlon = long2 - long1;
let dlat = lat2 - lat1;
let a = (dlat / 2.0).sin().powi(2) + lat1.cos() * lat2.cos() * (dlon / 2.0).sin().powi(2);
let c = 2.0 * a.sqrt().asin();
c * 6_371_000.0
}
pub fn speed_kmh(&self, other: &Location) -> f32 {
let dist = self.haversine_distance(other);
let time = self.timestamp.timestamp() - other.timestamp.timestamp();
if time > 0 {
let meter_second = dist / time as f32;
meter_second * 3.6
} else {
dist / 1000.0
}
}
}
fn parse_date<'de, D>(de: D) -> Result<NaiveDateTime, D::Error>
where
D: serde::Deserializer<'de>,
{
let deser_result: serde_json::Value = serde::Deserialize::deserialize(de)?;
match deser_result {
serde_json::Value::String(ref s) => Ok(NaiveDateTime::from_timestamp(
s.parse::<i64>().unwrap() / 1000,
0,
)),
_ => Err(serde::de::Error::custom("Unexpected value")),
}
}
fn parse_location<'de, D>(de: D) -> Result<f32, D::Error>
where
D: serde::Deserializer<'de>,
{
let deser_result: serde_json::Value = serde::Deserialize::deserialize(de)?;
match deser_result {
serde_json::Value::Number(ref i) => Ok((i.as_f64().unwrap() / 10_000_000.0) as f32),
_ => Err(serde::de::Error::custom("Unexpected value")),
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
use crate::LocationsExt;
let test_data = r#"{"locations" : [ {
"timestampMs" : "1491801919709",
"latitudeE7" : 500373489,
"longitudeE7" : 83320934,
"accuracy" : 19,
"activitys" : [ {
"timestampMs" : "1491802042056",
"activities" : [ {
"type" : "still",
"confidence" : 100
} ]
}, {
"timestampMs" : "1491801923049",
"activities" : [ {
"type" : "still",
"confidence" : 100
} ]
} ]
}]}"#;
let _locations = crate::deserialize(&test_data).filter_outliers();
}
}