-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathinjectables.js
More file actions
130 lines (120 loc) · 3.78 KB
/
injectables.js
File metadata and controls
130 lines (120 loc) · 3.78 KB
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
import { calls } from "./calls";
import { data as colormapData } from '../external/js-colormaps.js'
const injectablesDefaults = {
TILE_MATRIX_SETS: ["WebMercatorQuad"],
COLORMAP_NAMES: ["viridis"],
VELOCITY_COLORMAP_NAMES: ["RDYLBU_R"],
};
// Initialize with reasonable defaults
const injectables = {
TILE_MATRIX_SETS: injectablesDefaults["TILE_MATRIX_SETS"],
COLORMAP_NAMES: injectablesDefaults["COLORMAP_NAMES"],
VELOCITY_COLORMAP_NAMES: injectablesDefaults["VELOCITY_COLORMAP_NAMES"],
};
export const getInjectables = () => {
getTileMatrixSets();
getColormapNames("COLORMAP_NAMES");
getColormapNames("VELOCITY_COLORMAP_NAMES");
};
export const inject = (configJson) => {
let injected = JSON.stringify(configJson);
Object.keys(injectables).forEach((inj) => {
injected = injected.replaceAll(
`"{{${inj}}}"`,
Array.isArray(injectables[inj])
? JSON.stringify(injectables[inj])
: injectables[inj]
);
});
return JSON.parse(injected);
};
function getTileMatrixSets() {
const injectableName = "TILE_MATRIX_SETS";
if (window.mmgisglobal.WITH_TITILER === "true") {
calls.api(
"titiler_tileMatrixSets",
null,
(res) => {
// ... new Set removes duplicates
injectables[injectableName] = [
...new Set(
injectablesDefaults["TILE_MATRIX_SETS"].concat(
res.tileMatrixSets.map((s) => s.id)
)
),
];
},
(res) => {
console.warn(`Failed to query for ${injectableName}. Using defaults.`);
injectables[injectableName] = [
"WebMercatorQuad",
"CanadianNAD83_LCC",
"CDB1GlobalGrid",
"EuropeanETRS89_LAEAQuad",
"GNOSISGlobalGrid",
"LINZAntarticaMapTilegrid",
"NZTM2000Quad",
"UPSAntarcticWGS84Quad",
"UPSArcticWGS84Quad",
"UTM31WGS84Quad",
"WGS1984Quad",
"WorldCRS84Quad",
"WorldMercatorWGS84Quad",
];
}
);
}
}
function getColormapNames(injectableName) {
if (window.mmgisglobal.WITH_TITILER === "true") {
calls.api(
"titiler_colormapNames",
null,
(res) => {
// Get the intersection of colormaps from js-colormaps and TiTiler
const js_colormaps = Object.keys(colormapData).map((color => color.toLowerCase()));
let colormaps = res.colorMaps;
colormaps = colormaps.filter((color) => {
if (js_colormaps.includes(color.toLowerCase())) {
return color;
}
// js-colormaps only includes the non reversed names so check for the reverse
if (color.endsWith("_r") && js_colormaps.includes(color.substr(0, color.length - 2))) {
return color;
}
});
// Sort
colormaps.sort();
// ... new Set removes duplicates
injectables[injectableName] = [
...new Set(
injectablesDefaults[injectableName].concat(colormaps)
),
];
},
(res) => {
console.warn(`Failed to query for ${injectableName}. Using defaults.`);
injectables[injectableName] = Object.keys(colormapData);
}
);
} else {
// Get colormaps from js-colormaps and the inversed colors
const js_colormaps = Object.keys(colormapData).map((color => color.toLowerCase()));
let colormaps = [];
js_colormaps.forEach((color) => {
colormaps.push(color);
// js-colormaps only includes the non reversed names so add the reverse
if (!color.endsWith("_r")) {
colormaps.push(`${color}_r`);
}
});
// Sort
colormaps.sort();
// ... new Set removes duplicates
injectables[injectableName] = [
...new Set(
injectablesDefaults[injectableName].concat(colormaps)
),
];
}
}