wkb

package
v1.3.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2021 License: BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package wkb implements Well Known Binary encoding and decoding.

If you are encoding geometries in WKB to send to PostgreSQL/PostGIS, then you must specify binary_parameters=yes in the data source name that you pass to sql.Open.

Example (Scan)
type City struct {
	Name     string
	Location wkb.Point
}

db, mock, err := sqlmock.New()
if err != nil {
	log.Fatal(err)
}
defer db.Close()

mock.ExpectQuery(`SELECT name, ST_AsBinary\(location\) FROM cities WHERE name = \?;`).
	WithArgs("London").
	WillReturnRows(
		sqlmock.NewRows([]string{"name", "location"}).
			AddRow("London", geomtest.MustHexDecode("010100000052B81E85EB51C03F45F0BF95ECC04940")),
	)

var c City
if err := db.QueryRow(`SELECT name, ST_AsBinary(location) FROM cities WHERE name = ?;`, "London").Scan(&c.Name, &c.Location); err != nil {
	log.Fatal(err)
}

fmt.Printf("Longitude: %v\n", c.Location.X())
fmt.Printf("Latitude: %v\n", c.Location.Y())
Output:

Longitude: 0.1275
Latitude: 51.50722
Example (Scan_different_shapes)
type Shape struct {
	Name string
	Geom wkb.Geom
}

db, mock, err := sqlmock.New()
if err != nil {
	log.Fatal(err)
}
defer db.Close()

mock.ExpectQuery(`SELECT name, ST_AsBinary\(geom\) FROM shapes`).
	WillReturnRows(
		sqlmock.NewRows([]string{"name", "location"}).
			AddRow("Point", geomtest.MustHexDecode("0101000000000000000000F03F0000000000000040")).
			AddRow("LineString", geomtest.MustHexDecode("010200000002000000000000000000F03F000000000000004000000000000008400000000000001040")).
			AddRow("Polygon", geomtest.MustHexDecode("01030000000100000004000000000000000000F03F00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000F03F0000000000000040")),
	)

rows, err := db.Query(`SELECT name, ST_AsBinary(geom) FROM shapes`)
if err != nil {
	log.Fatal(err)
}
for rows.Next() {
	var s Shape
	err := rows.Scan(&s.Name, &s.Geom)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s: %v\n", s.Name, s.Geom.FlatCoords())
}
Output:

Point: [1 2]
LineString: [1 2 3 4]
Polygon: [1 2 3 4 5 6 1 2]
Example (Value)
type City struct {
	Name     string
	Location wkb.Point
}

db, mock, err := sqlmock.New()
if err != nil {
	log.Fatal(err)
}
defer db.Close()

mock.ExpectExec(`INSERT INTO cities \(name, location\) VALUES \(\?, \?\);`).
	WithArgs("London", geomtest.MustHexDecode("010100000052B81E85EB51C03F45F0BF95ECC04940")).
	WillReturnResult(sqlmock.NewResult(1, 1))

c := City{
	Name:     "London",
	Location: wkb.Point{Point: geom.NewPoint(geom.XY).MustSetCoords(geom.Coord{0.1275, 51.50722})},
}

result, err := db.Exec(`INSERT INTO cities (name, location) VALUES (?, ?);`, c.Name, &c.Location)
if err != nil {
	log.Fatal(err)
}
rowsAffected, _ := result.RowsAffected()
fmt.Printf("%d rows affected", rowsAffected)
Output:

1 rows affected
Example (Value_different_shapes)
type Shape struct {
	Name string
	Geom wkb.Geom
}

db, mock, err := sqlmock.New()
if err != nil {
	log.Fatal(err)
}
defer db.Close()

mock.ExpectExec(`INSERT INTO objects \(name, location\) VALUES \(\?, \?\);`).
	WithArgs("Point", geomtest.MustHexDecode("0101000000000000000000F03F0000000000000040")).
	WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`INSERT INTO objects \(name, location\) VALUES \(\?, \?\);`).
	WithArgs("LineString", geomtest.MustHexDecode("010200000002000000000000000000F03F000000000000004000000000000008400000000000001040")).
	WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`INSERT INTO objects \(name, location\) VALUES \(\?, \?\);`).
	WithArgs("Polygon", geomtest.MustHexDecode("01030000000100000004000000000000000000F03F00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000F03F0000000000000040")).
	WillReturnResult(sqlmock.NewResult(1, 1))

shapes := []Shape{
	{
		Name: "Point",
		Geom: wkb.Geom{T: geom.NewPoint(geom.XY).MustSetCoords(geom.Coord{1, 2})},
	},
	{
		Name: "LineString",
		Geom: wkb.Geom{T: geom.NewLineString(geom.XY).MustSetCoords([]geom.Coord{{1, 2}, {3, 4}})},
	},
	{
		Name: "Polygon",
		Geom: wkb.Geom{
			T: geom.NewPolygon(geom.XY).MustSetCoords([][]geom.Coord{
				{{1, 2}, {3, 4}, {5, 6}, {1, 2}},
			}),
		},
	},
}

for _, s := range shapes {
	result, err := db.Exec(`INSERT INTO objects (name, location) VALUES (?, ?);`, s.Name, &s.Geom)
	if err != nil {
		log.Fatal(err)
	}
	rowsAffected, _ := result.RowsAffected()
	fmt.Printf("%d rows affected\n", rowsAffected)
}
Output:

