geohashi

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2017 License: BSD-3-Clause Imports: 2 Imported by: 0

README

GeoHashi

Build Status GoDoc Go Report Card License

Geohash library for Go, optimised for performance. Inspired by geohash-int.

Examples

Encode coordinates:

func Encode() {
	lat, lon := 51.52463, -0.08411

	hash1 := geohashi.EncodeWithPrecision(lat, lon, 10)
	fmt.Printf("%d (%d)\n", hash1, hash1.Precision())

	hash2 := geohashi.EncodeWithPrecision(lat, lon, 20)
	fmt.Printf("%d (%d)\n", hash2, hash2.Precision())
}

Decode hash:

func Decode() {
	hash := geohashi.Hash(90072520759854475)
	area := hash.Decode()
	lat, lon := area.Center()

	fmt.Printf("%.5f,%.5f\n", lat, lon)
	fmt.Println(area.Contains(51.52463, -0.08411))
}

Precision

This library allows you to select the precision of the hash you want to create, up to a maximum of 26 bits.

The following table shows the maximum uncertainty (at the equator) for a given bit-precision:

Bits Uncertainty
26 ±0.35m
25 ±0.65m
24 ±1.3m
23 ±2.6m
22 ±5.3m
21 ±11m
20 ±21m
19 ±42m
18 ±84m
17 ±170m
16 ±340m
15 ±680m
14 ±1400m
13 ±2700m
12 ±5400m
11 ±11000m
10 ±22000m
9 ±43000m
8 ±86000m
7 ±170000m
6 ±350000m
5 ±690000m
4 ±1400000m
3 ±2800000m
2 ±5400000m
1 ±10000000m

Documentation

Index

Examples

Constants

View Source
const (
	LatMin = -85.05112878
	LatMax = -LatMin
	LonMin = -180.0
	LonMax = -LonMin

	PrecisionMin = 1
	PrecisionMax = 26
)

Limits from EPSG:900913 / EPSG:3785 / OSGEO:41001

Variables

This section is empty.

Functions

This section is empty.

Types

type Area

type Area struct{ MinLat, MaxLat, MinLon, MaxLon float64 }

Area is a rectangle area defined through a min and max lat/lon

func (Area) Center

func (a Area) Center() (lat, lon float64)

Center returns the area's centeroid coordinates

func (Area) Contains

func (a Area) Contains(lat, lon float64) bool

Contains returns true if coordinates are contained within the area.

type Hash

type Hash uint64

A Hash a numeric geohash value

Example
package main

import (
	"fmt"

	"github.com/bsm/geohashi"
)

func main() {
	hash := geohashi.Hash(90072520759854475)
	area := hash.Decode()
	lat, lon := area.Center()

	fmt.Printf("%.5f,%.5f\n", lat, lon)
	fmt.Println(area.Contains(51.52463, -0.08411))

}
Output:

51.52460,-0.08394
true

func Encode

func Encode(lat, lon float64) Hash

Encode converts a lat/lon to an geohash with maximum precision

Example
package main

import (
	"fmt"

	"github.com/bsm/geohashi"
)

func main() {
	lat, lon := 51.52463, -0.08411

	hash1 := geohashi.EncodeWithPrecision(lat, lon, 10)
	fmt.Printf("%d (%d)\n", hash1, hash1.Precision())

	hash2 := geohashi.EncodeWithPrecision(lat, lon, 20)
	fmt.Printf("%d (%d)\n", hash2, hash2.Precision())

}
Output:

45035996274208702 (10)
90072520759854475 (20)

func EncodeWithPrecision

func EncodeWithPrecision(lat, lon float64, prec uint8) Hash

EncodeWithPrecision converts a lat/lon to an numeric geohash

func (Hash) Children

func (h Hash) Children() []Hash

Children zooms in, returning four child hashes, in the following order SW, SE, NW, NE. This function may return nil if unable to zoom in further

func (Hash) Decode

func (h Hash) Decode() (area Area)

Decode decodes a hash into an area

func (Hash) MoveX

func (h Hash) MoveX(n int) Hash

MoveX moves n steps east (positive number) or west (negative number) and returns the resulting hash

func (Hash) MoveY

func (h Hash) MoveY(n int) Hash

MoveY moves n steps north (positive number) or south (negative number) and returns the resulting hash

func (Hash) Parent

func (h Hash) Parent() Hash

Parent zooms out, returning the parent hash, lowering the precision. This function may return Hash(0) if unable to zoom out further

func (Hash) Precision

func (h Hash) Precision() uint8

Precision returns the prec level

Jump to

Keyboard shortcuts

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