Przeglądaj źródła

Add IndexedReader example

Johannes Hofmann 5 lat temu
rodzic
commit
243e9a9eac
1 zmienionych plików z 39 dodań i 0 usunięć
  1. 39
    0
      examples/indexed.rs

+ 39
- 0
examples/indexed.rs Wyświetl plik

@@ -0,0 +1,39 @@
1
+// Count the number of buildings and their nodes from a PBF file
2
+// given as the first command line argument.
3
+
4
+extern crate osmpbf;
5
+
6
+use osmpbf::{IndexedReader, Element};
7
+use std::error::Error;
8
+
9
+fn main() -> Result<(), Box<dyn Error>> {
10
+    // Read command line argument and create IndexedReader
11
+    let arg = std::env::args_os()
12
+        .nth(1)
13
+        .ok_or("need a *.osm.pbf file as argument")?;
14
+    let mut reader = IndexedReader::from_path(&arg)?;
15
+
16
+    println!("Counting...");
17
+    let mut ways = 0;
18
+    let mut nodes = 0;
19
+
20
+    reader.read_ways_and_deps(
21
+        |way| {
22
+            // Filter ways. Return true if tags contain "building": "yes".
23
+            way.tags().any(|key_value| key_value == ("building", "yes"))
24
+        },
25
+        |element| {
26
+            // Increment counter for ways and nodes
27
+            match element {
28
+                Element::Way(_way) => ways += 1,
29
+                Element::Node(_node) => nodes += 1,
30
+                Element::DenseNode(_dense_node) => nodes += 1,
31
+                Element::Relation(_) => {}, // should not occur
32
+            }
33
+        },
34
+    )?;
35
+
36
+    // Print result
37
+    println!("ways:  {}\nnodes: {}", ways, nodes);
38
+    Ok(())
39
+}