Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

geohash.gno

// Package geohash encodes a latitude/longitude pair into the short base32
// string known as a geohash, decodes one back into the cell it names, and
// walks from a cell to the eight around it.
//
// A geohash is a prefix code: every character narrows the box, so two places
// that share a long prefix are close together, and truncating a hash zooms
// out. That is the whole reason to use one on chain, where an avl tree keyed
// by geohash gives a cheap "everything near here" range scan without storing
// any geometry: Get the prefix, iterate, done.
//
//	hash, _ := geohash.Encode(42.6, -5.6, 5)   // "ezs42"
//	box, _ := geohash.DecodeBox("ezs42")       // the cell it names
//	north, _ := geohash.Neighbor("ezs42", geohash.North)
//
// The alphabet is the standard one and deliberately drops a, i, l and o, so a
// hash survives being read aloud or written down. Anything outside it is
// refused rather than quietly skipped.
//
// Everything here is pure arithmetic on exact binary fractions: both world
// ranges are powers of two divided by powers of two, so every box bound is
// representable in a float64 and two identical calls can never disagree. No
// map is iterated and no value is rounded, which is what makes a Render built
// on this deterministic.
//
// A live demo of this package is at
// [r/moul/x/daily/geohashdemo](/r/moul/x/daily/geohashdemo/v0).
package geohash

import "errors"

// Alphabet is the standard geohash base32 alphabet. It omits a, i, l and o so
// that a hash read aloud or copied by hand stays unambiguous.
const Alphabet = "0123456789bcdefghjkmnpqrstuvwxyz"

// MaxPrecision is the longest hash this package will produce or accept: 12
// characters, 60 bits, a cell well under a millimetre across. Past that the
// extra characters say nothing a float64 can still distinguish.
const MaxPrecision = 12

var (
	// ErrPrecision is returned by Encode for a length outside 1..MaxPrecision.
	ErrPrecision = errors.New("geohash: precision out of range")
	// ErrLatitude is returned by Encode for a latitude outside -90..90.
	ErrLatitude = errors.New("geohash: latitude out of range")
	// ErrLongitude is returned by Encode for a longitude outside -180..180.
	ErrLongitude = errors.New("geohash: longitude out of range")
	// ErrEmpty is returned when a hash is required and none was given.
	ErrEmpty = errors.New("geohash: empty hash")
	// ErrTooLong is returned for a hash longer than MaxPrecision.
	ErrTooLong = errors.New("geohash: hash longer than the maximum precision")
	// ErrInvalidChar is returned for a character outside Alphabet, which
	// includes the four letters the alphabet deliberately leaves out.
	ErrInvalidChar = errors.New("geohash: character is not in the geohash alphabet")
	// ErrDirection is returned by Neighbor for a Direction it does not know.
	ErrDirection = errors.New("geohash: unknown direction")
	// ErrNoNeighbor is returned by Neighbor when the step leaves the world:
	// north of the top row, or south of the bottom one. Longitude wraps and
	// never produces this.
	ErrNoNeighbor = errors.New("geohash: no neighbour in that direction")
)

// Box is the rectangle of latitudes and longitudes a geohash names. A geohash
// is a box, never a point, and the shorter the hash the larger the box.
type Box struct {
	MinLat, MaxLat float64
	MinLon, MaxLon float64
}

// Center returns the midpoint of the box, which is what Decode reports.
func (b Box) Center() (lat, lon float64) {
	return (b.MinLat + b.MaxLat) / 2, (b.MinLon + b.MaxLon) / 2
}

// Contains reports whether a point falls inside the box. The lower and left
// edges belong to the box and the upper and right ones do not, matching how
// Encode assigns a point sitting exactly on a midpoint.
func (b Box) Contains(lat, lon float64) bool {
	return lat >= b.MinLat && lat < b.MaxLat && lon >= b.MinLon && lon < b.MaxLon
}

