|
|
@@ -28,48 +28,42 @@ extern crate osmpbf;
|
|
28
|
28
|
Here's a simple example that counts all the ways in a file:
|
|
29
|
29
|
|
|
30
|
30
|
```rust
|
|
31
|
|
-extern crate osmpbf;
|
|
32
|
|
-
|
|
33
|
|
-use osmpbf::*;
|
|
|
31
|
+use osmpbf::{ElementReader, Element};
|
|
34
|
32
|
|
|
35
|
|
-fn main() {
|
|
36
|
|
- let reader = ElementReader::from_path("tests/test.osm.pbf").unwrap();
|
|
37
|
|
- let mut ways = 0_u64;
|
|
|
33
|
+let reader = ElementReader::from_path("tests/test.osm.pbf")?;
|
|
|
34
|
+let mut ways = 0_u64;
|
|
38
|
35
|
|
|
39
|
|
- // Increment the counter by one for each way.
|
|
40
|
|
- reader.for_each(|element| {
|
|
41
|
|
- if let Element::Way(_) = element {
|
|
42
|
|
- ways += 1;
|
|
43
|
|
- }
|
|
44
|
|
- }).unwrap();
|
|
|
36
|
+// Increment the counter by one for each way.
|
|
|
37
|
+reader.for_each(|element| {
|
|
|
38
|
+ if let Element::Way(_) = element {
|
|
|
39
|
+ ways += 1;
|
|
|
40
|
+ }
|
|
|
41
|
+})?;
|
|
45
|
42
|
|
|
46
|
|
- println!("Number of ways: {}", ways);
|
|
47
|
|
-}
|
|
|
43
|
+println!("Number of ways: {}", ways);
|
|
48
|
44
|
```
|
|
49
|
45
|
|
|
50
|
46
|
In this second example, we also count the ways but make use of all cores by
|
|
51
|
47
|
decoding the file in parallel:
|
|
52
|
48
|
|
|
53
|
49
|
```rust
|
|
54
|
|
-use osmpbf::*;
|
|
55
|
|
-
|
|
56
|
|
-fn main() {
|
|
57
|
|
- let reader = ElementReader::from_path("tests/test.osm.pbf").unwrap();
|
|
58
|
|
-
|
|
59
|
|
- // Count the ways
|
|
60
|
|
- let ways = reader.par_map_reduce(
|
|
61
|
|
- |element| {
|
|
62
|
|
- match element {
|
|
63
|
|
- Element::Way(_) => 1,
|
|
64
|
|
- _ => 0,
|
|
65
|
|
- }
|
|
66
|
|
- },
|
|
67
|
|
- || 0_u64, // Zero is the identity value for addition
|
|
68
|
|
- |a, b| a + b // Sum the partial results
|
|
69
|
|
- ).unwrap();
|
|
70
|
|
-
|
|
71
|
|
- println!("Number of ways: {}", ways);
|
|
72
|
|
-}
|
|
|
50
|
+use osmpbf::{ElementReader, Element};
|
|
|
51
|
+
|
|
|
52
|
+let reader = ElementReader::from_path("tests/test.osm.pbf")?;
|
|
|
53
|
+
|
|
|
54
|
+// Count the ways
|
|
|
55
|
+let ways = reader.par_map_reduce(
|
|
|
56
|
+ |element| {
|
|
|
57
|
+ match element {
|
|
|
58
|
+ Element::Way(_) => 1,
|
|
|
59
|
+ _ => 0,
|
|
|
60
|
+ }
|
|
|
61
|
+ },
|
|
|
62
|
+ || 0_u64, // Zero is the identity value for addition
|
|
|
63
|
+ |a, b| a + b // Sum the partial results
|
|
|
64
|
+)?;
|
|
|
65
|
+
|
|
|
66
|
+println!("Number of ways: {}", ways);
|
|
73
|
67
|
```
|
|
74
|
68
|
|
|
75
|
69
|
## The PBF format
|