-
Notifications
You must be signed in to change notification settings - Fork 105
/
wkb.go
162 lines (131 loc) · 4.22 KB
/
wkb.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
// Package wkb is for decoding ESRI's Well Known Binary (WKB) format
// sepcification at https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary
package wkb
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"io"
"github.com/paulmach/orb"
"github.com/paulmach/orb/encoding/internal/wkbcommon"
)
var (
// ErrUnsupportedDataType is returned by Scan methods when asked to scan
// non []byte data from the database. This should never happen
// if the driver is acting appropriately.
ErrUnsupportedDataType = errors.New("wkb: scan value must be []byte")
// ErrNotWKB is returned when unmarshalling WKB and the data is not valid.
ErrNotWKB = errors.New("wkb: invalid data")
// ErrIncorrectGeometry is returned when unmarshalling WKB data into the wrong type.
// For example, unmarshaling linestring data into a point.
ErrIncorrectGeometry = errors.New("wkb: incorrect geometry")
// ErrUnsupportedGeometry is returned when geometry type is not supported by this lib.
ErrUnsupportedGeometry = errors.New("wkb: unsupported geometry")
)
var commonErrorMap = map[error]error{
wkbcommon.ErrUnsupportedDataType: ErrUnsupportedDataType,
wkbcommon.ErrNotWKB: ErrNotWKB,
wkbcommon.ErrNotWKBHeader: ErrNotWKB,
wkbcommon.ErrIncorrectGeometry: ErrIncorrectGeometry,
wkbcommon.ErrUnsupportedGeometry: ErrUnsupportedGeometry,
}
func mapCommonError(err error) error {
e, ok := commonErrorMap[err]
if ok {
return e
}
return err
}
// DefaultByteOrder is the order used for marshalling or encoding
// is none is specified.
var DefaultByteOrder binary.ByteOrder = binary.LittleEndian
// An Encoder will encode a geometry as WKB to the writer given at
// creation time.
type Encoder struct {
e *wkbcommon.Encoder
}
// MustMarshal will encode the geometry and panic on error.
// Currently there is no reason to error during geometry marshalling.
func MustMarshal(geom orb.Geometry, byteOrder ...binary.ByteOrder) []byte {
d, err := Marshal(geom, byteOrder...)
if err != nil {
panic(err)
}
return d
}
// Marshal encodes the geometry with the given byte order.
func Marshal(geom orb.Geometry, byteOrder ...binary.ByteOrder) ([]byte, error) {
buf := bytes.NewBuffer(make([]byte, 0, wkbcommon.GeomLength(geom, false)))
e := NewEncoder(buf)
if len(byteOrder) > 0 {
e.SetByteOrder(byteOrder[0])
}
err := e.Encode(geom)
if err != nil {
return nil, err
}
if buf.Len() == 0 {
return nil, nil
}
return buf.Bytes(), nil
}
// MarshalToHex will encode the geometry into a hex string representation of the binary wkb.
func MarshalToHex(geom orb.Geometry, byteOrder ...binary.ByteOrder) (string, error) {
data, err := Marshal(geom, byteOrder...)
if err != nil {
return "", err
}
return hex.EncodeToString(data), nil
}
// MustMarshalToHex will encode the geometry and panic on error.
// Currently there is no reason to error during geometry marshalling.
func MustMarshalToHex(geom orb.Geometry, byteOrder ...binary.ByteOrder) string {
d, err := MarshalToHex(geom, byteOrder...)
if err != nil {
panic(err)
}
return d
}
// NewEncoder creates a new Encoder for the given writer.
func NewEncoder(w io.Writer) *Encoder {
e := wkbcommon.NewEncoder(w)
e.SetByteOrder(DefaultByteOrder)
return &Encoder{e: e}
}
// SetByteOrder will override the default byte order set when
// the encoder was created.
func (e *Encoder) SetByteOrder(bo binary.ByteOrder) *Encoder {
e.e.SetByteOrder(bo)
return e
}
// Encode will write the geometry encoded as WKB to the given writer.
func (e *Encoder) Encode(geom orb.Geometry) error {
return e.e.Encode(geom, 0)
}
// Decoder can decoder WKB geometry off of the stream.
type Decoder struct {
d *wkbcommon.Decoder
}
// Unmarshal will decode the type into a Geometry.
func Unmarshal(data []byte) (orb.Geometry, error) {
g, _, err := wkbcommon.Unmarshal(data)
if err != nil {
return nil, mapCommonError(err)
}
return g, nil
}
// NewDecoder will create a new WKB decoder.
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{
d: wkbcommon.NewDecoder(r),
}
}
// Decode will decode the next geometry off of the stream.
func (d *Decoder) Decode() (orb.Geometry, error) {
g, _, err := d.d.Decode()
if err != nil {
return nil, mapCommonError(err)
}
return g, nil
}