regionagogo

package module
v0.0.0-...-46cfdfa Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2016 License: MIT Imports: 8 Imported by: 0

README

wercker status Go Report Card GoDoc

Region à gogo is a microservice, it's a simple database that returns metadata associated to a fence for a given location.

It uses S2 and a segment tree to create a fast geo shape database, details of implementation are described in this blog post.

It can also be used directly from docker docker run -P akhenakh/regionagogo

Data

You can use any geo data but the provided GeoJSON comes from Natural Earth Data. Some regions are not precise enough and some accentuated names are wrong, if you are aware of a better source please tell me.

It works too with the better Gadm Data but the data are not free for commercial use.

Regionagogo is using a BoltDB datafile to store the fences and a small segment tree as index lives in memory.

Build & Install

go get github.com/akhenakh/regionagogo
cd $GOPATH/src/github.com/akhenakh/regionagogo
make

To generate the database from GeoJSON use the provided ragogenfromjson command, you can specify the fields you want from the GeoJSON properties to be saved into the DB:

ragogenfromjson -filename testdata/world_states_10m.geojson -fields iso_a2,name -dbpath ./region.db

Usage

Run regionagogo -dbpath ./region.db, it will listen on port 8082.

You can query via HTTP GET:

GET /query?lat=19.542915&lng=-155.665857

{
    "code": "US",
    "name": "Hawaii"
}

Using it as a library

You can use it in your own code without the HTTP interface:

gs := regionagogo.NewGeoSearch("region.db")
r := gs.StabbingQuery(msg.Latitude, msg.Longitude)

TODO

  • move the cache outside of boltdb storage to make it generic
  • implement an in memory storage

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ImportGeoJSONFile

func ImportGeoJSONFile(gs GeoFenceDB, r io.Reader, fields []string) error

ImportGeoJSONFile will load a geo json and save the polygons into the GeoFence for later lookup fields are the properties fields names you want to be associated with each fences

Types

type BySize

type BySize []*Fence

func (BySize) Len

func (d BySize) Len() int

func (BySize) Less

func (d BySize) Less(i, j int) bool

func (BySize) Swap

func (d BySize) Swap(i, j int)

type Fence

type Fence struct {
	Data map[string]string `json:"data"`
	Loop *s2.Loop          `json:"-"`
}

Fence is an s2 represented FenceStorage it contains an S2 loop and the associated metadata

func NewFenceFromStorage

func NewFenceFromStorage(rs *geostore.FenceStorage) *Fence

NewFenceFromStorage returns a Fence from a FenceStorage Fence can be extended, FenceStorage is a protocol buffer instance

func (*Fence) ToGeoJSON

func (f *Fence) ToGeoJSON() *geojson.FeatureCollection

ToGeoJSON transforms a Region to a valid GeoJSON

type Fences

type Fences []*Fence

Fences a slice of *Fence (type used mainly to return one GeoJSON of the regions)

func (*Fences) ToGeoJSON

func (f *Fences) ToGeoJSON() *geojson.FeatureCollection

ToGeoJSON transforms a set of Fences to a valid GeoJSON

type GeoFenceDB

type GeoFenceDB interface {
	// returns a Fence by it's storage id
	FenceByID(loopID uint64) *Fence

	// returns the fence for the corresponding lat, lng coordinates
	StubbingQuery(lat, lng float64, opts ...QueryOptionsFunc) (Fences, error)

	// RectQuery perform rectangular query ur upper right bl bottom left
	RectQuery(urlat, urlng, bllat, bllng float64, opts ...QueryOptionsFunc) (Fences, error)

	// RadiusQuery is performing a radius query
	RadiusQuery(lat, lng, radius float64, opts ...QueryOptionsFunc) (Fences, error)

	// Store a Fence into the DB
	StoreFence(rs *geostore.FenceStorage, cover []uint64) error

	// Close the DB
	Close() error
}

GeoFenceDB is the main interface to store and query your geo database

type LoopFence

type LoopFence struct {
	*s2.Loop
}

LoopFence Making s2.Loop implements methods needed for coverage

func LoopFenceFromPoints

func LoopFenceFromPoints(points []s2.Point) *LoopFence

LoopFenceFromPoints creates a LoopFence from a list of s2.Point

func (*LoopFence) CapBound

func (l *LoopFence) CapBound() s2.Cap

CapBound returns the cap that contains this loop

func (*LoopFence) ContainsCell

func (l *LoopFence) ContainsCell(c s2.Cell) bool

ContainsCell checks whether the cell is completely enclosed by this loop. Does not count for loop interior and uses raycasting.

func (*LoopFence) IntersectsCell

func (l *LoopFence) IntersectsCell(c s2.Cell) bool

IntersectsCell checks if any edge of the cell intersects the loop or if the cell is contained. Does not count for loop interior and uses raycasting.

type QueryOptions

type QueryOptions struct {
	// Returns all fences when multiple fences match
	MultipleFences bool
}

queryOptions used to pass options to DB queries

type QueryOptionsFunc

type QueryOptionsFunc func(*QueryOptions)

func WithMultipleFences

func WithMultipleFences(mf bool) QueryOptionsFunc

WithMultipleFences enable multi fences in responses

type S2Interval

type S2Interval struct {
	s2.CellID
	LoopIDs []uint64
}

S2Interval is a CellID interval conforms augmentedtree Interval interface

func (*S2Interval) HighAtDimension

func (s *S2Interval) HighAtDimension(d uint64) int64

HighAtDimension returns an integer representing the higher bound at the requested dimension.

func (*S2Interval) ID

func (s *S2Interval) ID() uint64

ID should be a unique ID representing this interval. This is used to identify which interval to delete from the tree if there are duplicates.

func (*S2Interval) LowAtDimension

func (s *S2Interval) LowAtDimension(d uint64) int64

LowAtDimension returns an integer representing the lower bound at the requested dimension.

func (*S2Interval) OverlapsAtDimension

func (s *S2Interval) OverlapsAtDimension(iv augmentedtree.Interval, d uint64) bool

OverlapsAtDimension should return a bool indicating if the provided interval overlaps this interval at the dimension requested.

Directories

Path Synopsis
cmd
db
Package geostore is a generated protocol buffer package.
Package geostore is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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