Преглед изворни кода

Add raw_{stringtable,tags} methods

Johannes Hofmann пре 8 година
родитељ
комит
8ca5c0f73a
2 измењених фајлова са 162 додато и 4 уклоњено
  1. 8
    0
      src/block.rs
  2. 154
    4
      src/elements.rs

+ 8
- 0
src/block.rs Прегледај датотеку

@@ -45,6 +45,14 @@ impl PrimitiveBlock {
45 45
     pub fn groups(&self) -> GroupIter {
46 46
         GroupIter::new(&self.block)
47 47
     }
48
+
49
+    /// Returns the raw stringtable. Elements in a `PrimitiveBlock` do not store strings
50
+    /// themselves; instead, they just store indices to the stringtable. By convention, the
51
+    /// contained strings are UTF-8 encoded but it is not safe to assume that (use
52
+    /// `std::str::from_utf8`).
53
+    pub fn raw_stringtable(&self) -> &[Vec<u8>] {
54
+        self.block.get_stringtable().get_s()
55
+    }
48 56
 }
49 57
 
50 58
 /// A `PrimitiveGroup` contains a sequence of elements of one type.

+ 154
- 4
src/elements.rs Прегледај датотеку

@@ -28,7 +28,28 @@ impl<'a> Node<'a> {
28 28
         self.osmnode.get_id()
29 29
     }
30 30
 
31
-    /// Returns an iterator over the tags of this node (See [OSM wiki](http://wiki.openstreetmap.org/wiki/Tags)).
31
+    /// Returns an iterator over the tags of this node
32
+    /// (See [OSM wiki](http://wiki.openstreetmap.org/wiki/Tags)).
33
+    /// A tag is represented as a pair of strings (key and value).
34
+    ///
35
+    /// # Example
36
+    /// ```
37
+    /// use osmpbf::*;
38
+    ///
39
+    /// # fn foo() -> Result<()> {
40
+    /// let reader = ElementReader::from_path("tests/test.osm.pbf")?;
41
+    ///
42
+    /// reader.for_each(|element| {
43
+    ///     if let Element::Node(node) = element {
44
+    ///         for (key, value) in node.tags() {
45
+    ///             println!("key: {}, value: {}", key, value);
46
+    ///         }
47
+    ///     }
48
+    /// })?;
49
+    ///
50
+    /// # Ok(())
51
+    /// # }
52
+    /// ```
32 53
     pub fn tags(&self) -> TagIter<'a> {
33 54
         TagIter {
34 55
             block: self.block,
@@ -55,6 +76,26 @@ impl<'a> Node<'a> {
55 76
                              (i64::from(self.block.get_granularity()) *
56 77
                               self.osmnode.get_lon())) as f64
57 78
     }
79
+
80
+    /// Returns an iterator over the tags of this node
81
+    /// (See [OSM wiki](http://wiki.openstreetmap.org/wiki/Tags)).
82
+    /// A tag is represented as a pair of indices (key and value) to the stringtable of the current
83
+    /// `PrimitiveBlock`.
84
+    pub fn raw_tags(&self) -> RawTagIter<'a> {
85
+        RawTagIter {
86
+            block: self.block,
87
+            key_indices: self.osmnode.get_keys().iter(),
88
+            val_indices: self.osmnode.get_vals().iter(),
89
+        }
90
+    }
91
+
92
+    /// Returns the raw stringtable. Elements in a `PrimitiveBlock` do not store strings
93
+    /// themselves; instead, they just store indices to a common stringtable. By convention, the
94
+    /// contained strings are UTF-8 encoded but it is not safe to assume that (use
95
+    /// `std::str::from_utf8`).
96
+    pub fn raw_stringtable(&self) -> &[Vec<u8>] {
97
+        self.block.get_stringtable().get_s()
98
+    }
58 99
 }
59 100
 
60 101
 /// An OpenStreetMap way element (See [OSM wiki](http://wiki.openstreetmap.org/wiki/Way)).
@@ -80,7 +121,28 @@ impl<'a> Way<'a> {
80 121
         self.osmway.get_id()
81 122
     }
82 123
 
83
-    /// Returns an iterator over the tags of this way (See [OSM wiki](http://wiki.openstreetmap.org/wiki/Tags)).
124
+    /// Returns an iterator over the tags of this way
125
+    /// (See [OSM wiki](http://wiki.openstreetmap.org/wiki/Tags)).
126
+    /// A tag is represented as a pair of strings (key and value).
127
+    ///
128
+    /// # Example
129
+    /// ```
130
+    /// use osmpbf::*;
131
+    ///
132
+    /// # fn foo() -> Result<()> {
133
+    /// let reader = ElementReader::from_path("tests/test.osm.pbf")?;
134
+    ///
135
+    /// reader.for_each(|element| {
136
+    ///     if let Element::Way(way) = element {
137
+    ///         for (key, value) in way.tags() {
138
+    ///             println!("key: {}, value: {}", key, value);
139
+    ///         }
140
+    ///     }
141
+    /// })?;
142
+    ///
143
+    /// # Ok(())
144
+    /// # }
145
+    /// ```
84 146
     pub fn tags(&self) -> TagIter<'a> {
