|
1 | | -encoding/wkb [](https://godoc.org/github.com/paulmach/orb/encoding/wkb) |
2 | | -============ |
| 1 | +# encoding/wkb [](https://godoc.org/github.com/paulmach/orb/encoding/wkb) |
3 | 2 |
|
4 | 3 | This package provides encoding and decoding of [WKB](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary) |
5 | 4 | data. The interface is defined as: |
6 | 5 |
|
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 |
9 | 8 |
|
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 |
13 | 12 |
|
14 | | - func Unmarshal(b []byte) (orb.Geometry, error) |
| 13 | + func Unmarshal(b []byte) (orb.Geometry, error) |
15 | 14 |
|
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) |
18 | 17 |
|
19 | | -### Reading and Writing to a SQL database |
| 18 | +## Reading and Writing to a SQL database |
20 | 19 |
|
21 | 20 | This package provides wrappers for `orb.Geometry` types that implement |
22 | 21 | `sql.Scanner` and `driver.Value`. For example: |
23 | 22 |
|
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") |
25 | 25 |
|
26 | | - var p orb.Point |
27 | | - err := row.Scan(wkb.Scanner(&p)) |
| 26 | +var p orb.Point |
| 27 | +err := row.Scan(wkb.Scanner(&p)) |
28 | 28 |
|
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 | +``` |
31 | 31 |
|
32 | 32 | If you don't know the type of the geometry try something like |
33 | 33 |
|
34 | | - s := wkb.Scanner(nil) |
35 | | - err := row.Scan(&s) |
| 34 | +```go |
| 35 | +s := wkb.Scanner(nil) |
| 36 | +err := row.Scan(&s) |
36 | 37 |
|
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 | +``` |
41 | 43 |
|
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