-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgeohash.go
200 lines (180 loc) · 4.3 KB
/
geohash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
Package geohash provides an implementation of geohash.
http://en.wikipedia.com/wiki/Geohash
http://geohash.org
*/
package geohash
import (
"fmt"
"math"
)
const (
base32 = "0123456789bcdefghjkmnpqrstuvwxyz"
)
var (
base32Lookup [1 << 8]int
defaultBox = Box{Lat: Range{Min: -90, Max: 90}, Lon: Range{Min: -180, Max: 180}}
)
func init() {
for i := range base32Lookup {
base32Lookup[i] = -1
}
for i := range len(base32) {
base32Lookup[base32[i]] = i
}
}
// EncodeAuto encodes a location to a geohash using the most suitable precision.
func EncodeAuto(lat, lon float64) string {
var gh string
for precision := 1; precision <= encodeMaxPrecision; precision++ {
gh = Encode(lat, lon, precision)
b, _ := Decode(gh)
p := b.Round()
if p.Lat == lat && p.Lon == lon {
break
}
}
return gh
}
const encodeMaxPrecision = 32
// Encode encodes a location to a geohash.
//
// The maximum supported precision is 32.
func Encode(lat, lon float64, precision int) string {
if precision > encodeMaxPrecision {
precision = encodeMaxPrecision
}
var buf [encodeMaxPrecision]byte
box := defaultBox
even := true
for i := range precision {
ci := 0
for mask := 1 << 4; mask != 0; mask >>= 1 {
var r *Range
var u float64
if even {
r = &box.Lon
u = lon
} else {
r = &box.Lat
u = lat
}
if mid := r.Mid(); u >= mid {
ci += mask
r.Min = mid
} else {
r.Max = mid
}
even = !even
}
buf[i] = base32[ci]
}
return string(buf[:precision])
}
// Decode decode a geohash to a [Box].
func Decode(gh string) (Box, error) {
box := defaultBox
even := true
for i := range len(gh) {
ci := base32Lookup[gh[i]]
if ci == -1 {
return box, fmt.Errorf("geohash decode '%s': invalid character at index %d", gh, i)
}
for mask := 1 << 4; mask != 0; mask >>= 1 {
var r *Range
if even {
r = &box.Lon
} else {
r = &box.Lat
}
if mid := r.Mid(); ci&mask != 0 {
r.Min = mid
} else {
r.Max = mid
}
even = !even
}
}
return box, nil
}
// Box is a spatial data structure.
//
// It is defined by 2 ranges of latitude/longitude.
type Box struct {
Lat, Lon Range
}
// Center returns the Box's center as a Point.
func (b Box) Center() Point {
return Point{Lat: b.Lat.Mid(), Lon: b.Lon.Mid()}
}
// Round returns the Box's approximate location as a [Point].
//
// It uses decimal rounding and is in general more useful than Center.
func (b Box) Round() Point {
return Point{Lat: b.Lat.Round(), Lon: b.Lon.Round()}
}
// Point represents a location (latitude and longitude).
type Point struct {
Lat, Lon float64
}
// Range represents a range (min/max) on latitude or longitude.
type Range struct {
Min, Max float64
}
// Val returns the difference between Min and Max.
func (r Range) Val() float64 {
return math.Abs(r.Max - r.Min)
}
// Mid return the middle value between Min and Max.
func (r Range) Mid() float64 {
return (r.Min + r.Max) / 2
}
// Round returns the rounded value between Min and Max.
//
// It uses decimal rounding.
func (r Range) Round() float64 {
dec := int(math.Floor(-math.Log10(r.Val())))
if dec < 0 {
dec = 0
}
return roundDecimal(r.Mid(), dec)
}
// Neighbors will contain the geohashes for the neighbors of the supplied
// geohash in each of the cardinal and intercardinal directions.
type Neighbors struct {
North string
NorthEast string
East string
SouthEast string
South string
SouthWest string
West string
NorthWest string
}
// GetNeighbors returns a struct representing the [Neighbors] of the supplied
// geohash in each of the cardinal and intercardinal directions.
func GetNeighbors(gh string) (Neighbors, error) {
box, err := Decode(gh)
if err != nil {
return Neighbors{}, err
}
latMid := box.Lat.Mid()
lonMid := box.Lon.Mid()
latVal := box.Lat.Val()
lonVal := box.Lon.Val()
precision := len(gh)
encode := func(lat, lon float64) string {
lat, lon = normalize(lat, lon)
return Encode(lat, lon, precision)
}
return Neighbors{
North: encode(latMid+latVal, lonMid),
NorthEast: encode(latMid+latVal, lonMid+lonVal),
East: encode(latMid, lonMid+lonVal),
SouthEast: encode(latMid-latVal, lonMid+lonVal),
South: encode(latMid-latVal, lonMid),
SouthWest: encode(latMid-latVal, lonMid-lonVal),
West: encode(latMid, lonMid-lonVal),
NorthWest: encode(latMid+latVal, lonMid-lonVal),
}, nil
}