1 rows affected
1 rows affected
1 rows affected

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// XDR is big endian.
	XDR = wkbcommon.XDR
	// NDR is little endian.
	NDR = wkbcommon.NDR
)

Functions

func Marshal

func Marshal(g geom.T, byteOrder binary.ByteOrder, opts ...wkbcommon.WKBOption) ([]byte, error)

Marshal marshals an arbitrary geometry to a []byte.

func Read

func Read(r io.Reader, opts ...wkbcommon.WKBOption) (geom.T, error)

Read reads an arbitrary geometry from r.

func Unmarshal

func Unmarshal(data []byte, opts ...wkbcommon.WKBOption) (geom.T, error)

Unmarshal unmrshals an arbitrary geometry from a []byte.

func Write

func Write(w io.Writer, byteOrder binary.ByteOrder, g geom.T, opts ...wkbcommon.WKBOption) error

Write writes an arbitrary geometry to w.

Types

type ErrExpectedByteSlice

type ErrExpectedByteSlice struct {
	Value interface{}
}

ErrExpectedByteSlice is returned when a []byte is expected.

func (ErrExpectedByteSlice) Error

func (e ErrExpectedByteSlice) Error() string

type Geom

type Geom struct {
	geom.T
	// contains filtered or unexported fields
}

A Geom is a WKB-ecoded Geometry that implements the sql.Scanner and driver.Value interfaces. It can be used when the geometry shape is not defined.

func (*Geom) Geom

func (g *Geom) Geom() geom.T

Geom returns the underlying geom.T.

func (*Geom) Scan

func (g *Geom) Scan(src interface{}) error

Scan scans from a []byte.

func (*Geom) Value

func (g *Geom) Value() (driver.Value, error)

Value returns the WKB encoding of g.

type GeometryCollection

type GeometryCollection struct {
	*geom.GeometryCollection
	// contains filtered or unexported fields
}

A GeometryCollection is a WKB-encoded GeometryCollection that implements the sql.Scanner and driver.Valuer interfaces.

func (*GeometryCollection) Scan

func (gc *GeometryCollection) Scan(src interface{}) error

Scan scans from a []byte.

func (*GeometryCollection) Value

func (gc *GeometryCollection) Value() (driver.Value, error)

Value returns the WKB encoding of gc.

type LineString

type LineString struct {
	*geom.LineString
	// contains filtered or unexported fields
}

A LineString is a WKB-encoded LineString that implements the sql.Scanner and driver.Valuer interfaces.

func (*LineString) Scan

func (ls *LineString) Scan(src interface{}) error

Scan scans from a []byte.

func (*LineString) Value

func (ls *LineString) Value() (driver.Value, error)

Value returns the WKB encoding of ls.

type MultiLineString

type MultiLineString struct {
	*geom.MultiLineString
	// contains filtered or unexported fields
}

A MultiLineString is a WKB-encoded MultiLineString that implements the sql.Scanner and driver.Valuer interfaces.

func (*MultiLineString) Scan

func (mls *MultiLineString) Scan(src interface{}) error

Scan scans from a []byte.

func (*MultiLineString) Value

func (mls *MultiLineString) Value() (driver.Value, error)

Value returns the WKB encoding of mls.

type MultiPoint

type MultiPoint struct {
	*geom.MultiPoint
	// contains filtered or unexported fields
}

A MultiPoint is a WKB-encoded MultiPoint that implements the sql.Scanner and driver.Valuer interfaces.

func (*MultiPoint) Scan

func (mp *MultiPoint) Scan(src interface{}) error

Scan scans from a []byte.

func (*MultiPoint) Value

func (mp *MultiPoint) Value() (driver.Value, error)

Value returns the WKB encoding of mp.

type MultiPolygon

type MultiPolygon struct {
	*geom.MultiPolygon
	// contains filtered or unexported fields
}

A MultiPolygon is a WKB-encoded MultiPolygon that implements the sql.Scanner and driver.Valuer interfaces.

func (*MultiPolygon) Scan

func (mp *MultiPolygon) Scan(src interface{}) error

Scan scans from a []byte.

func (*MultiPolygon) Value

func (mp *MultiPolygon) Value() (driver.Value, error)

Value returns the WKB encoding of mp.

type Point

type Point struct {
	*geom.Point
	// contains filtered or unexported fields
}

A Point is a WKB-encoded Point that implements the sql.Scanner and driver.Valuer interfaces.

func (*Point) Scan

func (p *Point) Scan(src interface{}) error

Scan scans from a []byte.

func (*Point) Value

func (p *Point) Value() (driver.Value, error)

Value returns the WKB encoding of p.

type Polygon

type Polygon struct {
	*geom.Polygon
	// contains filtered or unexported fields
}

A Polygon is a WKB-encoded Polygon that implements the sql.Scanner and driver.Valuer interfaces.

func (*Polygon) Scan

func (p *Polygon) Scan(src interface{}) error

Scan scans from a []byte.

func (*Polygon) Value

func (p *Polygon) Value() (driver.Value, error)

Value returns the WKB encoding of p.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL