-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathutils.js
449 lines (377 loc) · 11.3 KB
/
utils.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import { point, rhumbBearing, rhumbDestination } from '@turf/turf'
import { select } from 'd3-selection'
const d2r = Math.PI / 180
const clip = (v, max) => {
let result
if (v < 0) {
result = v + max + 1
} else if (v > max) {
result = v - max - 1
} else {
result = v
}
return Math.min(Math.max(result, 0), max)
}
export const keyToTile = (key) => {
return key.split(',').map((d) => parseInt(d))
}
export const tileToKey = (tile) => {
return tile.join(',')
}
export const pointToTile = (lon, lat, z) => {
const z2 = Math.pow(2, z)
let tile = pointToCamera(lon, lat, z)
tile[0] = Math.floor(tile[0])
tile[1] = Math.min(Math.floor(tile[1]), z2 - 1)
return tile
}
export const pointToCamera = (lon, lat, z) => {
let sin = Math.sin(lat * d2r),
z2 = Math.pow(2, z),
x = z2 * (lon / 360 + 0.5),
y = z2 * (0.5 - (0.25 * Math.log((1 + sin) / (1 - sin))) / Math.PI)
x = x % z2
y = Math.max(Math.min(y, z2), 0)
if (x < 0) x = x + z2
return [x, y, z]
}
export const cameraToPoint = (x, y, z) => {
const z2 = Math.pow(2, z)
const lon = 360 * (x / z2) - 180
const y2 = 180 - (y / z2) * 360
const lat = (360 / Math.PI) * Math.atan(Math.exp((y2 * Math.PI) / 180)) - 90
return [lon, lat]
}
export const zoomToLevel = (zoom, maxZoom) => {
if (maxZoom) return Math.min(Math.max(0, Math.floor(zoom)), maxZoom)
return Math.max(0, Math.floor(zoom))
}
const getOffsets = (length, tileSize, camera) => {
const siblingCount = (length - tileSize) / tileSize
// Do not add offset for very small fraction of tile
if (Math.abs(siblingCount) < 0.001) {
return [0, 0]
}
const cameraOffset = camera - Math.floor(camera)
const prev = siblingCount / 2 + 0.5 - cameraOffset
const next = siblingCount - prev
return [-1 * Math.ceil(prev), Math.ceil(next)]
}
export const getSiblings = (tile, { viewport, zoom, size, camera }) => {
const [tileX, tileY, tileZ] = tile
const { viewportHeight, viewportWidth } = viewport
const [cameraX, cameraY] = camera
const magnification = Math.pow(2, zoom - tileZ)
const scale = (window.devicePixelRatio * 512) / size
const tileSize = size * scale * magnification
const deltaX = getOffsets(viewportWidth, tileSize, cameraX)
const deltaY = getOffsets(viewportHeight, tileSize, cameraY)
let offsets = []
for (let x = deltaX[0]; x <= deltaX[1]; x++) {
for (let y = deltaY[0]; y <= deltaY[1]; y++) {
offsets.push([tileX + x, tileY + y, tileZ])
}
}
const max = Math.pow(2, tileZ) - 1
return offsets.reduce((accum, offset) => {
const [x, y, z] = offset
const tile = [clip(x, max), clip(y, max), z]
const key = tileToKey(tile)
if (!accum[key]) {
accum[key] = []
}
accum[key].push(offset)
return accum
}, {})
}
export const getKeysToRender = (targetKey, tiles, maxZoom) => {
const ancestor = getAncestorToRender(targetKey, tiles)
if (ancestor) {
return [ancestor]
}
const descendants = getDescendantsToRender(targetKey, tiles, maxZoom)
if (descendants.length) {
return descendants
}
return [targetKey]
}
export const getAncestorToRender = (targetKey, tiles) => {
let [x, y, z] = keyToTile(targetKey)
while (z >= 0) {
const key = tileToKey([x, y, z])
if (tiles[key].isBufferPopulated()) {
return key
}
z--
x = Math.floor(x / 2)
y = Math.floor(y / 2)
}
}
export const getDescendantsToRender = (targetKey, tiles, maxZoom) => {
let [initialX, initialY, initialZ] = keyToTile(targetKey)
let [x, y, z] = [initialX, initialY, initialZ]
let coverage = 0
let descendants = []
while (z <= maxZoom) {
const delta = z - initialZ
const keys = []
for (let deltaX = 0; deltaX <= delta; deltaX++) {
for (let deltaY = 0; deltaY <= delta; deltaY++) {
keys.push(tileToKey([x + deltaX, y + deltaY, z]))
}
}
const coveringKeys = keys.filter((key) => tiles[key].isBufferPopulated())
const currentCoverage = coveringKeys.length / keys.length
if (coverage === 1) {
return keys
} else if (currentCoverage > coverage) {
descendants = keys
}
z++
x = x * 2
y = y * 2
}
return descendants
}
export const getOverlappingAncestor = (key, renderedKeys) => {
const [aX, aY, aZ] = keyToTile(key)
const child = { x: aX, y: aY, z: aZ }
return renderedKeys.find((parentKey) => {
const [bX, bY, bZ] = keyToTile(parentKey)
const parent = { x: bX, y: bY, z: bZ }
if (child.z <= parent.z) {
return false
} else {
const factor = Math.pow(2, child.z - parent.z)
return (
Math.floor(child.x / factor) === parent.x &&
Math.floor(child.y / factor) === parent.y
)
}
})
}
export const getAdjustedOffset = (offset, renderedKey) => {
const [renderedX, renderedY, renderedLevel] = keyToTile(renderedKey)
const [offsetX, offsetY, level] = offset
// Overall factor to scale offset by
const factor = Math.pow(2, level - renderedLevel)
// Factor used to calculate adjustment when rendering a descendant tile
const descendantFactor =
renderedLevel > level ? Math.pow(2, renderedLevel - level) : 1
return [
Math.floor(offsetX / factor) + (renderedX % descendantFactor),
Math.floor(offsetY / factor) + (renderedY % descendantFactor),
]
}
export const getTilesOfRegion = (region, level) => {
const { center, radius, units } = region.properties
const centralTile = pointToTile(center.lng, center.lat, level)
const tiles = new Set([tileToKey(centralTile)])
region.geometry.coordinates[0].forEach(([lng, lat]) => {
// Add tile along edge of region
const edgeTile = pointToTile(lng, lat, level)
tiles.add(tileToKey(edgeTile))
// Add any intermediate tiles if edge is > 1 tile away from center
const maxDiff = Math.max(
Math.abs(edgeTile[0] - centralTile[0]),
Math.abs(edgeTile[1] - centralTile[1])
)
if (maxDiff > 1) {
const centerPoint = point([center.lng, center.lat])
const bearing = rhumbBearing(centerPoint, point([lng, lat]))
for (let i = 1; i < maxDiff; i++) {
const intermediatePoint = rhumbDestination(
centerPoint,
(i * radius) / maxDiff,
bearing,
{ units }
)
const intermediateTile = pointToTile(
intermediatePoint.geometry.coordinates[0],
intermediatePoint.geometry.coordinates[1],
level
)
tiles.add(tileToKey(intermediateTile))
}
}
})
return Array.from(tiles)
}
export const getPyramidMetadata = (metadata) => {
const multiscales = metadata.metadata['.zattrs'].multiscales
if (!multiscales) {
throw new Error(
'Missing `multiscales` value in .zattrs. Please check your pyramid generation code.'
)
}
const datasets = multiscales[0].datasets
if (!datasets || datasets.length === 0) {
throw new Error(
'No datasets provided in `multiscales` metadata. Please check your pyramid generation code.'
)
}
const levels = datasets.map((dataset) => Number(dataset.path))
const maxZoom = Math.max(...levels)
const tileSize = datasets[0].pixels_per_tile
if (!tileSize) {
throw new Error(
'Missing required `pixels_per_tile` value in `multiscales` metadata. Please check your pyramid generation code.'
)
}
return { levels, maxZoom, tileSize }
}
/**
* Given a selector, generates an Object mapping each bandName to an Object
* representing which values of each dimension that bandName represents.
* @param {selector} Object of {[dimension]: dimensionValue|Array<dimensionValue>} pairs
* @returns Object containing bandName, {[dimension]: dimensionValue} pairs
*/
export const getBandInformation = (selector) => {
const combinedBands = Object.keys(selector)
.filter((key) => Array.isArray(selector[key]))
.reduce((bandMapping, selectorKey) => {
const values = selector[selectorKey]
let keys
if (typeof values[0] === 'string') {
keys = values
} else {
keys = values.map((d) => selectorKey + '_' + d)
}
const bands = Object.keys(bandMapping)
const updatedBands = {}
keys.forEach((key, i) => {
if (bands.length > 0) {
bands.forEach((band) => {
const bandKey = `${band}_${key}`
updatedBands[bandKey] = {
...bandMapping[band],
[selectorKey]: values[i],
}
})
} else {
updatedBands[key] = { [selectorKey]: values[i] }
}
})
return updatedBands
}, {})
return combinedBands
}
export const getBands = (variable, selector = {}) => {
const bandInfo = getBandInformation(selector)
const bandNames = Object.keys(bandInfo)
if (bandNames.length > 0) {
return bandNames
} else {
return [variable]
}
}
/**
* Mutates a given object by adding `value` to array at nested location specified by `keys`
* @param {obj} Object of any structure
* @param {Array<string>} keys describing nested location where value should be set
* @param {any} value to be added to array at location specified by keys
* @returns reference to updated obj
*/
export const setObjectValues = (obj, keys, value) => {
let ref = obj
keys.forEach((key, i) => {
if (i === keys.length - 1) {
if (!ref[key]) {
ref[key] = []
}
} else {
if (!ref[key]) {
ref[key] = {}
}
}
ref = ref[key]
})
ref.push(value)
return obj
}
export const getSelectorHash = (selector) => {
return JSON.stringify(selector)
}
export const getChunks = (
selector,
dimensions,
coordinates,
shape,
chunks,
x,
y
) => {
const chunkIndicesToUse = dimensions.map((dimension, i) => {
if (dimension === 'x') {
return [x]
} else if (dimension === 'y') {
return [y]
}
const selectorValue = selector[dimension]
const coords = coordinates[dimension]
const chunkSize = chunks[i]
let indices
if (Array.isArray(selectorValue)) {
// Return all indices of selector value when array
indices = selectorValue.map((v) => coords.indexOf(v))
} else if (selectorValue != undefined) {
// Return index of single selector value otherwise when present
indices = [coords.indexOf(selectorValue)]
} else {
// Otherwise, vary over the entire shape of the dimension
indices = Array(shape[i])
.fill(null)
.map((_, j) => j)
}
return (
indices
.map((index) => Math.floor(index / chunkSize))
// Filter out repeated instances of indices
.filter((v, i, a) => a.indexOf(v) === i)
)
})
let result = [[]]
chunkIndicesToUse.forEach((indices) => {
const updatedResult = []
indices.forEach((index) => {
result.forEach((prev) => {
updatedResult.push([...prev, index])
})
})
result = updatedResult
})
return result
}
export const getPositions = (size, mode) => {
let position = []
if (mode === 'grid' || mode === 'dotgrid') {
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
position.push([j + 0.5, i + 0.5])
}
}
}
if (mode === 'texture') {
position = [
0.0,
0.0,
0.0,
size,
size,
0.0,
size,
0.0,
0.0,
size,
size,
size,
]
}
return position
}
export const updatePaintProperty = (map, ref, key, value) => {
const { current: id } = ref
if (map.getLayer(id)) {
map.setPaintProperty(id, key, value)
}
}