This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.go
89 lines (71 loc) · 1.69 KB
/
point.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
package wkb
import (
"encoding/binary"
"errors"
"io"
"math"
"github.com/planetfederal/orb"
)
func readPoint(r io.Reader, bom binary.ByteOrder) (orb.Point, error) {
var p orb.Point
if err := binary.Read(r, bom, &p[0]); err != nil {
return orb.Point{}, err
}
if err := binary.Read(r, bom, &p[1]); err != nil {
return orb.Point{}, err
}
return p, nil
}
func (e *Encoder) writePoint(p orb.Point) error {
e.order.PutUint32(e.buf, pointType)
_, err := e.w.Write(e.buf[:4])
if err != nil {
return err
}
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)
return err
}
func readMultiPoint(r io.Reader, bom binary.ByteOrder) (orb.MultiPoint, error) {
var num uint32 // Number of points.
if err := binary.Read(r, bom, &num); err != nil {
return nil, err
}
alloc := num
if alloc > maxPointsAlloc {
// invalid data can come in here and allocate tons of memory.
alloc = maxPointsAlloc
}
result := make(orb.MultiPoint, 0, alloc)
for i := 0; i < int(num); i++ {
byteOrder, typ, err := readByteOrderType(r)
if err != nil {
return nil, err
}
if typ != pointType {
return nil, errors.New("expect multipoint to contains points, did not find a point")
}
p, err := readPoint(r, byteOrder)
if err != nil {
return nil, err
}
result = append(result, p)
}
return result, nil
}
func (e *Encoder) writeMultiPoint(mp orb.MultiPoint) error {
e.order.PutUint32(e.buf, multiPointType)
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
}