forked from hailocab/go-geoindex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clustering-index.go
66 lines (55 loc) · 2.03 KB
/
clustering-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
package geoindex
type ClusteringIndex struct {
streetLevel *PointsIndex
cityLevel *CountIndex
worldLevel *CountIndex
}
var (
streetLevel = Km(45)
cityLevel = Km(1000)
)
// NewClusteringIndex creates index that clusters the points at three levels with cell size 0.5, 5 and 500km.
// Useful for creating maps.
func NewClusteringIndex() *ClusteringIndex {
index := &ClusteringIndex{}
index.streetLevel = NewPointsIndex(Km(0.5))
index.cityLevel = NewCountIndex(Km(10))
index.worldLevel = NewCountIndex(Km(500))
return index
}
// NewExpiringClusteringIndex creates index that clusters the points at three levels with cell size 0.5, 5 and 500km and
// expires them after expiration minutes.
func NewExpiringClusteringIndex(expiration Minutes) *ClusteringIndex {
index := &ClusteringIndex{}
index.streetLevel = NewExpiringPointsIndex(Km(0.5), expiration)
index.cityLevel = NewExpiringCountIndex(Km(10), expiration)
index.worldLevel = NewExpiringCountIndex(Km(500), expiration)
return index
}
// Add adds a point.
func (index *ClusteringIndex) Add(point Point) {
index.streetLevel.Add(point)
index.cityLevel.Add(point)
index.worldLevel.Add(point)
}
// Remove removes a point.
func (index *ClusteringIndex) Remove(id string) {
index.streetLevel.Remove(id)
index.cityLevel.Remove(id)
index.worldLevel.Remove(id)
}
// Range returns points or count points depending on the size of the topLeft and bottomRight range.
func (index *ClusteringIndex) Range(topLeft Point, bottomRight Point) []Point {
dist := distance(topLeft, bottomRight)
if dist < streetLevel {
return index.streetLevel.Range(topLeft, bottomRight)
} else if dist < cityLevel {
return index.cityLevel.Range(topLeft, bottomRight)
} else {
return index.worldLevel.Range(topLeft, bottomRight)
}
}
// KNearest returns the K-Nearest points near point within maxDistance, that match the accept function.
func (index *ClusteringIndex) KNearest(point Point, k int, maxDistance Meters, accept func(p Point) bool) []Point {
return index.streetLevel.KNearest(point, k, maxDistance, accept)
}