-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster.go
334 lines (281 loc) · 9.72 KB
/
cluster.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package cluster
import (
"context"
"errors"
"math"
"github.com/electrious-go/kdbush"
)
const (
// InfinityZoomLevel indicate impossible large zoom level (Cluster's max is 21).
InfinityZoomLevel = 100
)
var (
ErrInvalidCoordinates = errors.New("invalid NW or SE coordinates")
)
// Cluster struct get a list or stream of geo objects
// and produce all levels of clusters
// Zoom range is limited by 0 to 21, and MinZoom could not be larger, then MaxZoom.
type Cluster struct {
// MinZoom minimum zoom level to generate clusters
MinZoom int
// MaxZoom maximum zoom level to generate clusters
MaxZoom int
// PointSize pixel size of marker, affects clustering radius
PointSize int
// TileSize size of tile in pixels, affects clustering radius
TileSize int
// NodeSize is size of the KD-tree node, 64 by default. Higher means faster indexing but slower search, and vise versa.
NodeSize int
// Indexes keeps all KDBush trees
Indexes []*kdbush.KDBush
// Points keeps original slice of given points
Points []GeoPoint
clusterIdxSeed int
}
// New create new Cluster instance with default params.
// Will use points and create multilevel clustered indexes.
// All points should implement GeoPoint interface.
// They are not copied in favor of memory efficiency.
// GetCoordinates called only once for each object. Can be recalculated on the fly, if needed.
func New(points []GeoPoint, opts ...Option) (*Cluster, error) {
cluster := &Cluster{
MinZoom: 0,
MaxZoom: 21,
PointSize: 40, // 240
TileSize: 512,
NodeSize: 64,
}
for _, opt := range opts {
if err := opt(cluster); err != nil {
return nil, err
}
}
// limit max Zoom
if cluster.MaxZoom > 21 {
cluster.MaxZoom = 21
}
// cluster.MaxZoom--
// adding extra layer for infinite zoom (initial) layers data storage
cluster.Indexes = make([]*kdbush.KDBush, cluster.MaxZoom-cluster.MinZoom+2)
cluster.Points = points
// get digits number, start from next exponent
// if we have 78, all cluster will start from 100...
// if we have 986 points, all clusters ids will start from 1000
cluster.clusterIdxSeed = int(math.Pow(10, float64(digitsCount(len(points)))))
clusters := translateGeoPointsToPoints(points)
for z := cluster.MaxZoom; z >= cluster.MinZoom; z-- {
// create index from clusters from previous iteration
cluster.Indexes[z+1-cluster.MinZoom] = kdbush.NewBush(clustersToPoints(clusters), cluster.NodeSize)
// create clusters for level up using just created index
clusters = cluster.clusterize(clusters, z)
}
// index topmost points
cluster.Indexes[0] = kdbush.NewBush(clustersToPoints(clusters), cluster.NodeSize)
return cluster, nil
}
// GetClusters returns the array of clusters for zoom level.
// The northWest and southEast points are boundary points of square, that should be returned.
// northWest is left topmost point, southEast is right bottom point.
// returns the array of clustered points,
// X coordinate of returned object is Longitude and Y coordinate of returned object is Latitude.
func (c *Cluster) GetClusters(northWest, southEast GeoPoint, zoom int, limit int) ([]Point, error) {
// According to benchmarks, implementation without a context is only 75 ns/op faster,
// not worth it to duplicate the functions.
return c.GetClustersWithContext(context.TODO(), northWest, southEast, zoom, limit)
}
// GetClustersWithContext returns the array of clusters for zoom level.
// The northWest and southEast points are boundary points of square, that should be returned.
// northWest is left topmost point, southEast is right bottom point.
// returns the array of clustered points,
// X coordinate of returned object is Longitude and Y coordinate of returned object is Latitude.
// Returns error when context is closed or provided NW or SE geo points are invalid.
func (c *Cluster) GetClustersWithContext(ctx context.Context, northWest, southEast GeoPoint, zoom, limit int) ([]Point, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
nw := northWest.GetCoordinates()
se := southEast.GetCoordinates()
if nw == nil || se == nil {
return nil, ErrInvalidCoordinates
}
// Original mapbox/supercluster library code has the following expression to calculate min and max longitudes:
// let minLng = ((bbox[0] + 180) % 360 + 360) % 360 - 180;
// Mozilla developer guide suggests such construction to obtain a modulo
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder:
// > To obtain a modulo in JavaScript, in place of a % n, use ((a % n ) + n ) % n.
minLng := math.Mod(math.Mod(nw.Lng+180, 360)+360, 360) - 180
minLat := math.Max(-90, math.Min(90, se.Lat))
var maxLng float64
if se.Lng == 180 {
maxLng = 180
} else {
maxLng = math.Mod(math.Mod(se.Lng+180, 360)+360, 360) - 180
}
maxLat := math.Max(-90, math.Min(90, nw.Lat))
if se.Lng-nw.Lng >= 360 {
minLng = -180
maxLng = 180
} else if minLng > maxLng {
easternHem, err := c.GetClusters(&Point{X: minLng, Y: maxLat}, &Point{X: 180, Y: minLat}, zoom, limit)
if err != nil {
return nil, err
}
westernHem, err := c.GetClusters(&Point{X: -180, Y: maxLat}, &Point{X: maxLng, Y: minLat}, zoom, limit)
if err != nil {
return nil, err
}
return append(easternHem, westernHem...), nil
}
zoom = c.LimitZoom(zoom) - c.MinZoom
index := c.Indexes[zoom]
nwX, nwY := MercatorProjection(GeoCoordinates{Lng: minLng, Lat: maxLat})
seX, seY := MercatorProjection(GeoCoordinates{Lng: maxLng, Lat: minLat})
ids := index.Range(nwX, nwY, seX, seY)
if (limit > 0) && (len(ids) > limit) {
ids = ids[:limit]
}
result := make([]Point, len(ids))
for i := range ids {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
p := index.Points[ids[i]].(*Point)
cp := *p
coordinates := ReverseMercatorProjection(cp.X, cp.Y)
cp.X = coordinates.Lng
cp.Y = coordinates.Lat
result[i] = cp
}
}
return result, nil
}
// GetClustersPointsInRadius will return child points for specific cluster
// this is done with kdbush.Within method allowing fast search.
func (c *Cluster) GetClustersPointsInRadius(clusterID int) []*Point {
// if clusterID is smaller than initial seed
// it means that it is original point from which
// cluster(s) are made
if clusterID < c.clusterIdxSeed {
return nil
}
originIndex := (clusterID >> 5) - c.clusterIdxSeed
originZoom := (clusterID % 32) - 1
originTree := c.Indexes[originZoom]
originPoint := originTree.Points[originIndex]
r := float64(c.PointSize) / float64(c.TileSize*(1<<uint(originZoom)))
treeBelow := c.Indexes[originZoom+1-c.MinZoom]
ids := treeBelow.Within(originPoint, r)
var children []*Point
for _, i := range ids {
children = append(children, treeBelow.Points[i].(*Point))
}
return children
}
// GetClusterExpansionZoom will return how much you need to zoom to get to a next cluster.
func (c *Cluster) GetClusterExpansionZoom(clusterID int) int {
if clusterID < c.clusterIdxSeed {
return c.MaxZoom
}
clusterZoom := (clusterID % 32) - 1
id := clusterID
for clusterZoom < c.MaxZoom {
children := c.GetClustersPointsInRadius(id)
// nil means it is point not cluster
if children == nil {
return c.MaxZoom
}
clusterZoom++
if clusterZoom >= c.MaxZoom+1 {
return c.MaxZoom
}
// in case it's more than 1, then return current zoom
if len(children) != 1 {
break
}
id = children[0].ID
}
return clusterZoom
}
// AllClusters returns all cluster points, array of Point, for zoom on the map.
// X coordinate of returned object is Longitude and Y coordinate is Latitude.
func (c *Cluster) AllClusters(zoom int, limit int) []Point {
index := c.Indexes[c.LimitZoom(zoom)-c.MinZoom]
points := index.Points
if (limit > 0) && (len(points) > limit) {
points = points[:limit]
}
result := make([]Point, len(points))
for i := range points {
p := index.Points[i].(*Point)
cp := *p
coordinates := ReverseMercatorProjection(cp.X, cp.Y)
cp.X = coordinates.Lng
cp.Y = coordinates.Lat
result[i] = cp
}
return result
}
// clusterize points for zoom level.
func (c *Cluster) clusterize(points []*Point, zoom int) []*Point {
var result []*Point
r := float64(c.PointSize) / float64(c.TileSize*(1<<uint(zoom)))
index := 0
// iterate all clusters
for pi := range points {
// skip points we have already clustered
p := points[pi]
if p.zoom <= zoom {
continue
}
// mark this point as visited
p.zoom = zoom
// find all neighbours
tree := c.Indexes[zoom+1-c.MinZoom]
neighbourIds := tree.Within(&kdbush.SimplePoint{X: p.X, Y: p.Y}, r)
nPoints := p.NumPoints
wx := p.X * float64(nPoints)
wy := p.Y * float64(nPoints)
var foundNeighbours []*Point
for j := range neighbourIds {
b := points[neighbourIds[j]]
// filter out neighbours, that are processed already (and processed point "p" as well)
if zoom < b.zoom {
wx += b.X * float64(b.NumPoints)
wy += b.Y * float64(b.NumPoints)
nPoints += b.NumPoints
b.zoom = zoom // set the zoom to skip in other iterations
foundNeighbours = append(foundNeighbours, b)
}
}
newCluster := p
// create new cluster
if len(foundNeighbours) > 0 {
newCluster = &Point{}
newCluster.X = wx / float64(nPoints)
newCluster.Y = wy / float64(nPoints)
newCluster.NumPoints = nPoints
newCluster.zoom = InfinityZoomLevel
// create ID based on seed + index
// this is then shifted to create space for zoom
// this is useful when you need extract zoom from ID
newCluster.ID = ((c.clusterIdxSeed + index) << 5) + zoom + 1
newCluster.Included = p.Included
for _, neighbour := range foundNeighbours {
newCluster.Included = append(newCluster.Included, neighbour.Included...)
}
}
result = append(result, newCluster)
index++
}
return result
}
func (c *Cluster) LimitZoom(zoom int) int {
if zoom > c.MaxZoom {
zoom = c.MaxZoom
}
if zoom < c.MinZoom {
zoom = c.MinZoom
}
return zoom
}