gopostgis

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2024 License: GPL-3.0 Imports: 7 Imported by: 0

README

go-postgis

go workflow

Golang support for PostGIS datatypes

Features

  1. Fully tested
  2. Supports both little endian and big endian byte orders
  3. Simple usage of PostGIS datatypes in structs or standalone variables
  4. Supports any postgresql driver that utilizes sql.Scanner and driver.Valuer interfaces
  5. Out of the box support for json marshal/unmarshal

Installation

To add the package to your project run -

go get -u github.com/asif-mahmud/go-postgis
Documentation

godoc: https://pkg.go.dev/github.com/asif-mahmud/go-postgis well known binary format - https://github.com/postgis/postgis/blob/master/doc/bnf-wkb.txt well known text format - https://github.com/postgis/postgis/blob/master/doc/bnf-wkt.txt

Supported datatypes
  1. Point (X, Y) - added in v0.1.1
  2. PointS (SRID X, Y) - added in v0.1.1
  3. PointZ (X, Y, Z) - added in v0.1.2
  4. PointZS (SRID X, Y, Z) - added in v0.1.2
  5. PointM (X, Y, M) - added in v0.1.2
  6. PointMS (SRID X, Y, M) - added in v0.1.2
  7. PointZM (X, Y, Z, M) - added in v0.1.2
  8. PointZMS (SRID X, Y, Z, M) - added in v0.1.2
Example usage
// construct a point
point := gopostgis.Point{
	X:     10,
	Y:     20,
	Valid: true, // if you don't mark it as valid, null will be saved in db
}

// insert a point
_, e = db.Exec(`
 INSERT INTO test_table (location) VALUES($1)`,
	point,
)
if e != nil {
	panic(e)
}

// read a point
row := db.QueryRow(`SELECT location from test_table LIMIT 1`)
if e := row.Scan(&point); e != nil {
	panic(e)
}

Version history

Version 0.1.2
  • Added PointZ, PointZS, PointM, PointMS, PointZM and PointZMS types
  • Updated readme
Version 0.1.1
  • Added Point and PointS types
  • Updated readme
  • First release in go pkg
Version 0.1.0

Initial version with tests for hex ewkb decoder.

Documentation

Overview

Example
package main

import (
	"database/sql"

	gopostgis "github.com/asif-mahmud/go-postgis"
)

func main() {
	// we know it will panic at testing time, so
	// recovering silently, ignore this part
	defer func() {
		recover()
	}()

	// this is written for purely example purpose
	// create db connection
	db, e := sql.Open("postgres", "database=test_db")
	if e != nil {
		panic(e)
	}

	// create a table with geometry column
	_, e = db.Exec(`CREATE TABLE IF NOT EXISTS test_table ( 
    id SERIAL PRIMARY KEY,
    location GEOMETRY
  )`)
	if e != nil {
		panic(e)
	}

	// construct a point
	point := gopostgis.Point{
		X:     10,
		Y:     20,
		Valid: true, // if you don't mark it as valid, null will be saved in db
	}

	// insert a point
	_, e = db.Exec(`
  INSERT INTO test_table (location) VALUES($1)`,
		point,
	)
	if e != nil {
		panic(e)
	}

	// read a point
	row := db.QueryRow(`SELECT location from test_table LIMIT 1`)
	if e := row.Scan(&point); e != nil {
		panic(e)
	}

}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HexEWKBDecoder

type HexEWKBDecoder interface {
	// Decoded datatype that represents a postgis datatype.
	// Example - 0x00000001 means it is Point type.
	// See the reference doc for full list of datatypes.
	Type() uint32

	// Reads a single byte
	ReadByte() (byte, error)

	// Reads a 32-bit unsigned integer
	ReadUint32() (uint32, error)

	// Reads a 64-bit float
	ReadDouble() (float64, error)

	// Reads into any fixed-size value
	ReadAny(interface{}) error
}

Decoder for hex encoded EWKB data. reference - https://github.com/postgis/postgis/blob/master/doc/bnf-wkb.txt It does not implement any specific postgis datatype, instead it is intended to be used as a reader for specific postgis datatype implementation. Instance of this struct should be initialized by NewHexEWKBDecoder function. Creating an instance of this struct directly will cause incorrect behavior

func NewHexEWKBDecoder

func NewHexEWKBDecoder(data []byte) (HexEWKBDecoder, error)

Creates an instance of HexEWKBDecoder.

type Point added in v0.1.1

type Point struct {
	X     float64
	Y     float64
	Valid bool `json:"-"`
}

Point (X, Y) datatype. Supports NULL value.

func (Point) MarshalJSON added in v0.1.1

func (p Point) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*Point) Scan added in v0.1.1

func (p *Point) Scan(src any) error

Scan implements sql.Scanner

func (*Point) UnmarshalJSON added in v0.1.1

func (p *Point) UnmarshalJSON(d []byte) error

UnmarshalJSON implements json.Unmarshaler

func (Point) Value added in v0.1.1

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

Value implements driver.Valuer

type PointM added in v0.1.2

type PointM struct {
	X     float64
	Y     float64
	M     float64
	Valid bool `json:"-"`
}

