gridviz 3.0.35 → 3.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +129 -129
- package/dist/gridviz.js +21056 -17487
- package/dist/gridviz.min.js +2 -2
- package/package.json +74 -74
- package/src/button/Button.js +67 -67
- package/src/button/FullscreenButton.js +121 -121
- package/src/button/ZoomButtons.js +103 -103
- package/src/core/Dataset.js +232 -154
- package/src/core/Drawable.js +78 -78
- package/src/core/GeoCanvas.js +443 -443
- package/src/core/Layer.js +23 -23
- package/src/core/Legend.js +108 -108
- package/src/core/Map.js +650 -568
- package/src/core/MultiResolutionDataset.js +93 -93
- package/src/core/Style.js +98 -98
- package/src/core/Tooltip.js +182 -182
- package/src/dataset/COGeo.js +25 -25
- package/src/dataset/CSVGrid.js +112 -112
- package/src/dataset/GeoTIFF.js +122 -122
- package/src/dataset/JSGrid.js +55 -55
- package/src/dataset/TiledGrid.js +311 -302
- package/src/dataset/worker.js +18 -18
- package/src/index.js +79 -79
- package/src/layer/BackgroundLayer.js +173 -173
- package/src/layer/BackgroundLayerImage.js +81 -81
- package/src/layer/BackgroundLayerWMS.js +104 -104
- package/src/layer/GeoJSONLayer.js +276 -276
- package/src/layer/GridLayer.js +166 -166
- package/src/layer/LabelLayer.js +194 -194
- package/src/legend/ColorCategoryLegend.js +109 -109
- package/src/legend/ColorDiscreteLegend.js +100 -100
- package/src/legend/ColorLegend.js +115 -115
- package/src/legend/OrientationLegend.js +89 -89
- package/src/legend/SizeLegend.js +224 -224
- package/src/legend/TernaryLegend.js +302 -302
- package/src/style/CompositionStyle.js +291 -291
- package/src/style/DotDensityStyle.js +133 -133
- package/src/style/ImageStyle.js +100 -100
- package/src/style/Interpolator.js +258 -258
- package/src/style/IsoFenceStyle.js +217 -217
- package/src/style/JoyPlotStyle.js +180 -180
- package/src/style/LegoStyle.js +132 -132
- package/src/style/MosaicStyle.js +99 -99
- package/src/style/NinjaStarStyle.js +102 -102
- package/src/style/PillarStyle.js +207 -207
- package/src/style/SegmentStyle.js +105 -105
- package/src/style/ShadingRayStyle.js +280 -280
- package/src/style/ShadingStyle.js +72 -72
- package/src/style/ShapeColorSizeStyle.js +147 -147
- package/src/style/SideCategoryStyle.js +112 -112
- package/src/style/SideStyle.js +217 -217
- package/src/style/SideTanakaStyle.js +154 -154
- package/src/style/SquareColorCategoryWebGLStyle.js +311 -294
- package/src/style/SquareColorWebGLStyle.js +126 -126
- package/src/style/StrokeStyle.js +135 -135
- package/src/style/TextStyle.js +110 -110
- package/src/style/TimeSeriesStyle.js +222 -222
- package/src/utils/WebGLRectangleColoring.js +106 -106
- package/src/utils/WebGLSquareColoring.js +114 -118
- package/src/utils/WebGLSquareColoringAdvanced.js +223 -227
- package/src/utils/WebGLSquareColoringKS.js +153 -153
- package/src/utils/scale.js +255 -255
- package/src/utils/stretching.js +120 -120
- package/src/utils/ternary.js +208 -208
- package/src/utils/utils.js +125 -125
- package/src/utils/webGLUtils.js +178 -73
- package/src/utils/zoomUtils.js +142 -142
|
@@ -1,258 +1,258 @@
|
|
|
1
|
-
//@ts-check
|
|
2
|
-
'use strict'
|
|
3
|
-
|
|
4
|
-
import { Style } from '../core/Style.js'
|
|
5
|
-
import { cellsToMatrix } from '../utils/utils.js'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @module style
|
|
9
|
-
* @author Julien Gaffuri
|
|
10
|
-
*/
|
|
11
|
-
export class Interpolator extends Style {
|
|
12
|
-
/** @param {object} opts */
|
|
13
|
-
constructor(opts) {
|
|
14
|
-
super(opts)
|
|
15
|
-
opts = opts || {}
|
|
16
|
-
|
|
17
|
-
/** The cell value to interpolate
|
|
18
|
-
* @type { function } } */
|
|
19
|
-
this.value = opts.value //(c) => elevation
|
|
20
|
-
|
|
21
|
-
/** The target resolution. As a function (resolution, z) => targetResolution
|
|
22
|
-
* NB: Try to make sure that resolution/targetResolution is an integer.
|
|
23
|
-
* @type { function } } */
|
|
24
|
-
this.targetResolution = opts.targetResolution || ((r, z) => r / 5)
|
|
25
|
-
|
|
26
|
-
/** the property name to store the interpolated value in the cell
|
|
27
|
-
* @type { string } } */
|
|
28
|
-
this.interpolatedProperty = opts.interpolatedProperty || 'value'
|
|
29
|
-
|
|
30
|
-
// the interpolation method: currently only 'bilinear' is supported
|
|
31
|
-
//this.method = opts.method || 'bilinear' // 'nearest', 'bilinear'
|
|
32
|
-
|
|
33
|
-
/** when a neighbor cell has no value, take zero value or average of surrounding cells
|
|
34
|
-
* @type { string } } */
|
|
35
|
-
this.nodataHandling = opts.nodataHandling || "zero" // 'zero', 'average'
|
|
36
|
-
|
|
37
|
-
/** The styles to represent the interpolated grid.
|
|
38
|
-
* @type {Array.<Style>} */
|
|
39
|
-
this.styles = opts.styles || []
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
*
|
|
45
|
-
* @param {Array.<import("../core/Dataset.js").Cell>} cells
|
|
46
|
-
* @param {import("../core/GeoCanvas.js").GeoCanvas} geoCanvas
|
|
47
|
-
* @param {number} resolution
|
|
48
|
-
*/
|
|
49
|
-
draw(cells, geoCanvas, resolution) {
|
|
50
|
-
|
|
51
|
-
//filter cells
|
|
52
|
-
if (this.filter) cells = cells.filter(this.filter)
|
|
53
|
-
if (cells.length == 0) return;
|
|
54
|
-
|
|
55
|
-
//
|
|
56
|
-
const z = geoCanvas.view.z
|
|
57
|
-
|
|
58
|
-
//get view scale
|
|
59
|
-
//const viewScale = this.viewScale ? this.viewScale(cells, resolution, z) : undefined
|
|
60
|
-
|
|
61
|
-
//index cells by y and x
|
|
62
|
-
let m = cellsToMatrix(cells, resolution, c => this.value(c))
|
|
63
|
-
const x0 = m.x0, y0 = m.y0
|
|
64
|
-
|
|
65
|
-
//get target resolution and scale factor
|
|
66
|
-
let targetResolution = this.targetResolution(resolution, z)
|
|
67
|
-
const scaleFactor = Math.round(resolution / targetResolution)
|
|
68
|
-
targetResolution = resolution / scaleFactor
|
|
69
|
-
|
|
70
|
-
// compute ray casting shadow
|
|
71
|
-
m = bilinearInterpolator(m, scaleFactor, this.nodataHandling);
|
|
72
|
-
|
|
73
|
-
// make cells
|
|
74
|
-
cells = []
|
|
75
|
-
const ra = (resolution - targetResolution) / 2
|
|
76
|
-
for (let i = 0; i < m.length; i++) {
|
|
77
|
-
const row = m[i]
|
|
78
|
-
const y = y0 - i * targetResolution + ra
|
|
79
|
-
for (let j = 0; j < row.length; j++) {
|
|
80
|
-
const v = row[j]
|
|
81
|
-
if (!v) continue
|
|
82
|
-
const c = { x: x0 + j * targetResolution + ra, y: y }
|
|
83
|
-
c[this.interpolatedProperty] = v
|
|
84
|
-
cells.push(c)
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
//draw smoothed cells from styles
|
|
89
|
-
const ctx = geoCanvas.offscreenCtx
|
|
90
|
-
for (let s of this.styles) {
|
|
91
|
-
|
|
92
|
-
//check if style is visible
|
|
93
|
-
if (s.visible && !s.visible(z)) continue
|
|
94
|
-
|
|
95
|
-
//set style alpha and blend mode
|
|
96
|
-
//TODO: multiply by layer alpha ?
|
|
97
|
-
if (s.alpha || s.blendOperation) {
|
|
98
|
-
ctx.save()
|
|
99
|
-
if (s.alpha) ctx.globalAlpha = s.alpha(z)
|
|
100
|
-
if (s.blendOperation) ctx.globalCompositeOperation = s.blendOperation(z)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
//set affin transform to draw with geographical coordinates
|
|
104
|
-
geoCanvas.setCanvasTransform()
|
|
105
|
-
|
|
106
|
-
//draw with style
|
|
107
|
-
s.draw(cells, geoCanvas, targetResolution)
|
|
108
|
-
|
|
109
|
-
//draw style filter
|
|
110
|
-
if (s.filterColor) s.drawFilter(geoCanvas)
|
|
111
|
-
|
|
112
|
-
//restore ctx
|
|
113
|
-
if (s.alpha || s.blendOperation) ctx.restore()
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const interp = (topLeft, topRight, bottomLeft, bottomRight, xRatio, yRatio) => {
|
|
121
|
-
const top = topLeft + (topRight - topLeft) * xRatio;
|
|
122
|
-
const bottom = bottomLeft + (bottomRight - bottomLeft) * xRatio;
|
|
123
|
-
return top + (bottom - top) * yRatio;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
function bilinearInterpolator(grid, scaleFactor = 5, nodataHandling = "zero") {
|
|
128
|
-
|
|
129
|
-
if (scaleFactor === 1) return grid;
|
|
130
|
-
|
|
131
|
-
// input grid dimensions
|
|
132
|
-
const rows = grid.length;
|
|
133
|
-
if (rows === 0) return [];
|
|
134
|
-
const cols = grid[0].length;
|
|
135
|
-
|
|
136
|
-
// output grid dimensions
|
|
137
|
-
const fineRows = rows * scaleFactor;
|
|
138
|
-
const fineCols = cols * scaleFactor;
|
|
139
|
-
const fineGrid = Array(fineRows).fill().map(() => Array(fineCols).fill(undefined));
|
|
140
|
-
|
|
141
|
-
// check if scale factor is even
|
|
142
|
-
const sfIsEven = scaleFactor % 2 === 0
|
|
143
|
-
|
|
144
|
-
//compute output grid values
|
|
145
|
-
for (let i = 0; i < fineRows; i++) {
|
|
146
|
-
|
|
147
|
-
const coarseI = Math.min(Math.floor(i / scaleFactor), rows - 2);
|
|
148
|
-
const yRatio = (i % scaleFactor) / scaleFactor;
|
|
149
|
-
const i_ = coarseI * scaleFactor + (scaleFactor / 2)
|
|
150
|
-
|
|
151
|
-
for (let j = 0; j < fineCols; j++) {
|
|
152
|
-
const coarseJ = Math.min(Math.floor(j / scaleFactor), cols - 2);
|
|
153
|
-
const xRatio = (j % scaleFactor) / scaleFactor;
|
|
154
|
-
const j_ = coarseJ * scaleFactor + (scaleFactor / 2)
|
|
155
|
-
|
|
156
|
-
// get four corner values
|
|
157
|
-
const topLeft = grid[coarseI][coarseJ];
|
|
158
|
-
const topRight = grid[coarseI][coarseJ + 1];
|
|
159
|
-
const bottomLeft = grid[coarseI + 1][coarseJ];
|
|
160
|
-
const bottomRight = grid[coarseI + 1][coarseJ + 1];
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (sfIsEven) {
|
|
164
|
-
if (topLeft === undefined && i <= i_ && j <= j_) continue
|
|
165
|
-
if (topRight === undefined && i <= i_ && j >= j_) continue
|
|
166
|
-
if (bottomLeft === undefined && i >= i_ && j <= j_) continue
|
|
167
|
-
if (bottomRight === undefined && i >= i_ && j >= j_) continue
|
|
168
|
-
} else {
|
|
169
|
-
if (topLeft === undefined && i < i_ && j < j_) continue
|
|
170
|
-
if (topRight === undefined && i < i_ && j > j_) continue
|
|
171
|
-
if (bottomLeft === undefined && i > i_ && j < j_) continue
|
|
172
|
-
if (bottomRight === undefined && i > i_ && j > j_) continue
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if(nodataHandling === "zero") {
|
|
176
|
-
// replace undefined values by zero
|
|
177
|
-
fineGrid[i][j] = interp(topLeft || 0, topRight || 0, bottomLeft || 0, bottomRight || 0, xRatio, yRatio)
|
|
178
|
-
continue
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// general case: bilinear interpolation
|
|
182
|
-
// If all four are defined, interpolate normally
|
|
183
|
-
if (topLeft !== undefined && topRight !== undefined && bottomLeft !== undefined && bottomRight !== undefined) {
|
|
184
|
-
fineGrid[i][j] = interp(topLeft, topRight, bottomLeft, bottomRight, xRatio, yRatio)
|
|
185
|
-
continue
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// If only one is not defined, use average value of 2 adjacents and interpolate
|
|
189
|
-
if (topLeft === undefined && topRight !== undefined && bottomLeft !== undefined && bottomRight !== undefined) {
|
|
190
|
-
const v = topRight + bottomLeft
|
|
191
|
-
fineGrid[i][j] = interp(v / 2, topRight, bottomLeft, bottomRight, xRatio, yRatio)
|
|
192
|
-
continue
|
|
193
|
-
}
|
|
194
|
-
if (topLeft !== undefined && topRight === undefined && bottomLeft !== undefined && bottomRight !== undefined) {
|
|
195
|
-
const v = topLeft + bottomRight
|
|
196
|
-
fineGrid[i][j] = interp(topLeft, v / 2, bottomLeft, bottomRight, xRatio, yRatio)
|
|
197
|
-
continue
|
|
198
|
-
}
|
|
199
|
-
if (topLeft !== undefined && topRight !== undefined && bottomLeft === undefined && bottomRight !== undefined) {
|
|
200
|
-
const v = topLeft + bottomRight
|
|
201
|
-
fineGrid[i][j] = interp(topLeft, topRight, v / 2, bottomRight, xRatio, yRatio)
|
|
202
|
-
continue
|
|
203
|
-
}
|
|
204
|
-
if (topLeft !== undefined && topRight !== undefined && bottomLeft !== undefined && bottomRight === undefined) {
|
|
205
|
-
const v = topRight + bottomLeft
|
|
206
|
-
fineGrid[i][j] = interp(topLeft, topRight, bottomLeft, v / 2, xRatio, yRatio)
|
|
207
|
-
continue
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// If only two diagonally opposite points are defined, interpolate along diagonal
|
|
211
|
-
if (topLeft !== undefined && bottomRight !== undefined) {
|
|
212
|
-
const t = (xRatio + yRatio) * Math.SQRT1_2
|
|
213
|
-
fineGrid[i][j] = topLeft * (1 - t) + bottomRight * t;
|
|
214
|
-
continue
|
|
215
|
-
}
|
|
216
|
-
if (topRight !== undefined && bottomLeft !== undefined) {
|
|
217
|
-
const t = (1 - xRatio + yRatio) * Math.SQRT1_2
|
|
218
|
-
fineGrid[i][j] = topRight * (1 - t) + bottomLeft * t;
|
|
219
|
-
continue
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// If only two adjacent points are defined, interpolate between them
|
|
223
|
-
if (topLeft !== undefined && bottomLeft !== undefined) {
|
|
224
|
-
fineGrid[i][j] = bottomLeft * yRatio + topLeft * (1 - yRatio);
|
|
225
|
-
continue
|
|
226
|
-
}
|
|
227
|
-
if (topRight !== undefined && bottomRight !== undefined) {
|
|
228
|
-
fineGrid[i][j] = bottomRight * yRatio + topRight * (1 - yRatio);
|
|
229
|
-
continue
|
|
230
|
-
}
|
|
231
|
-
if (topLeft !== undefined && topRight !== undefined) {
|
|
232
|
-
fineGrid[i][j] = topRight * xRatio + topLeft * (1 - xRatio);
|
|
233
|
-
}
|
|
234
|
-
if (bottomLeft !== undefined && bottomRight !== undefined) {
|
|
235
|
-
fineGrid[i][j] = bottomRight * xRatio + bottomLeft * (1 - xRatio);
|
|
236
|
-
continue
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// If only one point is defined, use that value
|
|
240
|
-
if (topLeft !== undefined) {
|
|
241
|
-
fineGrid[i][j] = topLeft;
|
|
242
|
-
continue
|
|
243
|
-
}
|
|
244
|
-
if (topRight !== undefined) {
|
|
245
|
-
fineGrid[i][j] = topRight;
|
|
246
|
-
continue
|
|
247
|
-
}
|
|
248
|
-
if (bottomLeft !== undefined) {
|
|
249
|
-
fineGrid[i][j] = bottomLeft;
|
|
250
|
-
continue
|
|
251
|
-
}
|
|
252
|
-
if (bottomRight !== undefined) {
|
|
253
|
-
fineGrid[i][j] = bottomRight;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
return fineGrid;
|
|
258
|
-
}
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { Style } from '../core/Style.js'
|
|
5
|
+
import { cellsToMatrix } from '../utils/utils.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @module style
|
|
9
|
+
* @author Julien Gaffuri
|
|
10
|
+
*/
|
|
11
|
+
export class Interpolator extends Style {
|
|
12
|
+
/** @param {object} opts */
|
|
13
|
+
constructor(opts) {
|
|
14
|
+
super(opts)
|
|
15
|
+
opts = opts || {}
|
|
16
|
+
|
|
17
|
+
/** The cell value to interpolate
|
|
18
|
+
* @type { function } } */
|
|
19
|
+
this.value = opts.value //(c) => elevation
|
|
20
|
+
|
|
21
|
+
/** The target resolution. As a function (resolution, z) => targetResolution
|
|
22
|
+
* NB: Try to make sure that resolution/targetResolution is an integer.
|
|
23
|
+
* @type { function } } */
|
|
24
|
+
this.targetResolution = opts.targetResolution || ((r, z) => r / 5)
|
|
25
|
+
|
|
26
|
+
/** the property name to store the interpolated value in the cell
|
|
27
|
+
* @type { string } } */
|
|
28
|
+
this.interpolatedProperty = opts.interpolatedProperty || 'value'
|
|
29
|
+
|
|
30
|
+
// the interpolation method: currently only 'bilinear' is supported
|
|
31
|
+
//this.method = opts.method || 'bilinear' // 'nearest', 'bilinear'
|
|
32
|
+
|
|
33
|
+
/** when a neighbor cell has no value, take zero value or average of surrounding cells
|
|
34
|
+
* @type { string } } */
|
|
35
|
+
this.nodataHandling = opts.nodataHandling || "zero" // 'zero', 'average'
|
|
36
|
+
|
|
37
|
+
/** The styles to represent the interpolated grid.
|
|
38
|
+
* @type {Array.<Style>} */
|
|
39
|
+
this.styles = opts.styles || []
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
*
|
|
45
|
+
* @param {Array.<import("../core/Dataset.js").Cell>} cells
|
|
46
|
+
* @param {import("../core/GeoCanvas.js").GeoCanvas} geoCanvas
|
|
47
|
+
* @param {number} resolution
|
|
48
|
+
*/
|
|
49
|
+
draw(cells, geoCanvas, resolution) {
|
|
50
|
+
|
|
51
|
+
//filter cells
|
|
52
|
+
if (this.filter) cells = cells.filter(this.filter)
|
|
53
|
+
if (cells.length == 0) return;
|
|
54
|
+
|
|
55
|
+
//
|
|
56
|
+
const z = geoCanvas.view.z
|
|
57
|
+
|
|
58
|
+
//get view scale
|
|
59
|
+
//const viewScale = this.viewScale ? this.viewScale(cells, resolution, z) : undefined
|
|
60
|
+
|
|
61
|
+
//index cells by y and x
|
|
62
|
+
let m = cellsToMatrix(cells, resolution, c => this.value(c))
|
|
63
|
+
const x0 = m.x0, y0 = m.y0
|
|
64
|
+
|
|
65
|
+
//get target resolution and scale factor
|
|
66
|
+
let targetResolution = this.targetResolution(resolution, z)
|
|
67
|
+
const scaleFactor = Math.round(resolution / targetResolution)
|
|
68
|
+
targetResolution = resolution / scaleFactor
|
|
69
|
+
|
|
70
|
+
// compute ray casting shadow
|
|
71
|
+
m = bilinearInterpolator(m, scaleFactor, this.nodataHandling);
|
|
72
|
+
|
|
73
|
+
// make cells
|
|
74
|
+
cells = []
|
|
75
|
+
const ra = (resolution - targetResolution) / 2
|
|
76
|
+
for (let i = 0; i < m.length; i++) {
|
|
77
|
+
const row = m[i]
|
|
78
|
+
const y = y0 - i * targetResolution + ra
|
|
79
|
+
for (let j = 0; j < row.length; j++) {
|
|
80
|
+
const v = row[j]
|
|
81
|
+
if (!v) continue
|
|
82
|
+
const c = { x: x0 + j * targetResolution + ra, y: y }
|
|
83
|
+
c[this.interpolatedProperty] = v
|
|
84
|
+
cells.push(c)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//draw smoothed cells from styles
|
|
89
|
+
const ctx = geoCanvas.offscreenCtx
|
|
90
|
+
for (let s of this.styles) {
|
|
91
|
+
|
|
92
|
+
//check if style is visible
|
|
93
|
+
if (s.visible && !s.visible(z)) continue
|
|
94
|
+
|
|
95
|
+
//set style alpha and blend mode
|
|
96
|
+
//TODO: multiply by layer alpha ?
|
|
97
|
+
if (s.alpha || s.blendOperation) {
|
|
98
|
+
ctx.save()
|
|
99
|
+
if (s.alpha) ctx.globalAlpha = s.alpha(z)
|
|
100
|
+
if (s.blendOperation) ctx.globalCompositeOperation = s.blendOperation(z)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
//set affin transform to draw with geographical coordinates
|
|
104
|
+
geoCanvas.setCanvasTransform()
|
|
105
|
+
|
|
106
|
+
//draw with style
|
|
107
|
+
s.draw(cells, geoCanvas, targetResolution)
|
|
108
|
+
|
|
109
|
+
//draw style filter
|
|
110
|
+
if (s.filterColor) s.drawFilter(geoCanvas)
|
|
111
|
+
|
|
112
|
+
//restore ctx
|
|
113
|
+
if (s.alpha || s.blendOperation) ctx.restore()
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
const interp = (topLeft, topRight, bottomLeft, bottomRight, xRatio, yRatio) => {
|
|
121
|
+
const top = topLeft + (topRight - topLeft) * xRatio;
|
|
122
|
+
const bottom = bottomLeft + (bottomRight - bottomLeft) * xRatio;
|
|
123
|
+
return top + (bottom - top) * yRatio;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
function bilinearInterpolator(grid, scaleFactor = 5, nodataHandling = "zero") {
|
|
128
|
+
|
|
129
|
+
if (scaleFactor === 1) return grid;
|
|
130
|
+
|
|
131
|
+
// input grid dimensions
|
|
132
|
+
const rows = grid.length;
|
|
133
|
+
if (rows === 0) return [];
|
|
134
|
+
const cols = grid[0].length;
|
|
135
|
+
|
|
136
|
+
// output grid dimensions
|
|
137
|
+
const fineRows = rows * scaleFactor;
|
|
138
|
+
const fineCols = cols * scaleFactor;
|
|
139
|
+
const fineGrid = Array(fineRows).fill().map(() => Array(fineCols).fill(undefined));
|
|
140
|
+
|
|
141
|
+
// check if scale factor is even
|
|
142
|
+
const sfIsEven = scaleFactor % 2 === 0
|
|
143
|
+
|
|
144
|
+
//compute output grid values
|
|
145
|
+
for (let i = 0; i < fineRows; i++) {
|
|
146
|
+
|
|
147
|
+
const coarseI = Math.min(Math.floor(i / scaleFactor), rows - 2);
|
|
148
|
+
const yRatio = (i % scaleFactor) / scaleFactor;
|
|
149
|
+
const i_ = coarseI * scaleFactor + (scaleFactor / 2)
|
|
150
|
+
|
|
151
|
+
for (let j = 0; j < fineCols; j++) {
|
|
152
|
+
const coarseJ = Math.min(Math.floor(j / scaleFactor), cols - 2);
|
|
153
|
+
const xRatio = (j % scaleFactor) / scaleFactor;
|
|
154
|
+
const j_ = coarseJ * scaleFactor + (scaleFactor / 2)
|
|
155
|
+
|
|
156
|
+
// get four corner values
|
|
157
|
+
const topLeft = grid[coarseI][coarseJ];
|
|
158
|
+
const topRight = grid[coarseI][coarseJ + 1];
|
|
159
|
+
const bottomLeft = grid[coarseI + 1][coarseJ];
|
|
160
|
+
const bottomRight = grid[coarseI + 1][coarseJ + 1];
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
if (sfIsEven) {
|
|
164
|
+
if (topLeft === undefined && i <= i_ && j <= j_) continue
|
|
165
|
+
if (topRight === undefined && i <= i_ && j >= j_) continue
|
|
166
|
+
if (bottomLeft === undefined && i >= i_ && j <= j_) continue
|
|
167
|
+
if (bottomRight === undefined && i >= i_ && j >= j_) continue
|
|
168
|
+
} else {
|
|
169
|
+
if (topLeft === undefined && i < i_ && j < j_) continue
|
|
170
|
+
if (topRight === undefined && i < i_ && j > j_) continue
|
|
171
|
+
if (bottomLeft === undefined && i > i_ && j < j_) continue
|
|
172
|
+
if (bottomRight === undefined && i > i_ && j > j_) continue
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if(nodataHandling === "zero") {
|
|
176
|
+
// replace undefined values by zero
|
|
177
|
+
fineGrid[i][j] = interp(topLeft || 0, topRight || 0, bottomLeft || 0, bottomRight || 0, xRatio, yRatio)
|
|
178
|
+
continue
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// general case: bilinear interpolation
|
|
182
|
+
// If all four are defined, interpolate normally
|
|
183
|
+
if (topLeft !== undefined && topRight !== undefined && bottomLeft !== undefined && bottomRight !== undefined) {
|
|
184
|
+
fineGrid[i][j] = interp(topLeft, topRight, bottomLeft, bottomRight, xRatio, yRatio)
|
|
185
|
+
continue
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// If only one is not defined, use average value of 2 adjacents and interpolate
|
|
189
|
+
if (topLeft === undefined && topRight !== undefined && bottomLeft !== undefined && bottomRight !== undefined) {
|
|
190
|
+
const v = topRight + bottomLeft
|
|
191
|
+
fineGrid[i][j] = interp(v / 2, topRight, bottomLeft, bottomRight, xRatio, yRatio)
|
|
192
|
+
continue
|
|
193
|
+
}
|
|
194
|
+
if (topLeft !== undefined && topRight === undefined && bottomLeft !== undefined && bottomRight !== undefined) {
|
|
195
|
+
const v = topLeft + bottomRight
|
|
196
|
+
fineGrid[i][j] = interp(topLeft, v / 2, bottomLeft, bottomRight, xRatio, yRatio)
|
|
197
|
+
continue
|
|
198
|
+
}
|
|
199
|
+
if (topLeft !== undefined && topRight !== undefined && bottomLeft === undefined && bottomRight !== undefined) {
|
|
200
|
+
const v = topLeft + bottomRight
|
|
201
|
+
fineGrid[i][j] = interp(topLeft, topRight, v / 2, bottomRight, xRatio, yRatio)
|
|
202
|
+
continue
|
|
203
|
+
}
|
|
204
|
+
if (topLeft !== undefined && topRight !== undefined && bottomLeft !== undefined && bottomRight === undefined) {
|
|
205
|
+
const v = topRight + bottomLeft
|
|
206
|
+
fineGrid[i][j] = interp(topLeft, topRight, bottomLeft, v / 2, xRatio, yRatio)
|
|
207
|
+
continue
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// If only two diagonally opposite points are defined, interpolate along diagonal
|
|
211
|
+
if (topLeft !== undefined && bottomRight !== undefined) {
|
|
212
|
+
const t = (xRatio + yRatio) * Math.SQRT1_2
|
|
213
|
+
fineGrid[i][j] = topLeft * (1 - t) + bottomRight * t;
|
|
214
|
+
continue
|
|
215
|
+
}
|
|
216
|
+
if (topRight !== undefined && bottomLeft !== undefined) {
|
|
217
|
+
const t = (1 - xRatio + yRatio) * Math.SQRT1_2
|
|
218
|
+
fineGrid[i][j] = topRight * (1 - t) + bottomLeft * t;
|
|
219
|
+
continue
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// If only two adjacent points are defined, interpolate between them
|
|
223
|
+
if (topLeft !== undefined && bottomLeft !== undefined) {
|
|
224
|
+
fineGrid[i][j] = bottomLeft * yRatio + topLeft * (1 - yRatio);
|
|
225
|
+
continue
|
|
226
|
+
}
|
|
227
|
+
if (topRight !== undefined && bottomRight !== undefined) {
|
|
228
|
+
fineGrid[i][j] = bottomRight * yRatio + topRight * (1 - yRatio);
|
|
229
|
+
continue
|
|
230
|
+
}
|
|
231
|
+
if (topLeft !== undefined && topRight !== undefined) {
|
|
232
|
+
fineGrid[i][j] = topRight * xRatio + topLeft * (1 - xRatio);
|
|
233
|
+
}
|
|
234
|
+
if (bottomLeft !== undefined && bottomRight !== undefined) {
|
|
235
|
+
fineGrid[i][j] = bottomRight * xRatio + bottomLeft * (1 - xRatio);
|
|
236
|
+
continue
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// If only one point is defined, use that value
|
|
240
|
+
if (topLeft !== undefined) {
|
|
241
|
+
fineGrid[i][j] = topLeft;
|
|
242
|
+
continue
|
|
243
|
+
}
|
|
244
|
+
if (topRight !== undefined) {
|
|
245
|
+
fineGrid[i][j] = topRight;
|
|
246
|
+
continue
|
|
247
|
+
}
|
|
248
|
+
if (bottomLeft !== undefined) {
|
|
249
|
+
fineGrid[i][j] = bottomLeft;
|
|
250
|
+
continue
|
|
251
|
+
}
|
|
252
|
+
if (bottomRight !== undefined) {
|
|
253
|
+
fineGrid[i][j] = bottomRight;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return fineGrid;
|
|
258
|
+
}
|