package
0.11.1
Repository: https://github.com/paulmach/orb.git
Documentation: pkg.go.dev

# README

encoding/ewkb Godoc Reference

This package provides encoding and decoding of extended WKB data. This format includes the SRID in the data. If the SRID is not needed use the wkb package for a simpler interface. The interface is defined as:

func Marshal(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) ([]byte, error)
func MarshalToHex(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) (string, error)
func MustMarshal(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) []byte
func MustMarshalToHex(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) string

func NewEncoder(w io.Writer) *Encoder
func (e *Encoder) SetByteOrder(bo binary.ByteOrder) *Encoder
func (e *Encoder) SetSRID(srid int) *Encoder
func (e *Encoder) Encode(geom orb.Geometry) error

func Unmarshal(b []byte) (orb.Geometry, int, error)

func NewDecoder(r io.Reader) *Decoder
func (d *Decoder) Decode() (orb.Geometry, int, error)

Inserting geometry into a database

Depending on the database different formats and functions are supported.

PostgreSQL and PostGIS

PostGIS stores geometry as EWKB internally. As a result it can be inserted without a wrapper function.

db.Exec("INSERT INTO geodata(geom) VALUES (ST_GeomFromEWKB($1))", ewkb.Value(coord, 4326))

db.Exec("INSERT INTO geodata(geom) VALUES ($1)", ewkb.Value(coord, 4326))

MySQL/MariaDB

MySQL and MariaDB store geometry data in WKB format with a 4 byte SRID prefix.

coord := orb.Point{1, 2}

// as WKB in hex format
data := wkb.MustMarshalToHex(coord)
db.Exec("INSERT INTO geodata(geom) VALUES (ST_GeomFromWKB(UNHEX(?), 4326))", data)

// relying on the raw encoding
db.Exec("INSERT INTO geodata(geom) VALUES (?)", ewkb.ValuePrefixSRID(coord, 4326))

Reading geometry from a database query

As stated above, different databases supported different formats and functions.

PostgreSQL and PostGIS

When working with PostGIS the raw format is EWKB so the wrapper function is not necessary

// both of these queries return the same data
row := db.QueryRow("SELECT ST_AsEWKB(geom) FROM geodata")
row := db.QueryRow("SELECT geom FROM geodata")

// if you don't need the SRID
p := orb.Point{}
err := row.Scan(ewkb.Scanner(&p))
log.Printf("geom: %v", p)

// if you need the SRID
p := orb.Point{}
gs := ewkb.Scanner(&p)
err := row.Scan(gs)

log.Printf("srid: %v", gs.SRID)
log.Printf("geom: %v", gs.Geometry)
log.Printf("also geom: %v", p)

MySQL/MariaDB

// using the ST_AsBinary function
row := db.QueryRow("SELECT st_srid(geom), ST_AsBinary(geom) FROM geodata")
row.Scan(&srid, ewkb.Scanner(&data))

// relying on the raw encoding
row := db.QueryRow("SELECT geom FROM geodata")

// if you don't need the SRID
p := orb.Point{}
err := row.Scan(ewkb.ScannerPrefixSRID(&p))
log.Printf("geom: %v", p)

// if you need the SRID
p := orb.Point{}
gs := ewkb.ScannerPrefixSRID(&p)
err := row.Scan(gs)

log.Printf("srid: %v", gs.SRID)
log.Printf("geom: %v", gs.Geometry)

# Functions

Marshal encodes the geometry with the given byte order.
MarshalToHex will encode the geometry into a hex string representation of the binary ewkb.
MustMarshal will encode the geometry and panic on error.
MustMarshalToHex will encode the geometry and panic on error.
NewDecoder will create a new EWKB decoder.
NewEncoder creates a new Encoder for the given writer.
Scanner will return a GeometryScanner that can scan sql query results.
ScannerPrefixSRID will scan ewkb data were the SRID is in the first 4 bytes of the data.
Unmarshal will decode the type into a Geometry.
Value will create a driver.Valuer that will EWKB the geometry into the database query.
ValuePrefixSRID will create a driver.Valuer that will WKB the geometry but add the srid as a 4 byte prefix.

# Variables

DefaultByteOrder is the order used for marshalling or encoding is none is specified.
DefaultSRID is set to 4326, a common SRID, which represents spatial data using longitude and latitude coordinates on the Earth's surface as defined in the WGS84 standard, which is also used for the Global Positioning System (GPS).
ErrIncorrectGeometry is returned when unmarshalling EWKB data into the wrong type.
ErrNotEWKB is returned when unmarshalling EWKB 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 EWKB to the writer given at creation time.
GeometryScanner is a thing that can scan in sql query results.