Skip to content

Commit 3aa9785

Browse files
committed
encoding/e?wkb: add *ToHex methods
1 parent 826a51a commit 3aa9785

File tree

7 files changed

+110
-0
lines changed

7 files changed

+110
-0
lines changed

encoding/ewkb/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ The interface is defined as:
88

99
```go
1010
func Marshal(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) ([]byte, error)
11+
func MarshalToHex(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) (string, error)
1112
func MustMarshal(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) []byte
13+
func MustMarshalToHex(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) string
1214

1315
func NewEncoder(w io.Writer) *Encoder
1416
func (e *Encoder) SetByteOrder(bo binary.ByteOrder) *Encoder

encoding/ewkb/point_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,37 @@ func TestPoint(t *testing.T) {
4747
}
4848
}
4949

50+
func TestPointToHex(t *testing.T) {
51+
cases := []struct {
52+
name string
53+
srid int
54+
data orb.Point
55+
expected string
56+
}{
57+
{
58+
name: "point",
59+
srid: 4326,
60+
data: orb.Point{1, 2},
61+
expected: "0101000020e6100000000000000000f03f0000000000000040",
62+
},
63+
{
64+
name: "zero point",
65+
srid: 4,
66+
data: orb.Point{0, 0},
67+
expected: "01010000200400000000000000000000000000000000000000",
68+
},
69+
}
70+
71+
for _, tc := range cases {
72+
t.Run(tc.name, func(t *testing.T) {
73+
s := MustMarshalToHex(tc.data, tc.srid)
74+
if s != tc.expected {
75+
t.Errorf("incorrect hex: %v", s)
76+
}
77+
})
78+
}
79+
}
80+
5081
func TestMultiPoint(t *testing.T) {
5182
large := orb.MultiPoint{}
5283
for i := 0; i < wkbcommon.MaxPointsAlloc+100; i++ {

encoding/ewkb/scanner.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ func Scanner(g interface{}) *GeometryScanner {
7373
// var p orb.Point
7474
// err := db.QueryRow("SELECT ST_SRID(latlon), ST_AsBinary(latlon) FROM foo WHERE id=?", id).
7575
// Scan(&srid, wkb.Scanner(&p))
76+
//
77+
// https://dev.mysql.com/doc/refman/5.7/en/gis-data-formats.html
7678
func ScannerPrefixSRID(g interface{}) *GeometryScanner {
7779
return &GeometryScanner{sridInPrefix: true, g: g}
7880
}

encoding/ewkb/wkb.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ewkb
33
import (
44
"bytes"
55
"encoding/binary"
6+
"encoding/hex"
67
"errors"
78
"io"
89

@@ -71,6 +72,7 @@ func MustMarshal(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) []b
7172
}
7273

7374
// Marshal encodes the geometry with the given byte order.
75+
// An SRID of 0 will not be included in the encoding and the result will be a wkb encoding of the geometry.
7476
func Marshal(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) ([]byte, error) {
7577
buf := bytes.NewBuffer(make([]byte, 0, wkbcommon.GeomLength(geom, srid != 0)))
7678

@@ -93,6 +95,27 @@ func Marshal(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) ([]byte
9395
return buf.Bytes(), nil
9496
}
9597

98+
// MarshalToHex will encode the geometry into a hex string representation of the binary ewkb.
99+
func MarshalToHex(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) (string, error) {
100+
data, err := Marshal(geom, srid, byteOrder...)
101+
if err != nil {
102+
return "", err
103+
}
104+
105+
return hex.EncodeToString(data), nil
106+
}
107+
108+
// MustMarshalToHex will encode the geometry and panic on error.
109+
// Currently there is no reason to error during geometry marshalling.
110+
func MustMarshalToHex(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) string {
111+
d, err := MarshalToHex(geom, srid, byteOrder...)
112+
if err != nil {
113+
panic(err)
114+
}
115+
116+
return d
117+
}
118+
96119
// NewEncoder creates a new Encoder for the given writer.
97120
func NewEncoder(w io.Writer) *Encoder {
98121
e := wkbcommon.NewEncoder(w)

encoding/wkb/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ data. The interface is defined as:
55

66
```go
77
func Marshal(geom orb.Geometry, byteOrder ...binary.ByteOrder) ([]byte, error)
8+
func MarshalToHex(geom orb.Geometry, byteOrder ...binary.ByteOrder) (string, error)
89
func MustMarshal(geom orb.Geometry, byteOrder ...binary.ByteOrder) []byte
10+
func MustMarshalToHex(geom orb.Geometry, byteOrder ...binary.ByteOrder) string
911

1012
func NewEncoder(w io.Writer) *Encoder
1113
func (e *Encoder) SetByteOrder(bo binary.ByteOrder)

encoding/wkb/point_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ func TestPoint(t *testing.T) {
5252
}
5353
}
5454

55+
func TestPointToHex(t *testing.T) {
56+
cases := []struct {
57+
name string
58+
data orb.Point
59+
expected string
60+
}{
61+
{
62+
name: "point",
63+
data: orb.Point{1, 2},
64+
expected: "0101000000000000000000f03f0000000000000040",
65+
},
66+
{
67+
name: "zero point",
68+
data: orb.Point{0, 0},
69+
expected: "010100000000000000000000000000000000000000",
70+
},
71+
}
72+
73+
for _, tc := range cases {
74+
t.Run(tc.name, func(t *testing.T) {
75+
s := MustMarshalToHex(tc.data)
76+
if s != tc.expected {
77+
t.Errorf("incorrect hex: %v", s)
78+
}
79+
})
80+
}
81+
}
82+
5583
var (
5684
testMultiPoint = orb.MultiPoint{{10, 40}, {40, 30}, {20, 20}, {30, 10}}
5785
testMultiPointData = []byte{

encoding/wkb/wkb.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package wkb
55
import (
66
"bytes"
77
"encoding/binary"
8+
"encoding/hex"
89
"errors"
910
"io"
1011

@@ -88,6 +89,27 @@ func Marshal(geom orb.Geometry, byteOrder ...binary.ByteOrder) ([]byte, error) {
8889
return buf.Bytes(), nil
8990
}
9091

92+
// MarshalToHex will encode the geometry into a hex string representation of the binary wkb.
93+
func MarshalToHex(geom orb.Geometry, byteOrder ...binary.ByteOrder) (string, error) {
94+
data, err := Marshal(geom, byteOrder...)
95+
if err != nil {
96+
return "", err
97+
}
98+
99+
return hex.EncodeToString(data), nil
100+
}
101+
102+
// MustMarshalToHex will encode the geometry and panic on error.
103+
// Currently there is no reason to error during geometry marshalling.
104+
func MustMarshalToHex(geom orb.Geometry, byteOrder ...binary.ByteOrder) string {
105+
d, err := MarshalToHex(geom, byteOrder...)
106+
if err != nil {
107+
panic(err)
108+
}
109+
110+
return d
111+
}
112+
91113
// NewEncoder creates a new Encoder for the given writer.
92114
func NewEncoder(w io.Writer) *Encoder {
93115
e := wkbcommon.NewEncoder(w)

0 commit comments

Comments
 (0)