A Rust library for reading the OpenStreetMap PBF file format (*.osm.pbf).

block.rs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //! `HeaderBlock`, `PrimitiveBlock` and `Group`s
  2. use dense::DenseNodeIter;
  3. use elements::{Node, Way, Relation};
  4. use errors::*;
  5. use proto::osmformat;
  6. use std;
  7. pub struct HeaderBlock {
  8. header: osmformat::HeaderBlock,
  9. }
  10. impl HeaderBlock {
  11. pub(crate) fn new(header: osmformat::HeaderBlock) -> HeaderBlock {
  12. HeaderBlock { header: header }
  13. }
  14. pub fn required_features(&self) -> &[String] {
  15. self.header.get_required_features()
  16. }
  17. pub fn optional_features(&self) -> &[String] {
  18. self.header.get_optional_features()
  19. }
  20. }
  21. pub struct PrimitiveBlock {
  22. block: osmformat::PrimitiveBlock,
  23. }
  24. impl PrimitiveBlock {
  25. pub fn new(block: osmformat::PrimitiveBlock) -> PrimitiveBlock {
  26. PrimitiveBlock { block: block }
  27. }
  28. pub fn groups(&self) -> GroupIter {
  29. GroupIter::new(&self.block)
  30. }
  31. }
  32. pub struct PrimitiveGroup<'a> {
  33. block: &'a osmformat::PrimitiveBlock,
  34. group: &'a osmformat::PrimitiveGroup,
  35. }
  36. impl<'a> PrimitiveGroup<'a> {
  37. fn new(block: &'a osmformat::PrimitiveBlock,
  38. group: &'a osmformat::PrimitiveGroup)
  39. -> PrimitiveGroup<'a> {
  40. PrimitiveGroup {
  41. block: block,
  42. group: group,
  43. }
  44. }
  45. pub fn nodes(&self) -> GroupNodeIter<'a> {
  46. GroupNodeIter::new(self.block, self.group)
  47. }
  48. pub fn dense_nodes(&self) -> DenseNodeIter<'a> {
  49. DenseNodeIter::new(self.block, self.group.get_dense())
  50. }
  51. pub fn ways(&self) -> GroupWayIter<'a> {
  52. GroupWayIter::new(self.block, self.group)
  53. }
  54. pub fn relations(&self) -> GroupRelationIter<'a> {
  55. GroupRelationIter::new(self.block, self.group)
  56. }
  57. }
  58. pub struct GroupIter<'a> {
  59. block: &'a osmformat::PrimitiveBlock,
  60. groups: std::slice::Iter<'a, osmformat::PrimitiveGroup>,
  61. }
  62. impl<'a> GroupIter<'a> {
  63. fn new(block: &'a osmformat::PrimitiveBlock) -> GroupIter<'a> {
  64. GroupIter {
  65. block: block,
  66. groups: block.get_primitivegroup().iter(),
  67. }
  68. }
  69. }
  70. impl<'a> Iterator for GroupIter<'a> {
  71. type Item = PrimitiveGroup<'a>;
  72. fn next(&mut self) -> Option<Self::Item> {
  73. match self.groups.next() {
  74. Some(g) => Some(PrimitiveGroup::new(self.block, g)),
  75. None => None,
  76. }
  77. }
  78. fn size_hint(&self) -> (usize, Option<usize>) {
  79. self.groups.size_hint()
  80. }
  81. }
  82. impl<'a> ExactSizeIterator for GroupIter<'a> {}
  83. pub struct GroupNodeIter<'a> {
  84. block: &'a osmformat::PrimitiveBlock,
  85. nodes: std::slice::Iter<'a, osmformat::Node>,
  86. }
  87. impl<'a> GroupNodeIter<'a> {
  88. fn new(block: &'a osmformat::PrimitiveBlock,
  89. group: &'a osmformat::PrimitiveGroup)
  90. -> GroupNodeIter<'a> {
  91. GroupNodeIter {
  92. block: block,
  93. nodes: group.get_nodes().iter(),
  94. }
  95. }
  96. }
  97. impl<'a> Iterator for GroupNodeIter<'a> {
  98. type Item = Node<'a>;
  99. fn next(&mut self) -> Option<Self::Item> {
  100. match self.nodes.next() {
  101. Some(n) => Some(Node::new(self.block, n)),
  102. None => None,
  103. }
  104. }
  105. fn size_hint(&self) -> (usize, Option<usize>) {
  106. self.nodes.size_hint()
  107. }
  108. }
  109. impl<'a> ExactSizeIterator for GroupNodeIter<'a> {}
  110. pub struct GroupWayIter<'a> {
  111. block: &'a osmformat::PrimitiveBlock,
  112. ways: std::slice::Iter<'a, osmformat::Way>,
  113. }
  114. impl<'a> GroupWayIter<'a> {
  115. fn new(block: &'a osmformat::PrimitiveBlock,
  116. group: &'a osmformat::PrimitiveGroup)
  117. -> GroupWayIter<'a> {
  118. GroupWayIter {
  119. block: block,
  120. ways: group.get_ways().iter(),
  121. }
  122. }
  123. }
  124. impl<'a> Iterator for GroupWayIter<'a> {
  125. type Item = Way<'a>;
  126. fn next(&mut self) -> Option<Self::Item> {
  127. match self.ways.next() {
  128. Some(way) => Some(Way::new(self.block, way)),
  129. None => None,
  130. }
  131. }
  132. fn size_hint(&self) -> (usize, Option<usize>) {
  133. self.ways.size_hint()
  134. }
  135. }
  136. impl<'a> ExactSizeIterator for GroupWayIter<'a> {}
  137. pub struct GroupRelationIter<'a> {
  138. block: &'a osmformat::PrimitiveBlock,
  139. rels: std::slice::Iter<'a, osmformat::Relation>,
  140. }
  141. impl<'a> GroupRelationIter<'a> {
  142. fn new(block: &'a osmformat::PrimitiveBlock,
  143. group: &'a osmformat::PrimitiveGroup)
  144. -> GroupRelationIter<'a> {
  145. GroupRelationIter {
  146. block: block,
  147. rels: group.get_relations().iter(),
  148. }
  149. }
  150. }
  151. impl<'a> Iterator for GroupRelationIter<'a> {
  152. type Item = Relation<'a>;
  153. fn next(&mut self) -> Option<Self::Item> {
  154. match self.rels.next() {
  155. Some(rel) => Some(Relation::new(self.block, rel)),
  156. None => None,
  157. }
  158. }
  159. fn size_hint(&self) -> (usize, Option<usize>) {
  160. self.rels.size_hint()
  161. }
  162. }
  163. impl<'a> ExactSizeIterator for GroupRelationIter<'a> {}
  164. pub(crate) fn str_from_stringtable(block: &osmformat::PrimitiveBlock, index: usize) -> Result<&str> {
  165. if let Some(vec) = block.get_stringtable().get_s().get(index) {
  166. std::str::from_utf8(vec)
  167. .chain_err(|| "failed to decode string from string table")
  168. } else {
  169. Err(ErrorKind::StringtableIndexOutOfBounds(index).into())
  170. }
  171. }