-
Notifications
You must be signed in to change notification settings - Fork 42
/
waypoint.rs
259 lines (224 loc) · 8.45 KB
/
waypoint.rs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//! waypoint handles parsing of GPX-spec waypoints.
use std::io::Read;
use geo_types::Point;
use xml::reader::XmlEvent;
use crate::errors::{GpxError, GpxResult};
use crate::parser::{extensions, fix, link, string, time, verify_starting_tag, Context};
use crate::{GpxVersion, Waypoint};
/// consume consumes a GPX waypoint from the `reader` until it ends.
pub fn consume<R: Read>(context: &mut Context<R>, tagname: &'static str) -> GpxResult<Waypoint> {
let attributes = verify_starting_tag(context, tagname)?;
// get required latitude and longitude
let latitude = attributes
.iter()
.find(|attr| attr.name.local_name == "lat")
.ok_or(GpxError::InvalidElementLacksAttribute(
"latitude", "waypoint",
))?;
let latitude: f64 = latitude.value.parse()?;
if !(-90.0..=90.0).contains(&latitude) {
return Err(GpxError::LonLatOutOfBoundsError(
"latitude",
"[-90.0, 90.0]",
latitude,
));
};
let longitude = attributes
.iter()
.find(|attr| attr.name.local_name == "lon")
.ok_or(GpxError::InvalidElementLacksAttribute(
"longitude",
"waypoint",
))?;
let longitude: f64 = longitude.value.parse()?;
if !(-180.0..180.0).contains(&longitude) {
return Err(GpxError::LonLatOutOfBoundsError(
"Longitude",
"[-180.0, 180.0",
longitude,
));
};
let mut waypoint: Waypoint = Waypoint::new(Point::new(longitude, latitude));
loop {
let next_event = {
if let Some(next) = context.reader.peek() {
match next {
Ok(n) => n,
Err(_) => return Err(GpxError::EventParsingError("waypoint event")),
}
} else {
break;
}
};
match next_event {
XmlEvent::StartElement { ref name, .. } => {
match name.local_name.as_ref() {
"ele" => {
// Cast the elevation to an f64, from a string.
waypoint.elevation = Some(string::consume(context, "ele", false)?.parse()?)
}
"speed" if context.version == GpxVersion::Gpx10 => {
// Speed is from GPX 1.0
waypoint.speed = Some(string::consume(context, "speed", false)?.parse()?);
}
"time" => waypoint.time = Some(time::consume(context)?),
"name" => waypoint.name = Some(string::consume(context, "name", false)?),
"cmt" => waypoint.comment = Some(string::consume(context, "cmt", true)?),
"desc" => waypoint.description = Some(string::consume(context, "desc", true)?),
"src" => waypoint.source = Some(string::consume(context, "src", true)?),
"link" => waypoint.links.push(link::consume(context)?),
"sym" => waypoint.symbol = Some(string::consume(context, "sym", false)?),
"type" => waypoint._type = Some(string::consume(context, "type", false)?),
// Optional accuracy information
"fix" => waypoint.fix = Some(fix::consume(context)?),
"geoidheight" => {
waypoint.geoidheight =
Some(string::consume(context, "geoidheight", false)?.parse()?)
}
"sat" => waypoint.sat = Some(string::consume(context, "sat", false)?.parse()?),
"hdop" => {
waypoint.hdop = Some(string::consume(context, "hdop", false)?.parse()?)
}
"vdop" => {
waypoint.vdop = Some(string::consume(context, "vdop", false)?.parse()?)
}
"pdop" => {
waypoint.pdop = Some(string::consume(context, "pdop", false)?.parse()?)
}
"ageofdgpsdata" => {
waypoint.dgps_age =
Some(string::consume(context, "ageofdgpsdata", false)?.parse()?)
}
"dgpsid" => {
waypoint.dgpsid = Some(string::consume(context, "dgpsid", false)?.parse()?)
}
// Finally the GPX 1.1 extensions
"extensions" => extensions::consume(context)?,
child => {
return Err(GpxError::InvalidChildElement(
String::from(child),
"waypoint",
));
}
}
}
XmlEvent::EndElement { ref name } => {
if name.local_name != tagname {
return Err(GpxError::InvalidClosingTag(
name.local_name.clone(),
"waypoint",
));
}
context.reader.next(); //consume the end tag
return Ok(waypoint);
}
_ => {
context.reader.next(); //consume and ignore this event
}
}
}
Err(GpxError::MissingClosingTag("waypoint"))
}
#[cfg(test)]
mod tests {
use geo_types::Point;
use super::consume;
use crate::{Fix, GpxVersion};
#[test]
fn consume_waypoint() {
let waypoint = consume!(
"
<wpt lon=\"-77.0365\" lat=\"38.8977\">
<name>The White House</name>
<cmt>This is a comment about the white house.</cmt>
<desc>The white house is very nice!</desc>
<src>Garmin eTrex</src>
<type>waypoint classification</type>
<ele>4608.12</ele>
<fix>dgps</fix>
<sat>4</sat>
<hdop>6.058</hdop>
<speed>0.0000</speed>
</wpt>
",
GpxVersion::Gpx10,
"wpt"
);
assert!(waypoint.is_ok());
let waypoint = waypoint.unwrap();
assert_eq!(waypoint.point(), Point::new(-77.0365, 38.8977));
assert_eq!(waypoint.name.unwrap(), "The White House");
assert_eq!(
waypoint.comment.unwrap(),
"This is a comment about the white house."
);
assert_eq!(
waypoint.description.unwrap(),
"The white house is very nice!"
);
assert_eq!(waypoint.source.unwrap(), "Garmin eTrex");
assert_eq!(waypoint._type.unwrap(), "waypoint classification");
assert_eq!(waypoint.elevation.unwrap(), 4608.12);
assert_eq!(waypoint.fix.unwrap(), Fix::DGPS);
assert_eq!(waypoint.sat.unwrap(), 4);
assert_eq!(waypoint.hdop.unwrap(), 6.058);
}
#[test]
fn consume_empty() {
let waypoint = consume!(
"<trkpt lat=\"2.345\" lon=\"1.234\"></trkpt>",
GpxVersion::Gpx11,
"trkpt"
);
assert!(waypoint.is_ok());
let waypoint = waypoint.unwrap();
assert_eq!(waypoint.point(), Point::new(1.234, 2.345));
assert_eq!(waypoint.point().lng(), 1.234);
assert_eq!(waypoint.point().lat(), 2.345);
}
#[test]
fn consume_bad_waypoint() {
let waypoint = consume!(
"<wpt lat=\"32.4\" lon=\"notanumber\"></wpt>",
GpxVersion::Gpx11,
"wpt"
);
assert!(waypoint.is_err());
}
#[test]
fn consume_bad_latitude_1() {
let waypoint = consume!(
"<trkpt lat=\"-90.1\" lon=\"1.234\"></trkpt>",
GpxVersion::Gpx11,
"trkpt"
);
assert!(waypoint.is_err());
}
#[test]
fn consume_bad_latitude_2() {
let waypoint = consume!(
"<trkpt lat=\"90.1\" lon=\"1.234\"></trkpt>",
GpxVersion::Gpx11,
"trkpt"
);
assert!(waypoint.is_err());
}
#[test]
fn consume_bad_longitude_1() {
let waypoint = consume!(
"<trkpt lat=\"-32.4\" lon=\"-180.1\"></trkpt>",
GpxVersion::Gpx11,
"trkpt"
);
assert!(waypoint.is_err());
}
#[test]
fn consume_bad_longitude_2() {
let waypoint = consume!(
"<trkpt lat=\"32.4\" lon=\"180.0\"></trkpt>",
GpxVersion::Gpx11,
"trkpt"
);
assert!(waypoint.is_err());
}
}