-
Notifications
You must be signed in to change notification settings - Fork 7
/
debug.rs
215 lines (193 loc) · 6.06 KB
/
debug.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
//! Dumps the contents of the input archive in a debug format.
//!
//! Demonstrates
//!
//! * iteration through all fundamental types
//! * accessing of fields and following of references
//!
//! LICENSE
//!
//! The code in this example file is released into the Public Domain.
use clap::Parser;
use osmflat::{iter_tags, FileResourceStorage, Osm, RelationMembersRef};
use std::path::PathBuf;
use std::str::{self, Utf8Error};
#[derive(Debug)]
struct Header<'ar> {
#[allow(unused)]
bbox: (f64, f64, f64, f64),
#[allow(unused)]
writingprogram: &'ar str,
#[allow(unused)]
source: &'ar str,
#[allow(unused)]
replication_timestamp: i64,
#[allow(unused)]
replication_sequence_number: i64,
#[allow(unused)]
replication_base_url: &'ar str,
}
#[derive(Debug)]
struct Node<'ar> {
#[allow(unused)]
id: Option<u64>,
#[allow(unused)]
lat: f64,
#[allow(unused)]
lon: f64,
#[allow(unused)]
tags: Vec<(&'ar str, &'ar str)>,
}
#[derive(Debug)]
struct Way<'ar> {
#[allow(unused)]
id: Option<u64>,
#[allow(unused)]
tags: Vec<(&'ar str, &'ar str)>,
#[allow(unused)]
nodes: Vec<Option<u64>>,
}
#[derive(Debug)]
struct Relation<'ar> {
#[allow(unused)]
id: Option<u64>,
#[allow(unused)]
tags: Vec<(&'ar str, &'ar str)>,
#[allow(unused)]
members: Vec<Member<'ar>>,
}
#[derive(Debug)]
struct Member<'ar> {
#[allow(unused)]
r#type: Type,
#[allow(unused)]
idx: Option<u64>,
#[allow(unused)]
role: &'ar str,
}
#[derive(Debug)]
enum Type {
Node,
Way,
Relation,
}
impl<'ar> Member<'ar> {
fn new_slice(
archive: &'ar Osm,
relation_idx: usize,
) -> impl Iterator<Item = Result<Member<'ar>, Utf8Error>> {
let strings = archive.stringtable();
archive
.relation_members()
.at(relation_idx)
.map(move |member| {
let res = match member {
RelationMembersRef::NodeMember(m) => Member {
r#type: Type::Node,
idx: m.node_idx(),
role: strings.substring(m.role_idx() as usize)?,
},
RelationMembersRef::WayMember(m) => Member {
r#type: Type::Way,
idx: m.way_idx(),
role: strings.substring(m.role_idx() as usize)?,
},
RelationMembersRef::RelationMember(m) => Member {
r#type: Type::Relation,
idx: m.relation_idx(),
role: strings.substring(m.role_idx() as usize)?,
},
};
Ok(res)
})
}
}
/// output osmflatdata: nodes, ways, and/or relations
#[derive(Debug, Parser)]
struct Args {
/// input osmflat archive
input: PathBuf,
/// which types to print: (n)odes, (w)ays, or (r)elations
#[arg(long, default_value = "nwr")]
types: String,
/// amount of entities to print
#[arg(long)]
num: Option<usize>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let archive = Osm::open(FileResourceStorage::new(args.input))?;
let header = archive.header();
let strings = archive.stringtable();
let scale_coord = |x| x as f64 / header.coord_scale() as f64;
// print header
let header = Header {
bbox: (
scale_coord(header.bbox_left()),
scale_coord(header.bbox_right()),
scale_coord(header.bbox_top()),
scale_coord(header.bbox_bottom()),
),
writingprogram: strings.substring(header.writingprogram_idx() as usize)?,
source: strings.substring(header.source_idx() as usize)?,
replication_timestamp: header.replication_timestamp(),
replication_sequence_number: header.replication_sequence_number(),
replication_base_url: strings.substring(header.replication_base_url_idx() as usize)?,
};
println!("{header:#?}");
let collect_utf8_tags = |tags| -> Vec<(&str, &str)> {
iter_tags(&archive, tags)
.filter_map(|(k, v)| match (str::from_utf8(k), str::from_utf8(v)) {
(Ok(k), Ok(v)) => Some((k, v)),
_ => None,
})
.collect()
};
// print nodes
let mut node_ids = archive.ids().map(|x| x.nodes()).into_iter().flatten();
if args.types.contains('n') {
for node in archive.nodes().iter().take(args.num.unwrap_or(usize::MAX)) {
let node = Node {
id: node_ids.next().map(|x| x.value()),
lat: scale_coord(node.lat()),
lon: scale_coord(node.lon()),
tags: collect_utf8_tags(node.tags()),
};
println!("{node:#?}");
}
}
// print ways
let nodes_index = archive.nodes_index();
let mut way_ids = archive.ids().map(|x| x.ways()).into_iter().flatten();
if args.types.contains('w') {
for way in archive.ways().iter().take(args.num.unwrap_or(usize::MAX)) {
let way = Way {
id: way_ids.next().map(|x| x.value()),
tags: collect_utf8_tags(way.tags()),
nodes: way
.refs()
.map(|idx| nodes_index[idx as usize].value())
.collect(),
};
println!("{way:#?}");
}
}
// print relations
let mut relation_ids = archive.ids().map(|x| x.ways()).into_iter().flatten();
if args.types.contains('r') {
for (relation_idx, relation) in archive.relations()[..3]
.iter()
.take(args.num.unwrap_or(usize::MAX))
.enumerate()
{
let members: Result<Vec<_>, _> = Member::new_slice(&archive, relation_idx).collect();
let relation = Relation {
id: relation_ids.next().map(|x| x.value()),
tags: collect_utf8_tags(relation.tags()),
members: members?,
};
println!("{relation:#?}");
}
}
Ok(())
}