Skip to content

Commit 2d49b3c

Browse files
committed
encoding/wkb: update readme for new mysql functionality
1 parent 5e1efdd commit 2d49b3c

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

encoding/wkb/README.md

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,59 @@
1-
encoding/wkb [![Godoc Reference](https://godoc.org/github.com/paulmach/orb?status.svg)](https://godoc.org/github.com/paulmach/orb/encoding/wkb)
2-
============
1+
# encoding/wkb [![Godoc Reference](https://godoc.org/github.com/paulmach/orb?status.svg)](https://godoc.org/github.com/paulmach/orb/encoding/wkb)
32

43
This package provides encoding and decoding of [WKB](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary)
54
data. The interface is defined as:
65

7-
func Marshal(geom orb.Geometry, byteOrder ...binary.ByteOrder) ([]byte, error)
8-
func MustMarshal(geom orb.Geometry, byteOrder ...binary.ByteOrder) []byte
6+
func Marshal(geom orb.Geometry, byteOrder ...binary.ByteOrder) ([]byte, error)
7+
func MustMarshal(geom orb.Geometry, byteOrder ...binary.ByteOrder) []byte
98

10-
func NewEncoder(w io.Writer) *Encoder
11-
func (e *Encoder) SetByteOrder(bo binary.ByteOrder)
12-
func (e *Encoder) Encode(geom orb.Geometry) error
9+
func NewEncoder(w io.Writer) *Encoder
10+
func (e *Encoder) SetByteOrder(bo binary.ByteOrder)
11+
func (e *Encoder) Encode(geom orb.Geometry) error
1312

14-
func Unmarshal(b []byte) (orb.Geometry, error)
13+
func Unmarshal(b []byte) (orb.Geometry, error)
1514

16-
func NewDecoder(r io.Reader) *Decoder
17-
func (d *Decoder) Decode() (orb.Geometry, error)
15+
func NewDecoder(r io.Reader) *Decoder
16+
func (d *Decoder) Decode() (orb.Geometry, error)
1817

19-
### Reading and Writing to a SQL database
18+
## Reading and Writing to a SQL database
2019

2120
This package provides wrappers for `orb.Geometry` types that implement
2221
`sql.Scanner` and `driver.Value`. For example:
2322

24-
row := db.QueryRow("SELECT ST_AsBinary(point_column) FROM postgis_table")
23+
```go
24+
row := db.QueryRow("SELECT ST_AsBinary(point_column) FROM postgis_table")
2525

26-
var p orb.Point
27-
err := row.Scan(wkb.Scanner(&p))
26+
var p orb.Point
27+
err := row.Scan(wkb.Scanner(&p))
2828

29-
db.Exec("INSERT INTO table (point_column) VALUES (?)",
30-
wkb.Value(p))
29+
db.Exec("INSERT INTO table (point_column) VALUES (?)", wkb.Value(p))
30+
```
3131

3232
If you don't know the type of the geometry try something like
3333

34-
s := wkb.Scanner(nil)
35-
err := row.Scan(&s)
34+
```go
35+
s := wkb.Scanner(nil)
36+
err := row.Scan(&s)
3637

37-
switch g := s.Geometry.(type) {
38-
case orb.Point:
39-
case orb.LineString:
40-
}
38+
switch g := s.Geometry.(type) {
39+
case orb.Point:
40+
case orb.LineString:
41+
}
42+
```
4143

42-
Scanning directly from MySQL columns is supported. By default MySQL returns geometry
43-
data as WKB but prefixed with a 4 byte SRID. To support this, if the data is not
44-
valid WKB, the code will strip the first 4 bytes, the SRID, and try again.
45-
This works for most use cases.
44+
## MySQL or MariaDB
45+
46+
By default MySQL returns geometry data as WKB but prefixed with a 4 byte SRID.
47+
This is supported using the `MySQLScanner` and `MySQLValue`. For example:
48+
49+
```go
50+
db.Exec("INSERT INTO geotest(id, geom) VALUES (?, ?)", 1, wkb.MySQLValue(orb.Point{1, 1}))
51+
```
52+
53+
Reading of MySQL point data is supported using the regular scanner but other types may have issues.
54+
For best results use `MySQLScanner` to handle the 4 byte SRID prefix.
55+
56+
```go
57+
var p orb.Point
58+
rows, err := db.QueryRow("SELECT geom FROM geotest").Scan(wkb.MySQLScanner(p))
59+
```

0 commit comments

Comments
 (0)