gistools/readers/osm/
primitive.rs1use super::{
2 info::InfoBlock,
3 node::{DenseNodes, Node},
4 relation::{IntermediateNodeMember, MemberType, Relation},
5 way::Way,
6};
7use crate::data_structures::HasLayer;
8use alloc::{string::String, vec::Vec};
9use pbf::{ProtoRead, Protobuf};
10use s2json::Properties;
11
12#[derive(Debug, Clone, PartialEq)]
14pub struct OSMMetadataRelation {
15 pub role: String,
17 pub properties: Properties,
19}
20
21#[derive(Debug, Default, Clone, PartialEq)]
23pub struct OSMMetadata {
24 pub osm_type: MemberType,
26 pub info: Option<InfoBlock>,
28 pub nodes: Option<Vec<IntermediateNodeMember>>,
30 pub relation: Option<OSMMetadataRelation>,
32}
33impl HasLayer for OSMMetadata {
34 fn get_layer(&self) -> Option<String> {
35 None
36 }
37}
38
39#[derive(Debug)]
48pub struct PrimitiveBlock {
49 stringtable: StringTable,
50 pub primitive_groups: Vec<PrimitiveGroup>,
52 pub granularity: i32,
54 pub lat_offset: i64,
56 pub lon_offset: i64,
58 pub date_granularity: i32,
60}
61impl Default for PrimitiveBlock {
62 fn default() -> Self {
63 Self {
64 stringtable: StringTable::default(),
65 primitive_groups: Vec::new(),
66 granularity: 100,
67 lat_offset: 0,
68 lon_offset: 0,
69 date_granularity: 1000,
70 }
71 }
72}
73impl PrimitiveBlock {
74 pub fn get_string(&self, index: usize) -> &str {
76 self.stringtable.get(index)
77 }
78
79 pub fn tags(&self, keys: &[u32], values: &[u32]) -> Properties {
81 let mut res = Properties::default();
82 for (key, value) in keys.iter().zip(values.iter()) {
84 let key = self.get_string(*key as usize);
85 let value = self.get_string(*value as usize);
86 res.insert(key.into(), value.into());
87 }
88 res
89 }
90}
91impl ProtoRead for PrimitiveBlock {
93 fn read(&mut self, tag: u64, pb: &mut Protobuf) {
94 match tag {
95 1 => pb.read_message(&mut self.stringtable),
96 2 => {
97 let mut group = PrimitiveGroup::default();
98 pb.read_message(&mut group);
99 self.primitive_groups.push(group);
100 }
101 17 => self.granularity = pb.read_varint(),
102 18 => self.date_granularity = pb.read_varint(),
103 19 => self.lat_offset = pb.read_varint(),
104 20 => self.lon_offset = pb.read_varint(),
105 _ => panic!("unknown tag {}", tag),
106 }
107 }
108}
109
110#[derive(Debug, Default)]
112pub struct PrimitiveGroup {
113 pub nodes: Vec<Node>,
115 pub ways: Vec<Way>,
117 pub relations: Vec<Relation>,
119 pub changesets: Vec<ChangeSet>,
121}
122impl ProtoRead for PrimitiveGroup {
124 fn read(&mut self, tag: u64, pb: &mut Protobuf) {
125 match tag {
126 1 => {
127 let mut node = Node::default();
128 pb.read_message(&mut node);
129 self.nodes.push(node);
130 }
131 2 => {
132 let mut dense_nodes = DenseNodes::default();
133 pb.read_message(&mut dense_nodes);
134 self.nodes.extend(dense_nodes.nodes());
135 }
136 3 => {
137 let mut way = Way::default();
138 pb.read_message(&mut way);
139 self.ways.push(way);
140 }
141 4 => {
142 let mut relation = Relation::default();
143 pb.read_message(&mut relation);
144 self.relations.push(relation);
145 }
146 5 => {
147 let mut changeset = ChangeSet::default();
148 pb.read_message(&mut changeset);
149 self.changesets.push(changeset);
150 }
151 _ => panic!("unknown tag {}", tag),
152 }
153 }
154}
155
156#[derive(Debug, Default)]
162pub struct StringTable {
163 strings: Vec<String>,
164 empty_string: String,
165}
166impl StringTable {
167 pub fn get(&self, index: usize) -> &str {
169 self.strings.get(index).unwrap_or(&self.empty_string)
170 }
171}
172impl ProtoRead for StringTable {
174 fn read(&mut self, tag: u64, pb: &mut Protobuf) {
175 match tag {
176 1 => self.strings.push(pb.read_string()),
177 _ => panic!("unknown tag {}", tag),
178 }
179 }
180}
181
182#[derive(Debug, Default)]
184pub struct ChangeSet {
185 pub id: i64,
187}
188impl ProtoRead for ChangeSet {
190 fn read(&mut self, tag: u64, pb: &mut Protobuf) {
191 match tag {
192 1 => self.id = pb.read_varint(),
193 _ => panic!("unknown tag {}", tag),
194 }
195 }
196}