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
use std::collections::BTreeMap;
use anyhow::Result;
use abstio::slurp_file;
use abstutil::{prettyprint_usize, Tags, Timer};
use geom::{GPSBounds, LonLat, Pt2D};
use map_model::osm::{NodeID, OsmID, RelationID, WayID};
pub struct Document {
pub gps_bounds: GPSBounds,
pub nodes: BTreeMap<NodeID, Node>,
pub ways: BTreeMap<WayID, Way>,
pub relations: BTreeMap<RelationID, Relation>,
}
pub struct Node {
pub pt: Pt2D,
pub tags: Tags,
}
pub struct Way {
pub nodes: Vec<NodeID>,
pub pts: Vec<Pt2D>,
pub tags: Tags,
}
pub struct Relation {
pub tags: Tags,
pub members: Vec<(String, OsmID)>,
}
pub fn read(path: &str, input_gps_bounds: &GPSBounds, timer: &mut Timer) -> Result<Document> {
timer.start(format!("read {}", path));
let bytes = slurp_file(path)?;
let raw_string = std::str::from_utf8(&bytes)?;
let tree = roxmltree::Document::parse(raw_string)?;
timer.stop(format!("read {}", path));
let mut doc = Document {
gps_bounds: input_gps_bounds.clone(),
nodes: BTreeMap::new(),
ways: BTreeMap::new(),
relations: BTreeMap::new(),
};
timer.start("scrape objects");
for obj in tree.descendants() {
if !obj.is_element() {
continue;
}
match obj.tag_name().name() {
"bounds" => {
if doc.gps_bounds != GPSBounds::new() {
continue;
}
doc.gps_bounds.update(LonLat::new(
obj.attribute("minlon").unwrap().parse::<f64>().unwrap(),
obj.attribute("minlat").unwrap().parse::<f64>().unwrap(),
));
doc.gps_bounds.update(LonLat::new(
obj.attribute("maxlon").unwrap().parse::<f64>().unwrap(),
obj.attribute("maxlat").unwrap().parse::<f64>().unwrap(),
));
}
"node" => {
if doc.gps_bounds == GPSBounds::new() {
warn!(
"No clipping polygon provided and the .osm is missing a <bounds> element, \
so figuring out the bounds manually."
);
doc.gps_bounds = scrape_bounds(&tree);
}
let id = NodeID(obj.attribute("id").unwrap().parse::<i64>().unwrap());
if doc.nodes.contains_key(&id) {
bail!("Duplicate {}, your .osm is corrupt", id);
}
let pt = LonLat::new(
obj.attribute("lon").unwrap().parse::<f64>().unwrap(),
obj.attribute("lat").unwrap().parse::<f64>().unwrap(),
)
.to_pt(&doc.gps_bounds);
let tags = read_tags(obj);
doc.nodes.insert(id, Node { pt, tags });
}
"way" => {
let id = WayID(obj.attribute("id").unwrap().parse::<i64>().unwrap());
if doc.ways.contains_key(&id) {
bail!("Duplicate {}, your .osm is corrupt", id);
}
let tags = read_tags(obj);
let mut nodes = Vec::new();
let mut pts = Vec::new();
for child in obj.children() {
if child.tag_name().name() == "nd" {
let n = NodeID(child.attribute("ref").unwrap().parse::<i64>().unwrap());
if let Some(ref node) = doc.nodes.get(&n) {
nodes.push(n);
pts.push(node.pt);
}
}
}
if !nodes.is_empty() {
doc.ways.insert(id, Way { tags, nodes, pts });
}
}
"relation" => {
let id = RelationID(obj.attribute("id").unwrap().parse::<i64>().unwrap());
if doc.relations.contains_key(&id) {
bail!("Duplicate {}, your .osm is corrupt", id);
}
let tags = read_tags(obj);
let mut members = Vec::new();
for child in obj.children() {
if child.tag_name().name() == "member" {
let member = match child.attribute("type").unwrap() {
"node" => {
let n =
NodeID(child.attribute("ref").unwrap().parse::<i64>().unwrap());
if !doc.nodes.contains_key(&n) {
continue;
}
OsmID::Node(n)
}
"way" => {
let w =
WayID(child.attribute("ref").unwrap().parse::<i64>().unwrap());
if !doc.ways.contains_key(&w) {
continue;
}
OsmID::Way(w)
}
"relation" => {
let r = RelationID(
child.attribute("ref").unwrap().parse::<i64>().unwrap(),
);
if !doc.relations.contains_key(&r) {
continue;
}
OsmID::Relation(r)
}
_ => continue,
};
members.push((child.attribute("role").unwrap().to_string(), member));
}
}
doc.relations.insert(id, Relation { tags, members });
}
_ => {}
}
}
timer.stop("scrape objects");
info!(
"Found {} nodes, {} ways, {} relations",
prettyprint_usize(doc.nodes.len()),
prettyprint_usize(doc.ways.len()),
prettyprint_usize(doc.relations.len())
);
Ok(doc)
}
fn read_tags(obj: roxmltree::Node) -> Tags {
let mut tags = Tags::empty();
for child in obj.children() {
if child.tag_name().name() == "tag" {
let key = child.attribute("k").unwrap();
if key.starts_with("tiger:") || key.starts_with("old_name:") {
continue;
}
tags.insert(key, child.attribute("v").unwrap());
}
}
tags
}
fn scrape_bounds(doc: &roxmltree::Document) -> GPSBounds {
let mut b = GPSBounds::new();
for obj in doc.descendants() {
if obj.is_element() && obj.tag_name().name() == "node" {
b.update(LonLat::new(
obj.attribute("lon").unwrap().parse::<f64>().unwrap(),
obj.attribute("lat").unwrap().parse::<f64>().unwrap(),
));
}
}
b
}