// HeightDeg is the box's north-south extent in degrees.
func (b Box) HeightDeg() float64 { return b.MaxLat - b.MinLat }

// WidthDeg is the box's east-west extent in degrees.
func (b Box) WidthDeg() float64 { return b.MaxLon - b.MinLon }

// Direction names one of the eight cells around a geohash. The values are
// ordered clockwise from north, so adding four modulo eight is the reverse.
type Direction int

// The eight directions, clockwise from north.
const (
	North Direction = iota
	NorthEast
	East
	SouthEast
	South
	SouthWest
	West
	NorthWest
)

// String returns the lowercase hyphenated name, or "unknown" for a value that
// is not one of the eight.
func (d Direction) String() string {
	switch d {
	case North:
		return "north"
	case NorthEast:
		return "north-east"
	case East:
		return "east"
	case SouthEast:
		return "south-east"
	case South:
		return "south"
	case SouthWest:
		return "south-west"
	case West:
		return "west"
	case NorthWest:
		return "north-west"
	}
	return "unknown"
}

// step returns the latitude and longitude offsets, in whole cells, for a
// direction. The second result is false for a direction that is not one of
// the eight.
func (d Direction) step() (dLat, dLon float64, ok bool) {
	switch d {
	case North:
		return 1, 0, true
	case NorthEast:
		return 1, 1, true
	case East:
		return 0, 1, true
	case SouthEast:
		return -1, 1, true
	case South:
		return -1, 0, true
	case SouthWest:
		return -1, -1, true
	case West:
		return 0, -1, true
	case NorthWest:
		return 1, -1, true
	}
	return 0, 0, false
}

// Encode returns the geohash of a point, precision characters long.
//
// Longitude takes the first bit and the two alternate, so an odd number of
// characters gives longitude one bit more than latitude and the cell comes out
// roughly square in degrees. A point sitting exactly on a dividing line goes
// to the upper, or eastern, half.
func Encode(lat, lon float64, precision int) (string, error) {
	if precision < 1 || precision > MaxPrecision {
		return "", ErrPrecision
	}
	if lat < -90 || lat > 90 {
		return "", ErrLatitude
	}
	if lon < -180 || lon > 180 {
		return "", ErrLongitude
	}

	latMin, latMax := -90.0, 90.0
	lonMin, lonMax := -180.0, 180.0

	out := make([]byte, 0, precision)
	ch, bit, even := 0, 0, true
	for len(out) < precision {
		if even {
			mid := (lonMin + lonMax) / 2
			if lon >= mid {
				ch |= 1 << uint(4-bit)
				lonMin = mid
			} else {
				lonMax = mid
			}
		} else {
			mid := (latMin + latMax) / 2
			if lat >= mid {
				ch |= 1 << uint(4-bit)
				latMin = mid
			} else {
				latMax = mid
			}
		}
		even = !even
		if bit < 4 {
			bit++
			continue
		}
		out = append(out, Alphabet[ch])
		ch, bit = 0, 0
	}
	return string(out), nil
}

// Decode returns the centre of the cell a geohash names. The centre is the
// best single point a hash can give: the cell itself is what was encoded, and
// DecodeBox is how to ask how big it is.
func Decode(hash string) (lat, lon float64, err error) {
	b, err := DecodeBox(hash)
	if err != nil {
		return 0, 0, err
	}
	lat, lon = b.Center()
	return lat, lon, nil
}

// DecodeBox returns the cell a geohash names.
func DecodeBox(hash string) (Box, error) {
	if hash == "" {
		return Box{}, ErrEmpty
	}
	if len(hash) > MaxPrecision {
		return Box{}, ErrTooLong
	}

	b := Box{MinLat: -90, MaxLat: 90, MinLon: -180, MaxLon: 180}
	even := true
	for i := 0; i < len(hash); i++ {
		v, ok := symbol(hash[i])
		if !ok {
			return Box{}, ErrInvalidChar
		}
		for _, mask := range [5]int{16, 8, 4, 2, 1} {
			if even {
				mid := (b.MinLon + b.MaxLon) / 2
				if v&mask != 0 {
					b.MinLon = mid
				} else {
					b.MaxLon = mid
				}
			} else {
				mid := (b.MinLat + b.MaxLat) / 2
				if v&mask != 0 {
					b.MinLat = mid
				} else {
					b.MaxLat = mid
				}
			}
			even = !even
		}
	}
	return b, nil
}

