@metoceanapi/wxtiles-common 0.0.6 → 0.0.8
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/.github/workflows/lint.yml +24 -0
- package/.husky/pre-commit +1 -0
- package/GPLv3.txt +674 -0
- package/biome.json +12 -0
- package/controls/WxAPIControl.ts +131 -0
- package/controls/WxInfoControl.ts +114 -0
- package/controls/WxLegendControl.ts +132 -0
- package/controls/WxStyleEditorControl.ts +560 -0
- package/controls/WxTimeControl.ts +133 -0
- package/defaults/colorschemes.ts +82 -0
- package/defaults/styles.ts +63 -0
- package/defaults/uconv.ts +56 -0
- package/hooks/pre-commit +29 -0
- package/package.json +4 -7
- package/utils/RawCLUT.ts +272 -0
- package/utils/mercator.ts +136 -0
- package/utils/qtree.ts +185 -0
- package/utils/tests_wxtools.ts +194 -0
- package/utils/wxtools.ts +1108 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { WxColorSchemes } from "../utils/wxtools";
|
|
2
|
+
export const __colorSchemes_default_preset: WxColorSchemes = {
|
|
3
|
+
none: ["#00000000", "#00000000"],
|
|
4
|
+
diverging_blue_red: [
|
|
5
|
+
"#2166acff",
|
|
6
|
+
"#92c5deff",
|
|
7
|
+
"#f7f7f7ff",
|
|
8
|
+
"#f4a582ff",
|
|
9
|
+
"#b2182bff",
|
|
10
|
+
],
|
|
11
|
+
rainbow: [
|
|
12
|
+
"#ff0000ff",
|
|
13
|
+
"#ffff00ff",
|
|
14
|
+
"#00ff00ff",
|
|
15
|
+
"#00ffffff",
|
|
16
|
+
"#0000ffff",
|
|
17
|
+
"#ff00ffff",
|
|
18
|
+
],
|
|
19
|
+
rainbow_zero: [
|
|
20
|
+
"#ff000000",
|
|
21
|
+
"#ff0000ff",
|
|
22
|
+
"#ffff00ff",
|
|
23
|
+
"#00ff00ff",
|
|
24
|
+
"#00ffffff",
|
|
25
|
+
"#0000ffff",
|
|
26
|
+
"#ff00ffff",
|
|
27
|
+
],
|
|
28
|
+
rainbow_wrap: [
|
|
29
|
+
"#ff0000ff",
|
|
30
|
+
"#ffff00ff",
|
|
31
|
+
"#00ff00ff",
|
|
32
|
+
"#00ffffff",
|
|
33
|
+
"#0000ffff",
|
|
34
|
+
"#ff00ffff",
|
|
35
|
+
"#ff0000ff",
|
|
36
|
+
],
|
|
37
|
+
rainbow_backwards: [
|
|
38
|
+
"#0000ffff",
|
|
39
|
+
"#00ffffff",
|
|
40
|
+
"#00ff00ff",
|
|
41
|
+
"#ffff00ff",
|
|
42
|
+
"#ff0000ff",
|
|
43
|
+
"#ff00ffff",
|
|
44
|
+
],
|
|
45
|
+
rainbow_backwards_zero: [
|
|
46
|
+
"#0000ff00",
|
|
47
|
+
"#0000ffff",
|
|
48
|
+
"#00ffffff",
|
|
49
|
+
"#00ff00ff",
|
|
50
|
+
"#ffff00ff",
|
|
51
|
+
"#ff0000ff",
|
|
52
|
+
"#ff00ffff",
|
|
53
|
+
],
|
|
54
|
+
bluebird: ["#0000ffff", "#ff00ffff", "#00ffffff", "#9000ffff", "#9090ffff"],
|
|
55
|
+
bluebird_zero: [
|
|
56
|
+
"#0000ff00",
|
|
57
|
+
"#0000ffff",
|
|
58
|
+
"#ff00ffff",
|
|
59
|
+
"#00ffffff",
|
|
60
|
+
"#9000ffff",
|
|
61
|
+
"#9090ffff",
|
|
62
|
+
],
|
|
63
|
+
black_white: ["#000000ff", "#ffffffff"],
|
|
64
|
+
white_black: ["#ffffffff", "#000000ff"],
|
|
65
|
+
reddish: ["#ff00ffff", "#ff0000ff", "#ffff00ff"],
|
|
66
|
+
greenish: ["#ffff00ff", "#00ff00ff", "#00ffffff"],
|
|
67
|
+
blueish: ["#ff00ffff", "#0000ffff", "#00ffffff"],
|
|
68
|
+
hspastel: [
|
|
69
|
+
"#AC6EA4FF",
|
|
70
|
+
"#8E92BDFF",
|
|
71
|
+
"#ACD4DEFF",
|
|
72
|
+
"#E9DC8EFF",
|
|
73
|
+
"#E7A97DFF",
|
|
74
|
+
"#E59074FF",
|
|
75
|
+
"#BE7E68FF",
|
|
76
|
+
"#A88F86FF",
|
|
77
|
+
],
|
|
78
|
+
black: ["#000000ff", "#000000ff"],
|
|
79
|
+
white: ["#ffffffff", "#ffffffff"],
|
|
80
|
+
transparent: ["#ffffff00"],
|
|
81
|
+
peach_red_zero: ["#fdd49f00", "#fdd49fff", "#d7301fff"],
|
|
82
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { WxColorStylesStrict } from "../utils/wxtools";
|
|
2
|
+
|
|
3
|
+
export const __colorStyles_default_preset: WxColorStylesStrict = {
|
|
4
|
+
base: {
|
|
5
|
+
parent: undefined,
|
|
6
|
+
name: "base",
|
|
7
|
+
fill: "gradient",
|
|
8
|
+
isolineColor: "none",
|
|
9
|
+
isolineText: true,
|
|
10
|
+
isolineTextSizeEm: 1.1,
|
|
11
|
+
isolineTextBackground: false,
|
|
12
|
+
vectorType: "arrows",
|
|
13
|
+
vectorColor: "inverted",
|
|
14
|
+
vectorFactor: 1,
|
|
15
|
+
streamLineColor: "none",
|
|
16
|
+
streamLineSpeedFactor: 1,
|
|
17
|
+
streamLineStatic: false,
|
|
18
|
+
showBelowMin: true,
|
|
19
|
+
showAboveMax: true,
|
|
20
|
+
colorScheme: "diverging_blue_red",
|
|
21
|
+
colors: undefined,
|
|
22
|
+
colorMap: undefined,
|
|
23
|
+
levels: undefined,
|
|
24
|
+
blurRadius: 0,
|
|
25
|
+
addDegrees: 0,
|
|
26
|
+
units: "",
|
|
27
|
+
extraUnits: undefined,
|
|
28
|
+
mask: "none",
|
|
29
|
+
gl: {
|
|
30
|
+
animationIntensity: 5,
|
|
31
|
+
animationSpeed: 1,
|
|
32
|
+
noiseTexturePow: 5,
|
|
33
|
+
vectorFieldFactor: 1,
|
|
34
|
+
wavesCount: 4,
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
custom: {
|
|
38
|
+
parent: undefined,
|
|
39
|
+
name: "custom",
|
|
40
|
+
fill: "gradient",
|
|
41
|
+
isolineColor: "inverted",
|
|
42
|
+
isolineText: true,
|
|
43
|
+
isolineTextSizeEm: 1.1,
|
|
44
|
+
isolineTextBackground: false,
|
|
45
|
+
vectorType: "arrows",
|
|
46
|
+
vectorColor: "inverted",
|
|
47
|
+
vectorFactor: 1,
|
|
48
|
+
streamLineColor: "#777",
|
|
49
|
+
streamLineSpeedFactor: 1,
|
|
50
|
+
streamLineStatic: false,
|
|
51
|
+
showBelowMin: true,
|
|
52
|
+
showAboveMax: true,
|
|
53
|
+
colorScheme: "rainbow",
|
|
54
|
+
colors: undefined,
|
|
55
|
+
colorMap: undefined,
|
|
56
|
+
levels: undefined,
|
|
57
|
+
blurRadius: 0,
|
|
58
|
+
addDegrees: 0,
|
|
59
|
+
units: "",
|
|
60
|
+
extraUnits: undefined,
|
|
61
|
+
mask: "none",
|
|
62
|
+
},
|
|
63
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { WxUnits } from "../utils/wxtools";
|
|
2
|
+
|
|
3
|
+
export const __units_default_preset: WxUnits = {
|
|
4
|
+
comment1: ["'degC': ['K', 1, 273.15] -> degC = K * 1 + 273.15", 0],
|
|
5
|
+
comment2: [
|
|
6
|
+
"'hPa': ['Pa', 1000]' -> hPa = Pa * 1000 + 0 (0 - could be ommited)",
|
|
7
|
+
0,
|
|
8
|
+
],
|
|
9
|
+
K: ["K", 1],
|
|
10
|
+
kelvin: ["K", 1],
|
|
11
|
+
F: ["K", 0.5555555555, 255.372222222],
|
|
12
|
+
C: ["K", 1, 273.15],
|
|
13
|
+
degC: ["K", 1, 273.15],
|
|
14
|
+
"kg/m^2/s": ["kg/m^2/s", 1],
|
|
15
|
+
"Kg/m^2/s": ["kg/m^2/s", 1],
|
|
16
|
+
"Kg m**-2 s**-1": ["kg/m^2/s", 1],
|
|
17
|
+
"Kg m^-2 s^-1": ["kg/m^2/s", 1],
|
|
18
|
+
"Kg m^{-2} s^-1": ["kg/m^2/s", 1],
|
|
19
|
+
"mm/h": ["kg/m^2/s", 0.00027777777],
|
|
20
|
+
"mm/hr": ["kg/m^2/s", 0.00027777777],
|
|
21
|
+
"W/m^2": ["W/m^2", 1],
|
|
22
|
+
"W m^-2": ["W/m^2", 1],
|
|
23
|
+
"W m^{-2}": ["W/m^2", 1],
|
|
24
|
+
"W/m**2": ["W/m^2", 1],
|
|
25
|
+
"W m**-2": ["W/m^2", 1],
|
|
26
|
+
"m/s": ["m/s", 1],
|
|
27
|
+
"m s**-1": ["m/s", 1],
|
|
28
|
+
"m s^-1": ["m/s", 1],
|
|
29
|
+
"m.s^{-1}": ["m/s", 1],
|
|
30
|
+
knot: ["m/s", 0.514444],
|
|
31
|
+
knots: ["m/s", 0.514444],
|
|
32
|
+
"km/h": ["m/s", 0.27777777777],
|
|
33
|
+
"km h^-1": ["m/s", 0.27777777777],
|
|
34
|
+
"km h**-1": ["m/s", 0.27777777777],
|
|
35
|
+
mph: ["m/s", 0.44704],
|
|
36
|
+
"miles/h": ["m/s", 0.44704],
|
|
37
|
+
s: ["s", 1],
|
|
38
|
+
sec: ["s", 1],
|
|
39
|
+
h: ["s", 3600],
|
|
40
|
+
min: ["s", 60],
|
|
41
|
+
m: ["m", 1],
|
|
42
|
+
cm: ["m", 0.01],
|
|
43
|
+
km: ["m", 1000],
|
|
44
|
+
mile: ["m", 1609.344],
|
|
45
|
+
miles: ["m", 1609.344],
|
|
46
|
+
inch: ["m", 0.0254],
|
|
47
|
+
Pa: ["Pa", 1],
|
|
48
|
+
hPa: ["Pa", 100],
|
|
49
|
+
degree: ["degree", 1],
|
|
50
|
+
radian: ["degree", 57.3],
|
|
51
|
+
"mg/m^3": ["mg/m^3", 1],
|
|
52
|
+
"mg m^-3": ["mg/m^3", 1],
|
|
53
|
+
"mg.m^-3": ["mg/m^3", 1],
|
|
54
|
+
"%": ["%", 1],
|
|
55
|
+
"(0 - 1)": ["%", 100],
|
|
56
|
+
};
|
package/hooks/pre-commit
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
3
|
+
# Check if Node.js is installed
|
|
4
|
+
node --version > /dev/null 2>&1
|
|
5
|
+
if [[ $? != 0 ]]; then
|
|
6
|
+
echo "Node.js could not be found. Please install Node.js."
|
|
7
|
+
exit 1
|
|
8
|
+
fi
|
|
9
|
+
|
|
10
|
+
# Check if Biome is installed
|
|
11
|
+
npx biome --version > /dev/null 2>&1
|
|
12
|
+
if [[ $? != 0 ]]; then
|
|
13
|
+
echo "Biome could not be found. Installing Biome..."
|
|
14
|
+
npm install --save-dev --save-exact @biomejs/biome
|
|
15
|
+
fi
|
|
16
|
+
|
|
17
|
+
# Run Biome linter
|
|
18
|
+
npx biome lint
|
|
19
|
+
|
|
20
|
+
# Capture the exit code of the linter
|
|
21
|
+
LINT_EXIT_CODE=$?
|
|
22
|
+
|
|
23
|
+
# Exit with the linter's exit code
|
|
24
|
+
if [ $LINT_EXIT_CODE -ne 0 ]; then
|
|
25
|
+
echo "Biome linter failed. Please fix the issues before committing."
|
|
26
|
+
exit 1
|
|
27
|
+
fi
|
|
28
|
+
|
|
29
|
+
exit 0
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metoceanapi/wxtiles-common",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Common resources shared by wxtiles mapbox and leaflet packages.",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./*": "./*.ts",
|
|
7
|
-
"./*.css": "./*.css"
|
|
7
|
+
"./*.css": "./*.css",
|
|
8
|
+
"./*.woff": "./*.woff"
|
|
8
9
|
},
|
|
9
10
|
"private": false,
|
|
10
11
|
"scripts": {
|
|
@@ -27,9 +28,5 @@
|
|
|
27
28
|
"bugs": {
|
|
28
29
|
"url": "https://github.com/metoceanapi/wxtiles-common/issues"
|
|
29
30
|
},
|
|
30
|
-
"homepage": "https://github.com/metoceanapi/wxtiles-common#readme"
|
|
31
|
-
"files": [
|
|
32
|
-
"wxtiles.css",
|
|
33
|
-
"fonts/*.woff"
|
|
34
|
-
]
|
|
31
|
+
"homepage": "https://github.com/metoceanapi/wxtiles-common#readme"
|
|
35
32
|
}
|
package/utils/RawCLUT.ts
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import type { Converter, WxColorStyleStrict } from "./wxtools";
|
|
2
|
+
import {
|
|
3
|
+
HEXtoRGBA,
|
|
4
|
+
RGBtoHEX,
|
|
5
|
+
WXLOG,
|
|
6
|
+
WxGetColorSchemes,
|
|
7
|
+
createLevels,
|
|
8
|
+
makeConverter,
|
|
9
|
+
mixColor,
|
|
10
|
+
} from "./wxtools";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Clamps a value between a minimum and maximum value.
|
|
14
|
+
* @param {number} val The value to clamp.
|
|
15
|
+
* @param {number} min The minimum value to clamp to.
|
|
16
|
+
* @param {number} max The maximum value to clamp to.
|
|
17
|
+
* @returns {number} The clamped value.
|
|
18
|
+
*/
|
|
19
|
+
function clamp(val: number, min: number, max: number): number {
|
|
20
|
+
return val > max ? max : val < min ? min : val;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Interface representing a tick on the legend of a color style.
|
|
25
|
+
* @interface
|
|
26
|
+
*/
|
|
27
|
+
export interface WxTick {
|
|
28
|
+
/** the data value */
|
|
29
|
+
data: number;
|
|
30
|
+
/** compact string representation of the data value */
|
|
31
|
+
dataString: string;
|
|
32
|
+
/** the color of the data value in web format */
|
|
33
|
+
color: string;
|
|
34
|
+
/** the position of the data value on the legend in [0, legendSize] */
|
|
35
|
+
pos: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @internal
|
|
40
|
+
* Class contains information about the color style and the legend
|
|
41
|
+
* Based on the idea of CLUT (Color Look Up Table) to fill tiles with colors from the style
|
|
42
|
+
*/
|
|
43
|
+
export class RawCLUT {
|
|
44
|
+
/** An array that converts a data value to a level index. */
|
|
45
|
+
levelIndex: Uint32Array;
|
|
46
|
+
/** An array that converts data value to color in U32 representation */
|
|
47
|
+
colorsI: Uint32Array;
|
|
48
|
+
/** convert data value to style value */
|
|
49
|
+
DataToStyle: Converter;
|
|
50
|
+
/** convert data value to knots. Useful for styles for vector data to get the direction of the vector */
|
|
51
|
+
DataToKnots?: Converter;
|
|
52
|
+
/** data values of the legend */
|
|
53
|
+
ticks: WxTick[];
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param {WxColorStyleStrict} style the color style
|
|
57
|
+
* @param {string} dUnits the units of the data
|
|
58
|
+
* @param {[number, number]} minmax the array of minimum and maximum data values
|
|
59
|
+
* @param {boolean} vector if true, the style is for vector data
|
|
60
|
+
* */
|
|
61
|
+
constructor(
|
|
62
|
+
style: WxColorStyleStrict,
|
|
63
|
+
dUnits: string,
|
|
64
|
+
[dataMin, dataMax]: [number, number],
|
|
65
|
+
vector: boolean,
|
|
66
|
+
) {
|
|
67
|
+
WXLOG("RawCLUT.constructor");
|
|
68
|
+
this.levelIndex = new Uint32Array(65536);
|
|
69
|
+
this.colorsI = new Uint32Array(65536);
|
|
70
|
+
let levels: number[]; // data values of the legend
|
|
71
|
+
|
|
72
|
+
this.DataToStyle = makeConverter(dUnits, style.units, style.extraUnits);
|
|
73
|
+
if (this.DataToStyle.trivial) style.units = dUnits; // in case Style doesn't contain unit or incorrect units, use dUnits
|
|
74
|
+
if (vector) {
|
|
75
|
+
this.DataToKnots = makeConverter(dUnits, "knot")
|
|
76
|
+
}
|
|
77
|
+
const styleValToData = makeConverter(style.units, dUnits, style.extraUnits);
|
|
78
|
+
const styleValToRAW = (x: number) =>
|
|
79
|
+
~~(
|
|
80
|
+
65535 * clamp((styleValToData(x) - dataMin) / (dataMax - dataMin), 0, 1)
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
// A MAGIC with colors and levels is happening here
|
|
84
|
+
if (style.colorMap?.length) {
|
|
85
|
+
/** convert style ticks to data ticks, data ticks to indexes [0-65535]
|
|
86
|
+
* used for filling {@link levelIndex} */
|
|
87
|
+
levels = style.colorMap
|
|
88
|
+
.sort((a, b) => a[0] - b[0])
|
|
89
|
+
.map(([val]) => styleValToRAW(val));
|
|
90
|
+
} else {
|
|
91
|
+
// check levels, if empty - create them
|
|
92
|
+
style.levels ||= createLevels(
|
|
93
|
+
this.DataToStyle(dataMin),
|
|
94
|
+
this.DataToStyle(dataMax),
|
|
95
|
+
10,
|
|
96
|
+
);
|
|
97
|
+
style.levels.sort((a: number, b: number) => a - b);
|
|
98
|
+
|
|
99
|
+
// if no colors in the style, try predefined color schemes
|
|
100
|
+
const colorSchemes = WxGetColorSchemes();
|
|
101
|
+
style.colors ||= colorSchemes[style.colorScheme] || [
|
|
102
|
+
"#ffffffff",
|
|
103
|
+
"#000000ff",
|
|
104
|
+
];
|
|
105
|
+
|
|
106
|
+
levels = style.levels.map(styleValToRAW);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const dataDifMul = dataMax - dataMin;
|
|
110
|
+
const legend = WxCreateLegend(65536, style);
|
|
111
|
+
this.ticks = legend.ticks;
|
|
112
|
+
const legendDataMin = legend.ticks[0].data;
|
|
113
|
+
const legendDataMax = legend.ticks[legend.ticks.length - 1].data;
|
|
114
|
+
const legendDataDif = legendDataMax - legendDataMin;
|
|
115
|
+
const colorBelowMin = style.showBelowMin ? legend.colors[0] : 0;
|
|
116
|
+
const colorAboveMax = style.showAboveMax ? legend.colors[65535] : 0;
|
|
117
|
+
// fill up 'colorsI' (is the actual color look up table)
|
|
118
|
+
// legend.colors and colorsI are different as legend.colors contains only colors from the legend
|
|
119
|
+
// colorsI contains colors for all possible data values
|
|
120
|
+
// for each index in [0, 65535] for the packed data value
|
|
121
|
+
// find the corresponding color index in the legend
|
|
122
|
+
this.colorsI[0] = 0; // set color for NODATA
|
|
123
|
+
for (let i = 1; i < 65536; ++i) {
|
|
124
|
+
const dataInStyleUnits = this.DataToStyle(
|
|
125
|
+
(dataDifMul * i) / 65535 + dataMin,
|
|
126
|
+
); // index -> data -> style units
|
|
127
|
+
const legendIndex = Math.round(
|
|
128
|
+
(65535 * (dataInStyleUnits - legendDataMin)) / legendDataDif,
|
|
129
|
+
); // style units -> legend index
|
|
130
|
+
this.colorsI[i] =
|
|
131
|
+
legendIndex <= 0
|
|
132
|
+
? colorBelowMin
|
|
133
|
+
: legendIndex > 65535
|
|
134
|
+
? colorAboveMax
|
|
135
|
+
: legend.colors[legendIndex];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// fill up 'levelIndex'
|
|
139
|
+
// 1. fill up left part of levelIndex (before the first level)
|
|
140
|
+
for (let i = 0; i < levels[0]; ++i) {
|
|
141
|
+
this.levelIndex[i] = 0;
|
|
142
|
+
}
|
|
143
|
+
// 2. fill up middle part of levelIndex
|
|
144
|
+
for (let li = 0; li < levels.length - 1; li++) {
|
|
145
|
+
for (let i = levels[li]; i < levels[li + 1] + 1; ++i) {
|
|
146
|
+
this.levelIndex[i] = li;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// 3. fill up the rest of levelIndex after the last colorMap value
|
|
150
|
+
for (let i = levels[levels.length - 1]; i < 65536; ++i) {
|
|
151
|
+
this.levelIndex[i] = levels.length - 1;
|
|
152
|
+
}
|
|
153
|
+
} // constructor
|
|
154
|
+
} // class CLUT
|
|
155
|
+
|
|
156
|
+
/** Build compact string representation of a number */
|
|
157
|
+
function numToString(n: number) {
|
|
158
|
+
if (n !== 0 && -0.1 < n && n < 0.1) return n.toExponential(2);
|
|
159
|
+
const ns = n.toString();
|
|
160
|
+
if (ns.split(".")[1]?.length > 2) return n.toFixed(2);
|
|
161
|
+
return ns;
|
|
162
|
+
} // numToString
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Interface representing a legend for a color style usef for rendering legends.
|
|
166
|
+
* @interface
|
|
167
|
+
*/
|
|
168
|
+
export interface WxLegend {
|
|
169
|
+
/** The size of the legend in pixels. */
|
|
170
|
+
size: number;
|
|
171
|
+
/** Whether to show the color for values below the minimum. */
|
|
172
|
+
showBelowMin: boolean;
|
|
173
|
+
/** Whether to show the color for values above the maximum. */
|
|
174
|
+
showAboveMax: boolean;
|
|
175
|
+
/** The units of the legend. */
|
|
176
|
+
units: string;
|
|
177
|
+
/** An array of colors. */
|
|
178
|
+
colors: Uint32Array;
|
|
179
|
+
/** An array of ticks. */
|
|
180
|
+
ticks: WxTick[];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Creates a legend for a given color style.
|
|
185
|
+
* @param {number} legendSize - The number of color-values in the legend.
|
|
186
|
+
* @param {WxColorStyleStrict} style - The color style to create the legend for.
|
|
187
|
+
* @returns {WxLegend} The legend for the given color style.
|
|
188
|
+
*/
|
|
189
|
+
export function WxCreateLegend(
|
|
190
|
+
legendSize: number,
|
|
191
|
+
style: WxColorStyleStrict,
|
|
192
|
+
): WxLegend {
|
|
193
|
+
const legend: WxLegend = {
|
|
194
|
+
size: legendSize,
|
|
195
|
+
showBelowMin: style.showBelowMin,
|
|
196
|
+
showAboveMax: style.showAboveMax,
|
|
197
|
+
units: style.units,
|
|
198
|
+
colors: new Uint32Array(legendSize),
|
|
199
|
+
ticks: [],
|
|
200
|
+
};
|
|
201
|
+
const { colorMap, levels, colors } = style;
|
|
202
|
+
const gradient = style.fill !== "solid";
|
|
203
|
+
|
|
204
|
+
// use colorMap if presented
|
|
205
|
+
if (colorMap) {
|
|
206
|
+
const mapsDataMin = colorMap[0][0];
|
|
207
|
+
const mapsDataDifMul =
|
|
208
|
+
(legendSize - 1) / (colorMap[colorMap.length - 1][0] - mapsDataMin);
|
|
209
|
+
// fill 'ticks'
|
|
210
|
+
legend.ticks = colorMap.map(
|
|
211
|
+
([data, color]) =>
|
|
212
|
+
<WxTick>{
|
|
213
|
+
data,
|
|
214
|
+
dataString: numToString(data),
|
|
215
|
+
color,
|
|
216
|
+
pos: ~~((data - mapsDataMin) * mapsDataDifMul),
|
|
217
|
+
},
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
// fill 'colors'
|
|
221
|
+
for (let li = 0; li < colorMap.length - 1; li++) {
|
|
222
|
+
const pos0 = legend.ticks[li].pos;
|
|
223
|
+
const pos1 = legend.ticks[li + 1].pos;
|
|
224
|
+
const c0 = HEXtoRGBA(colorMap[li][1]);
|
|
225
|
+
const c1 = gradient ? HEXtoRGBA(colorMap[li + 1][1]) : 0;
|
|
226
|
+
for (let i = pos0; i < pos1; ++i) {
|
|
227
|
+
legend.colors[i] = gradient
|
|
228
|
+
? mixColor(c0, c1, (i - pos0) / (pos1 - pos0))
|
|
229
|
+
: c0;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// fill the last of 'colors' with the exact last colorMap's color value
|
|
234
|
+
legend.colors[legendSize - 1] = HEXtoRGBA(colorMap[colorMap.length - 1][1]);
|
|
235
|
+
return legend;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (!colors || !levels) return legend; // empty
|
|
239
|
+
|
|
240
|
+
let c0 = 0;
|
|
241
|
+
let c1 = 0;
|
|
242
|
+
let ci = -1;
|
|
243
|
+
for (let i = 0; i < legendSize; ++i) {
|
|
244
|
+
const cf = (i * (colors.length - 1)) / legendSize; // float index in colors for the current legend.colors index
|
|
245
|
+
if (ci !== ~~cf) {
|
|
246
|
+
ci = ~~cf;
|
|
247
|
+
c0 = HEXtoRGBA(colors[ci]);
|
|
248
|
+
c1 = colors.length > ci + 1 ? HEXtoRGBA(colors[ci + 1]) : c0;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
legend.colors[i] = gradient ? mixColor(c0, c1, cf - ci) : c0;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
legend.colors[legendSize - 1] = HEXtoRGBA(colors[colors.length - 1]);
|
|
255
|
+
|
|
256
|
+
// fill 'ticks'
|
|
257
|
+
const levelsDataMin = levels[0];
|
|
258
|
+
const levelsDataMul =
|
|
259
|
+
(legendSize - 1) / (levels[levels.length - 1] - levelsDataMin);
|
|
260
|
+
|
|
261
|
+
legend.ticks = levels.map((data) => {
|
|
262
|
+
const pos = ~~((data - levelsDataMin) * levelsDataMul);
|
|
263
|
+
return {
|
|
264
|
+
data,
|
|
265
|
+
dataString: numToString(data),
|
|
266
|
+
pos,
|
|
267
|
+
color: RGBtoHEX(legend.colors[pos]),
|
|
268
|
+
};
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
return legend;
|
|
272
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview This file contains utility functions for converting between different coordinate systems.
|
|
3
|
+
* It includes functions for converting between latitude/longitude coordinates in the WGS84 Datum and XY coordinates in the Spherical Mercator EPSG:900913 projection.
|
|
4
|
+
* It also includes functions for converting between pixel coordinates in a given zoom level of a pyramid and EPSG:900913 coordinates.
|
|
5
|
+
* Finally, it includes functions for converting between pixel coordinates in a given zoom level and latitude/longitude coordinates in the WGS84 Datum.
|
|
6
|
+
*/
|
|
7
|
+
const tileSize = 256;
|
|
8
|
+
const initialResolution = (2 * Math.PI * 6378137) / tileSize;
|
|
9
|
+
const originShift = (2 * Math.PI * 6378137) / 2;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Converts given latitude and longitude in WGS84 Datum to the X and Y values in meters.
|
|
13
|
+
* @param lat - The latitude value in degrees.
|
|
14
|
+
* @param lon - The longitude value in degrees.
|
|
15
|
+
* @returns An array of two numbers representing the X and Y values in meters.
|
|
16
|
+
*/
|
|
17
|
+
function LatLonToMeters(lat: number, lon: number): [number, number] {
|
|
18
|
+
const x = (lon * originShift) / 180;
|
|
19
|
+
let y = Math.log(Math.tan(((90 + lat) * Math.PI) / 360)) / (Math.PI / 180);
|
|
20
|
+
y = (y * originShift) / 180;
|
|
21
|
+
return [x, y];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Converts given XY point in meters to latitude and longitude in WGS84 Datum.
|
|
26
|
+
* @param x - The X value in meters.
|
|
27
|
+
* @param y - The Y value in meters.
|
|
28
|
+
* @returns An array of two numbers representing the latitude and longitude values in degrees.
|
|
29
|
+
*/
|
|
30
|
+
function MetersToLatLon(x: number, y: number): [number, number] {
|
|
31
|
+
const lon = (x / originShift) * 180;
|
|
32
|
+
let lat = (y / originShift) * 180;
|
|
33
|
+
lat =
|
|
34
|
+
(180 / Math.PI) *
|
|
35
|
+
(2 * Math.atan(Math.exp((lat * Math.PI) / 180)) - Math.PI / 2);
|
|
36
|
+
return [lat, lon];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Converts pixel coordinates in a given zoom level of a pyramid to EPSG:900913.
|
|
41
|
+
* @param px - The X value in pixels.
|
|
42
|
+
* @param py - The Y value in pixels.
|
|
43
|
+
* @param zoom - The zoom level.
|
|
44
|
+
* @returns An array of two numbers representing the X and Y values in meters.
|
|
45
|
+
*/
|
|
46
|
+
function PixelsToMeters(
|
|
47
|
+
px: number,
|
|
48
|
+
py: number,
|
|
49
|
+
zoom: number,
|
|
50
|
+
): [number, number] {
|
|
51
|
+
const res = Resolution(zoom);
|
|
52
|
+
const x = px * res - originShift;
|
|
53
|
+
const y = py * res - originShift;
|
|
54
|
+
return [x, y];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Converts meters to pixel coordinates in a given zoom level.
|
|
59
|
+
* @param x - The X value in meters.
|
|
60
|
+
* @param y - The Y value in meters.
|
|
61
|
+
* @param zoom - The zoom level.
|
|
62
|
+
* @returns An array of two numbers representing the X and Y values in pixels.
|
|
63
|
+
*/
|
|
64
|
+
function MetersToPixels(x: number, y: number, zoom: number): [number, number] {
|
|
65
|
+
const res = Resolution(zoom);
|
|
66
|
+
const px = (x + originShift) / res;
|
|
67
|
+
const py = (y + originShift) / res;
|
|
68
|
+
return [px, py];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Calculates the resolution (meters/pixel) for a given zoom level (measured at Equator).
|
|
73
|
+
* @param zoom - The zoom level.
|
|
74
|
+
* @returns The resolution in meters/pixel.
|
|
75
|
+
*/
|
|
76
|
+
function Resolution(zoom: number): number {
|
|
77
|
+
return initialResolution / 2 ** zoom;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Converts given latitude and longitude in WGS84 Datum to pixel coordinates in a given zoom level.
|
|
82
|
+
* @param lat - The latitude value in degrees.
|
|
83
|
+
* @param lon - The longitude value in degrees.
|
|
84
|
+
* @param zoom - The zoom level.
|
|
85
|
+
* @returns An array of two numbers representing the X and Y values in pixels.
|
|
86
|
+
*/
|
|
87
|
+
export function LatLonToPixels(
|
|
88
|
+
lat: number,
|
|
89
|
+
lon: number,
|
|
90
|
+
zoom: number,
|
|
91
|
+
): [number, number] {
|
|
92
|
+
const [x, y] = LatLonToMeters(lat, lon);
|
|
93
|
+
return MetersToPixels(x, y, zoom);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Converts pixel coordinates in a given zoom level to latitude and longitude in WGS84 Datum.
|
|
98
|
+
* @param px - The X value in pixels.
|
|
99
|
+
* @param py - The Y value in pixels.
|
|
100
|
+
* @param zoom - The zoom level.
|
|
101
|
+
* @returns An array of two numbers representing the latitude and longitude values in degrees.
|
|
102
|
+
*/
|
|
103
|
+
export function PixelsToLatLon(
|
|
104
|
+
px: number,
|
|
105
|
+
py: number,
|
|
106
|
+
zoom: number,
|
|
107
|
+
): [number, number] {
|
|
108
|
+
const [x, y] = PixelsToMeters(px, py, zoom);
|
|
109
|
+
return MetersToLatLon(x, y);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Converts pixel coordinates in a given zoom level to longitude and latitude in WGS84 Datum.
|
|
114
|
+
* @param px - The X value in pixels.
|
|
115
|
+
* @param py - The Y value in pixels.
|
|
116
|
+
* @param zoom - The zoom level.
|
|
117
|
+
* @returns An array of two numbers representing the longitude and latitude values in degrees.
|
|
118
|
+
*/
|
|
119
|
+
export function PixelsToLonLat(
|
|
120
|
+
px: number,
|
|
121
|
+
py: number,
|
|
122
|
+
zoom: number,
|
|
123
|
+
): [number, number] {
|
|
124
|
+
const [lat, lon] = MetersToLatLon(...PixelsToMeters(px, py, zoom));
|
|
125
|
+
return [lon, -lat];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Converts given coordinates to pixel coordinates.
|
|
130
|
+
* @param x - The X value.
|
|
131
|
+
* @param y - The Y value.
|
|
132
|
+
* @returns An array of two numbers representing the X and Y values in pixels.
|
|
133
|
+
*/
|
|
134
|
+
export function coordToPixel(x: number, y: number) {
|
|
135
|
+
return [x * tileSize, y * tileSize];
|
|
136
|
+
}
|