-
Notifications
You must be signed in to change notification settings - Fork 4
/
geohashtree.go
269 lines (237 loc) · 6.15 KB
/
geohashtree.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package geohashtree
/*
Flat, memory bound, k/v store based point-in-polygon alg. using geohash.
*/
import (
"github.com/mmcloughlin/geohash"
"math"
)
// Extrema structure (Bounding Box)
type Extrema struct {
W float64
E float64
N float64
S float64
}
// gets the extrema object of a given geohash
func GetExtrema(ghash string) Extrema {
box := geohash.BoundingBox(ghash)
return Extrema{S: box.MinLat, W: box.MinLng, N: box.MaxLat, E: box.MaxLng}
}
// gets the extrema object of a given geohash
func Middle(ghash string) []float64 {
box := geohash.BoundingBox(ghash)
lat, lng := box.Center()
return []float64{lng, lat}
}
// gets a geohash
func Geohash(pt []float64, size int) string {
return geohash.EncodeWithPrecision(pt[1], pt[0], uint(size))
}
// drill geohash
func ExpandGeohash(geohash string) []string {
newlist := make([]string, 32)
for pos, i := range "0123456789bcdefghjkmnpqrstuvwxyz" {
newlist[pos] = geohash + string(i)
}
return newlist
}
// gets the starting geohashs from the bounds and minimum precision
func GetStartingHashs(bds Extrema, minp int) []string {
en := Geohash([]float64{bds.E, bds.N}, minp)
es := Geohash([]float64{bds.E, bds.S}, minp)
wn := Geohash([]float64{bds.W, bds.N}, minp)
ws := Geohash([]float64{bds.W, bds.S}, minp)
// creating map and adding each corner geohash
newmap := map[string]string{}
newmap[en] = ""
newmap[es] = ""
newmap[wn] = ""
newmap[ws] = ""
// starting the iterating process
bdsg := GetExtrema(en)
deltax := bdsg.E - bdsg.W
deltay := bdsg.N - bdsg.S
for i := bds.W; i < bdsg.E; i += deltax {
for j := bds.S; j < bdsg.N; j += deltay {
ghash := Geohash([]float64{i, j}, minp)
newmap[ghash] = ""
}
}
newlist := make([]string, len(newmap))
i := 0
for k := range newmap {
newlist[i] = k
i++
}
return newlist
}
// The polygon structure used to build the index.
type Poly struct {
Polygon [][][]float64
Extrema Extrema // extrema of polygon
Map map[int]map[string]string // map of precision geohash fro every precision
Min, Max int // minimum and maximum percentage
}
// creates the polygon structure
func CreatePolygon(polygon [][][]float64, minp, maxp int) *Poly {
west, south, east, north := 180.0, 90.0, -180.0, -90.0
maxpmap := map[string]string{}
for _, cont := range polygon {
for _, pt := range cont {
x, y := pt[0], pt[1]
// can only be one condition
// using else if reduces one comparison
if x < west {
west = x
} else if x > east {
east = x
}
if y < south {
south = y
} else if y > north {
north = y
}
ghash := Geohash(pt, maxp)
maxpmap[ghash] = ""
}
}
// logic for creating the smaller maps of each geohash
currentmap := maxpmap
totalmap := map[int]map[string]string{}
totalmap[maxp] = maxpmap
for i := maxp - 1; i >= minp; i-- {
newmap := map[string]string{}
for k := range currentmap {
newmap[k[:len(k)-1]] = ""
}
totalmap[i] = newmap
currentmap = newmap
}
return &Poly{
Polygon: polygon,
Extrema: Extrema{N: north, S: south, E: east, W: west},
Map: totalmap,
Min: minp,
Max: maxp,
}
}
func (cont Poly) Pip(p []float64) bool {
// Cast ray from p.x towards the right
intersections := 0
for _, c := range cont.Polygon {
for i := range c {
curr := c[i]
ii := i + 1
if ii == len(c) {
ii = 0
}
next := c[ii]
// Is the point out of the edge's bounding box?
// bottom vertex is inclusive (belongs to edge), top vertex is
// exclusive (not part of edge) -- i.e. p lies "slightly above
// the ray"
bottom, top := curr, next
if bottom[1] > top[1] {
bottom, top = top, bottom
}
if p[1] < bottom[1] || p[1] >= top[1] {
continue
}
// Edge is from curr to next.
if p[0] >= math.Max(curr[0], next[0]) ||
next[1] == curr[1] {
continue
}
// Find where the line intersects...
xint := (p[1]-curr[1])*(next[0]-curr[0])/(next[1]-curr[1]) + curr[0]
if curr[0] != next[0] && p[0] > xint {
continue
}
intersections++
}
}
return intersections%2 != 0
}
// gettting a hard point in polygon
func (polygon Poly) HardPip(geohash string) int {
size := len(geohash)
_, boolval := polygon.Map[size][geohash]
if boolval {
return 2
}
count := 0
bds := GetExtrema(geohash)
pts := [][]float64{{bds.W, bds.N}, {bds.E, bds.N}, {bds.E, bds.S}, {bds.W, bds.S}} // wn,en,es,ws
for _, i := range pts {
if polygon.Pip(i) {
count += 1
}
}
// if within
if count == len(pts) {
return 1
} else if count > 0 {
// part within
return 2
} else {
// out
return 3
}
return 3
}
// drills a single geohash to the maximum precision
func (poly *Poly) DrillGeohash(geohash string, newlist []string) []string {
// creating channel
c := make(chan []string)
// checking to see if the geohash starting is within the polygon
// covering a corner case
if poly.Min == len(geohash) {
hardpip := poly.HardPip(geohash)
if hardpip == 1 {
return append(newlist, geohash)
}
}
// iterating through expanded geohash
for _, newgeohash := range ExpandGeohash(geohash) {
if len(newgeohash) <= poly.Max {
hardpip := poly.HardPip(newgeohash)
go func(newgeohash string, hardpip int, c chan []string) {
if hardpip == 1 {
c <- []string{newgeohash}
} else if hardpip == 2 {
c <- poly.DrillGeohash(newgeohash, []string{})
} else {
c <- []string{}
}
}(newgeohash, hardpip, c)
} else {
go func(c chan []string) {
c <- []string{}
}(c)
}
}
for count := 0; count < 32; count++ {
newlist = append(newlist, <-c...)
}
return newlist
}
// creates a polygon index given a polygon a min & max geohash precision
// returns a string with all geohashs that are within the polygon.
func MakePolygonIndex(polygon [][][]float64, minp, maxp int) []string {
poly := CreatePolygon(polygon, minp, maxp)
// getting staritng geohashs
s_geohashs := GetStartingHashs(poly.Extrema, minp)
// iterating through starting geohashs
c := make(chan []string)
for _, ghash := range s_geohashs {
go func(ghash string, c chan []string) {
c <- poly.DrillGeohash(ghash, []string{})
}(ghash, c)
}
total := []string{}
for range s_geohashs {
total = append(total, <-c...)
}
return total
}