georedis

package module
v0.0.0-...-825551b Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2018 License: MIT Imports: 8 Imported by: 2

README

georedis

georedis provide the operations of geolocation with redis.

include GEOADD, GEODIST, GEOHASH, GEOPOS, GEORADIUS and GEORADIUSBYMEMBER

Install

go get github.com/Natata/georedis

Operation

Init
configFilePath := "config_example.json"
pool, _ := georedis.NewPool(configFilePath)
geo := georedis.NewGeo(pool)
Add
key := "japan"
members := []*georedis.Member{
    georedis.NewMember("tokyo", 35.688825, 139.700804),
    georedis.NewMember("osaka", 34.662707, 135.502293),
}   
err := geo.Add(key, members)
Dist
dist, err := geo.Dist("japan", "tokyo", "osaka", georedis.KM)
fmt.Println("Distance from toyko to osaka: ", dist)
Hash
hs, err := geo.Hash("japan", "tokyo")
Pos
pos, err := geo.Pos(key, "tokyo")
RadiusByName
neighbors, err := geo.RadiusByName(
	"japan", 
	"tokyo", 
	400, 
	georedis.KM, 
	georedis.WithDist)
Radius
neighbors, err := geo.Radius(
	"japan", 
	"tokyo", 
	georedis.Coordinate{
		Lat: 35.688825,
		Lon: 139.700804,
	}, 
	georedis.KM, 
	georedis.WithDist)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPool

func NewPool(configPath string) (*redis.Pool, error)

NewPool creates a redis connection pool

Types

type Coordinate

type Coordinate struct {
	Lat float64
	Lon float64
}

Coordinate present geo location

type Geo

type Geo struct {
	// contains filtered or unexported fields
}

Geo is the core service for geolocation-related operation

func NewGeo

func NewGeo(pool *redis.Pool) *Geo

NewGeo creates a Geo service

func (*Geo) Add

func (s *Geo) Add(key string, data []*Member) error

Add adds key and related meta data to redis

func (*Geo) Dist

func (s *Geo) Dist(key, a, b string, u Unit) (float64, error)

Dist cc

func (*Geo) Hash

func (s *Geo) Hash(key string, list ...string) ([]string, error)

Hash return the geohash of place

func (*Geo) Pos

func (s *Geo) Pos(key string, names ...string) ([]*Member, error)

Pos gets the meta data by key returned meta data hase the same order of names leave nil for the keys have no data

func (*Geo) Radius

func (s *Geo) Radius(key string, coord Coordinate, radius int, unit string, options ...Option) ([]*Neighbor, error)

Radius find the neighbor with coordinate

func (*Geo) RadiusByName

func (s *Geo) RadiusByName(key string, name string, radius int, unit string, options ...Option) ([]*Neighbor, error)

RadiusByName find nearby members of member the result include the name itself

type Member

type Member struct {
	Name  string     `json:"name"`
	Coord Coordinate `json:"coord"`
}

Member present the Coordinate and the key of data

func NewMember

func NewMember(name string, lat, lon float64) *Member

NewMember create a meta dta

type Neighbor

type Neighbor struct {
	Member
	Dist float64
	Hash int64
}

Neighbor is member with the distance and geohash

func NewNeighbor

func NewNeighbor(raw reflect.Value, opts ...Option) (*Neighbor, error)

NewNeighbor transfers the raw value from GEOREDIUS to Member

type Option

type Option = int

Option is the georedius option

const (
	// WithDist returns the distance between the location to neighbor
	WithDist Option = iota
	// WithHash returns the value with geohash
	WithHash
	// WithCoord returns the coordinate of nieghbor
	WithCoord
)

type PoolConfig

type PoolConfig struct {
	IdleConn     int    `json:"idle_conn"`
	ActiveConn   int    `json:"active_conn"`
	Protocol     string `json:"protocol"`
	Addr         string `json:"addr"`
	DB           int    `json:"db"`
	TobTimeout   string `json:"tob_timeout"`
	IdleTimeout  string `json:"idle_timeout"`
	ConnTimeout  string `json:"conn_timeout"`
	ReadTimeout  string `json:"read_timeout"`
	WriteTimeout string `json:"write_timeout"`
}

PoolConfig is the struct for config pool

type Unit

type Unit = string

Unit is the unit of distance

const (
	M  Unit = "m"
	KM Unit = "km"
	Mi Unit = "mi"
	Ft Unit = "ft"
)

Distance unit

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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