forked from hailocab/go-geoindex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-index.go
72 lines (57 loc) · 1.92 KB
/
count-index.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
package geoindex
import (
"fmt"
)
type CountIndex struct {
index *geoIndex
currentPosition map[string]Point
}
type CountPoint struct {
*GeoPoint
Count interface{}
}
func (p *CountPoint) String() string {
return fmt.Sprintf("%f %f %d", p.Lat(), p.Lon(), p.Count)
}
// NewCountIndex creates an index which counts the points in each cell.
func NewCountIndex(resolution Meters) *CountIndex {
newCounter := func() interface{} {
return &singleValueAccumulatingCounter{}
}
return &CountIndex{newGeoIndex(resolution, newCounter), make(map[string]Point)}
}
// NewExpiringCountIndex creates an index, which maintains an expiring counter for each cell.
func NewExpiringCountIndex(resolution Meters, expiration Minutes) *CountIndex {
newExpiringCounter := func() interface{} {
return newExpiringCounter(expiration)
}
return &CountIndex{newGeoIndex(resolution, newExpiringCounter), make(map[string]Point)}
}
// Add adds a point.
func (countIndex *CountIndex) Add(point Point) {
countIndex.Remove(point.Id())
countIndex.currentPosition[point.Id()] = point
countIndex.index.AddEntryAt(point).(counter).Add(point)
}
// Remove removes a point.
func (countIndex *CountIndex) Remove(id string) {
if prev, ok := countIndex.currentPosition[id]; ok {
countIndex.index.GetEntryAt(prev).(counter).Remove(prev)
delete(countIndex.currentPosition, id)
}
}
// Range returns the counters within some lat, lng range.
func (countIndex *CountIndex) Range(topLeft Point, bottomRight Point) []Point {
counters := countIndex.index.Range(topLeft, bottomRight)
points := make([]Point, 0)
for _, c := range counters {
if c.(counter).Point() != nil {
points = append(points, c.(counter).Point())
}
}
return points
}
// KNearest just to satisfy an interface. Doesn't make much sense for count index.
func (index *CountIndex) KNearest(point Point, k int, maxDistance Meters, accept func(p Point) bool) []Point {
panic("Unsupported operation")
}