forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon.go
170 lines (141 loc) · 3.31 KB
/
polygon.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
package wkb
import (
"errors"
"io"
"math"
"github.com/dadadamarine/orb"
)
func unmarshalPolygon(order byteOrder, data []byte) (orb.Polygon, error) {
if len(data) < 4 {
return nil, ErrNotWKB
}
num := unmarshalUint32(order, data)
data = data[4:]
alloc := num
if alloc > maxMultiAlloc {
// invalid data can come in here and allocate tons of memory.
alloc = maxMultiAlloc
}
result := make(orb.Polygon, 0, alloc)
for i := 0; i < int(num); i++ {
ps, err := unmarshalPoints(order, data)
if err != nil {
return nil, err
}
data = data[16*len(ps)+4:]
result = append(result, orb.Ring(ps))
}
return result, nil
}
func readPolygon(r io.Reader, order byteOrder, buf []byte) (orb.Polygon, error) {
num, err := readUint32(r, order, buf[:4])
if err != nil {
return nil, err
}
alloc := num
if alloc > maxMultiAlloc {
// invalid data can come in here and allocate tons of memory.
alloc = maxMultiAlloc
}
result := make(orb.Polygon, 0, alloc)
for i := 0; i < int(num); i++ {
ls, err := readLineString(r, order, buf)
if err != nil {
return nil, err
}
result = append(result, orb.Ring(ls))
}
return result, nil
}
func (e *Encoder) writePolygon(p orb.Polygon) error {
e.order.PutUint32(e.buf, polygonType)
e.order.PutUint32(e.buf[4:], uint32(len(p)))
_, err := e.w.Write(e.buf[:8])
if err != nil {
return err
}
for _, r := range p {
e.order.PutUint32(e.buf, uint32(len(r)))
_, err := e.w.Write(e.buf[:4])
if err != nil {
return err
}
for _, p := range r {
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 unmarshalMultiPolygon(order byteOrder, data []byte) (orb.MultiPolygon, error) {
if len(data) < 4 {
return nil, ErrNotWKB
}
num := unmarshalUint32(order, data)
data = data[4:]
alloc := num
if alloc > maxMultiAlloc {
// invalid data can come in here and allocate tons of memory.
alloc = maxMultiAlloc
}
result := make(orb.MultiPolygon, 0, alloc)
for i := 0; i < int(num); i++ {
p, err := scanPolygon(data)
if err != nil {
return nil, err
}
l := 9
for _, r := range p {
l += 4 + 16*len(r)
}
data = data[l:]
result = append(result, p)
}
return result, nil
}
func readMultiPolygon(r io.Reader, order byteOrder, buf []byte) (orb.MultiPolygon, error) {
num, err := readUint32(r, order, buf[:4])
if err != nil {
return nil, err
}
alloc := num
if alloc > maxMultiAlloc {
// invalid data can come in here and allocate tons of memory.
alloc = maxMultiAlloc
}
result := make(orb.MultiPolygon, 0, alloc)
for i := 0; i < int(num); i++ {
pOrder, typ, err := readByteOrderType(r, buf)
if err != nil {
return nil, err
}
if typ != polygonType {
return nil, errors.New("expect multipolygons to contains polygons, did not find a polygon")
}
p, err := readPolygon(r, pOrder, buf)
if err != nil {
return nil, err
}
result = append(result, p)
}
return result, nil
}
func (e *Encoder) writeMultiPolygon(mp orb.MultiPolygon) error {
e.order.PutUint32(e.buf, multiPolygonType)
e.order.PutUint32(e.buf[4:], uint32(len(mp)))
_, err := e.w.Write(e.buf[:8])
if err != nil {
return err
}
for _, p := range mp {
err := e.Encode(p)
if err != nil {
return err
}
}
return nil
}