PointM (X, Y, M) datatype. Supports NULL value.

func (PointM) MarshalJSON added in v0.1.2

func (p PointM) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*PointM) Scan added in v0.1.2

func (p *PointM) Scan(src any) error

Scan implements sql.Scanner

func (*PointM) UnmarshalJSON added in v0.1.2

func (p *PointM) UnmarshalJSON(d []byte) error

UnmarshalJSON implements json.Unmarshaler

func (PointM) Value added in v0.1.2

func (p PointM) Value() (driver.Value, error)

Value implements driver.Valuer

type PointMS added in v0.1.2

type PointMS struct {
	SRID  uint32
	X     float64
	Y     float64
	M     float64
	Valid bool `json:"-"`
}

PointMS (SRID X, Y, M) datatype. Supports NULL value.

func (PointMS) MarshalJSON added in v0.1.2

func (p PointMS) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*PointMS) Scan added in v0.1.2

func (p *PointMS) Scan(src any) error

Scan implements sql.Scanner

func (*PointMS) UnmarshalJSON added in v0.1.2

func (p *PointMS) UnmarshalJSON(d []byte) error

UnmarshalJSON implements json.Unmarshaler

func (PointMS) Value added in v0.1.2

func (p PointMS) Value() (driver.Value, error)

Value implements driver.Valuer

type PointS added in v0.1.1

type PointS struct {
	SRID  uint32
	X     float64
	Y     float64
	Valid bool `json:"-"`
}

PointS (SRID X, Y) datatype. Supports NULL value.

func (PointS) MarshalJSON added in v0.1.1

func (p PointS) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*PointS) Scan added in v0.1.1

func (p *PointS) Scan(src any) error

Scan implements sql.Scanner

func (*PointS) UnmarshalJSON added in v0.1.1

func (p *PointS) UnmarshalJSON(d []byte) error

UnmarshalJSON implements json.Unmarshaler

func (PointS) Value added in v0.1.1

func (p PointS) Value() (driver.Value, error)

Value implements driver.Valuer

type PointZ added in v0.1.2

type PointZ struct {
	X     float64
	Y     float64
	Z     float64
	Valid bool `json:"-"`
}

PointZ (X, Y, Z) datatype. Supports NULL value.

func (PointZ) MarshalJSON added in v0.1.2

func (p PointZ) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*PointZ) Scan added in v0.1.2

func (p *PointZ) Scan(src any) error

Scan implements sql.Scanner

func (*PointZ) UnmarshalJSON added in v0.1.2

func (p *PointZ) UnmarshalJSON(d []byte) error

UnmarshalJSON implements json.Unmarshaler

func (PointZ) Value added in v0.1.2

func (p PointZ) Value() (driver.Value, error)

Value implements driver.Valuer

type PointZM added in v0.1.2

type PointZM struct {
	X     float64
	Y     float64
	Z     float64
	M     float64
	Valid bool `json:"-"`
}

PointZM (X, Y, Z, M) datatype. Supports NULL value.

func (PointZM) MarshalJSON added in v0.1.2

func (p PointZM) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*PointZM) Scan added in v0.1.2

func (p *PointZM) Scan(src any) error

Scan implements sql.Scanner

func (*PointZM) UnmarshalJSON added in v0.1.2

func (p *PointZM) UnmarshalJSON(d []byte) error

UnmarshalJSON implements json.Unmarshaler

func (PointZM) Value added in v0.1.2

func (p PointZM) Value() (driver.Value, error)

Value implements driver.Valuer

type PointZMS added in v0.1.2

type PointZMS struct {
	SRID  uint32
	X     float64
	Y     float64
	Z     float64
	M     float64
	Valid bool `json:"-"`
}

PointZMS (SRID X, Y, Z, M) datatype. Supports NULL value.

func (PointZMS) MarshalJSON added in v0.1.2

func (p PointZMS) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*PointZMS) Scan added in v0.1.2

func (p *PointZMS) Scan(src any) error

Scan implements sql.Scanner

func (*PointZMS) UnmarshalJSON added in v0.1.2

func (p *PointZMS) UnmarshalJSON(d []byte) error

UnmarshalJSON implements json.Unmarshaler

func (PointZMS) Value added in v0.1.2

func (p PointZMS) Value() (driver.Value, error)

Value implements driver.Valuer

type PointZS added in v0.1.2

type PointZS struct {
	SRID  uint32
	X     float64
	Y     float64
	Z     float64
	Valid bool `json:"-"`
}

PointZS (SRID X, Y, Z) datatype. Supports NULL value.

func (PointZS) MarshalJSON added in v0.1.2

func (p PointZS) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*PointZS) Scan added in v0.1.2

func (p *PointZS) Scan(src any) error

Scan implements sql.Scanner

func (*PointZS) UnmarshalJSON added in v0.1.2

func (p *PointZS) UnmarshalJSON(d []byte) error

UnmarshalJSON implements json.Unmarshaler

func (PointZS) Value added in v0.1.2

func (p PointZS) Value() (driver.Value, error)

Value implements driver.Valuer

Jump to

Keyboard shortcuts

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