// Neighbor returns the geohash of the cell one step away, at the same
// precision.
//
// It works by walking one whole cell from the centre and re-encoding, which is
// exact: every cell bound is a binary fraction, so the step lands on another
// centre rather than near one.
//
// Longitude wraps, so the cell east of the last column is the first column
// again: the antimeridian is a line on a map, not an edge of the world. The
// classic lookup-table implementations report nothing there, which is a
// property of the tables and not of the globe. Latitude genuinely stops, and
// a step past a pole returns ErrNoNeighbor.
func Neighbor(hash string, d Direction) (string, error) {
	dLat, dLon, ok := d.step()
	if !ok {
		return "", ErrDirection
	}
	b, err := DecodeBox(hash)
	if err != nil {
		return "", err
	}
	lat, lon := b.Center()

	lat += dLat * b.HeightDeg()
	if lat > 90 || lat < -90 {
		return "", ErrNoNeighbor
	}
	lon += dLon * b.WidthDeg()
	if lon > 180 {
		lon -= 360
	}
	if lon < -180 {
		lon += 360
	}
	return Encode(lat, lon, len(hash))
}

// Neighbors returns the cells around a geohash, clockwise from north.
//
// The result is short rather than padded when a direction has no cell, which
// only happens at the poles: a caller iterating the result never has to filter
// an empty string out of it.
func Neighbors(hash string) ([]string, error) {
	if _, err := DecodeBox(hash); err != nil {
		return nil, err
	}
	out := make([]string, 0, 8)
	for d := North; d <= NorthWest; d++ {
		n, err := Neighbor(hash, d)
		if err == ErrNoNeighbor {
			continue
		}
		if err != nil {
			return nil, err
		}
		out = append(out, n)
	}
	return out, nil
}

// symbol maps an alphabet character to its five-bit value. It is a linear scan
// rather than a map because the alphabet is 32 bytes long and a package-level
// map would be one more object for every realm that imports this.
func symbol(c byte) (int, bool) {
	for i := 0; i < len(Alphabet); i++ {
		if Alphabet[i] == c {
			return i, true
		}
	}
	return 0, false
}

geohash_test.gno

package geohash

import (
	"testing"

	"gno.land/p/nt/uassert/v0"
)

func TestEncode(t *testing.T) {
	tests := []struct {
		name      string
		lat, lon  float64
		precision int
		want      string
	}{
		// The two reference vectors every geohash implementation is checked
		// against; both come from the algorithm's public description.
		{"jutland 11 chars", 57.64911, 10.40744, 11, "u4pruydqqvj"},
		{"leon 5 chars", 42.6, -5.6, 5, "ezs42"},
		// Null island sits on both midpoints, so every comparison takes the
		// "greater or equal" branch.
		{"null island", 0, 0, 5, "s0000"},
		{"south west corner", -90, -180, 5, "00000"},
		{"north east corner", 90, 180, 5, "zzzzz"},
		{"precision one", 57.64911, 10.40744, 1, "u"},
		{"shorter hash is a prefix", 57.64911, 10.40744, 4, "u4pr"},
	}
	for _, tt := range tests {
		got, err := Encode(tt.lat, tt.lon, tt.precision)
		uassert.NoError(t, err, tt.name)
		uassert.Equal(t, tt.want, got, tt.name)
	}
}

