spatial

package module
v0.0.0-...-8df499d Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2019 License: MIT Imports: 8 Imported by: 5

README

spatial

Add simple types to use PostGIS with any database driver in golang.

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

This section is empty.

Functions

func Encode

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

Types

type NullPoint

type NullPoint struct {
	Point Point
	Valid bool
}

func (*NullPoint) Scan

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

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) Scan

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

func (*Point) String

func (p *Point) String() string

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