forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.go
50 lines (40 loc) · 902 Bytes
/
collection.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
package wkb
import (
"encoding/binary"
"io"
"github.com/paulmach/orb"
)
func readCollection(r io.Reader, bom binary.ByteOrder) (orb.Collection, error) {
var num uint32
if err := binary.Read(r, bom, &num); err != nil {
return nil, err
}
if num > maxMultiAlloc {
// invalid data can come in here and allocate tons of memory.
num = maxMultiAlloc
}
result := make(orb.Collection, 0, num)
for i := 0; i < int(num); i++ {
geom, err := NewDecoder(r).Decode()
if err != nil {
return nil, err
}
result = append(result, geom)
}
return result, nil
}
func (e *Encoder) writeCollection(c orb.Collection) error {
e.order.PutUint32(e.buf, geometryCollectionType)
e.order.PutUint32(e.buf[4:], uint32(len(c)))
_, err := e.w.Write(e.buf[:8])
if err != nil {
return err
}
for _, geom := range c {
err := e.Encode(geom)
if err != nil {
return err
}
}
return nil
}