Ver código fonte

Add PrimitiveBlock::for_each_element method

Johannes Hofmann 8 anos atrás
pai
commit
dd7b9d12a1
3 arquivos alterados com 43 adições e 39 exclusões
  1. 21
    1
      src/block.rs
  2. 19
    0
      src/elements.rs
  3. 3
    38
      src/reader.rs

+ 21
- 1
src/block.rs Ver arquivo

@@ -1,7 +1,7 @@
1 1
 //! `HeaderBlock`, `PrimitiveBlock` and `PrimitiveGroup`s
2 2
 
3 3
 use dense::DenseNodeIter;
4
-use elements::{Node, Way, Relation};
4
+use elements::{Element, Node, Way, Relation};
5 5
 use errors::*;
6 6
 use proto::osmformat;
7 7
 use std;
@@ -46,6 +46,26 @@ impl PrimitiveBlock {
46 46
         GroupIter::new(&self.block)
47 47
     }
48 48
 
49
+    /// Calls the given closure on each element.
50
+    pub fn for_each_element<F>(&self, mut f: F)
51
+        where F: for<'a> FnMut(Element<'a>)
52
+    {
53
+        for group in self.groups() {
54
+            for node in group.nodes() {
55
+                f(Element::Node(node))
56
+            }
57
+            for dnode in group.dense_nodes() {
58
+                f(Element::DenseNode(dnode))
59
+            }
60
+            for way in group.ways() {
61
+                f(Element::Way(way));
62
+            }
63
+            for relation in group.relations() {
64
+                f(Element::Relation(relation));
65
+            }
66
+        }
67
+    }
68
+
49 69
     /// Returns the raw stringtable. Elements in a `PrimitiveBlock` do not store strings
50 70
     /// themselves; instead, they just store indices to the stringtable. By convention, the
51 71
     /// contained strings are UTF-8 encoded but it is not safe to assume that (use

+ 19
- 0
src/elements.rs Ver arquivo

@@ -2,11 +2,30 @@
2 2
 
3 3
 use errors::*;
4 4
 use block::str_from_stringtable;
5
+use dense::DenseNode;
5 6
 use proto::osmformat::PrimitiveBlock;
6 7
 use proto::osmformat;
7 8
 use std;
8 9
 
9 10
 
11
+/// An enum with the OSM core elements: nodes, ways and relations.
12
+#[derive(Clone, Debug)]
13
+pub enum Element<'a> {
14
+    /// A node. Also, see `DenseNode`.
15
+    Node(Node<'a>),
16
+
17
+    /// Just like `Node`, but with a different representation in memory. This distinction is
18
+    /// usually not important but is not abstracted away to avoid copying. So, if you want to match
19
+    /// `Node`, you also likely want to match `DenseNode`.
20
+    DenseNode(DenseNode<'a>),
21
+
22
+    /// A way.
23
+    Way(Way<'a>),
24
+
25
+    /// A relation.
26
+    Relation(Relation<'a>),
27
+}
28
+
10 29
 /// An OpenStreetMap node element (See [OSM wiki](http://wiki.openstreetmap.org/wiki/Node)).
11 30
 #[derive(Clone, Debug)]
12 31
 pub struct Node<'a> {

+ 3
- 38
src/reader.rs Ver arquivo

@@ -1,8 +1,7 @@
1 1
 //! High level reader interface
2 2
 
3 3
 use blob::{BlobDecode, BlobReader};
4
-use dense::DenseNode;
5
-use elements::{Node, Way, Relation};
4
+use elements::Element;
6 5
 use errors::*;
7 6
 use rayon::prelude::*;
8 7
 use std::fs::File;
@@ -74,20 +73,7 @@ impl<R: Read> ElementReader<R> {
74 73
             match blob.decode() {
75 74
                 Ok(BlobDecode::OsmHeader(_)) | Ok(BlobDecode::Unknown(_)) => {},
76 75
                 Ok(BlobDecode::OsmData(block)) => {
77
-                    for group in block.groups() {
78
-                        for node in group.nodes() {
79
-                            f(Element::Node(node))
80
-                        }
81
-                        for dnode in group.dense_nodes() {
82
-                            f(Element::DenseNode(dnode))
83
-                        }
84
-                        for way in group.ways() {
85
-                            f(Element::Way(way));
86
-                        }
87
-                        for relation in group.relations() {
88
-                            f(Element::Relation(relation));
89
-                        }
90
-                    }
76
+                    block.for_each_element(&mut f);
91 77
                 },
92 78
                 Err(e) => return Err(e),
93 79
             }
@@ -189,29 +175,8 @@ impl ElementReader<BufReader<File>> {
189 175
     /// ```
190 176
     pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self>
191 177
     {
192
-        let f = File::open(path)?;
193
-        let reader = BufReader::new(f);
194
-
195 178
         Ok(ElementReader {
196
-            blob_iter: BlobReader::new(reader),
179
+            blob_iter: BlobReader::from_path(path)?,
197 180
         })
198 181
     }
199 182
 }
200
-
201
-/// An enum with the OSM core elements: nodes, ways and relations.
202
-#[derive(Clone, Debug)]
203
-pub enum Element<'a> {
204
-    /// A node. Also, see `DenseNode`.
205
-    Node(Node<'a>),
206
-
207
-    /// Just like `Node`, but with a different representation in memory. This distinction is
208
-    /// usually not important but is not abstracted away to avoid copying. So, if you want to match
209
-    /// `Node`, you also likely want to match `DenseNode`.
210
-    DenseNode(DenseNode<'a>),
211
-
212
-    /// A way.
213
-    Way(Way<'a>),
214
-
215
-    /// A relation.
216
-    Relation(Relation<'a>),
217
-}