@metoceanapi/wxtiles-common 0.0.8 → 0.0.11
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/LICENSE.txt +1 -1
- package/biome.json +12 -0
- package/controls/WxAPIControl/WxAPIControl-busy.ts +16 -0
- package/controls/WxAPIControl/WxAPIControl-datasets.ts +102 -0
- package/controls/WxAPIControl/WxAPIControl-filter.ts +66 -0
- package/controls/WxAPIControl/WxAPIControl-variables.ts +73 -0
- package/controls/WxAPIControl.ts +91 -120
- package/controls/WxInfoControl.ts +87 -2
- package/controls/WxLegendControl.ts +28 -2
- package/controls/WxStyleEditorControl.ts +123 -6
- package/controls/WxTimeControl.ts +132 -4
- package/docs/docs/.nojekyll +1 -0
- package/docs/docs/assets/highlight.css +92 -0
- package/docs/docs/assets/icons.svg +1 -0
- package/docs/docs/assets/style.css +1610 -0
- package/docs/docs/classes/controls_WxAPIControl.WxAPIControl.html +28 -0
- package/docs/docs/classes/controls_WxInfoControl.WxInfoControl.html +20 -0
- package/docs/docs/classes/controls_WxLegendControl.WxLegendControl.html +19 -0
- package/docs/docs/classes/controls_WxStyleEditorControl.WxStyleEditorControl.html +111 -0
- package/docs/docs/classes/controls_WxTimeControl.WxTimeControl.html +28 -0
- package/docs/docs/classes/managers_BaseDatasetManager.BaseDatasetManager.html +104 -0
- package/docs/docs/classes/managers_WxAllDatasetsManager.WxAllDatasetsManager.html +24 -0
- package/docs/docs/classes/types.WxRasterDataCache.html +27 -0
- package/docs/docs/classes/utils_RawCLUT.RawCLUT.html +18 -0
- package/docs/docs/classes/utils_qtree.QTree.html +22 -0
- package/docs/docs/classes/utils_wxtools.DataIntegral.html +10 -0
- package/docs/docs/classes/wxapi.WxAPI.html +45 -0
- package/docs/docs/classes/wxlayer_loader.Loader.html +31 -0
- package/docs/docs/classes/wxlayer_painter.Painter.html +20 -0
- package/docs/docs/classes/wxlayer_wxlayer.WxLayer.html +57 -0
- package/docs/docs/enums/utils_qtree.TileType.html +5 -0
- package/docs/docs/functions/utils_RawCLUT.WxCreateLegend.html +5 -0
- package/docs/docs/functions/utils_mercator.LatLonToPixels.html +6 -0
- package/docs/docs/functions/utils_mercator.PixelsToLatLon.html +6 -0
- package/docs/docs/functions/utils_mercator.PixelsToLonLat.html +6 -0
- package/docs/docs/functions/utils_mercator.coordToPixel.html +5 -0
- package/docs/docs/functions/utils_tests_wxtools.tests.html +1 -0
- package/docs/docs/functions/utils_wxtools.HEXtoRGBA.html +3 -0
- package/docs/docs/functions/utils_wxtools.HashXYZ.html +4 -0
- package/docs/docs/functions/utils_wxtools.RGBAtoHEX.html +3 -0
- package/docs/docs/functions/utils_wxtools.RGBtoHEX.html +3 -0
- package/docs/docs/functions/utils_wxtools.WXLOG.html +2 -0
- package/docs/docs/functions/utils_wxtools.WxGetColorSchemes.html +2 -0
- package/docs/docs/functions/utils_wxtools.WxGetColorStyles.html +2 -0
- package/docs/docs/functions/utils_wxtools.WxTilesLibSetup.html +3 -0
- package/docs/docs/functions/utils_wxtools.WxTilesLogging.html +5 -0
- package/docs/docs/functions/utils_wxtools.blurData.html +7 -0
- package/docs/docs/functions/utils_wxtools.buildIntegralPare.html +6 -0
- package/docs/docs/functions/utils_wxtools.cacheUriPromise.html +2 -0
- package/docs/docs/functions/utils_wxtools.create2DContext.html +6 -0
- package/docs/docs/functions/utils_wxtools.createEl.html +5 -0
- package/docs/docs/functions/utils_wxtools.createLevels.html +6 -0
- package/docs/docs/functions/utils_wxtools.fetchJson.html +6 -0
- package/docs/docs/functions/utils_wxtools.getClosestTimeString.html +5 -0
- package/docs/docs/functions/utils_wxtools.loadDataIntegral.html +5 -0
- package/docs/docs/functions/utils_wxtools.loadImage.html +4 -0
- package/docs/docs/functions/utils_wxtools.loadImageData.html +5 -0
- package/docs/docs/functions/utils_wxtools.makeConverter.html +9 -0
- package/docs/docs/functions/utils_wxtools.mixColor.html +3 -0
- package/docs/docs/functions/utils_wxtools.refineColor.html +3 -0
- package/docs/docs/functions/utils_wxtools.uriXYZ.html +5 -0
- package/docs/docs/functions/wxlayer_WxImplementation.WxLayerBaseImplementationMixin.html +4 -0
- package/docs/docs/functions/wxlayer_loadertools.applyMask.html +10 -0
- package/docs/docs/functions/wxlayer_loadertools.makeBox.html +4 -0
- package/docs/docs/functions/wxlayer_loadertools.splitCoords.html +6 -0
- package/docs/docs/functions/wxlayer_loadertools.subData.html +5 -0
- package/docs/docs/functions/wxlayer_loadertools.subDataDegree.html +5 -0
- package/docs/docs/functions/wxlayer_loadertools.subMask.html +5 -0
- package/docs/docs/hierarchy.html +1 -0
- package/docs/docs/index.html +7 -0
- package/docs/docs/interfaces/types.IsoInfo.html +8 -0
- package/docs/docs/interfaces/types.SLinePoint.html +4 -0
- package/docs/docs/interfaces/types.WxAPIOptions.html +37 -0
- package/docs/docs/interfaces/types.WxAllBoundariesMeta.html +8 -0
- package/docs/docs/interfaces/types.WxAllDatasetsShortMetas.html +4 -0
- package/docs/docs/interfaces/types.WxBoundaryMeta.html +10 -0
- package/docs/docs/interfaces/types.WxData.html +10 -0
- package/docs/docs/interfaces/types.WxDataSetManagerOptions.html +14 -0
- package/docs/docs/interfaces/types.WxDatasetMeta.html +20 -0
- package/docs/docs/interfaces/types.WxDatasetShortMeta.html +9 -0
- package/docs/docs/interfaces/types.WxLayerAPI.html +24 -0
- package/docs/docs/interfaces/types.WxLayerBaseAPI.html +16 -0
- package/docs/docs/interfaces/types.WxLayerOptions.html +12 -0
- package/docs/docs/interfaces/types.WxLngLat.html +6 -0
- package/docs/docs/interfaces/types.WxRasterData.html +13 -0
- package/docs/docs/interfaces/types.WxRequestInit.html +3 -0
- package/docs/docs/interfaces/types.WxTileInfo.html +18 -0
- package/docs/docs/interfaces/types.WxVariableMeta.html +13 -0
- package/docs/docs/interfaces/types.WxVariablesMetas.html +4 -0
- package/docs/docs/interfaces/utils_RawCLUT.WxLegend.html +14 -0
- package/docs/docs/interfaces/utils_RawCLUT.WxTick.html +10 -0
- package/docs/docs/interfaces/utils_qtree.Tree.html +7 -0
- package/docs/docs/interfaces/utils_wxtools.CachedUriLoaderPromiseFunc.html +2 -0
- package/docs/docs/interfaces/utils_wxtools.ColorStylesIncomplete.html +2 -0
- package/docs/docs/interfaces/utils_wxtools.Converter.html +5 -0
- package/docs/docs/interfaces/utils_wxtools.DataPicture.html +6 -0
- package/docs/docs/interfaces/utils_wxtools.IntegralPare.html +6 -0
- package/docs/docs/interfaces/utils_wxtools.WxColorSchemes.html +3 -0
- package/docs/docs/interfaces/utils_wxtools.WxColorStyleStrict.html +95 -0
- package/docs/docs/interfaces/utils_wxtools.WxColorStyleWeak.html +95 -0
- package/docs/docs/interfaces/utils_wxtools.WxColorStylesStrict.html +2 -0
- package/docs/docs/interfaces/utils_wxtools.WxColorStylesWeakMixed.html +2 -0
- package/docs/docs/interfaces/utils_wxtools.WxTilesLibOptions.html +8 -0
- package/docs/docs/interfaces/utils_wxtools.WxUnits.html +2 -0
- package/docs/docs/interfaces/utils_wxtools.XYZ.html +5 -0
- package/docs/docs/modules/controls_WxAPIControl.html +1 -0
- package/docs/docs/modules/controls_WxInfoControl.html +1 -0
- package/docs/docs/modules/controls_WxLegendControl.html +1 -0
- package/docs/docs/modules/controls_WxStyleEditorControl.html +1 -0
- package/docs/docs/modules/controls_WxTimeControl.html +1 -0
- package/docs/docs/modules/defaults_colorschemes.html +1 -0
- package/docs/docs/modules/defaults_styles.html +1 -0
- package/docs/docs/modules/defaults_uconv.html +1 -0
- package/docs/docs/modules/managers_BaseDatasetManager.html +1 -0
- package/docs/docs/modules/managers_WxAllDatasetsManager.html +1 -0
- package/docs/docs/modules/types.html +1 -0
- package/docs/docs/modules/utils_RawCLUT.html +1 -0
- package/docs/docs/modules/utils_mercator.html +1 -0
- package/docs/docs/modules/utils_qtree.html +1 -0
- package/docs/docs/modules/utils_tests_wxtools.html +1 -0
- package/docs/docs/modules/utils_wxtools.html +1 -0
- package/docs/docs/modules/wxapi.html +1 -0
- package/docs/docs/modules/wxlayer_WxImplementation.html +1 -0
- package/docs/docs/modules/wxlayer_loader.html +1 -0
- package/docs/docs/modules/wxlayer_loadertools.html +1 -0
- package/docs/docs/modules/wxlayer_painter.html +1 -0
- package/docs/docs/modules/wxlayer_wxlayer.html +1 -0
- package/docs/docs/modules.html +1 -0
- package/docs/docs/types/types.ListenerMethod.html +1 -0
- package/docs/docs/types/types.SLine.html +2 -0
- package/docs/docs/types/types.WxDate.html +9 -0
- package/docs/docs/types/types.WxEventType.html +1 -0
- package/docs/docs/types/types.WxInstances.html +2 -0
- package/docs/docs/types/types.WxLayerVarsNames.html +2 -0
- package/docs/docs/types/types.WxURIs.html +2 -0
- package/docs/docs/types/utils_qtree.SubTrees.html +2 -0
- package/docs/docs/types/utils_qtree.SubTreesN.html +2 -0
- package/docs/docs/types/utils_qtree.TreeN.html +2 -0
- package/docs/docs/types/utils_wxtools.DataIntegrals.html +2 -0
- package/docs/docs/types/utils_wxtools.DataPictures.html +2 -0
- package/docs/docs/types/utils_wxtools.WxUnitTuple.html +2 -0
- package/docs/docs/types/utils_wxtools.colorMapTuple.html +2 -0
- package/docs/docs/variables/defaults_colorschemes.__colorSchemes_default_preset.html +1 -0
- package/docs/docs/variables/defaults_styles.__colorStyles_default_preset.html +1 -0
- package/docs/docs/variables/defaults_uconv.__units_default_preset.html +1 -0
- package/managers/BaseDatasetManager.ts +274 -0
- package/managers/WxAllDatasetsManager.ts +60 -0
- package/package.json +6 -3
- package/tsconfig.json +19 -0
- package/typedoc.json +4 -0
- package/types.ts +471 -0
- package/wxapi.ts +223 -0
- package/wxlayer/WxImplementation.ts +372 -0
- package/wxlayer/loader.ts +305 -0
- package/wxlayer/loadertools.ts +380 -0
- package/wxlayer/painter.ts +451 -0
- package/wxlayer/wxlayer.ts +379 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import { PixelsToLonLat, coordToPixel } from "../utils/mercator";
|
|
2
|
+
import type { DataPicture, XYZ } from "../utils/wxtools";
|
|
3
|
+
import type { WxBoundaryMeta } from "../types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Convert a degree to a value on a linear scale, wrapping around at 360 degrees.
|
|
7
|
+
* @param start - The starting angle in degrees.
|
|
8
|
+
* @param end - The ending angle in degrees.
|
|
9
|
+
* @param amount - The interpolation amount between the start and end.
|
|
10
|
+
* @returns The interpolated angle, wrapped to the [0, 360) degree range.
|
|
11
|
+
*/
|
|
12
|
+
function interpolatorDegreeLinear(
|
|
13
|
+
start: number,
|
|
14
|
+
end: number,
|
|
15
|
+
amount: number,
|
|
16
|
+
): number {
|
|
17
|
+
const shortestAngle = ((((end - start) % 360) + 540) % 360) - 180;
|
|
18
|
+
return (start + shortestAngle * amount + 360) % 360;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type InterpolatorSquare = (
|
|
22
|
+
a: number,
|
|
23
|
+
b: number,
|
|
24
|
+
c: number,
|
|
25
|
+
d: number,
|
|
26
|
+
dxt: number,
|
|
27
|
+
dyt: number,
|
|
28
|
+
dmin: number,
|
|
29
|
+
dmul: number,
|
|
30
|
+
) => number;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Interpolator function for square-based bilinear interpolation.
|
|
34
|
+
* This function calculates the interpolated value based on the corners of the square.
|
|
35
|
+
* The interpolation is applied within a 2D grid of values.
|
|
36
|
+
*
|
|
37
|
+
* @param a_ - Value at the top-left corner of the square.
|
|
38
|
+
* @param b_ - Value at the top-right corner of the square.
|
|
39
|
+
* @param c_ - Value at the bottom-left corner of the square.
|
|
40
|
+
* @param d_ - Value at the bottom-right corner of the square.
|
|
41
|
+
* @param dxt - Interpolation coefficient in the x-direction.
|
|
42
|
+
* @param dyt - Interpolation coefficient in the y-direction.
|
|
43
|
+
* @param dmin - Minimum data value.
|
|
44
|
+
* @param dmul - Multiplication factor for scaling.
|
|
45
|
+
* @returns The interpolated value within the square.
|
|
46
|
+
*/
|
|
47
|
+
function interpolatorSquareDegree(
|
|
48
|
+
a_: number,
|
|
49
|
+
b_: number,
|
|
50
|
+
c_: number,
|
|
51
|
+
d_: number,
|
|
52
|
+
dxt: number,
|
|
53
|
+
dyt: number,
|
|
54
|
+
dmin: number,
|
|
55
|
+
dmul: number,
|
|
56
|
+
): number {
|
|
57
|
+
// a----b
|
|
58
|
+
// | |
|
|
59
|
+
// | |
|
|
60
|
+
// c----d
|
|
61
|
+
let a = a_;
|
|
62
|
+
let b = b_;
|
|
63
|
+
let c = c_;
|
|
64
|
+
let d = d_;
|
|
65
|
+
|
|
66
|
+
// 16 possible variants for a,b,c,d != 0 ( != NaN)
|
|
67
|
+
const sq = (a ? 1 : 0) | (b ? 2 : 0) | (c ? 4 : 0) | (d ? 8 : 0);
|
|
68
|
+
switch (sq) {
|
|
69
|
+
case 0b0000:
|
|
70
|
+
return 0; // NaN
|
|
71
|
+
case 0b0001: // only a != NaN
|
|
72
|
+
return a;
|
|
73
|
+
case 0b0010: // only b != NaN
|
|
74
|
+
return b;
|
|
75
|
+
case 0b0011: // a, b != NaN
|
|
76
|
+
c = a;
|
|
77
|
+
d = b;
|
|
78
|
+
break;
|
|
79
|
+
case 0b0100: // ... etc
|
|
80
|
+
return c;
|
|
81
|
+
case 0b0101:
|
|
82
|
+
b = a;
|
|
83
|
+
d = c;
|
|
84
|
+
break;
|
|
85
|
+
case 0b0110:
|
|
86
|
+
d = (b + c) >> 1;
|
|
87
|
+
a = d;
|
|
88
|
+
break;
|
|
89
|
+
case 0b0111:
|
|
90
|
+
d = (b + c) >> 1;
|
|
91
|
+
break;
|
|
92
|
+
case 0b1000:
|
|
93
|
+
return d;
|
|
94
|
+
case 0b1001:
|
|
95
|
+
b = (a + d) >> 1;
|
|
96
|
+
c = b;
|
|
97
|
+
break;
|
|
98
|
+
case 0b1010:
|
|
99
|
+
a = b;
|
|
100
|
+
c = d;
|
|
101
|
+
break;
|
|
102
|
+
case 0b1011:
|
|
103
|
+
c = (a + d) >> 1;
|
|
104
|
+
break;
|
|
105
|
+
case 0b1100:
|
|
106
|
+
a = c;
|
|
107
|
+
b = d;
|
|
108
|
+
break;
|
|
109
|
+
case 0b1101:
|
|
110
|
+
b = (a + d) >> 1;
|
|
111
|
+
break;
|
|
112
|
+
case 0b1110:
|
|
113
|
+
a = (b + c) >> 1;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// decode Data
|
|
118
|
+
a = dmin + dmul * a;
|
|
119
|
+
b = dmin + dmul * b;
|
|
120
|
+
c = dmin + dmul * c;
|
|
121
|
+
d = dmin + dmul * d;
|
|
122
|
+
|
|
123
|
+
// 2) bilinear
|
|
124
|
+
const u = interpolatorDegreeLinear(a, b, dxt); // upper line
|
|
125
|
+
const l = interpolatorDegreeLinear(c, d, dxt); // lower line
|
|
126
|
+
// Encode Data back before returning
|
|
127
|
+
const ul = (interpolatorDegreeLinear(u, l, dyt) - dmin) / dmul;
|
|
128
|
+
if (ul < 1) return 1; // As 0 is NaN, we don't need NaN here! so return 1 instead of NaN
|
|
129
|
+
return ul;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Interpolator function for square-based interpolation.
|
|
134
|
+
* This function calculates the interpolated value within a square grid.
|
|
135
|
+
*
|
|
136
|
+
* @param a - Value at the top-left corner of the square.
|
|
137
|
+
* @param b - Value at the top-right corner of the square.
|
|
138
|
+
* @param c - Value at the bottom-left corner of the square.
|
|
139
|
+
* @param d - Value at the bottom-right corner of the square.
|
|
140
|
+
* @param dxt - Interpolation coefficient in the x-direction.
|
|
141
|
+
* @param dyt - Interpolation coefficient in the y-direction.
|
|
142
|
+
* @param dmin - Minimum data value.
|
|
143
|
+
* @param dmul - Multiplication factor for scaling.
|
|
144
|
+
* @returns The interpolated value within the square.
|
|
145
|
+
*/
|
|
146
|
+
function interpolatorSquare(
|
|
147
|
+
a: number,
|
|
148
|
+
b: number,
|
|
149
|
+
c: number,
|
|
150
|
+
d: number,
|
|
151
|
+
dxt: number,
|
|
152
|
+
dyt: number,
|
|
153
|
+
dmin: number,
|
|
154
|
+
dmul: number,
|
|
155
|
+
): number {
|
|
156
|
+
// 0 1
|
|
157
|
+
// a --- b default version a --- b flipped version
|
|
158
|
+
// | / | | \ |
|
|
159
|
+
// | / x | - pyt | \ |
|
|
160
|
+
// c --- d c --- d
|
|
161
|
+
// 2 | 3
|
|
162
|
+
// pxt
|
|
163
|
+
//
|
|
164
|
+
// x - point to interpolate
|
|
165
|
+
// a, b, c, d - corners of the square
|
|
166
|
+
// 16 possible variants for a,b,c,d != 0 ( != NaN)
|
|
167
|
+
const sq = (a ? 1 : 0) | (b ? 2 : 0) | (c ? 4 : 0) | (d ? 8 : 0);
|
|
168
|
+
switch (sq) {
|
|
169
|
+
case 0b0111: // -cba -default version
|
|
170
|
+
return dxt + dyt < 1 ? dxt * (b - a) + dyt * (c - a) + a : 0;
|
|
171
|
+
case 0b1110: // dcb- -default version
|
|
172
|
+
return dxt + dyt < 1 ? 0 : dxt * (d - c) + dyt * (d - b) + b + c - d;
|
|
173
|
+
case 0b1011: // d-ba - flipped version
|
|
174
|
+
return dyt < dxt ? (1 - dxt) * (a - b) + dyt * (d - b) + b : 0;
|
|
175
|
+
case 0b1101: // dc-a - flipped version
|
|
176
|
+
return dyt < dxt ? 0 : (1 - dxt) * (c - d) + dyt * (c - a) + a + d - c;
|
|
177
|
+
case 0b1111: // dcba - default version
|
|
178
|
+
return dxt + dyt < 1
|
|
179
|
+
? dxt * (b - a) + dyt * (c - a) + a
|
|
180
|
+
: dxt * (d - c) + dyt * (d - b) + b + c - d;
|
|
181
|
+
default:
|
|
182
|
+
return 0;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Subdivide a data picture using a specified interpolation method.
|
|
188
|
+
* The interpolation is performed to extract a sub-region (subtile) from a larger data picture.
|
|
189
|
+
*
|
|
190
|
+
* @param interpolator - The interpolation function to use.
|
|
191
|
+
* @param inputData - The input data picture from which to extract the subtile.
|
|
192
|
+
* @param subCoords - The coordinates of the subtile within the grid.
|
|
193
|
+
* @returns The subdivided data picture containing the interpolated subtile.
|
|
194
|
+
*/
|
|
195
|
+
function subDataPicture(
|
|
196
|
+
interpolator: InterpolatorSquare,
|
|
197
|
+
inputData: DataPicture,
|
|
198
|
+
subCoords: XYZ,
|
|
199
|
+
): DataPicture {
|
|
200
|
+
const subTileSize = 1 / 2 ** subCoords.z; // a size of a subtile
|
|
201
|
+
const subTileStartX = subCoords.x * 256 * subTileSize - 0.5; // upper left point of a subtile Shifted by 0.5 to get the center of the pixel
|
|
202
|
+
const subTileStartY = subCoords.y * 256 * subTileSize - 0.5;
|
|
203
|
+
const { raw: inRaw, dmin, dmax, dmul } = inputData;
|
|
204
|
+
const subData: DataPicture = {
|
|
205
|
+
raw: new Uint16Array(258 * 258),
|
|
206
|
+
dmin,
|
|
207
|
+
dmax,
|
|
208
|
+
dmul,
|
|
209
|
+
};
|
|
210
|
+
const { raw: outRaw } = subData;
|
|
211
|
+
for (
|
|
212
|
+
let outY = -1,
|
|
213
|
+
outIndex = 0,
|
|
214
|
+
inYf = subTileStartY - subTileSize + 1 /*+1 to shift index */;
|
|
215
|
+
outY <= 256;
|
|
216
|
+
outY++, inYf += subTileSize
|
|
217
|
+
) {
|
|
218
|
+
const inYi = Math.floor(inYf); // don't use `~~` because of negatives on left and upper borders
|
|
219
|
+
const yt = inYf - inYi; // [0, 1] - `y` interpolati`on coeff
|
|
220
|
+
const inYi258 = inYi * 258;
|
|
221
|
+
for (
|
|
222
|
+
let outX = -1,
|
|
223
|
+
inXf = subTileStartX - subTileSize + 1 /*+1 to shift index */;
|
|
224
|
+
outX <= 256;
|
|
225
|
+
outX++, outIndex++, inXf += subTileSize
|
|
226
|
+
) {
|
|
227
|
+
const inXi = Math.floor(inXf); // don't use ~~ because of negatives
|
|
228
|
+
const xt = inXf - inXi;
|
|
229
|
+
const inIndex = inXi + inYi258; // data index
|
|
230
|
+
// interpolation inside a rectangular
|
|
231
|
+
const a = inRaw[inIndex]; // upper left corner
|
|
232
|
+
const b = inRaw[inIndex + 1]; // upper right
|
|
233
|
+
const c = inRaw[inIndex + 258]; // lower left
|
|
234
|
+
const d = inRaw[inIndex + 258 + 1]; // lower right
|
|
235
|
+
outRaw[outIndex] = interpolator(a, b, c, d, xt, yt, dmin, dmul);
|
|
236
|
+
} // for x
|
|
237
|
+
} // for y
|
|
238
|
+
|
|
239
|
+
return subData;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Get sub-tile of a mask via baricemrtric interpolation
|
|
244
|
+
* @param {ImageData} inputData - input data
|
|
245
|
+
* @param {XYZ | undefined} subCoords - subtile coordinates
|
|
246
|
+
* @returns {ImageData} subtile data
|
|
247
|
+
* */
|
|
248
|
+
export function subMask(
|
|
249
|
+
inputData: ImageData,
|
|
250
|
+
subCoords: XYZ | undefined,
|
|
251
|
+
channel: number,
|
|
252
|
+
): ImageData {
|
|
253
|
+
if (!subCoords) return inputData;
|
|
254
|
+
|
|
255
|
+
const clamp = (v: number) => (v < 0 ? 0 : v > 254 ? 254 : v);
|
|
256
|
+
const subTileSize = 1 / 2 ** subCoords.z; // a size of a subtile
|
|
257
|
+
const subTileStartX = subCoords.x * 256 * subTileSize - 0.5; // upper left point of a subtile Shifted by 0.5 to get the center of the pixel
|
|
258
|
+
const subTileStartY = subCoords.y * 256 * subTileSize - 0.5;
|
|
259
|
+
const { data: inData } = inputData;
|
|
260
|
+
const subData: ImageData = new ImageData(256, 256);
|
|
261
|
+
const { data: outData } = subData;
|
|
262
|
+
for (
|
|
263
|
+
let outY = 0, outIndex = channel, inYf = subTileStartY;
|
|
264
|
+
outY < 256;
|
|
265
|
+
outY++, inYf += subTileSize
|
|
266
|
+
) {
|
|
267
|
+
const inYi = ~~clamp(inYf);
|
|
268
|
+
const yt = inYf - inYi; // [0, 1] - `y` interpolation coeff
|
|
269
|
+
const inYi256 = inYi * 256;
|
|
270
|
+
for (
|
|
271
|
+
let outX = 0, inXf = subTileStartX;
|
|
272
|
+
outX < 256;
|
|
273
|
+
outX++, outIndex += 4, inXf += subTileSize
|
|
274
|
+
) {
|
|
275
|
+
const inXi = ~~clamp(inXf);
|
|
276
|
+
const xt = inXf - inXi;
|
|
277
|
+
const inIndex = (inXi + inYi256) * 4 + channel; // data index to pixel/channel used as a mask
|
|
278
|
+
// interpolation inside a rectangular
|
|
279
|
+
const a = inData[inIndex]; // upper left corner
|
|
280
|
+
const b = inData[inIndex + 4]; // upper right
|
|
281
|
+
const c = inData[inIndex + 4 * 256]; // lower left
|
|
282
|
+
const d = inData[inIndex + 4 * 256 + 4]; // lower right
|
|
283
|
+
const r = interpolatorSquare(a, b, c, d, xt, yt, 0, 0);
|
|
284
|
+
// const r = xt + yt < 1 ? xt * (b - a) + yt * (c - a) + a : xt * (d - c) + yt * (d - b) + b + c - d; // barycentric interpolation
|
|
285
|
+
outData[outIndex] = r > 127 ? 255 : 0;
|
|
286
|
+
} // for x
|
|
287
|
+
} // for y
|
|
288
|
+
|
|
289
|
+
return subData;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Get sub-tile of a regular data via baricemrtric interpolation
|
|
294
|
+
* @param {DataPicture} inputData - input data
|
|
295
|
+
* @param {XYZ | undefined} subCoords - subtile coordinates
|
|
296
|
+
* @returns {DataPicture} subtile data
|
|
297
|
+
* */
|
|
298
|
+
export function subData(inputData: DataPicture, subCoords?: XYZ): DataPicture {
|
|
299
|
+
if (!subCoords) return inputData;
|
|
300
|
+
return subDataPicture(interpolatorSquare, inputData, subCoords);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Get sub-tile of a degree data tile via bilinear degree interpolation, so middle of 350..10 degree is 0 degree.
|
|
305
|
+
* @param {DataPicture} inputData - input data
|
|
306
|
+
* @param {XYZ | undefined} subCoords - subtile coordinates
|
|
307
|
+
* @returns {DataPicture} subtile data
|
|
308
|
+
* */
|
|
309
|
+
export function subDataDegree(
|
|
310
|
+
inputData: DataPicture,
|
|
311
|
+
subCoords?: XYZ,
|
|
312
|
+
): DataPicture {
|
|
313
|
+
if (!subCoords) return inputData;
|
|
314
|
+
return subDataPicture(interpolatorSquareDegree, inputData, subCoords);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Upply sea/land mask to a data tile
|
|
319
|
+
* 0 - for the masks from Sarah (current), 3 - for the masks from Mapbox
|
|
320
|
+
* @param {DataPicture} dataIn - data tile
|
|
321
|
+
* @param {ImageData} mask - sea/land mask
|
|
322
|
+
* @param {number} mc - mask channel of the mask picture to use (0 - Red, 1 - Green, 2 - Blue, 3 - Alpha)
|
|
323
|
+
* @param {'sea' | 'land'} maskType - sea or land masking to apply
|
|
324
|
+
* @returns {DataPicture} - masked data tile
|
|
325
|
+
* */
|
|
326
|
+
export function applyMask(
|
|
327
|
+
dataIn: DataPicture,
|
|
328
|
+
{ data }: ImageData,
|
|
329
|
+
mc: number,
|
|
330
|
+
maskType: "land" | "sea",
|
|
331
|
+
): DataPicture {
|
|
332
|
+
const sea = maskType === "sea";
|
|
333
|
+
const { raw } = dataIn;
|
|
334
|
+
for (let i = mc, j = 259, y = 0; y < 256; j += 2, y++) {
|
|
335
|
+
for (let x = 0; x < 256; x++, j++, i += 4) {
|
|
336
|
+
if (sea === !data[i]) {
|
|
337
|
+
raw[j] = 0; // Perform assignment clearly in the if block
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return dataIn;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Create a bounding box for a tile
|
|
347
|
+
* @param coords - tile coordinates
|
|
348
|
+
* @returns {WxBoundaryMeta} [minLon, minLat, maxLon, maxLat]
|
|
349
|
+
* */
|
|
350
|
+
export function makeBox(coords: XYZ): WxBoundaryMeta {
|
|
351
|
+
const [px, py] = coordToPixel(coords.x, coords.y);
|
|
352
|
+
const [west, north] = PixelsToLonLat(px, py, coords.z);
|
|
353
|
+
const [east, south] = PixelsToLonLat(px + 256, py + 256, coords.z);
|
|
354
|
+
return { west, north, east, south };
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Splits tile coordinates into a tile coords at maximum zoom and a subtile coords.
|
|
359
|
+
* If the tile is below maximum zoom, the subtile coords are undefined.
|
|
360
|
+
* @param coords - tile coordinates
|
|
361
|
+
* @param maxZoom - maximum zoom
|
|
362
|
+
* @returns [tile coords, subtile coords (or undefined)]
|
|
363
|
+
* */
|
|
364
|
+
export function splitCoords(
|
|
365
|
+
coords: XYZ,
|
|
366
|
+
maxZoom: number,
|
|
367
|
+
): { upCoords: XYZ; subCoords?: XYZ } {
|
|
368
|
+
const zDif = coords.z - maxZoom;
|
|
369
|
+
if (zDif <= 0) {
|
|
370
|
+
return { upCoords: coords };
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const upCoords = { x: coords.x >>> zDif, y: coords.y >>> zDif, z: maxZoom };
|
|
374
|
+
const subCoords = {
|
|
375
|
+
x: coords.x & ((1 << zDif) - 1),
|
|
376
|
+
y: coords.y & ((1 << zDif) - 1),
|
|
377
|
+
z: zDif,
|
|
378
|
+
};
|
|
379
|
+
return { upCoords, subCoords };
|
|
380
|
+
}
|