forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line_string.go
105 lines (85 loc) · 2.02 KB
/
line_string.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
package wkb
import (
"encoding/binary"
"errors"
"io"
"math"
"github.com/paulmach/orb"
)
func readLineString(r io.Reader, bom binary.ByteOrder) (orb.LineString, error) {
var num uint32
if err := binary.Read(r, bom, &num); err != nil {
return nil, err
}
if num > maxPointsAlloc {
// invalid data can come in here and allocate tons of memory.
num = maxPointsAlloc
}
result := make(orb.LineString, 0, num)
for i := 0; i < int(num); i++ {
p, err := readPoint(r, bom)
if err != nil {
return nil, err
}
result = append(result, p)
}
return result, nil
}
func (e *Encoder) writeLineString(ls orb.LineString) error {
e.order.PutUint32(e.buf, lineStringType)
e.order.PutUint32(e.buf[4:], uint32(len(ls)))
_, err := e.w.Write(e.buf[:8])
if err != nil {
return err
}
for _, p := range ls {
e.order.PutUint64(e.buf, math.Float64bits(p[0]))
e.order.PutUint64(e.buf[8:], math.Float64bits(p[1]))
_, err = e.w.Write(e.buf)
if err != nil {
return err
}
}
return nil
}
func readMultiLineString(r io.Reader, bom binary.ByteOrder) (orb.MultiLineString, 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.MultiLineString, 0, num)
for i := 0; i < int(num); i++ {
byteOrder, typ, err := readByteOrderType(r)
if err != nil {
return nil, err
}
if typ != lineStringType {
return nil, errors.New("expect multilines to contains lines, did not find a line")
}
ls, err := readLineString(r, byteOrder)
if err != nil {
return nil, err
}
result = append(result, ls)
}
return result, nil
}
func (e *Encoder) writeMultiLineString(mls orb.MultiLineString) error {
e.order.PutUint32(e.buf, multiLineStringType)
e.order.PutUint32(e.buf[4:], uint32(len(mls)))
_, err := e.w.Write(e.buf[:8])
if err != nil {
return err
}
for _, ls := range mls {
err := e.Encode(ls)
if err != nil {
return err
}
}
return nil
}