proximity

package module
v0.0.0-...-21aa0e7 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2015 License: MIT Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-proximity

This is a BETA. Interface might change a bit.

This library provides an easy way to do proximity queries to locations stored in a Redis set.

It should be noted that the method used here is not the most precise, but the query is pretty fast, and should be appropriate for most consumer applications looking for this basic function.

package main

import (
	"fmt"

	"github.com/fzzy/radix/redis"
	"github.com/ride/go-proximity"
	redis "github.com/mediocregopher/radix.v2/pool"
)

var redisConn *redis.Pool

func init() {
	redisConn, _ = redis.New("tcp", "127.0.0.1:6379", 10)
}

func main() {
	set := proximity.NewLocationSet("go-prox-test", radix.Wrap(redisConn))

	set.Add("Toronto", 43.6667, -79.4167)
	set.Add("Philadelphia", 39.9523, -75.1638)

	res, _ := set.Near(43.6687, -79.4167, 5000)
	fmt.Println(res)
}

Customizing

Setting the Redis Client

Since this library doesn't want to force you to use one Redis library or another, a LocationSet has a Client interface.

type Client interface {
	ZAdd(set string, score float64, value string) (int64, error)
	ZRangeByScore(set string, from float64, to float64) ([]string, error)
}

To make this easier, an interface is provided for Radix Pool:

func WrapRadixClient(radixClient) *Client

Getting Intervals

A LocationSet takes an IntervalFinder function that users can override to match their needs. If no function is provided a default one will be used.

To override it, create a function that matches:

type IntervalFinder func(lat, lng, radius float64) []Int64arr

Int64arr is a sortable type Int64arr []int64.

Tests

To run tests, you must have Redis running on the default port. Tests will use the go-proximity:test-set key to run. All tests cleanup after they're done to prevent problems.

Run tests with:

go test -v ./...

Copyright (c) 2015 ride group inc.

Documentation

Overview

Example
package main

import (
	"fmt"

	redis "github.com/mediocregopher/radix.v2/pool"
	"github.com/ride/go-proximity/wrappers/radix"
)

func hErr(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	client, err := redis.New("tcp", "127.0.0.1:6379", 10)
	hErr(err)

	wrapper := radix.Wrap(client)
	set := NewLocationSet(key, wrapper)

	err = set.Add("Toronto", 43.6667, -79.4167)
	hErr(err)

	err = set.Add("New York", 40.7143, -74.0060)
	hErr(err)

	results, err := set.Near(43.6687, -79.4167, 500)
	hErr(err)

	tmp, err := set.Near(43.6687, -79.4167, 500000)
	hErr(err)
	results = append(results, tmp...)
	fmt.Println(results)

	client.Cmd("DEL", key)

}
Output:
[Toronto Toronto New York]

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultIntervalFinder = intervalsFromRadius

DefaultIntervalFinder gets intervals from Redis given a radius. It will remove neighborhoods that are right next to each other to do less queries.

Functions

This section is empty.

Types

type Client

type Client interface {
	ZAdd(set string, score float64, value string) (int64, error)
	ZRangeByScore(set string, from float64, to float64) ([]string, error)
}

Client is an for a Redis client.

type Int64arr

type Int64arr []int64

Int64arr is an array of int64 elements that can be sorted using the `sort` package.

func (Int64arr) Include

func (a Int64arr) Include(x int64) bool

Include returns if `x` is included in the array `a` or not using binary search.

The array must be sorted before using `Include`

func (Int64arr) Len

func (a Int64arr) Len() int

func (Int64arr) Less

func (a Int64arr) Less(i, j int) bool

func (Int64arr) Search

func (a Int64arr) Search(x int64) int

Search looks for element `x` in array `a` using binary search. Uses `sort.Search` to find the index where `x` is or could be inserted to keep the array sorted.

The array must be sorted before using `Search`

func (Int64arr) Swap

func (a Int64arr) Swap(i, j int)

type IntervalFinder

type IntervalFinder func(lat, lng, radius float64) []Int64arr

IntervalFinder provides a way to override the way to look for geohashes intervals. Use `proximity.DefaultIntervalFinder` if you don't want to override it.

type LocationSet

type LocationSet struct {
	Name string

	IntervalFinder IntervalFinder
	// contains filtered or unexported fields
}

LocationSet is a set of named points inside a Redis database.

func NewLocationSet

func NewLocationSet(name string, client Client) *LocationSet

NewLocationSet returns a LocationSet

func (*LocationSet) Add

func (set *LocationSet) Add(name string, lat, lng float64) error

Add adds a point <lat, lng> with name `name` to the location set.

Usage:

set.Add("Place Name", lat, lng)

func (*LocationSet) Near

func (set *LocationSet) Near(lat, lng, radius float64) ([]string, error)

Near gets the points within <radius> from the <lat, lng> point.

Directories

Path Synopsis
wrappers

Jump to

Keyboard shortcuts

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