forked from TomiHiltunen/geohash-golang
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeohash.go
202 lines (188 loc) · 3.71 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
201
202
// geohash.go
// Geohash library for Golang
// Ported from David Troy's geohash-js library (https://github.com/davetroy/geohash-js)
// (c) Tomi Hiltunen 2014
// Distributed under the MIT License
package geohash
import (
"bytes"
"strings"
)
var (
bits = []int{16, 8, 4, 2, 1}
base32 = []byte("0123456789bcdefghjkmnpqrstuvwxyz")
neighbors = [][]string{
[]string{
"p0r21436x8zb9dcf5h7kjnmqesgutwvy",
"bc01fg45238967deuvhjyznpkmstqrwx",
},
[]string{
"bc01fg45238967deuvhjyznpkmstqrwx",
"p0r21436x8zb9dcf5h7kjnmqesgutwvy",
},
[]string{
"14365h7k9dcfesgujnmqp0r2twvyx8zb",
"238967debc01fg45kmstqrwxuvhjyznp",
},
[]string{
"238967debc01fg45kmstqrwxuvhjyznp",
"14365h7k9dcfesgujnmqp0r2twvyx8zb",
},
}
borders = [][]string{
[]string{
"prxz",
"bcfguvyz",
},
[]string{
"bcfguvyz",
"prxz",
},
[]string{
"028b",
"0145hjnp",
},
[]string{
"0145hjnp",
"028b",
},
}
)
// Calculates adjacent geohashes.
func CalculateAdjacent(s, dir string) string {
s = strings.ToLower(s)
lastChr := s[(len(s) - 1):]
oddEven := (len(s) % 2) // 0=even; 1=odd;
var dirInt int
switch dir {
default:
dirInt = 0
case "right":
dirInt = 1
case "bottom":
dirInt = 2
case "left":
dirInt = 3
}
base := s[0:]
if strings.Index(borders[dirInt][oddEven], lastChr) != -1 {
base = CalculateAdjacent(base, dir)
}
return base + string(base32[strings.Index(neighbors[dirInt][oddEven], lastChr)])
}
// Struct for passing Box.
type BoundingBox struct {
sw LatLng
ne LatLng
center LatLng
}
// Returns coordinates for box's center.
func (b *BoundingBox) Center() *LatLng {
return &b.center
}
// Returns coordinates for box's South-West corner.
func (b *BoundingBox) SouthWest() *LatLng {
return &b.sw
}
// Returns coordinates for box's North-East corner.
func (b *BoundingBox) NorthEast() *LatLng {
return &b.ne
}
// Struct for passing LatLng values.
type LatLng struct {
lat float64
lng float64
}
// Returns latitude.
func (ll *LatLng) Lat() float64 {
return ll.lat
}
// Returns longitude.
func (ll *LatLng) Lng() float64 {
return ll.lng
}
func refineInterval(interval []float64, cd, mask int) []float64 {
if cd&mask > 0 {
interval[0] = (interval[0] + interval[1]) / 2
} else {
interval[1] = (interval[0] + interval[1]) / 2
}
return interval
}
// Get LatLng coordinates from a geohash
func Decode(geohash string) *BoundingBox {
isEven := true
lat := []float64{-90, 90}
lng := []float64{-180, 180}
latErr := float64(90)
lngErr := float64(180)
var c string
var cd int
for i := 0; i < len(geohash); i++ {
c = geohash[i : i+1]
cd = bytes.Index(base32, []byte(c))
for j := 0; j < 5; j++ {
if isEven {
lngErr /= 2
lng = refineInterval(lng, cd, bits[j])
} else {
latErr /= 2
lat = refineInterval(lat, cd, bits[j])
}
isEven = !isEven
}
}
return &BoundingBox{
sw: LatLng{
lat: lat[0],
lng: lng[0],
},
ne: LatLng{
lat: lat[1],
lng: lng[1],
},
center: LatLng{
lat: (lat[0] + lat[1]) / 2,
lng: (lng[0] + lng[1]) / 2,
},
}
}
// Create a geohash based on LatLng coordinates
func Encode(latitude, longitude float64) string {
isEven := true
lat := []float64{-90, 90}
lng := []float64{-180, 180}
bit := 0
ch := 0
precision := 12
var geohash bytes.Buffer
var mid float64
for geohash.Len() < precision {
if isEven {
mid = (lng[0] + lng[1]) / 2
if longitude > mid {
ch |= bits[bit]
lng[0] = mid
} else {
lng[1] = mid
}
} else {
mid = (lat[0] + lat[1]) / 2
if latitude > mid {
ch |= bits[bit]
lat[0] = mid
} else {
lat[1] = mid
}
}
isEven = !isEven
if bit < 4 {
bit++
} else {
geohash.WriteByte(base32[ch])
bit = 0
ch = 0
}
}
return geohash.String()
}