85 147
         TagIter {
86 148
             block: self.block,
@@ -107,6 +169,26 @@ impl<'a> Way<'a> {
107 169
     pub fn refs_slice(&self) -> &[i64] {
108 170
         self.osmway.get_refs()
109 171
     }
172
+
173
+    /// Returns an iterator over the tags of this way
174
+    /// (See [OSM wiki](http://wiki.openstreetmap.org/wiki/Tags)).
175
+    /// A tag is represented as a pair of indices (key and value) to the stringtable of the current
176
+    /// `PrimitiveBlock`.
177
+    pub fn raw_tags(&self) -> RawTagIter<'a> {
178
+        RawTagIter {
179
+            block: self.block,
180
+            key_indices: self.osmway.get_keys().iter(),
181
+            val_indices: self.osmway.get_vals().iter(),
182
+        }
183
+    }
184
+
185
+    /// Returns the raw stringtable. Elements in a `PrimitiveBlock` do not store strings
186
+    /// themselves; instead, they just store indices to a common stringtable. By convention, the
187
+    /// contained strings are UTF-8 encoded but it is not safe to assume that (use
188
+    /// `std::str::from_utf8`).
189
+    pub fn raw_stringtable(&self) -> &[Vec<u8>] {
190
+        self.block.get_stringtable().get_s()
191
+    }
110 192
 }
111 193
 
112 194
 /// An OpenStreetMap relation element (See [OSM wiki](http://wiki.openstreetmap.org/wiki/Relation)).
@@ -131,7 +213,28 @@ impl<'a> Relation<'a> {
131 213
         self.osmrel.get_id()
132 214
     }
133 215
 
134
-    /// Returns an iterator over the tags of this relation (See [OSM wiki](http://wiki.openstreetmap.org/wiki/Tags)).
216
+    /// Returns an iterator over the tags of this relation
217
+    /// (See [OSM wiki](http://wiki.openstreetmap.org/wiki/Tags)).
218
+    /// A tag is represented as a pair of strings (key and value).
219
+    ///
220
+    /// # Example
221
+    /// ```
222
+    /// use osmpbf::*;
223
+    ///
224
+    /// # fn foo() -> Result<()> {
225
+    /// let reader = ElementReader::from_path("tests/test.osm.pbf")?;
226
+    ///
227
+    /// reader.for_each(|element| {
228
+    ///     if let Element::Relation(relation) = element {
229
+    ///         for (key, value) in relation.tags() {
230
+    ///             println!("key: {}, value: {}", key, value);
231
+    ///         }
232
+    ///     }
233
+    /// })?;
234
+    ///
235
+    /// # Ok(())
236
+    /// # }
237
+    /// ```
135 238
     pub fn tags(&self) -> TagIter<'a> {
136 239
         TagIter {
137 240
             block: self.block,
@@ -149,6 +252,26 @@ impl<'a> Relation<'a> {
149 252
     pub fn members(&self) -> RelMemberIter<'a> {
150 253
         RelMemberIter::new(self.block, self.osmrel)
151 254
     }
255
+
256
+    /// Returns an iterator over the tags of this relation
257
+    /// (See [OSM wiki](http://wiki.openstreetmap.org/wiki/Tags)).
258
+    /// A tag is represented as a pair of indices (key and value) to the stringtable of the current
259
+    /// `PrimitiveBlock`.
260
+    pub fn raw_tags(&self) -> RawTagIter<'a> {
261
+        RawTagIter {
262
+            block: self.block,
263
+            key_indices: self.osmrel.get_keys().iter(),
264
+            val_indices: self.osmrel.get_vals().iter(),
265
+        }
266
+    }
267
+
268
+    /// Returns the raw stringtable. Elements in a `PrimitiveBlock` do not store strings
269
+    /// themselves; instead, they just store indices to a common stringtable. By convention, the
270
+    /// contained strings are UTF-8 encoded but it is not safe to assume that (use
271
+    /// `std::str::from_utf8`).
272
+    pub fn raw_stringtable(&self) -> &[Vec<u8>] {
273
+        self.block.get_stringtable().get_s()
274
+    }
152 275
 }
153 276
 
154 277
 /// An iterator over the references of a way.
@@ -266,7 +389,7 @@ impl<'a> Iterator for RelMemberIter<'a> {
266 389
 
267 390
 impl<'a> ExactSizeIterator for RelMemberIter<'a> {}
268 391
 
269
-/// An iterator over the tags of an element.
392
+/// An iterator over the tags of an element. It returns a pair of strings (key and value).
270 393
 #[derive(Clone, Debug)]
271 394
 pub struct TagIter<'a> {
272 395
     block: &'a PrimitiveBlock,
@@ -300,6 +423,33 @@ impl<'a> Iterator for TagIter<'a> {
300 423
 
301 424
 impl<'a> ExactSizeIterator for TagIter<'a> {}
302 425
 
426
+/// An iterator over the tags of an element. It returns a pair of indices (key and value) to the
427
+/// stringtable of the current `PrimitiveBlock`.
428
+#[derive(Clone, Debug)]
429
+pub struct RawTagIter<'a> {
430
+    block: &'a PrimitiveBlock,
431
+    key_indices: std::slice::Iter<'a, u32>,
432
+    val_indices: std::slice::Iter<'a, u32>,
433
+}
434
+
435
+//TODO return Result?
436
+impl<'a> Iterator for RawTagIter<'a> {
437
+    type Item = (u32, u32);
438
+
439
+    fn next(&mut self) -> Option<Self::Item> {
440
+        match (self.key_indices.next(), self.val_indices.next()) {
441
+            (Some(&key_index), Some(&val_index)) => Some((key_index, val_index)),
442
+            _ => None,
443
+        }
444
+    }
445
+
446
+    fn size_hint(&self) -> (usize, Option<usize>) {
447
+        self.key_indices.size_hint()
448
+    }
449
+}
450
+
451
+impl<'a> ExactSizeIterator for RawTagIter<'a> {}
452
+
303 453
 /// Additional metadata that might be included in each element.
304 454
 #[derive(Clone, Debug)]
305 455
 pub struct Info<'a> {