-
Notifications
You must be signed in to change notification settings - Fork 0
/
geosearchfast.go
56 lines (47 loc) · 1.07 KB
/
geosearchfast.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
package geosearch
import (
"time"
)
type GeoSearchFast struct {
GeoSearch
seed map[int]*Object
}
func GeoSearchFastNew(distanceBetweenNeighbours float64) GeoSearchFast {
return GeoSearchFast{
GeoSearch: GeoSearchNew(distanceBetweenNeighbours),
seed: make(map[int]*Object),
}
}
func (gs *GeoSearchFast) AddObject(obj *Object) {
gs.GeoSearch.AddObject(obj)
lat := int(obj.Latitude) + 90
lng := int(obj.Longitude) + 180
idx := lat*1000 + lng
gs.seed[idx] = obj
}
func (gs *GeoSearchFast) Search(point Point) Result {
start := time.Now()
direct := [...][2]int{{0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {-1, -1}}
lat := int(point.Latitude) + 90
lng := int(point.Longitude) + 180
dir := 0
distance := 1
var result Result
for {
nextLat := lat + distance*direct[dir][0]
nextLng := lng + distance*direct[dir][1]
idx := nextLat*1000 + nextLng
seed, found := gs.seed[idx]
if found {
result = gs.GeoSearch.Search(point, seed)
break
}
dir++
if dir >= len(direct) {
dir = 0
distance++
}
}
result.Took = time.Since(start)
return result
}