-
Notifications
You must be signed in to change notification settings - Fork 26
/
decode.go
72 lines (61 loc) · 1.59 KB
/
decode.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
package postgis
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
)
const (
wkbXDR byte = 0
wkbNDR byte = 1
)
// Helper functions to read the EWKB (well-known binary) from PostGIS
// Format document available at http://trac.osgeo.org/postgis/browser/trunk/doc/ZMSgeoms.txt
// Since Postgres by default returns hex encoded strings we need to first get bytes back, only used from Scan
func decode(value interface{}) (io.Reader, error) {
var ewkb []byte
var err error
switch v := value.(type) {
case string:
// For pgx, decode the hex-encoded string into bytes
ewkb, err = hex.DecodeString(v)
if err != nil {
return nil, err
}
case []byte:
// For lib/pq, cast it to string and decode the hex-encoded string into bytes
ewkb, err = hex.DecodeString(string(v))
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported type: %T", value)
}
return bytes.NewReader(ewkb), nil
}
func readEWKB(reader io.Reader, g Geometry) error {
var byteOrder binary.ByteOrder
var wkbByteOrder byte
var wkbType uint32
// Read as Little Endian to attempt to determine byte order
if err := binary.Read(reader, binary.LittleEndian, &wkbByteOrder); err != nil {
return err
}
// Decide byte order
switch wkbByteOrder {
case wkbXDR:
byteOrder = binary.BigEndian
case wkbNDR:
byteOrder = binary.LittleEndian
default:
return errors.New("unsupported byte order")
}
// Determine the geometery type
if err := binary.Read(reader, byteOrder, &wkbType); err != nil {
return err
}
// Decode into our struct
return binary.Read(reader, byteOrder, g)
}