# README
encoding/wkb 
This package provides encoding and decoding of WKB data. The interface is defined as:
func Marshal(geom orb.Geometry, byteOrder ...binary.ByteOrder) ([]byte, error)
func MarshalToHex(geom orb.Geometry, byteOrder ...binary.ByteOrder) (string, error)
func MustMarshal(geom orb.Geometry, byteOrder ...binary.ByteOrder) []byte
func MustMarshalToHex(geom orb.Geometry, byteOrder ...binary.ByteOrder) string
func NewEncoder(w io.Writer) *Encoder
func (e *Encoder) SetByteOrder(bo binary.ByteOrder)
func (e *Encoder) Encode(geom orb.Geometry) error
func Unmarshal(b []byte) (orb.Geometry, error)
func NewDecoder(r io.Reader) *Decoder
func (d *Decoder) Decode() (orb.Geometry, error)
Reading and Writing to a SQL database
This package provides wrappers for orb.Geometry
types that implement
sql.Scanner
and driver.Value
. For example:
row := db.QueryRow("SELECT ST_AsBinary(point_column) FROM postgis_table")
var p orb.Point
err := row.Scan(wkb.Scanner(&p))
db.Exec("INSERT INTO table (point_column) VALUES (?)", wkb.Value(p))
The column can also be wrapped in ST_AsEWKB
. The SRID will be ignored.
If you don't know the type of the geometry try something like
s := wkb.Scanner(nil)
err := row.Scan(&s)
switch g := s.Geometry.(type) {
case orb.Point:
case orb.LineString:
}
Scanning directly from MySQL columns is supported. By default MySQL returns geometry data as WKB but prefixed with a 4 byte SRID. To support this, if the data is not valid WKB, the code will strip the first 4 bytes, the SRID, and try again. This works for most use cases.
# Functions
Marshal encodes the geometry with the given byte order.
MarshalToHex will encode the geometry into a hex string representation of the binary wkb.
MustMarshal will encode the geometry and panic on error.
MustMarshalToHex will encode the geometry and panic on error.
NewDecoder will create a new WKB decoder.
NewEncoder creates a new Encoder for the given writer.
Scanner will return a GeometryScanner that can scan sql query results.
Unmarshal will decode the type into a Geometry.
Value will create a driver.Valuer that will WKB the geometry into the database query.
# Variables
DefaultByteOrder is the order used for marshalling or encoding is none is specified.
ErrIncorrectGeometry is returned when unmarshalling WKB data into the wrong type.
ErrNotWKB is returned when unmarshalling WKB and the data is not valid.
ErrUnsupportedDataType is returned by Scan methods when asked to scan non []byte data from the database.
ErrUnsupportedGeometry is returned when geometry type is not supported by this lib.
# Structs
Decoder can decoder WKB geometry off of the stream.
An Encoder will encode a geometry as WKB to the writer given at creation time.
GeometryScanner is a thing that can scan in sql query results.