-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
354 lines (339 loc) · 11.2 KB
/
index.js
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
var prepare = require('mixmap-georender/prepare')
var shaders = require('mixmap-georender')
var geotext = require('mixmap-georender/text')
var planner = require('viewbox-query-planner')
var getImagePixels = require('get-image-pixels')
var bboxIntersect = require('bbox-intersect')
var storageHooks = require('./lib/storage-hooks.js')
var Features = require('./lib/features.js')
module.exports = P
function P(opts) {
var self = this
if (!(self instanceof P)) return new P(opts)
self._map = opts.map
self._map.on('resize', function () {
self._scheduleRecalc()
})
self._trace = {}
self._loading = new Set
self._storage = storageHooks(opts.storage, {
beforeLength: function (name) {
self._loading.add(name)
},
afterLength: function (name) {
self._loading.delete(name)
},
beforeRead: function (name) {
self._loading.add(name)
},
afterRead: function (name) {
self._loading.delete(name)
},
})
self._dbQueue = []
self._features = new Features
opts.eyros({ storage: self._storage, wasmSource: opts.wasmSource })
.then(db => {
self._db = db
for (var i = 0; i < self._dbQueue.length; i++) {
self._dbQueue[i](db)
}
self._dbQueue = null
})
.catch(err => self._error(err))
self._stylePixels = null
self._styleTexture = null
self._styleQueue = null
if (!opts.style) {
throw new Error('opts.style must be an Image or { width, height, data } object')
} else if (opts.style.data) { // pass through data directly
self._stylePixels = opts.style.data
self._styleTexture = self._map.regl.texture(opts.style)
} else if (opts.style.complete) { // img loaded
self._stylePixels = getImagePixels(opts.style),
self._styleTexture = self._map.regl.texture(opts.style)
} else if (opts.style.addEventListener) {
self._styleQueue = []
opts.style.addEventListener('load', function (ev) {
self._stylePixels = getImagePixels(opts.style),
self._styleTexture = self._map.regl.texture(opts.style)
for (var i = 0; i < self._styleQueue.length; i++) {
self._styleQueue[i]()
}
self._styleQueue = null
})
} else {
throw new Error('unexpected opts.style value. expected Image or { width, height, data } object')
}
var geoRender = shaders(self._map)
self._geoRender = geoRender
self.draw = {
area: self._map.createDraw(geoRender.areas),
areaT: self._map.createDraw(geoRender.areas),
areaBorder: self._map.createDraw(geoRender.areaBorders),
areaBorderT: self._map.createDraw(geoRender.areaBorders),
lineStroke: self._map.createDraw(geoRender.lineStroke),
lineStrokeT: self._map.createDraw(geoRender.lineStroke),
lineFill: self._map.createDraw(geoRender.lineFill),
lineFillT: self._map.createDraw(geoRender.lineFill),
point: self._map.createDraw(geoRender.points),
pointT: self._map.createDraw(geoRender.points),
label: {},
}
self._zoom = -1
self._geodata = null
self._props = null
self._geotext = null
self._font = opts.font
self._getFont(function (err, font) {
if (font) {
self._geotext = geotext({ font })
self._scheduleRecalc()
}
})
self._plan = planner()
self._queryResults = []
self._lastQueryIndex = -1
self._queryCanceled = {}
self._queryOpen = {}
self._recalcTimer = null
self._recalcTime = 0
self.props = {}
self.layer = self._map.addLayer({
viewbox: function (bbox, zoom, cb) { self._onviewbox(bbox,zoom,cb) }
})
// for debugging purposes, safe to remove
self._debug = opts.debug ? console.log : noop
self._recalcTotalTime = 0
self._recalcCount = 0
}
P.prototype._error = function (err) {
console.error('CAUGHT', err)
}
P.prototype._onviewbox = function (bbox, zoom, cb) {
var self = this
var boxes = self._plan.update(bbox)
for (var i = 0; i < boxes.length; i++) {
self._plan.add(boxes[i])
}
self._zoom = zoom
self._getDb(function (db) {
boxes.forEach(bbox => {
db.query(bbox, { trace })
.then(async (q) => {
var now = performance.now()
await self._loadQuery(bbox, q)
self._debug('_loadQuery bbox', bbox, 'time', performance.now() - now, 'ms')
})
.catch(e => self._error(e))
function trace(tr) {
self._trace[tr.file] = tr
}
})
})
}
P.prototype._getDb = function (cb) {
var self = this
if (self._db) cb(self._db)
else self._dbQueue.push(cb)
}
P.prototype._getStyle = function (cb) {
if (this._stylePixels && this._styleTexture) {
cb(this._stylePixels, this._styleTexture)
} else {
if (!this._styleQueue) this._styleQueue = []
this._styleQueue.push(cb)
}
}
P.prototype._cull = function () {
var self = this
var culling = 0
for (var i = 0; i < self._queryResults.length; i++) {
var qr = self._queryResults[i]
if (qr === null) continue
if (!bboxIntersect(self._map.viewbox, qr.bbox)) {
culling++
if (self._queryOpen[qr.index]) {
self._queryCanceled[qr.index] = true
delete self._queryOpen[qr.index]
}
self._plan.subtract(qr.bbox)
self._queryResults[i] = null
self._features.cull(i)
}
}
for (var file of self._loading) {
var tr = self._trace[file]
if (!tr) continue
if (!bboxIntersect(self._map.viewbox, tr.bbox)) {
self._storage.destroy(file)
}
}
}
P.prototype._scheduleRecalc = function () {
var self = this
if (self._recalcTimer) return
var interval = Math.min(2000, 200 + this._recalcTime)
self._debug('_scheduleRecalc interval', interval)
self._recalcTimer = setTimeout(function () {
self._debug('_scheduleRecalc calling _recalc()')
self._recalc(true)
self._recalcTimer = null
}, interval)
}
P.prototype._loadQuery = async function loadQuery(bbox, q) {
var self = this
self._debug('_loadQuery bbox', bbox, 'q.ptr', q.ptr)
var row
var index = ++self._lastQueryIndex
self._queryOpen[index] = true
self._queryResults.push({ bbox, index })
var rowCount = 0
while (row = await q.next()) {
if (self._queryCanceled[index]) {
self._scheduleRecalc()
return
}
++rowCount
self._features.addBuffer(index, Buffer.from(row[1]))
self._scheduleRecalc()
}
self._debug('_loadQuery number of rows', rowCount)
self._decodedCache = null
delete self._queryOpen[index]
}
P.prototype._recalc = function(fromScheduled) {
var self = this
if (!fromScheduled && !self._recalcTimer) {
self._debug('something is calling _recalc() directly and nothing is scheduled')
} else if (!fromScheduled && self._recalcTimer) {
self._debug('something is calling _recalc() directly while already scheduled')
return
}
self._getStyle(function (stylePixels, styleTexture) {
self._debug('- BEGIN _recalc() #', ++self._recalcCount)
var start = performance.now()
// todo: compare all-in-one props against pushing more props
self._cull()
var zoom = Math.round(self._map.getZoom())
var prepTime = performance.now()
self._geodata = prepare({
stylePixels: self._stylePixels,
styleTexture: self._styleTexture,
decoded: self._features.getDecoded(),
})
self._debug('-- PERF time prep', performance.now() - prepTime, 'ms')
var propsTime = performance.now()
var props = self._geodata.update(zoom)
setProps(self.draw.point.props, props.pointP)
setProps(self.draw.lineFill.props, props.lineP)
setProps(self.draw.lineStroke.props, props.lineP)
setProps(self.draw.area.props, props.areaP)
setProps(self.draw.areaBorder.props, props.areaBorderP)
setProps(self.draw.pointT.props, props.pointT)
setProps(self.draw.lineFillT.props, props.lineT)
setProps(self.draw.lineStrokeT.props, props.lineT)
setProps(self.draw.areaT.props, props.areaT)
setProps(self.draw.areaBorderT.props, props.areaBorderT)
self._debug('-- PERF time props', performance.now() - propsTime, 'ms')
var geoTextTime = performance.now()
if (self._geotext) {
var textProps = self._geotext.update(props, self._map)
var ns = Object.keys(textProps)
for (var i = 0; i < ns.length; i++) {
var n = ns[i]
if (!self.draw.label[n]) {
self.draw.label[n] = self._map.createDraw(self._geoRender.labels(n))
}
setProps(self.draw.label[n].props, textProps[n])
}
}
self._debug('-- PERF time geo text', performance.now() - geoTextTime, 'ms')
self._map.draw()
self._recalcTime = performance.now() - start
self._debug('-- PERF time _recalc()', self._recalcTime, 'ms')
self._recalcTotalTime += self._recalcTime
self._debug('-- PERF acc time _recalc()', self._recalcTotalTime, 'ms')
self._debug('- END _recalc()')
})
}
function setProps(dst, src) {
if (dst.length === 0) dst.push({})
Object.assign(dst[0],src)
}
P.prototype._getFont = function (cb) {
var self = this
var r0 = self._font
if (!r0) return cb(null, null)
if (typeof self._font === 'function') {
r0 = self._font(cb)
}
if (r0 && typeof r0.then === 'function') {
r0.then(r1 => {
if (r1 && typeof r1.arrayBuffer === 'function') {
var r2 = r1.arrayBuffer()
if (r2 && typeof r2.then === 'function') {
r2.then(r3 => cb(null, new Uint8Array(r3))).catch(cb)
} else {
cb(null, new Uint8Array(r2))
}
} else {
cb(null, r1)
}
}).catch(cb)
} else {
cb(null, r0)
}
}
P.prototype.pick = function (opts, cb) {
var self = this
if (!cb) cb = noop
this._map.pick(opts, function (err, data) {
if (err) return cb(err)
if (feq(data[0],0.0)) {
cb(null, { id: null, type: null })
} else if (Math.floor(data[2]/2) === 0 && data[2]%2 < 0.9999) {
if (self.draw.pointT.props.length === 0) {
cb(null, { id: null, type: null })
} else {
cb(null, { id: self.draw.pointT.props[0].indexToId[data[0]], type: data[1] })
}
} else if (Math.floor(data[2]/2) === 0) {
if (self.draw.point.props.length === 0) {
cb(null, { id: null, type: null })
} else {
cb(null, { id: self.draw.point.props[0].indexToId[data[0]], type: data[1] })
}
} else if (Math.floor(data[2]/2) === 1 && data[2]%2 < 0.9999) {
if (self.draw.lineFillT.props.length === 0) {
cb(null, { id: null, type: null })
} else {
cb(null, { id: self.draw.lineFillT.props[0].indexToId[data[0]], type: data[1] })
}
} else if (Math.floor(data[2]/2) === 1) {
if (self.draw.lineFill.props.length === 0) {
cb(null, { id: null, type: null })
} else {
cb(null, { id: self.draw.lineFill.props[0].indexToId[data[0]], type: data[1] })
}
} else if (Math.floor(data[2]/2) === 2 && data[2]%2 < 0.9999) {
if (self.draw.area.props.length === 0) {
cb(null, { id: null, type: null })
} else {
cb(null, { id: self.draw.area.props[0].indexToId[data[0]], type: data[1] })
}
} else if (Math.floor(data[2]/2) === 2) {
if (self.draw.area.props.length === 0) {
cb(null, { id: null, type: null })
} else {
cb(null, { id: self.draw.area.props[0].indexToId[data[0]], type: data[1] })
}
} else {
cb(null, { id: null, type: null })
}
})
}
function feq(a,b,epsilon) {
return Math.abs(a-b) < (epsilon === undefined ? 1e-6 : epsilon)
}
function noop() {}