spatial

package module
v0.0.1-2020 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2020 License: MIT Imports: 9 Imported by: 0

README

spatial

Add simple types to use PostGIS with any database driver in golang. Point and NullPoint types. Json marshalling and unmarshalling supported for nullable structs. SQL Boiler support for NullPoint

Types supported

  • geometry(Geometry,4326)

Usage with database

First define your struct:

package main

type TripEvent struct {
  Geo spatial.Point `db:"geo" json:"geo"`
}

func main() {
  path := []spatial.Point{}
  err := database.DB.
  	Select("geo").
  	From("trip_events").
  	QueryStructs(&path)

  if err != nil {
    panic(err)
  }

  // Encode the path with .000000 level of precision
  polyline := spatial.Encode(path, 6)
  fmt.Println(polyline) // _p~iF~ps|U_ulLnnqC_mqNvxq`@

  points := spatial.Decode(polyline, 6)
  fmt.Println(polyline) // make(map[]spatial.Point, 2)
}

If your users aren't going to be zooming you can ignore the level 6 precision and go with 5 to get the best compression.

Usage without database

First define your struct:

package main

func main() {
  path := []spatial.Point{
    spatial.Point{
      Lat: 38.889803,
      Lng: -77.009114,
    },
    spatial.Point{
      Lat: 38.889810,
      Lng: -77.009124,
    },
  }

  // Encode the path with .000000 level of precision
  polyline := spatial.Encode(path, 6)
  fmt.Println(polyline) // _p~iF~ps|U_ulLnnqC_mqNvxq`@

  points := spatial.Decode(polyline, 6)
  fmt.Println(polyline) // make(map[]spatial.Point, 2)
}

Documentation

Overview

Based on https://github.com/jinzhu/gorm/issues/142

Index

Constants

This section is empty.

Variables

View Source
var NullBytes = []byte("null")

NullBytes is a global byte slice of JSON null

Functions

func Encode

func Encode(path []Point, precision int) string

Types

type NullPoint

type NullPoint struct {
	Point Point
	Valid bool
}

func NewNullPoint

func NewNullPoint(s Point, valid bool) NullPoint

NewNullPoint creates a new NullPoint

func NullPointFrom

func NullPointFrom(s Point) NullPoint

NullPointFrom creates a new NullPoint that will never be blank.

func NullPointFromPtr

func NullPointFromPtr(s *Point) NullPoint

NullPointFromPtr creates a new NullPoint that be null if s is nil.

func (NullPoint) IsZero

func (s NullPoint) IsZero() bool

IsZero returns true for null Points, for potential future omitempty support.

func (NullPoint) MarshalJSON

func (s NullPoint) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (NullPoint) MarshalText

func (s NullPoint) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (NullPoint) Ptr

func (s NullPoint) Ptr() *Point

Ptr returns a pointer to this NullPoint's value, or a nil pointer if this NullPoint is null.

func (*NullPoint) Randomize

func (s *NullPoint) Randomize(nextInt func() int64, fieldType string, shouldBeNull bool)

Randomize for sqlboiler

func (*NullPoint) Scan

func (np *NullPoint) Scan(val interface{}) error

func (*NullPoint) SetValid

func (s *NullPoint) SetValid(v Point)

SetValid changes this NullPoint's value and also sets it to be non-null.

func (*NullPoint) UnmarshalJSON

func (s *NullPoint) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*NullPoint) UnmarshalText

func (s *NullPoint) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (NullPoint) Value

func (np NullPoint) Value() (driver.Value, error)

type Point

type Point struct {
	Lng float64 `json:"lng"`
	Lat float64 `json:"lat"`
}

func Decode

func Decode(points string, precision int) ([]Point, error)

func (Point) MarshalText

func (s Point) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Point) Scan

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

func (*Point) String

func (p *Point) String() string

func (*Point) UnmarshalText

func (s *Point) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (Point) Value

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

Jump to

Keyboard shortcuts

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