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
/*!
A fast reader and writer for the OpenStreetMap PBF file format (\*.osm.pbf).
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
pbfhogg = "0.3"
```
## Example: Count ways
Here's a simple example that counts all the OpenStreetMap way elements in a
file:
```rust
use pbfhogg::{ElementReader, Element};
let reader = ElementReader::from_path("tests/test.osm.pbf")?;
let mut ways = 0_u64;
// Increment the counter by one for each way.
reader.for_each(|element| {
if let Element::Way(_) = element {
ways += 1;
}
})?;
println!("Number of ways: {ways}");
# assert_eq!(ways, 1);
# Ok::<(), std::io::Error>(())
```
## Example: Count ways in parallel
In this second example, we also count the ways but make use of all cores by
decoding the file in parallel:
```rust
use pbfhogg::{ElementReader, Element};
let reader = ElementReader::from_path("tests/test.osm.pbf")?;
// Count the ways
let ways = reader.par_map_reduce(
|element| {
match element {
Element::Way(_) => 1,
_ => 0,
}
},
|| 0_u64, // Zero is the identity value for addition
|a, b| a + b // Sum the partial results
)?;
println!("Number of ways: {ways}");
# assert_eq!(ways, 1);
# Ok::<(), std::io::Error>(())
```
## Example: Write a PBF file
Build blocks with [`BlockBuilder`] and write them with [`PbfWriter`]:
```rust,no_run
use pbfhogg::write::block_builder::{BlockBuilder, HeaderBuilder};
use pbfhogg::write::writer::{PbfWriter, Compression};
let header_bytes = HeaderBuilder::new()
.bbox(9.0, 54.0, 13.0, 58.0)
.sorted()
.build()?;
let mut writer = PbfWriter::to_path(
"output.osm.pbf".as_ref(),
Compression::default(),
&header_bytes,
)?;
let mut bb = BlockBuilder::new();
bb.add_node(1, 556_761_000, 125_683_000, [("name", "Copenhagen")], None);
// Flush the block to the writer - compression dispatches to rayon
if let Some(block_bytes) = bb.take()? {
writer.write_primitive_block(block_bytes)?;
}
writer.flush()?;
# Ok::<(), std::io::Error>(())
```
## Example: In-memory writing
For tests or small PBFs, use [`PbfWriter::new`] with any [`Write`](std::io::Write) impl:
```rust,no_run
use pbfhogg::write::block_builder::{BlockBuilder, HeaderBuilder};
use pbfhogg::write::writer::{PbfWriter, Compression};
let header_bytes = HeaderBuilder::new().sorted().build()?;
let mut buf = std::io::Cursor::new(Vec::new());
let mut writer = PbfWriter::new(&mut buf, Compression::default());
writer.write_header(&header_bytes)?;
let mut bb = BlockBuilder::new();
// ... add elements, write blocks synchronously ...
writer.flush()?;
# Ok::<(), std::io::Error>(())
```
*/
// Module tree
// format is always available; reader requires geocode-reader feature
pub
pub
pub
pub
pub
pub
pub
/// Boxed-error Result alias used by command implementations and lifted
/// command-shared library code. Distinct from [`crate::Result`] (which is
/// over the typed [`crate::Error`]). The boxed flavor is used where
/// callers only display the error and exit, so typed enums add complexity
/// with no matching benefit.
pub type BoxResult<T> = Result;
// ---------------------------------------------------------------------------
// Public API re-exports
//
// 1. **Explicit item-level re-exports** flatten selected types into the crate
// root so external consumers get a clean API:
// use pbfhogg::{Element, BlobReader, PrimitiveBlock};
//
// 2. **Named module-level re-exports** create short `crate::blob`,
// `crate::block_builder`, `crate::writer` paths used throughout the crate.
// ---------------------------------------------------------------------------
// Explicit re-exports: flat public API (`pbfhogg::Element`, `pbfhogg::BlobReader`, etc.)
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
// Module re-exports: short internal paths (`crate::blob`, `crate::block_builder`, etc.)
// Required by imports and doc links in commands/, read/, and write/ modules.
pub use ;
pub use file_reader;
pub use ;
pub use file_writer;
pub use has_indexdata;
pub use HeaderOverrides;
pub use ;
pub use ;