func TestEncodeErrors(t *testing.T) {
	tests := []struct {
		name      string
		lat, lon  float64
		precision int
	}{
		{"precision zero", 0, 0, 0},
		{"precision negative", 0, 0, -1},
		{"precision too large", 0, 0, MaxPrecision + 1},
		{"latitude above range", 90.1, 0, 5},
		{"latitude below range", -90.1, 0, 5},
		{"longitude above range", 0, 180.1, 5},
		{"longitude below range", 0, -180.1, 5},
	}
	for _, tt := range tests {
		got, err := Encode(tt.lat, tt.lon, tt.precision)
		uassert.Error(t, err, tt.name)
		uassert.Equal(t, "", got, tt.name)
	}
}

func TestDecodeBox(t *testing.T) {
	// The cell the Leon vector lands in. Every bound is an exact binary
	// fraction, which is why they can be compared literally.
	b, err := DecodeBox("ezs42")
	uassert.NoError(t, err)
	uassert.Equal(t, 42.5830078125, b.MinLat)
	uassert.Equal(t, 42.626953125, b.MaxLat)
	uassert.Equal(t, -5.625, b.MinLon)
	uassert.Equal(t, -5.5810546875, b.MaxLon)

	uassert.True(t, b.Contains(42.6, -5.6), "contains its own point")
	uassert.False(t, b.Contains(0, 0), "does not contain null island")

	lat, lon := b.Center()
	uassert.Equal(t, 42.60498046875, lat)
	uassert.Equal(t, -5.60302734375, lon)
}

func TestDecodeIsTheBoxCenter(t *testing.T) {
	lat, lon, err := Decode("ezs42")
	uassert.NoError(t, err)
	b, err := DecodeBox("ezs42")
	uassert.NoError(t, err)
	clat, clon := b.Center()
	uassert.Equal(t, clat, lat)
	uassert.Equal(t, clon, lon)
}

func TestRoundTrip(t *testing.T) {
	// Decoding a hash and re-encoding its centre at the same length gives the
	// hash back: the centre can never fall in a neighbouring cell.
	hashes := []string{"u", "u4", "u4pr", "ezs42", "s0000", "00000", "zzzzz", "u4pruydqqvj"}
	for _, h := range hashes {
		lat, lon, err := Decode(h)
		uassert.NoError(t, err, h)
		got, err := Encode(lat, lon, len(h))
		uassert.NoError(t, err, h)
		uassert.Equal(t, h, got, h)
	}
}

func TestDecodeErrors(t *testing.T) {
	// The alphabet drops a, i, l and o on purpose; anything outside it, and an
	// over-long hash, are refused rather than silently truncated.
	for _, h := range []string{"", "a", "i", "l", "o", "ezs4A", "ezs4 ", "u4pruydqqvjxx"} {
		_, _, err := Decode(h)
		uassert.Error(t, err, h)
	}
}

func TestNeighborIsReversible(t *testing.T) {
	// A cell well inside the grid: stepping away and back is the identity, in
	// all eight directions.
	const h = "ezs42"
	for _, d := range []Direction{North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest} {
		n, err := Neighbor(h, d)
		uassert.NoError(t, err, d.String())
		uassert.NotEqual(t, h, n, d.String())
		back, err := Neighbor(n, opposite(d))
		uassert.NoError(t, err, d.String())
		uassert.Equal(t, h, back, d.String())
	}
}

func TestNeighborKnownCells(t *testing.T) {
	// Cross-checked against the classic border/neighbour lookup tables, which
	// are a completely different way of computing the same answer.
	tests := []struct {
		dir  Direction
		want string
	}{
		{North, "ezs48"},
		{South, "ezs40"},
		{East, "ezs43"},
		{West, "ezefr"},
		{NorthEast, "ezs49"},
		{SouthWest, "ezefp"},
	}
	for _, tt := range tests {
		got, err := Neighbor("ezs42", tt.dir)
		uassert.NoError(t, err, tt.dir.String())
		uassert.Equal(t, tt.want, got, tt.dir.String())
	}
}

