forked from qedus/osmpbf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
330 lines (282 loc) · 7.06 KB
/
decode.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
// Package osmpbf decodes OpenStreetMap (OSM) PBF files.
// Use this package by creating a NewDecoder and passing it a PBF file.
// Use Start to start decoding process.
// Use Decode to return Node, Way and Relation structs.
package osmpbf
import (
"bytes"
"compress/zlib"
"encoding/binary"
"errors"
"fmt"
"io"
"time"
"github.com/golang/protobuf/proto"
"github.com/qedus/osmpbf/OSMPBF"
)
const (
maxBlobHeaderSize = 64 * 1024
initialBlobBufSize = 1 * 1024 * 1024
// MaxBlobSize is maximum supported blob size.
MaxBlobSize = 32 * 1024 * 1024
)
var (
parseCapabilities = map[string]bool{
"OsmSchema-V0.6": true,
"DenseNodes": true,
}
)
type Info struct {
Version int32
Timestamp time.Time
Changeset int64
Uid int32
User string
Visible bool
}
type Node struct {
ID int64
Lat float64
Lon float64
Tags map[string]string
Info Info
}
type Way struct {
ID int64
Tags map[string]string
NodeIDs []int64
Info Info
}
type Relation struct {
ID int64
Tags map[string]string
Members []Member
Info Info
}
type MemberType int
const (
NodeType MemberType = iota
WayType
RelationType
)
type Member struct {
ID int64
Type MemberType
Role string
}
type pair struct {
i interface{}
e error
}
// A Decoder reads and decodes OpenStreetMap PBF data from an input stream.
type Decoder struct {
r io.Reader
serializer chan pair
buf *bytes.Buffer
// for data decoders
inputs []chan<- pair
outputs []<-chan pair
}
// NewDecoder returns a new decoder that reads from r.
func NewDecoder(r io.Reader) *Decoder {
d := &Decoder{
r: r,
serializer: make(chan pair, 8000), // typical PrimitiveBlock contains 8k OSM entities
}
d.SetBufferSize(initialBlobBufSize)
return d
}
// SetBufferSize sets initial size of decoding buffer. Default value is 1MB, you can set higher value
// (for example, MaxBlobSize) for (probably) faster decoding, or lower value for reduced memory consumption.
// Any value will produce valid results; buffer will grow automatically if required.
func (dec *Decoder) SetBufferSize(n int) {
dec.buf = bytes.NewBuffer(make([]byte, 0, n))
}
// Start decoding process using n goroutines.
func (dec *Decoder) Start(n int) error {
if n < 1 {
n = 1
}
// read OSMHeader
blobHeader, blob, err := dec.readFileBlock()
if err == nil {
if blobHeader.GetType() == "OSMHeader" {
err = decodeOSMHeader(blob)
} else {
err = fmt.Errorf("unexpected first fileblock of type %s", blobHeader.GetType())
}
}
if err != nil {
return err
}
// start data decoders
for i := 0; i < n; i++ {
input := make(chan pair)
output := make(chan pair)
go func() {
dd := new(dataDecoder)
for p := range input {
if p.e == nil {
// send decoded objects or decoding error
objects, err := dd.Decode(p.i.(*OSMPBF.Blob))
output <- pair{objects, err}
} else {
// send input error as is
output <- pair{nil, p.e}
}
}
close(output)
}()
dec.inputs = append(dec.inputs, input)
dec.outputs = append(dec.outputs, output)
}
// start reading OSMData
go func() {
var inputIndex int
for {
input := dec.inputs[inputIndex]
inputIndex = (inputIndex + 1) % n
blobHeader, blob, err = dec.readFileBlock()
if err == nil && blobHeader.GetType() != "OSMData" {
err = fmt.Errorf("unexpected fileblock of type %s", blobHeader.GetType())
}
if err == nil {
// send blob for decoding
input <- pair{blob, nil}
} else {
// send input error as is
input <- pair{nil, err}
for _, input := range dec.inputs {
close(input)
}
return
}
}
}()
go func() {
var outputIndex int
for {
output := dec.outputs[outputIndex]
outputIndex = (outputIndex + 1) % n
p := <-output
if p.i != nil {
// send decoded objects one by one
for _, o := range p.i.([]interface{}) {
dec.serializer <- pair{o, nil}
}
}
if p.e != nil {
// send input or decoding error
dec.serializer <- pair{nil, p.e}
close(dec.serializer)
return
}
}
}()
return nil
}
// Decode reads the next object from the input stream and returns either a
// pointer to Node, Way or Relation struct representing the underlying OpenStreetMap PBF
// data, or error encountered. The end of the input stream is reported by an io.EOF error.
//
// Decode is safe for parallel execution. Only first error encountered will be returned,
// subsequent invocations will return io.EOF.
func (dec *Decoder) Decode() (interface{}, error) {
p, ok := <-dec.serializer
if !ok {
return nil, io.EOF
}
return p.i, p.e
}
func (dec *Decoder) readFileBlock() (*OSMPBF.BlobHeader, *OSMPBF.Blob, error) {
blobHeaderSize, err := dec.readBlobHeaderSize()
if err != nil {
return nil, nil, err
}
blobHeader, err := dec.readBlobHeader(blobHeaderSize)
if err != nil {
return nil, nil, err
}
blob, err := dec.readBlob(blobHeader)
if err != nil {
return nil, nil, err
}
return blobHeader, blob, err
}
func (dec *Decoder) readBlobHeaderSize() (uint32, error) {
dec.buf.Reset()
if _, err := io.CopyN(dec.buf, dec.r, 4); err != nil {
return 0, err
}
size := binary.BigEndian.Uint32(dec.buf.Bytes())
if size >= maxBlobHeaderSize {
return 0, errors.New("BlobHeader size >= 64Kb")
}
return size, nil
}
func (dec *Decoder) readBlobHeader(size uint32) (*OSMPBF.BlobHeader, error) {
dec.buf.Reset()
if _, err := io.CopyN(dec.buf, dec.r, int64(size)); err != nil {
return nil, err
}
blobHeader := new(OSMPBF.BlobHeader)
if err := proto.Unmarshal(dec.buf.Bytes(), blobHeader); err != nil {
return nil, err
}
if blobHeader.GetDatasize() >= MaxBlobSize {
return nil, errors.New("Blob size >= 32Mb")
}
return blobHeader, nil
}
func (dec *Decoder) readBlob(blobHeader *OSMPBF.BlobHeader) (*OSMPBF.Blob, error) {
dec.buf.Reset()
if _, err := io.CopyN(dec.buf, dec.r, int64(blobHeader.GetDatasize())); err != nil {
return nil, err
}
blob := new(OSMPBF.Blob)
if err := proto.Unmarshal(dec.buf.Bytes(), blob); err != nil {
return nil, err
}
return blob, nil
}
func getData(blob *OSMPBF.Blob) ([]byte, error) {
switch {
case blob.Raw != nil:
return blob.GetRaw(), nil
case blob.ZlibData != nil:
r, err := zlib.NewReader(bytes.NewReader(blob.GetZlibData()))
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(make([]byte, 0, blob.GetRawSize()+bytes.MinRead))
_, err = buf.ReadFrom(r)
if err != nil {
return nil, err
}
if buf.Len() != int(blob.GetRawSize()) {
err = fmt.Errorf("raw blob data size %d but expected %d", buf.Len(), blob.GetRawSize())
return nil, err
}
return buf.Bytes(), nil
default:
return nil, errors.New("unknown blob data")
}
}
func decodeOSMHeader(blob *OSMPBF.Blob) error {
data, err := getData(blob)
if err != nil {
return err
}
headerBlock := new(OSMPBF.HeaderBlock)
if err := proto.Unmarshal(data, headerBlock); err != nil {
return err
}
// Check we have the parse capabilities
requiredFeatures := headerBlock.GetRequiredFeatures()
for _, feature := range requiredFeatures {
if !parseCapabilities[feature] {
return fmt.Errorf("parser does not have %s capability", feature)
}
}
return nil
}