forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon.go
110 lines (92 loc) · 2.14 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
package wkb
import (
"encoding/binary"
"errors"
"io"
"math"
"github.com/paulmach/orb"
)
func readPolygon(r io.Reader, bom binary.ByteOrder) (orb.Polygon, 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.Polygon, 0, num)
for i := 0; i < int(num); i++ {
ls, err := readLineString(r, bom)
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 readMultiPolygon(r io.Reader, bom binary.ByteOrder) (orb.MultiPolygon, 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.MultiPolygon, 0, num)
for i := 0; i < int(num); i++ {
byteOrder, typ, err := readByteOrderType(r)
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, byteOrder)
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
}