forked from qedus/osmpbf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
go_blob_decoder.go
362 lines (313 loc) · 7.81 KB
/
go_blob_decoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package gosmonaut
import (
"bytes"
"errors"
"io"
"github.com/MorbZ/gosmonaut/OSMPBF"
)
// goBlobDecoder uses the official Golang Protobuf package.
// All protobuf messages will be unmarshalled to temporary objects before
// processing.
type goBlobDecoder struct {
q []entityParser
buf *bytes.Buffer
}
func newGoBlobDecoder() *goBlobDecoder {
return &goBlobDecoder{
buf: new(bytes.Buffer),
}
}
func (dec *goBlobDecoder) decode(blob *OSMPBF.Blob, t OSMType) ([]entityParser, OSMTypeSet, error) {
data, err := getBlobData(blob, dec.buf)
if err != nil {
return nil, 0, err
}
primitiveBlock := new(OSMPBF.PrimitiveBlock)
if err := protoOptions.Unmarshal(data, primitiveBlock); err != nil {
return nil, 0, err
}
// Get types
types := dec.getTypes(primitiveBlock)
if !types.Get(t) {
return nil, types, nil
}
// Build entity parsers
dec.q = make([]entityParser, 0, len(primitiveBlock.GetPrimitivegroup()))
err = dec.parsePrimitiveBlock(primitiveBlock, t)
return dec.q, types, err
}
func (dec *goBlobDecoder) getTypes(pb *OSMPBF.PrimitiveBlock) (types OSMTypeSet) {
for _, pg := range pb.GetPrimitivegroup() {
if len(pg.GetNodes()) > 0 || len(pg.GetDense().GetId()) > 0 {
types.Set(NodeType, true)
}
if len(pg.GetWays()) > 0 {
types.Set(WayType, true)
}
if len(pg.GetRelations()) > 0 {
types.Set(RelationType, true)
}
}
return
}
func (dec *goBlobDecoder) parsePrimitiveBlock(pb *OSMPBF.PrimitiveBlock, t OSMType) error {
for _, pg := range pb.GetPrimitivegroup() {
st := stringTable(pb.GetStringtable().GetS())
var parser entityParser
switch t {
case NodeType:
if len(pg.GetNodes()) > 0 {
parser = newGoNodeParser(st, pb, pg.GetNodes())
} else if len(pg.GetDense().GetId()) > 0 {
var err error
parser, err = newGoDenseNodesParser(st, pb, pg.GetDense())
if err != nil {
return err
}
}
case WayType:
parser = newGoWayParser(st, pb, pg.GetWays())
case RelationType:
parser = newGoRelationParser(st, pb, pg.GetRelations())
}
if parser != nil {
dec.q = append(dec.q, parser)
}
}
return nil
}
/* Node Parser */
type goNodeParser struct {
index int
granularity int64
latOffset, lonOffset int64
st stringTable
nodes []*OSMPBF.Node
node *OSMPBF.Node
}
func newGoNodeParser(st stringTable, pb *OSMPBF.PrimitiveBlock, nodes []*OSMPBF.Node) *goNodeParser {
return &goNodeParser{
granularity: int64(pb.GetGranularity()),
latOffset: pb.GetLatOffset(),
lonOffset: pb.GetLonOffset(),
st: st,
nodes: nodes,
}
}
func (d *goNodeParser) next() (id int64, tags OSMTags, err error) {
if d.index >= len(d.nodes) {
err = io.EOF
return
}
node := d.nodes[d.index]
id = node.GetId()
tags, err = extractTags(d.st, node.GetKeys(), node.GetVals())
d.node = node
d.index++
return
}
func (d *goNodeParser) coords() (lat, lon float64, err error) {
lat = decodeCoord(d.latOffset, d.granularity, d.node.GetLat())
lon = decodeCoord(d.latOffset, d.granularity, d.node.GetLon())
return
}
/* Dense Nodes Parser */
type goDenseNodesParser struct {
index int
id, lat, lon int64
ids []int64
granularity int64
latOffset, lonOffset int64
lats, lons []int64
tu *tagUnpacker
}
func newGoDenseNodesParser(st stringTable, pb *OSMPBF.PrimitiveBlock, dn *OSMPBF.DenseNodes) (*goDenseNodesParser, error) {
ids := dn.GetId()
lats := dn.GetLat()
lons := dn.GetLon()
if len(ids) != len(lats) || len(ids) != len(lons) {
return nil, errors.New("length of dense nodes ids, lat and lons differs")
}
return &goDenseNodesParser{
granularity: int64(pb.GetGranularity()),
latOffset: pb.GetLatOffset(),
lonOffset: pb.GetLonOffset(),
ids: ids,
lats: lats,
lons: lons,
tu: newTagUnpacker(st, dn.GetKeysVals()),
}, nil
}
func (d *goDenseNodesParser) next() (id int64, tags OSMTags, err error) {
if d.index >= len(d.ids) {
err = io.EOF
return
}
d.id = d.ids[d.index] + d.id
id = d.id
d.lat = d.lats[d.index] + d.lat
d.lon = d.lons[d.index] + d.lon
tags, err = d.tu.next()
d.index++
return
}
func (d *goDenseNodesParser) coords() (lat, lon float64, err error) {
lat = decodeCoord(d.latOffset, d.granularity, d.lat)
lon = decodeCoord(d.lonOffset, d.granularity, d.lon)
return
}
// tagUnpacker creates a tags map from a stringtable and array of IDs (used in
// DenseNodes encoding).
type tagUnpacker struct {
st stringTable
keysVals []int32
index int
}
func newTagUnpacker(st stringTable, keyVals []int32) *tagUnpacker {
return &tagUnpacker{
st: st,
keysVals: keyVals,
}
}
func (tu *tagUnpacker) next() (OSMTags, error) {
// Tags are optional
if len(tu.keysVals) == 0 {
return nil, nil
}
// Count strings to make an correctly sized slice
if tu.index >= len(tu.keysVals) {
return nil, errors.New("dense nodes tag index is out of bounds")
}
n := 0
for i := tu.index; i < len(tu.keysVals); i++ {
if tu.keysVals[i] == 0 {
break
}
n++
}
if n%2 != 0 {
return nil, errors.New("dense nodes tag value is zero")
}
// Get tags
c := n / 2
var tags OSMTags = NewOSMTags(c)
for i := 0; i < c; i++ {
// Get next string
get := func() (string, error) {
id := tu.keysVals[tu.index]
tu.index++
return tu.st.get(int(id))
}
// Make tag
var key, val string
var err error
if key, err = get(); err != nil {
return nil, err
}
if val, err = get(); err != nil {
return nil, err
}
tags.Set(key, val)
}
tu.index++ // Skip the zero
return tags, nil
}
/* Way Parser */
type goWayParser struct {
index int
st stringTable
ways []*OSMPBF.Way
way *OSMPBF.Way
}
func newGoWayParser(st stringTable, pb *OSMPBF.PrimitiveBlock, ways []*OSMPBF.Way) *goWayParser {
return &goWayParser{
st: st,
ways: ways,
}
}
func (d *goWayParser) next() (id int64, tags OSMTags, err error) {
if d.index >= len(d.ways) {
err = io.EOF
return
}
way := d.ways[d.index]
id = way.GetId()
tags, err = extractTags(d.st, way.GetKeys(), way.GetVals())
d.way = way
d.index++
return
}
func (d *goWayParser) refs() ([]int64, error) {
protoIDs := d.way.GetRefs()
ids := make([]int64, len(protoIDs))
var id int64
for i, protoID := range protoIDs {
id += protoID // delta encoding
ids[i] = id
}
return ids, nil
}
/* Relation Parser */
type goRelationParser struct {
index int
st stringTable
relations []*OSMPBF.Relation
relation *OSMPBF.Relation
}
func newGoRelationParser(st stringTable, pb *OSMPBF.PrimitiveBlock, relations []*OSMPBF.Relation) *goRelationParser {
return &goRelationParser{
st: st,
relations: relations,
}
}
func (d *goRelationParser) next() (id int64, tags OSMTags, err error) {
if d.index >= len(d.relations) {
err = io.EOF
return
}
rel := d.relations[d.index]
id = rel.GetId()
tags, err = extractTags(d.st, rel.GetKeys(), rel.GetVals())
d.relation = rel
d.index++
return
}
func (d *goRelationParser) ids() ([]int64, error) {
protoIDs := d.relation.GetMemids()
ids := make([]int64, len(protoIDs))
var id int64
for i, protoID := range protoIDs {
id += protoID
ids[i] = id
}
return ids, nil
}
func (d *goRelationParser) roles() ([]string, error) {
protoRoles := d.relation.GetRolesSid()
roles := make([]string, len(protoRoles))
for i, protoRole := range protoRoles {
if role, err := d.st.get(int(protoRole)); err == nil {
roles[i] = role
} else {
return nil, err
}
}
return roles, nil
}
func (d *goRelationParser) types() ([]OSMType, error) {
protoTypes := d.relation.GetTypes()
types := make([]OSMType, len(protoTypes))
for i, protoT := range protoTypes {
var t OSMType
switch protoT {
case OSMPBF.Relation_NODE:
t = NodeType
case OSMPBF.Relation_WAY:
t = WayType
case OSMPBF.Relation_RELATION:
t = RelationType
}
types[i] = t
}
return types, nil
}