@maptiler/sdk 1.1.2 → 1.2.1
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/.eslintrc.cjs +15 -5
- package/.github/pull_request_template.md +11 -0
- package/.github/workflows/format-lint.yml +24 -0
- package/CHANGELOG.md +105 -51
- package/colorramp.md +93 -0
- package/dist/maptiler-sdk.d.ts +1226 -124
- package/dist/maptiler-sdk.min.mjs +3 -1
- package/dist/maptiler-sdk.mjs +3582 -483
- package/dist/maptiler-sdk.mjs.map +1 -1
- package/dist/maptiler-sdk.umd.js +4524 -863
- package/dist/maptiler-sdk.umd.js.map +1 -1
- package/dist/maptiler-sdk.umd.min.js +51 -49
- package/package.json +27 -13
- package/readme.md +493 -5
- package/rollup.config.js +2 -16
- package/src/Map.ts +515 -359
- package/src/MaptilerGeolocateControl.ts +23 -20
- package/src/MaptilerLogoControl.ts +3 -3
- package/src/MaptilerNavigationControl.ts +9 -6
- package/src/MaptilerTerrainControl.ts +15 -14
- package/src/Minimap.ts +373 -0
- package/src/Point.ts +3 -5
- package/src/colorramp.ts +1216 -0
- package/src/config.ts +4 -3
- package/src/converters/index.ts +1 -0
- package/src/converters/xml.ts +681 -0
- package/src/defaults.ts +1 -1
- package/src/helpers/index.ts +27 -0
- package/src/helpers/stylehelper.ts +395 -0
- package/src/helpers/vectorlayerhelpers.ts +1511 -0
- package/src/index.ts +90 -121
- package/src/language.ts +116 -79
- package/src/mapstyle.ts +4 -2
- package/src/tools.ts +68 -16
- package/tsconfig.json +8 -5
- package/vite.config.ts +10 -0
- package/demos/maptiler-sdk.css +0 -147
- package/demos/maptiler-sdk.umd.js +0 -4041
- package/demos/mountain.html +0 -67
- package/demos/simple.html +0 -67
- package/demos/transform-request.html +0 -81
package/src/defaults.ts
CHANGED
|
@@ -9,7 +9,7 @@ const defaults = {
|
|
|
9
9
|
maptilerApiHost: "api.maptiler.com",
|
|
10
10
|
rtlPluginURL:
|
|
11
11
|
"https://cdn.maptiler.com/mapbox-gl-rtl-text/v0.2.3/mapbox-gl-rtl-text.min.js",
|
|
12
|
-
primaryLanguage: Language.
|
|
12
|
+
primaryLanguage: Language.STYLE,
|
|
13
13
|
secondaryLanguage: Language.LOCAL,
|
|
14
14
|
terrainSourceURL: "https://api.maptiler.com/tiles/terrain-rgb-v2/tiles.json",
|
|
15
15
|
terrainSourceId: "maptiler-terrain",
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addPolyline,
|
|
3
|
+
addPolygon,
|
|
4
|
+
addPoint,
|
|
5
|
+
addHeatmap,
|
|
6
|
+
} from "./vectorlayerhelpers";
|
|
7
|
+
|
|
8
|
+
export type {
|
|
9
|
+
ZoomStringValues,
|
|
10
|
+
ZoomNumberValues,
|
|
11
|
+
PropertyValues,
|
|
12
|
+
CommonShapeLayerOptions,
|
|
13
|
+
PolylineLayerOptions,
|
|
14
|
+
PolygonLayerOptions,
|
|
15
|
+
PointLayerOptions,
|
|
16
|
+
HeatmapLayerOptions,
|
|
17
|
+
} from "./vectorlayerhelpers";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Helpers are a set of functions to facilitate the creation of sources and layers
|
|
21
|
+
*/
|
|
22
|
+
export const helpers = {
|
|
23
|
+
addPolyline,
|
|
24
|
+
addPolygon,
|
|
25
|
+
addPoint,
|
|
26
|
+
addHeatmap,
|
|
27
|
+
};
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DataDrivenPropertyValueSpecification,
|
|
3
|
+
ExpressionSpecification,
|
|
4
|
+
} from "maplibre-gl";
|
|
5
|
+
import { generateRandomString } from "../tools";
|
|
6
|
+
import { ColorRamp, RgbaColor } from "../colorramp";
|
|
7
|
+
import {
|
|
8
|
+
DataDrivenStyle,
|
|
9
|
+
PropertyValues,
|
|
10
|
+
ZoomNumberValues,
|
|
11
|
+
ZoomStringValues,
|
|
12
|
+
} from "./vectorlayerhelpers";
|
|
13
|
+
|
|
14
|
+
export type ColorPalette = [string, string, string, string];
|
|
15
|
+
|
|
16
|
+
export const colorPalettes: Array<ColorPalette> = [
|
|
17
|
+
// https://colorhunt.co/palette/1d5b79468b97ef6262f3aa60
|
|
18
|
+
["#1D5B79", "#468B97", "#EF6262", "#F3AA60"],
|
|
19
|
+
|
|
20
|
+
// https://colorhunt.co/palette/614bc333bbc585e6c5c8ffe0
|
|
21
|
+
["#614BC3", "#33BBC5", "#85E6C5", "#C8FFE0"],
|
|
22
|
+
|
|
23
|
+
// https://colorhunt.co/palette/4619597a316fcd6688aed8cc
|
|
24
|
+
["#461959", "#7A316F", "#CD6688", "#AED8CC"],
|
|
25
|
+
|
|
26
|
+
// https://colorhunt.co/palette/0079ff00dfa2f6fa70ff0060
|
|
27
|
+
["#0079FF", "#00DFA2", "#F6FA70", "#FF0060"],
|
|
28
|
+
|
|
29
|
+
//https://colorhunt.co/palette/39b5e0a31acbff78f0f5ea5a
|
|
30
|
+
["#39B5E0", "#A31ACB", "#FF78F0", "#F5EA5A"],
|
|
31
|
+
|
|
32
|
+
// https://colorhunt.co/palette/37e2d5590696c70a80fbcb0a
|
|
33
|
+
["#37E2D5", "#590696", "#C70A80", "#FBCB0A"],
|
|
34
|
+
|
|
35
|
+
// https://colorhunt.co/palette/ffd36efff56d99ffcd9fb4ff
|
|
36
|
+
["#FFD36E", "#FFF56D", "#99FFCD", "#9FB4FF"],
|
|
37
|
+
|
|
38
|
+
// https://colorhunt.co/palette/00ead3fff5b7ff449f005f99
|
|
39
|
+
["#00EAD3", "#FFF5B7", "#FF449F", "#005F99"],
|
|
40
|
+
|
|
41
|
+
// https://colorhunt.co/palette/10a19d540375ff7000ffbf00
|
|
42
|
+
["#10A19D", "#540375", "#FF7000", "#FFBF00"],
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
export function getRandomColor(): string {
|
|
46
|
+
return colorPalettes[~~(Math.random() * colorPalettes.length)][
|
|
47
|
+
~~(Math.random() * 4)
|
|
48
|
+
];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function generateRandomSourceName(): string {
|
|
52
|
+
return `maptiler_source_${generateRandomString()}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function generateRandomLayerName(): string {
|
|
56
|
+
return `maptiler_layer_${generateRandomString()}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Linera interpolation to find a value at an arbitrary zoom level, given a list of tuple zoom-value
|
|
61
|
+
*/
|
|
62
|
+
export function lerpZoomNumberValues(znv: ZoomNumberValues, z: number): number {
|
|
63
|
+
// before the range
|
|
64
|
+
if (z <= znv[0].zoom) {
|
|
65
|
+
return znv[0].value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// after the range
|
|
69
|
+
if (z >= znv[znv.length - 1].zoom) {
|
|
70
|
+
return znv[znv.length - 1].value;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// somewhere within the range
|
|
74
|
+
for (let i = 0; i < znv.length - 1; i += 1) {
|
|
75
|
+
if (z >= znv[i].zoom && z < znv[i + 1].zoom) {
|
|
76
|
+
const zoomRange = znv[i + 1].zoom - znv[i].zoom;
|
|
77
|
+
const normalizedDistanceFromLowerBound = (z - znv[i].zoom) / zoomRange;
|
|
78
|
+
return (
|
|
79
|
+
normalizedDistanceFromLowerBound * znv[i + 1].value +
|
|
80
|
+
(1 - normalizedDistanceFromLowerBound) * znv[i].value
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function paintColorOptionsToPaintSpec(
|
|
89
|
+
color: ZoomStringValues,
|
|
90
|
+
): DataDrivenPropertyValueSpecification<string> {
|
|
91
|
+
return [
|
|
92
|
+
"interpolate",
|
|
93
|
+
["linear"],
|
|
94
|
+
["zoom"],
|
|
95
|
+
...color.map((el) => [el.zoom, el.value]).flat(),
|
|
96
|
+
];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function rampedOptionsToLayerPaintSpec(
|
|
100
|
+
ramp: ZoomNumberValues,
|
|
101
|
+
): DataDrivenPropertyValueSpecification<number> {
|
|
102
|
+
return [
|
|
103
|
+
"interpolate",
|
|
104
|
+
["linear"],
|
|
105
|
+
["zoom"],
|
|
106
|
+
...ramp.map((el) => [el.zoom, el.value]).flat(),
|
|
107
|
+
];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function computeRampedOutlineWidth(
|
|
111
|
+
lineWidth: number | ZoomNumberValues,
|
|
112
|
+
outlineWidth: number | ZoomNumberValues,
|
|
113
|
+
): number | DataDrivenPropertyValueSpecification<number> {
|
|
114
|
+
// case 1: the line is fixed-width and the outline is fixed-width
|
|
115
|
+
if (typeof outlineWidth === "number" && typeof lineWidth === "number") {
|
|
116
|
+
return 2 * outlineWidth + lineWidth;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// case 2: the line is ramped-width, the outline is fixed-width
|
|
120
|
+
else if (typeof outlineWidth === "number" && Array.isArray(lineWidth)) {
|
|
121
|
+
return [
|
|
122
|
+
"interpolate",
|
|
123
|
+
["linear"],
|
|
124
|
+
["zoom"],
|
|
125
|
+
...lineWidth.map((el) => [el.zoom, 2 * outlineWidth + el.value]).flat(),
|
|
126
|
+
];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// case 3: the line is fixed-width, the outline is ramped-width
|
|
130
|
+
else if (typeof lineWidth === "number" && Array.isArray(outlineWidth)) {
|
|
131
|
+
return [
|
|
132
|
+
"interpolate",
|
|
133
|
+
["linear"],
|
|
134
|
+
["zoom"],
|
|
135
|
+
...outlineWidth.map((el) => [el.zoom, 2 * el.value + lineWidth]).flat(),
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// case 4: the line is ramped-width, the outline is ramped-width
|
|
140
|
+
if (Array.isArray(lineWidth) && Array.isArray(outlineWidth)) {
|
|
141
|
+
// We must create an artificial set of zoom stops that includes all the zoom stops from both lists
|
|
142
|
+
// const allStops = [...lineWidth.map(el => el.zoom), ...outlineWidth.map(el => el.zoom)].sort((a: number, b: number) => a < b ? -1 : 1);
|
|
143
|
+
const allStops = Array.from(
|
|
144
|
+
new Set([
|
|
145
|
+
...lineWidth.map((el) => el.zoom),
|
|
146
|
+
...outlineWidth.map((el) => el.zoom),
|
|
147
|
+
]),
|
|
148
|
+
).sort((a: number, b: number) => (a < b ? -1 : 1));
|
|
149
|
+
|
|
150
|
+
return [
|
|
151
|
+
"interpolate",
|
|
152
|
+
["linear"],
|
|
153
|
+
["zoom"],
|
|
154
|
+
...allStops
|
|
155
|
+
.map((z) => [
|
|
156
|
+
z,
|
|
157
|
+
2 * lerpZoomNumberValues(outlineWidth, z) +
|
|
158
|
+
lerpZoomNumberValues(lineWidth, z),
|
|
159
|
+
])
|
|
160
|
+
.flat(),
|
|
161
|
+
];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return 0;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function rampedPropertyValueWeight(
|
|
168
|
+
ramp: PropertyValues,
|
|
169
|
+
property: string,
|
|
170
|
+
): DataDrivenPropertyValueSpecification<number> {
|
|
171
|
+
return [
|
|
172
|
+
"interpolate",
|
|
173
|
+
["linear"],
|
|
174
|
+
["get", property],
|
|
175
|
+
...ramp.map((el) => [el.propertyValue, el.value]).flat(),
|
|
176
|
+
];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Create a dash array from a string pattern that uses underscore and whitespace characters
|
|
181
|
+
*/
|
|
182
|
+
export function dashArrayMaker(pattern: string): Array<number> {
|
|
183
|
+
// if the pattern starts with whitespaces, then move them towards the end
|
|
184
|
+
const startTrimmedPattern = pattern.trimStart();
|
|
185
|
+
const fixedPattern = `${startTrimmedPattern}${" ".repeat(
|
|
186
|
+
pattern.length - startTrimmedPattern.length,
|
|
187
|
+
)}`;
|
|
188
|
+
const patternArr = Array.from(fixedPattern);
|
|
189
|
+
|
|
190
|
+
const isOnlyDashesAndSpaces = patternArr.every((c) => c === " " || c === "_");
|
|
191
|
+
if (!isOnlyDashesAndSpaces) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
"A dash pattern must be composed only of whitespace and underscore characters.",
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const hasBothDashesAndWhitespaces =
|
|
198
|
+
patternArr.some((c) => c === "_") && patternArr.some((c) => c === " ");
|
|
199
|
+
if (!hasBothDashesAndWhitespaces) {
|
|
200
|
+
throw new Error(
|
|
201
|
+
"A dash pattern must contain at least one underscore and one whitespace character",
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const dashArray = [1];
|
|
206
|
+
|
|
207
|
+
for (let i = 1; i < patternArr.length; i += 1) {
|
|
208
|
+
const previous = patternArr[i - 1];
|
|
209
|
+
const current = patternArr[i];
|
|
210
|
+
|
|
211
|
+
if (previous === current) {
|
|
212
|
+
dashArray[dashArray.length - 1] += 1;
|
|
213
|
+
} else {
|
|
214
|
+
dashArray.push(1);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return dashArray;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function colorDrivenByProperty(
|
|
222
|
+
style: DataDrivenStyle,
|
|
223
|
+
property: string,
|
|
224
|
+
): DataDrivenPropertyValueSpecification<string> {
|
|
225
|
+
return [
|
|
226
|
+
"interpolate",
|
|
227
|
+
["linear"],
|
|
228
|
+
["get", property],
|
|
229
|
+
...style.map((el) => [el.value, el.color]).flat(),
|
|
230
|
+
];
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function radiusDrivenByProperty(
|
|
234
|
+
style: DataDrivenStyle,
|
|
235
|
+
property: string,
|
|
236
|
+
zoomCompensation = true,
|
|
237
|
+
): DataDrivenPropertyValueSpecification<number> {
|
|
238
|
+
if (!zoomCompensation) {
|
|
239
|
+
return [
|
|
240
|
+
"interpolate",
|
|
241
|
+
["linear"],
|
|
242
|
+
["get", property],
|
|
243
|
+
...style.map((el) => [el.value, el.pointRadius]).flat(),
|
|
244
|
+
];
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return [
|
|
248
|
+
"interpolate",
|
|
249
|
+
["linear"],
|
|
250
|
+
["zoom"],
|
|
251
|
+
|
|
252
|
+
0,
|
|
253
|
+
[
|
|
254
|
+
"interpolate",
|
|
255
|
+
["linear"],
|
|
256
|
+
["get", property],
|
|
257
|
+
...style.map((el) => [el.value, el.pointRadius * 0.025]).flat(),
|
|
258
|
+
],
|
|
259
|
+
|
|
260
|
+
2,
|
|
261
|
+
[
|
|
262
|
+
"interpolate",
|
|
263
|
+
["linear"],
|
|
264
|
+
["get", property],
|
|
265
|
+
...style.map((el) => [el.value, el.pointRadius * 0.05]).flat(),
|
|
266
|
+
],
|
|
267
|
+
|
|
268
|
+
4,
|
|
269
|
+
[
|
|
270
|
+
"interpolate",
|
|
271
|
+
["linear"],
|
|
272
|
+
["get", property],
|
|
273
|
+
...style.map((el) => [el.value, el.pointRadius * 0.1]).flat(),
|
|
274
|
+
],
|
|
275
|
+
|
|
276
|
+
8,
|
|
277
|
+
[
|
|
278
|
+
"interpolate",
|
|
279
|
+
["linear"],
|
|
280
|
+
["get", property],
|
|
281
|
+
...style.map((el) => [el.value, el.pointRadius * 0.25]).flat(),
|
|
282
|
+
],
|
|
283
|
+
|
|
284
|
+
16,
|
|
285
|
+
[
|
|
286
|
+
"interpolate",
|
|
287
|
+
["linear"],
|
|
288
|
+
["get", property],
|
|
289
|
+
...style.map((el) => [el.value, el.pointRadius]).flat(),
|
|
290
|
+
],
|
|
291
|
+
];
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function radiusDrivenByPropertyHeatmap(
|
|
295
|
+
style: PropertyValues,
|
|
296
|
+
property: string,
|
|
297
|
+
zoomCompensation = true,
|
|
298
|
+
): DataDrivenPropertyValueSpecification<number> {
|
|
299
|
+
if (!zoomCompensation) {
|
|
300
|
+
return [
|
|
301
|
+
"interpolate",
|
|
302
|
+
["linear"],
|
|
303
|
+
["get", property],
|
|
304
|
+
...style.map((el) => [el.propertyValue, el.value]).flat(),
|
|
305
|
+
];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return [
|
|
309
|
+
"interpolate",
|
|
310
|
+
["linear"],
|
|
311
|
+
["zoom"],
|
|
312
|
+
|
|
313
|
+
0,
|
|
314
|
+
[
|
|
315
|
+
"interpolate",
|
|
316
|
+
["linear"],
|
|
317
|
+
["get", property],
|
|
318
|
+
...style.map((el) => [el.propertyValue, el.value * 0.025]).flat(),
|
|
319
|
+
],
|
|
320
|
+
|
|
321
|
+
2,
|
|
322
|
+
[
|
|
323
|
+
"interpolate",
|
|
324
|
+
["linear"],
|
|
325
|
+
["get", property],
|
|
326
|
+
...style.map((el) => [el.propertyValue, el.value * 0.05]).flat(),
|
|
327
|
+
],
|
|
328
|
+
|
|
329
|
+
4,
|
|
330
|
+
[
|
|
331
|
+
"interpolate",
|
|
332
|
+
["linear"],
|
|
333
|
+
["get", property],
|
|
334
|
+
...style.map((el) => [el.propertyValue, el.value * 0.1]).flat(),
|
|
335
|
+
],
|
|
336
|
+
|
|
337
|
+
8,
|
|
338
|
+
[
|
|
339
|
+
"interpolate",
|
|
340
|
+
["linear"],
|
|
341
|
+
["get", property],
|
|
342
|
+
...style.map((el) => [el.propertyValue, el.value * 0.25]).flat(),
|
|
343
|
+
],
|
|
344
|
+
|
|
345
|
+
16,
|
|
346
|
+
[
|
|
347
|
+
"interpolate",
|
|
348
|
+
["linear"],
|
|
349
|
+
["get", property],
|
|
350
|
+
...style.map((el) => [el.propertyValue, el.value]).flat(),
|
|
351
|
+
],
|
|
352
|
+
];
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Turns a ColorRamp instance into a MapLibre style for ramping the opacity, driven by a property
|
|
357
|
+
*/
|
|
358
|
+
export function opacityDrivenByProperty(
|
|
359
|
+
colorramp: ColorRamp,
|
|
360
|
+
property: string,
|
|
361
|
+
): DataDrivenPropertyValueSpecification<number> {
|
|
362
|
+
// If all opacities are the same, just return the number without any ramping logic
|
|
363
|
+
if (colorramp.every((el) => el.color[3] === colorramp[0].color[3])) {
|
|
364
|
+
return colorramp[0].color[3] ? colorramp[0].color[3] / 255 : 1;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return [
|
|
368
|
+
"interpolate",
|
|
369
|
+
["linear"],
|
|
370
|
+
["get", property],
|
|
371
|
+
...colorramp
|
|
372
|
+
.getRawColorStops()
|
|
373
|
+
.map((el) => {
|
|
374
|
+
const value = el.value;
|
|
375
|
+
const color: RgbaColor = el.color;
|
|
376
|
+
return [value, color.length === 4 ? color[3] / 255 : 1];
|
|
377
|
+
})
|
|
378
|
+
.flat(),
|
|
379
|
+
];
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export function heatmapIntensityFromColorRamp(
|
|
383
|
+
colorRamp: ColorRamp,
|
|
384
|
+
steps = 10,
|
|
385
|
+
): ExpressionSpecification {
|
|
386
|
+
return [
|
|
387
|
+
"interpolate",
|
|
388
|
+
["linear"],
|
|
389
|
+
["heatmap-density"],
|
|
390
|
+
...Array.from({ length: steps + 1 }, (_, i) => {
|
|
391
|
+
const unitStep = i / steps;
|
|
392
|
+
return [unitStep, colorRamp.getColorHex(unitStep)];
|
|
393
|
+
}).flat(),
|
|
394
|
+
];
|
|
395
|
+
}
|