/
featurecache.go
163 lines (135 loc) · 3.13 KB
/
featurecache.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
package main
import (
"sync"
"github.com/thomersch/grandine/lib/spatial"
"github.com/thomersch/grandine/lib/tile"
)
type cacheInitFunc func([]int) (FeatureCache, error)
var (
caches map[string]cacheInitFunc
cacheinit sync.Once
)
func init() {
registerCache("table", func(zl []int) (FeatureCache, error) {
nt, err := NewFeatureTable(zl)
return nt, err
})
registerCache("map", func(zl []int) (FeatureCache, error) {
nm, err := NewFeatureMap(zl)
return nm, err
})
}
func registerCache(name string, c cacheInitFunc) {
cacheinit.Do(func() {
caches = make(map[string]cacheInitFunc)
})
caches[name] = c
}
func availableCaches() []string {
var cc []string
for k := range caches {
cc = append(cc, k)
}
return cc
}
type FeatureCache interface {
AddFeature(spatial.Feature)
GetFeatures(tile.ID) []spatial.Feature
BBox() spatial.BBox
Count() int
}
type FeatureTable struct {
Zoomlevels []int
count int
table map[int][][][]spatial.Feature
bbox *spatial.BBox
}
func NewFeatureTable(zoomlevels []int) (*FeatureTable, error) {
ftab := FeatureTable{Zoomlevels: zoomlevels}
ftab.table = map[int][][][]spatial.Feature{}
for _, zl := range ftab.Zoomlevels {
l := pow(2, zl)
ftab.table[zl] = make([][][]spatial.Feature, l)
for x := range ftab.table[zl] {
ftab.table[zl][x] = make([][]spatial.Feature, l)
}
}
return &ftab, nil
}
func (ftab *FeatureTable) AddFeature(ft spatial.Feature) {
for _, zl := range ftab.Zoomlevels {
if !renderable(ft.Props, zl) {
continue
}
for _, tid := range tile.Coverage(ft.Geometry.BBox(), zl) {
ftab.table[zl][tid.X][tid.Y] = append(ftab.table[zl][tid.X][tid.Y], ft)
}
}
if ftab.bbox == nil {
var bb = ft.Geometry.BBox()
ftab.bbox = &bb
} else {
ftab.bbox.ExtendWith(ft.Geometry.BBox())
}
ftab.count++
}
func (ftab *FeatureTable) GetFeatures(tid tile.ID) []spatial.Feature {
return ftab.table[tid.Z][tid.X][tid.Y]
}
func (ftab *FeatureTable) Count() int {
return ftab.count
}
func (ftab *FeatureTable) BBox() spatial.BBox {
if ftab.bbox == nil {
return spatial.BBox{}
}
return *ftab.bbox
}
type FeatureMap struct {
Zoomlevels []int
bbox *spatial.BBox
count int
m map[tile.ID][]spatial.Feature
}
func NewFeatureMap(zl []int) (*FeatureMap, error) {
return &FeatureMap{
Zoomlevels: zl,
m: map[tile.ID][]spatial.Feature{},
}, nil
}
func (fm *FeatureMap) AddFeature(ft spatial.Feature) {
for _, zl := range fm.Zoomlevels {
if !renderable(ft.Props, zl) {
continue
}
for _, tid := range tile.Coverage(ft.Geometry.BBox(), zl) {
_, ok := fm.m[tid]
if !ok {
fm.m[tid] = make([]spatial.Feature, 0, 1)
}
fm.m[tid] = append(fm.m[tid], ft)
}
}
if fm.bbox == nil {
var bb = ft.Geometry.BBox()
fm.bbox = &bb
} else {
fm.bbox.ExtendWith(ft.Geometry.BBox())
}
fm.count++
}
func (fm *FeatureMap) GetFeatures(tid tile.ID) []spatial.Feature {
return fm.m[tid]
}
func (fm *FeatureMap) BBox() spatial.BBox {
return *fm.bbox
}
func (fm *FeatureMap) Count() int {
return fm.count
}
func (fm *FeatureMap) Dump() map[tile.ID][]spatial.Feature {
return fm.m
}
func (fm *FeatureMap) Clear() {
fm.m = map[tile.ID][]spatial.Feature{}
}