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 (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{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
Index ¶
- Variables
- func Marshal(g geom.T, byteOrder binary.ByteOrder) ([]byte, error)
- func Read(r io.Reader) (geom.T, error)
- func Unmarshal(data []byte) (geom.T, error)
- func Write(w io.Writer, byteOrder binary.ByteOrder, g geom.T) error
- type ErrExpectedByteSlice
- type GeometryCollection
- type LineString
- type MultiLineString
- type MultiPoint
- type MultiPolygon
- type Point
- type Polygon
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // XDR is big endian. XDR = wkbcommon.XDR // NDR is little endian. NDR = wkbcommon.NDR )
Functions ¶
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 GeometryCollection ¶
type GeometryCollection struct {
*geom.GeometryCollection
}
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.
type LineString ¶
type LineString struct {
*geom.LineString
}
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.
type MultiLineString ¶
type MultiLineString struct {
*geom.MultiLineString
}
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.
type MultiPoint ¶
type MultiPoint struct {
*geom.MultiPoint
}
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.
type MultiPolygon ¶
type MultiPolygon struct {
*geom.MultiPolygon
}
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.
type Point ¶
A Point is a WKB-encoded Point that implements the sql.Scanner and driver.Valuer interfaces.