regionagogo

package module
v0.0.0-...-3f91875 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2016 License: MIT Imports: 10 Imported by: 0

README

Region à gogo is a microservice, simply returns the country and states/region 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

It uses data from Natural Earth Data and performs real points inside tests against geo shapes.

Some regions are not precise enough and some accentuated names are wrong, if you are aware of a better source please tell me.

The actual Go S2 implementation does not use the shape boundaries to perform a region coverage but a rect boundaries. regionagogo is reading a file named geodata in msgpack format so you can generate the datafile with another S2 implementation, you can use my C++/ObjC gluecode: regionagogogen.

The image submitted to Docker hub, will always be an optimized one using the C++ implementation.

Build & Install

go get github.com/jteeuwen/go-bindata/...
go get github.com/akhenakh/regionagogo
cd $GOPATH/src/github.com/akhenakh/regionagogo
make
go install github.com/akhenakh/regionagogo/cmd/regionagogo

The binary regionagogo embed the geodata so it can be copied without any other files.

Usage

Run regionagogo, it will listen on port 8082.

You can query via HTTP GET:

GET /country?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()
b, _ := ioutil.ReadFile("geodata")
gs.ImportGeoData(b)
r := gs.Query(msg.Latitude, msg.Longitude)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ImportGeoJSONFile

func ImportGeoJSONFile(filename string, debug bool, fields []string) error

importGeoJSONFile will load a geo json and save the polygons into a msgpack file named geodata fields to lookup for in GeoJSON

Types

type CPoint

type CPoint struct {
	Coordinate []float64 `msgpack:"c"`
}

CPoint is a []float64 used as coordinates

type CellIDLoopStorage

type CellIDLoopStorage struct {
	C     s2.CellID `msgpack:"c"`
	Loops []int     `msgpack:"l"`
}

CellIDLoopStorage is a cell with associated loops used for storage

type FieldFlag

type FieldFlag struct {
	Fields []string
}

FieldFlag reusable parse Value to create import command

func (*FieldFlag) Set

func (ff *FieldFlag) Set(value string) error

func (*FieldFlag) String

func (ff *FieldFlag) String() string

type GeoData

type GeoData struct {
	RS []RegionStorage     `msgpack:"rs"`
	CL []CellIDLoopStorage `msgpack:"cl"`
}

GeoData is used to pack the data in a msgpack file

type GeoSearch

type GeoSearch struct {
	augmentedtree.Tree

	Debug bool
	// contains filtered or unexported fields
}

GeoSearch provides in memory storage and query engine for regions lookup

func NewGeoSearch

func NewGeoSearch() *GeoSearch

NewGeoSearch

func (*GeoSearch) ImportGeoData

func (gs *GeoSearch) ImportGeoData(b []byte) error

ImportGeoData loads geodata file into a map loopID->Region fills the segment tree for fast lookup

func (*GeoSearch) Query

func (gs *GeoSearch) Query(lat, lng float64) map[string]string

Query returns the country for the corresponding lat, lng point

type Region

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

Region is region for memory use

type RegionStorage

type RegionStorage struct {
	Data         map[string]string `msgpack:"d"`
	Code         string            `msgpack:"i"`
	Points       []CPoint          `msgpack:"p"`
	s2.CellUnion `msgpack:"c"`
}

RegionStorage is a region representation for storage use

type S2Interval

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

func (*S2Interval) HighAtDimension

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

func (*S2Interval) ID

func (s *S2Interval) ID() uint64

func (*S2Interval) LowAtDimension

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

func (*S2Interval) OverlapsAtDimension

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

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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