func TestNeighborWrapsLongitude(t *testing.T) {
	// The antimeridian is not an edge of the world: the column east of the
	// last one is the first column again.
	east, err := Neighbor("z", East)
	uassert.NoError(t, err)
	uassert.Equal(t, "b", east)

	west, err := Neighbor("b", West)
	uassert.NoError(t, err)
	uassert.Equal(t, "z", west)
}

func TestNeighborStopsAtThePoles(t *testing.T) {
	// Latitude does not wrap: there is nothing north of the top row.
	_, err := Neighbor("z", North)
	uassert.ErrorIs(t, err, ErrNoNeighbor)

	_, err = Neighbor("0", South)
	uassert.ErrorIs(t, err, ErrNoNeighbor)

	// The same cell still has neighbours sideways and downwards.
	got, err := Neighbor("z", South)
	uassert.NoError(t, err)
	uassert.Equal(t, "x", got)
}

func TestNeighborErrors(t *testing.T) {
	_, err := Neighbor("", North)
	uassert.Error(t, err)

	_, err = Neighbor("ezs4a", North)
	uassert.Error(t, err)

	_, err = Neighbor("ezs42", Direction(42))
	uassert.ErrorIs(t, err, ErrDirection)
}

func TestNeighbors(t *testing.T) {
	got, err := Neighbors("ezs42")
	uassert.NoError(t, err)
	uassert.Equal(t, 8, len(got))
	uassert.Equal(t, "ezs48", got[0]) // North
	uassert.Equal(t, "ezs43", got[2]) // East
	uassert.Equal(t, "ezs40", got[4]) // South
	uassert.Equal(t, "ezefr", got[6]) // West

	// Unique: a repeat would mean the stepping collapsed two directions.
	seen := map[string]bool{}
	for _, n := range got {
		uassert.False(t, seen[n], n)
		seen[n] = true
	}

	// At the pole the row above is missing, and the list is short rather than
	// carrying empty strings a caller would have to filter.
	got, err = Neighbors("z")
	uassert.NoError(t, err)
	uassert.Equal(t, 5, len(got))
	for _, n := range got {
		uassert.NotEqual(t, "", n)
	}
}

func TestDirectionString(t *testing.T) {
	tests := []struct {
		d    Direction
		want string
	}{
		{North, "north"},
		{NorthEast, "north-east"},
		{East, "east"},
		{SouthEast, "south-east"},
		{South, "south"},
		{SouthWest, "south-west"},
		{West, "west"},
		{NorthWest, "north-west"},
		{Direction(42), "unknown"},
	}
	for _, tt := range tests {
		uassert.Equal(t, tt.want, tt.d.String())
	}
}

func TestBoxDimensions(t *testing.T) {
	b, err := DecodeBox("ezs42")
	uassert.NoError(t, err)
	// Five characters is 25 bits: 13 go to longitude, 12 to latitude, and the
	// two halves of the world differ by exactly that factor of two, so the
	// cell comes out square in degrees.
	uassert.Equal(t, 180.0/4096.0, b.HeightDeg())
	uassert.Equal(t, 360.0/8192.0, b.WidthDeg())

	// One more character is five more bits, so the cell is 32 times smaller.
	b6, err := DecodeBox("ezs42d")
	uassert.NoError(t, err)
	uassert.Equal(t, b.HeightDeg()/8, b6.HeightDeg())
	uassert.Equal(t, b.WidthDeg()/4, b6.WidthDeg())
}

// opposite is the test's own inverse of a direction, kept here rather than
// exported so the package surface stays the part callers need.
func opposite(d Direction) Direction {
	return Direction((int(d) + 4) % 8)
}

gnomod.toml

module = "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/geohash/v0"
gno = "0.9"

[addpkg]
  creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv"
  height = 751709
  max_deposit = "9000000ugnot"