@maptiler/client 1.7.0 → 1.8.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/.github/pull_request_template.md +11 -0
- package/.github/workflows/format-lint.yml +24 -0
- package/CHANGELOG.md +32 -0
- package/dist/maptiler-client.cjs +680 -306
- package/dist/maptiler-client.cjs.map +1 -1
- package/dist/maptiler-client.d.ts +270 -100
- package/dist/maptiler-client.min.mjs +1 -1
- package/dist/maptiler-client.mjs +380 -294
- package/dist/maptiler-client.mjs.map +1 -1
- package/package.json +10 -8
- package/readme.md +133 -1
- package/rollup.config.js +3 -3
- package/dist/maptiler-client.umd.js +0 -1541
- package/dist/maptiler-client.umd.js.map +0 -1
- package/dist/maptiler-client.umd.min.js +0 -1
- package/examples/test-browser-es.html +0 -103
- package/examples/test-browser-umd.html +0 -90
- package/examples/test-node.js +0 -52
- package/src/callFetch.ts +0 -18
- package/src/config.ts +0 -68
- package/src/defaults.ts +0 -11
- package/src/demProfiler/getElevation.ts +0 -46
- package/src/demProfiler/index.ts +0 -240
- package/src/demProfiler/tileCover.ts +0 -96
- package/src/geometry/area.ts +0 -96
- package/src/geometry/distance.ts +0 -45
- package/src/geometry/index.ts +0 -3
- package/src/geometry/util.ts +0 -34
- package/src/index.ts +0 -114
- package/src/language.ts +0 -87
- package/src/mapstyle.ts +0 -968
- package/src/services/ServiceError.ts +0 -13
- package/src/services/coordinates.ts +0 -263
- package/src/services/data.ts +0 -63
- package/src/services/geocoding.ts +0 -461
- package/src/services/geolocation.ts +0 -139
- package/src/services/simplify.ts +0 -87
- package/src/services/staticMaps.ts +0 -463
package/dist/maptiler-client.cjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var webMercProjection = require('web-merc-projection');
|
|
4
|
-
|
|
5
3
|
function tryGettingFetch() {
|
|
6
4
|
if (typeof self !== "undefined") {
|
|
7
5
|
return fetch.bind(self);
|
|
@@ -22,6 +20,10 @@ class ClientConfig {
|
|
|
22
20
|
* will be automatically resolved.
|
|
23
21
|
*/
|
|
24
22
|
this._fetch = tryGettingFetch();
|
|
23
|
+
/**
|
|
24
|
+
* Number of tiles to keep in cache
|
|
25
|
+
*/
|
|
26
|
+
this.tileCacheSize = 200;
|
|
25
27
|
}
|
|
26
28
|
/**
|
|
27
29
|
* Set the MapTiler Cloud API key
|
|
@@ -147,17 +149,18 @@ class ServiceError extends Error {
|
|
|
147
149
|
}
|
|
148
150
|
}
|
|
149
151
|
|
|
150
|
-
const customMessages$
|
|
152
|
+
const customMessages$4 = {
|
|
151
153
|
400: "Query too long / Invalid parameters",
|
|
152
154
|
403: "Key is missing, invalid or restricted"
|
|
153
155
|
};
|
|
154
156
|
function addLanguageGeocodingOptions(searchParams, options) {
|
|
155
|
-
|
|
157
|
+
const { language } = options;
|
|
158
|
+
if (language == void 0) {
|
|
156
159
|
return;
|
|
157
160
|
}
|
|
158
161
|
const languages = Array.from(
|
|
159
162
|
new Set(
|
|
160
|
-
(Array.isArray(
|
|
163
|
+
(Array.isArray(language) ? language : [language]).map(
|
|
161
164
|
(lang) => lang === LanguageGeocoding.AUTO ? getAutoLanguageGeocoding() : lang
|
|
162
165
|
)
|
|
163
166
|
)
|
|
@@ -165,31 +168,39 @@ function addLanguageGeocodingOptions(searchParams, options) {
|
|
|
165
168
|
searchParams.set("language", languages);
|
|
166
169
|
}
|
|
167
170
|
function addCommonForwardAndReverseGeocodingOptions(searchParams, options) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
+
const { apiKey, limit, types, excludeTypes } = options;
|
|
172
|
+
searchParams.set("key", apiKey ?? config.apiKey);
|
|
173
|
+
if (limit != void 0) {
|
|
174
|
+
searchParams.set("limit", String(limit));
|
|
175
|
+
}
|
|
176
|
+
if (types != void 0) {
|
|
177
|
+
searchParams.set("types", types.join(","));
|
|
171
178
|
}
|
|
172
|
-
if (
|
|
173
|
-
searchParams.set("
|
|
179
|
+
if (excludeTypes != void 0) {
|
|
180
|
+
searchParams.set("excludeTypes", String(excludeTypes));
|
|
174
181
|
}
|
|
175
182
|
addLanguageGeocodingOptions(searchParams, options);
|
|
176
183
|
}
|
|
177
184
|
function addForwardGeocodingOptions(searchParams, options) {
|
|
178
185
|
addCommonForwardAndReverseGeocodingOptions(searchParams, options);
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
186
|
+
const { bbox, proximity, country, fuzzyMatch, autocomplete } = options;
|
|
187
|
+
if (bbox != void 0) {
|
|
188
|
+
searchParams.set("bbox", bbox.join(","));
|
|
189
|
+
}
|
|
190
|
+
if (proximity != void 0) {
|
|
191
|
+
searchParams.set(
|
|
192
|
+
"proximity",
|
|
193
|
+
proximity === "ip" ? proximity : proximity.join(",")
|
|
194
|
+
);
|
|
184
195
|
}
|
|
185
|
-
if (
|
|
186
|
-
searchParams.set("country",
|
|
196
|
+
if (country != void 0) {
|
|
197
|
+
searchParams.set("country", country.join(","));
|
|
187
198
|
}
|
|
188
|
-
if (
|
|
189
|
-
searchParams.set("fuzzyMatch",
|
|
199
|
+
if (fuzzyMatch != void 0) {
|
|
200
|
+
searchParams.set("fuzzyMatch", fuzzyMatch ? "true" : "false");
|
|
190
201
|
}
|
|
191
|
-
if (
|
|
192
|
-
searchParams.set("autocomplete",
|
|
202
|
+
if (autocomplete != void 0) {
|
|
203
|
+
searchParams.set("autocomplete", autocomplete ? "true" : "false");
|
|
193
204
|
}
|
|
194
205
|
}
|
|
195
206
|
async function forward(query, options = {}) {
|
|
@@ -205,7 +216,7 @@ async function forward(query, options = {}) {
|
|
|
205
216
|
const urlWithParams = endpoint.toString();
|
|
206
217
|
const res = await callFetch(urlWithParams);
|
|
207
218
|
if (!res.ok) {
|
|
208
|
-
throw new ServiceError(res, customMessages$
|
|
219
|
+
throw new ServiceError(res, customMessages$4[res.status] ?? "");
|
|
209
220
|
}
|
|
210
221
|
const obj = await res.json();
|
|
211
222
|
return obj;
|
|
@@ -222,7 +233,7 @@ async function reverse(position, options = {}) {
|
|
|
222
233
|
const urlWithParams = endpoint.toString();
|
|
223
234
|
const res = await callFetch(urlWithParams);
|
|
224
235
|
if (!res.ok) {
|
|
225
|
-
throw new ServiceError(res, customMessages$
|
|
236
|
+
throw new ServiceError(res, customMessages$4[res.status] ?? "");
|
|
226
237
|
}
|
|
227
238
|
const obj = await res.json();
|
|
228
239
|
return obj;
|
|
@@ -234,12 +245,12 @@ async function byId(id, options = {}) {
|
|
|
234
245
|
const urlWithParams = endpoint.toString();
|
|
235
246
|
const res = await callFetch(urlWithParams);
|
|
236
247
|
if (!res.ok) {
|
|
237
|
-
throw new ServiceError(res, customMessages$
|
|
248
|
+
throw new ServiceError(res, customMessages$4[res.status] ?? "");
|
|
238
249
|
}
|
|
239
250
|
const obj = await res.json();
|
|
240
251
|
return obj;
|
|
241
252
|
}
|
|
242
|
-
async function batch(queries, options = {}) {
|
|
253
|
+
async function batch$1(queries, options = {}) {
|
|
243
254
|
if (!queries.length) {
|
|
244
255
|
return [];
|
|
245
256
|
}
|
|
@@ -253,7 +264,7 @@ async function batch(queries, options = {}) {
|
|
|
253
264
|
const urlWithParams = endpoint.toString();
|
|
254
265
|
const res = await callFetch(urlWithParams);
|
|
255
266
|
if (!res.ok) {
|
|
256
|
-
throw new ServiceError(res, customMessages$
|
|
267
|
+
throw new ServiceError(res, customMessages$4[res.status] ?? "");
|
|
257
268
|
}
|
|
258
269
|
const obj = await res.json();
|
|
259
270
|
return queries.length === 1 ? [obj] : obj;
|
|
@@ -262,11 +273,11 @@ const geocoding = {
|
|
|
262
273
|
forward,
|
|
263
274
|
reverse,
|
|
264
275
|
byId,
|
|
265
|
-
batch,
|
|
276
|
+
batch: batch$1,
|
|
266
277
|
language: LanguageGeocoding
|
|
267
278
|
};
|
|
268
279
|
|
|
269
|
-
const customMessages$
|
|
280
|
+
const customMessages$3 = {
|
|
270
281
|
403: "Key is missing, invalid or restricted"
|
|
271
282
|
};
|
|
272
283
|
async function info(options = {}) {
|
|
@@ -277,7 +288,7 @@ async function info(options = {}) {
|
|
|
277
288
|
if (!res.ok) {
|
|
278
289
|
throw new ServiceError(
|
|
279
290
|
res,
|
|
280
|
-
res.status in customMessages$
|
|
291
|
+
res.status in customMessages$3 ? customMessages$3[res.status] : ""
|
|
281
292
|
);
|
|
282
293
|
}
|
|
283
294
|
const obj = await res.json();
|
|
@@ -287,7 +298,7 @@ const geolocation = {
|
|
|
287
298
|
info
|
|
288
299
|
};
|
|
289
300
|
|
|
290
|
-
const customMessages$
|
|
301
|
+
const customMessages$2 = {
|
|
291
302
|
403: "Key is missing, invalid or restricted"
|
|
292
303
|
};
|
|
293
304
|
async function search(query, options = {}) {
|
|
@@ -316,7 +327,7 @@ async function search(query, options = {}) {
|
|
|
316
327
|
if (!res.ok) {
|
|
317
328
|
throw new ServiceError(
|
|
318
329
|
res,
|
|
319
|
-
res.status in customMessages$
|
|
330
|
+
res.status in customMessages$2 ? customMessages$2[res.status] : ""
|
|
320
331
|
);
|
|
321
332
|
}
|
|
322
333
|
const obj = await res.json();
|
|
@@ -346,7 +357,7 @@ async function transform(positions, options = {}) {
|
|
|
346
357
|
if (!res.ok) {
|
|
347
358
|
throw new ServiceError(
|
|
348
359
|
res,
|
|
349
|
-
res.status in customMessages$
|
|
360
|
+
res.status in customMessages$2 ? customMessages$2[res.status] : ""
|
|
350
361
|
);
|
|
351
362
|
}
|
|
352
363
|
const obj = await res.json();
|
|
@@ -357,7 +368,7 @@ const coordinates = {
|
|
|
357
368
|
transform
|
|
358
369
|
};
|
|
359
370
|
|
|
360
|
-
const customMessages = {
|
|
371
|
+
const customMessages$1 = {
|
|
361
372
|
403: "Key is missing, invalid or restricted"
|
|
362
373
|
};
|
|
363
374
|
async function get(dataId, options = {}) {
|
|
@@ -374,7 +385,7 @@ async function get(dataId, options = {}) {
|
|
|
374
385
|
if (!res.ok) {
|
|
375
386
|
throw new ServiceError(
|
|
376
387
|
res,
|
|
377
|
-
res.status in customMessages ? customMessages[res.status] : ""
|
|
388
|
+
res.status in customMessages$1 ? customMessages$1[res.status] : ""
|
|
378
389
|
);
|
|
379
390
|
}
|
|
380
391
|
const obj = await res.json();
|
|
@@ -981,6 +992,36 @@ function styleToStyle(style) {
|
|
|
981
992
|
}
|
|
982
993
|
const MapStyle = buildMapStyles();
|
|
983
994
|
|
|
995
|
+
function extractLineStrings(geoJson) {
|
|
996
|
+
const lineStrings = [];
|
|
997
|
+
function extractFromGeometry(geometry) {
|
|
998
|
+
if (geometry.type === "LineString" || geometry.type === "MultiLineString") {
|
|
999
|
+
lineStrings.push(geometry);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
function extractFromFeature(feature) {
|
|
1003
|
+
if (feature.geometry) {
|
|
1004
|
+
extractFromGeometry(feature.geometry);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
function extractFromFeatureCollection(collection) {
|
|
1008
|
+
for (const feature of collection.features) {
|
|
1009
|
+
if (feature.type === "Feature") {
|
|
1010
|
+
extractFromFeature(feature);
|
|
1011
|
+
} else if (feature.type === "FeatureCollection") {
|
|
1012
|
+
extractFromFeatureCollection(feature);
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
if (geoJson.type === "Feature") {
|
|
1017
|
+
extractFromFeature(geoJson);
|
|
1018
|
+
} else if (geoJson.type === "FeatureCollection") {
|
|
1019
|
+
extractFromFeatureCollection(geoJson);
|
|
1020
|
+
} else {
|
|
1021
|
+
extractFromGeometry(geoJson);
|
|
1022
|
+
}
|
|
1023
|
+
return lineStrings;
|
|
1024
|
+
}
|
|
984
1025
|
function getSqSegDist(p, p1, p2) {
|
|
985
1026
|
let x = p1[0], y = p1[1], dx = p2[0] - x, dy = p2[1] - y;
|
|
986
1027
|
if (dx !== 0 || dy !== 0) {
|
|
@@ -1031,6 +1072,10 @@ function simplify(points, tolerance) {
|
|
|
1031
1072
|
const simplePoints = simplifyDouglasPeucker(points, sqTolerance);
|
|
1032
1073
|
return simplePoints;
|
|
1033
1074
|
}
|
|
1075
|
+
const misc = {
|
|
1076
|
+
extractLineStrings,
|
|
1077
|
+
simplify
|
|
1078
|
+
};
|
|
1034
1079
|
|
|
1035
1080
|
function staticMapMarkerToString(marker, includeColor = true) {
|
|
1036
1081
|
let str = `${marker[0]},${marker[1]}`;
|
|
@@ -1044,7 +1089,7 @@ function simplifyAndStringify(path, maxNbChar = 3e3) {
|
|
|
1044
1089
|
let tolerance = 5e-6;
|
|
1045
1090
|
const toleranceStep = 1e-5;
|
|
1046
1091
|
while (str.length > maxNbChar) {
|
|
1047
|
-
const simplerPath = simplify(path, tolerance);
|
|
1092
|
+
const simplerPath = misc.simplify(path, tolerance);
|
|
1048
1093
|
str = simplerPath.map((point) => `${point[0]},${point[1]}`).join("|");
|
|
1049
1094
|
tolerance += toleranceStep;
|
|
1050
1095
|
}
|
|
@@ -1216,294 +1261,627 @@ const staticMaps = {
|
|
|
1216
1261
|
automatic
|
|
1217
1262
|
};
|
|
1218
1263
|
|
|
1219
|
-
const
|
|
1220
|
-
|
|
1221
|
-
|
|
1264
|
+
const EARTH_RADIUS = 63710088e-1;
|
|
1265
|
+
const EARTH_CIRCUMFERENCE = 2 * Math.PI * EARTH_RADIUS;
|
|
1266
|
+
function longitudeToMercatorX(lng) {
|
|
1267
|
+
return (180 + lng) / 360;
|
|
1268
|
+
}
|
|
1269
|
+
function latitudeToMercatorY(lat) {
|
|
1270
|
+
return (180 - 180 / Math.PI * Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360))) / 360;
|
|
1271
|
+
}
|
|
1272
|
+
function wgs84ToMercator(position) {
|
|
1273
|
+
const wrappedPos = wrapWgs84(position);
|
|
1274
|
+
return [
|
|
1275
|
+
longitudeToMercatorX(wrappedPos[0]),
|
|
1276
|
+
latitudeToMercatorY(wrappedPos[1])
|
|
1277
|
+
];
|
|
1278
|
+
}
|
|
1279
|
+
function mercatorXToLongitude(x) {
|
|
1280
|
+
return x * 360 - 180;
|
|
1281
|
+
}
|
|
1282
|
+
function mercatorYToLatitude(y) {
|
|
1283
|
+
const y2 = 180 - y * 360;
|
|
1284
|
+
return 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90;
|
|
1285
|
+
}
|
|
1286
|
+
function mercatorToWgs84(position) {
|
|
1287
|
+
return [mercatorXToLongitude(position[0]), mercatorYToLatitude(position[1])];
|
|
1222
1288
|
}
|
|
1223
|
-
function
|
|
1224
|
-
|
|
1289
|
+
function haversineDistanceWgs84(from, to) {
|
|
1290
|
+
const rad = Math.PI / 180;
|
|
1291
|
+
const lat1 = from[1] * rad;
|
|
1292
|
+
const lat2 = to[1] * rad;
|
|
1293
|
+
const a = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos((to[0] - from[0]) * rad);
|
|
1294
|
+
const maxMeters = EARTH_RADIUS * Math.acos(Math.min(a, 1));
|
|
1295
|
+
return maxMeters;
|
|
1225
1296
|
}
|
|
1226
|
-
function
|
|
1297
|
+
function haversineCumulatedDistanceWgs84(positions) {
|
|
1298
|
+
const cumulatedDistance = Array(positions.length);
|
|
1299
|
+
cumulatedDistance[0] = 0;
|
|
1300
|
+
const l = cumulatedDistance.length;
|
|
1301
|
+
for (let i = 1; i < l; i++) {
|
|
1302
|
+
cumulatedDistance[i] = haversineDistanceWgs84(positions[i - 1], positions[i]) + cumulatedDistance[i - 1];
|
|
1303
|
+
}
|
|
1304
|
+
return cumulatedDistance;
|
|
1305
|
+
}
|
|
1306
|
+
function wrapWgs84(position) {
|
|
1307
|
+
const lng = position[0];
|
|
1308
|
+
const lat = position[1];
|
|
1309
|
+
const d = 360;
|
|
1310
|
+
const w = ((lng + 180) % d + d) % d - 180;
|
|
1311
|
+
const wrapLong = w === -180 ? 180 : w;
|
|
1312
|
+
return [wrapLong, lat];
|
|
1313
|
+
}
|
|
1314
|
+
function circumferenceAtLatitude(latitude) {
|
|
1315
|
+
return EARTH_CIRCUMFERENCE * Math.cos(latitude * Math.PI / 180);
|
|
1316
|
+
}
|
|
1317
|
+
function mercatorToTileIndex(position, zoom, strict = true) {
|
|
1318
|
+
const numberOfTilePerAxis = 2 ** zoom;
|
|
1319
|
+
const fIndex = [
|
|
1320
|
+
position[0] * numberOfTilePerAxis,
|
|
1321
|
+
position[1] * numberOfTilePerAxis
|
|
1322
|
+
];
|
|
1323
|
+
return strict ? [~~fIndex[0], ~~fIndex[1]] : fIndex;
|
|
1324
|
+
}
|
|
1325
|
+
function wgs84ToTileIndex(position, zoom, strict = true) {
|
|
1326
|
+
const merc = wgs84ToMercator(position);
|
|
1327
|
+
return mercatorToTileIndex(merc, zoom, strict);
|
|
1328
|
+
}
|
|
1329
|
+
function toRadians(degrees) {
|
|
1330
|
+
return degrees * Math.PI / 180;
|
|
1331
|
+
}
|
|
1332
|
+
function toDegrees(radians) {
|
|
1227
1333
|
return radians * 180 / Math.PI;
|
|
1228
1334
|
}
|
|
1229
|
-
function
|
|
1230
|
-
|
|
1335
|
+
function haversineIntermediateWgs84(pos1, pos2, ratio) {
|
|
1336
|
+
const d = haversineDistanceWgs84(pos1, pos2);
|
|
1337
|
+
const \u03BB1 = toRadians(pos1[0]);
|
|
1338
|
+
const \u03C61 = toRadians(pos1[1]);
|
|
1339
|
+
const \u03BB2 = toRadians(pos2[0]);
|
|
1340
|
+
const \u03C62 = toRadians(pos2[1]);
|
|
1341
|
+
const \u03B4 = d / EARTH_RADIUS;
|
|
1342
|
+
const a = Math.sin((1 - ratio) * \u03B4) / Math.sin(\u03B4);
|
|
1343
|
+
const b = Math.sin(ratio * \u03B4) / Math.sin(\u03B4);
|
|
1344
|
+
const x = a * Math.cos(\u03C61) * Math.cos(\u03BB1) + b * Math.cos(\u03C62) * Math.cos(\u03BB2);
|
|
1345
|
+
const y = a * Math.cos(\u03C61) * Math.sin(\u03BB1) + b * Math.cos(\u03C62) * Math.sin(\u03BB2);
|
|
1346
|
+
const z = a * Math.sin(\u03C61) + b * Math.sin(\u03C62);
|
|
1347
|
+
const \u03C63 = Math.atan2(z, Math.sqrt(x * x + y * y));
|
|
1348
|
+
const \u03BB3 = Math.atan2(y, x);
|
|
1349
|
+
return [toDegrees(\u03BB3), toDegrees(\u03C63)];
|
|
1231
1350
|
}
|
|
1232
|
-
|
|
1233
|
-
|
|
1351
|
+
const math = {
|
|
1352
|
+
EARTH_RADIUS,
|
|
1353
|
+
EARTH_CIRCUMFERENCE,
|
|
1354
|
+
longitudeToMercatorX,
|
|
1355
|
+
latitudeToMercatorY,
|
|
1356
|
+
wgs84ToMercator,
|
|
1357
|
+
mercatorXToLongitude,
|
|
1358
|
+
mercatorYToLatitude,
|
|
1359
|
+
mercatorToWgs84,
|
|
1360
|
+
haversineDistanceWgs84,
|
|
1361
|
+
wrapWgs84,
|
|
1362
|
+
circumferenceAtLatitude,
|
|
1363
|
+
mercatorToTileIndex,
|
|
1364
|
+
wgs84ToTileIndex,
|
|
1365
|
+
toRadians,
|
|
1366
|
+
toDegrees,
|
|
1367
|
+
haversineIntermediateWgs84,
|
|
1368
|
+
haversineCumulatedDistanceWgs84
|
|
1369
|
+
};
|
|
1370
|
+
|
|
1371
|
+
class QuickLRU extends Map {
|
|
1372
|
+
#size = 0;
|
|
1373
|
+
#cache = new Map();
|
|
1374
|
+
#oldCache = new Map();
|
|
1375
|
+
#maxSize;
|
|
1376
|
+
#maxAge;
|
|
1377
|
+
#onEviction;
|
|
1378
|
+
|
|
1379
|
+
constructor(options = {}) {
|
|
1380
|
+
super();
|
|
1381
|
+
|
|
1382
|
+
if (!(options.maxSize && options.maxSize > 0)) {
|
|
1383
|
+
throw new TypeError('`maxSize` must be a number greater than 0');
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
if (typeof options.maxAge === 'number' && options.maxAge === 0) {
|
|
1387
|
+
throw new TypeError('`maxAge` must be a number greater than 0');
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
this.#maxSize = options.maxSize;
|
|
1391
|
+
this.#maxAge = options.maxAge || Number.POSITIVE_INFINITY;
|
|
1392
|
+
this.#onEviction = options.onEviction;
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
// For tests.
|
|
1396
|
+
get __oldCache() {
|
|
1397
|
+
return this.#oldCache;
|
|
1398
|
+
}
|
|
1399
|
+
|
|
1400
|
+
#emitEvictions(cache) {
|
|
1401
|
+
if (typeof this.#onEviction !== 'function') {
|
|
1402
|
+
return;
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
for (const [key, item] of cache) {
|
|
1406
|
+
this.#onEviction(key, item.value);
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
#deleteIfExpired(key, item) {
|
|
1411
|
+
if (typeof item.expiry === 'number' && item.expiry <= Date.now()) {
|
|
1412
|
+
if (typeof this.#onEviction === 'function') {
|
|
1413
|
+
this.#onEviction(key, item.value);
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
return this.delete(key);
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
return false;
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
#getOrDeleteIfExpired(key, item) {
|
|
1423
|
+
const deleted = this.#deleteIfExpired(key, item);
|
|
1424
|
+
if (deleted === false) {
|
|
1425
|
+
return item.value;
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
#getItemValue(key, item) {
|
|
1430
|
+
return item.expiry ? this.#getOrDeleteIfExpired(key, item) : item.value;
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
#peek(key, cache) {
|
|
1434
|
+
const item = cache.get(key);
|
|
1435
|
+
|
|
1436
|
+
return this.#getItemValue(key, item);
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
#set(key, value) {
|
|
1440
|
+
this.#cache.set(key, value);
|
|
1441
|
+
this.#size++;
|
|
1442
|
+
|
|
1443
|
+
if (this.#size >= this.#maxSize) {
|
|
1444
|
+
this.#size = 0;
|
|
1445
|
+
this.#emitEvictions(this.#oldCache);
|
|
1446
|
+
this.#oldCache = this.#cache;
|
|
1447
|
+
this.#cache = new Map();
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
#moveToRecent(key, item) {
|
|
1452
|
+
this.#oldCache.delete(key);
|
|
1453
|
+
this.#set(key, item);
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
* #entriesAscending() {
|
|
1457
|
+
for (const item of this.#oldCache) {
|
|
1458
|
+
const [key, value] = item;
|
|
1459
|
+
if (!this.#cache.has(key)) {
|
|
1460
|
+
const deleted = this.#deleteIfExpired(key, value);
|
|
1461
|
+
if (deleted === false) {
|
|
1462
|
+
yield item;
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
for (const item of this.#cache) {
|
|
1468
|
+
const [key, value] = item;
|
|
1469
|
+
const deleted = this.#deleteIfExpired(key, value);
|
|
1470
|
+
if (deleted === false) {
|
|
1471
|
+
yield item;
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1476
|
+
get(key) {
|
|
1477
|
+
if (this.#cache.has(key)) {
|
|
1478
|
+
const item = this.#cache.get(key);
|
|
1479
|
+
return this.#getItemValue(key, item);
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
if (this.#oldCache.has(key)) {
|
|
1483
|
+
const item = this.#oldCache.get(key);
|
|
1484
|
+
if (this.#deleteIfExpired(key, item) === false) {
|
|
1485
|
+
this.#moveToRecent(key, item);
|
|
1486
|
+
return item.value;
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
|
|
1491
|
+
set(key, value, {maxAge = this.#maxAge} = {}) {
|
|
1492
|
+
const expiry = typeof maxAge === 'number' && maxAge !== Number.POSITIVE_INFINITY
|
|
1493
|
+
? (Date.now() + maxAge)
|
|
1494
|
+
: undefined;
|
|
1495
|
+
|
|
1496
|
+
if (this.#cache.has(key)) {
|
|
1497
|
+
this.#cache.set(key, {
|
|
1498
|
+
value,
|
|
1499
|
+
expiry,
|
|
1500
|
+
});
|
|
1501
|
+
} else {
|
|
1502
|
+
this.#set(key, {value, expiry});
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
return this;
|
|
1506
|
+
}
|
|
1507
|
+
|
|
1508
|
+
has(key) {
|
|
1509
|
+
if (this.#cache.has(key)) {
|
|
1510
|
+
return !this.#deleteIfExpired(key, this.#cache.get(key));
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
if (this.#oldCache.has(key)) {
|
|
1514
|
+
return !this.#deleteIfExpired(key, this.#oldCache.get(key));
|
|
1515
|
+
}
|
|
1516
|
+
|
|
1517
|
+
return false;
|
|
1518
|
+
}
|
|
1519
|
+
|
|
1520
|
+
peek(key) {
|
|
1521
|
+
if (this.#cache.has(key)) {
|
|
1522
|
+
return this.#peek(key, this.#cache);
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
if (this.#oldCache.has(key)) {
|
|
1526
|
+
return this.#peek(key, this.#oldCache);
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
delete(key) {
|
|
1531
|
+
const deleted = this.#cache.delete(key);
|
|
1532
|
+
if (deleted) {
|
|
1533
|
+
this.#size--;
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
return this.#oldCache.delete(key) || deleted;
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
clear() {
|
|
1540
|
+
this.#cache.clear();
|
|
1541
|
+
this.#oldCache.clear();
|
|
1542
|
+
this.#size = 0;
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
resize(newSize) {
|
|
1546
|
+
if (!(newSize && newSize > 0)) {
|
|
1547
|
+
throw new TypeError('`maxSize` must be a number greater than 0');
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
const items = [...this.#entriesAscending()];
|
|
1551
|
+
const removeCount = items.length - newSize;
|
|
1552
|
+
if (removeCount < 0) {
|
|
1553
|
+
this.#cache = new Map(items);
|
|
1554
|
+
this.#oldCache = new Map();
|
|
1555
|
+
this.#size = items.length;
|
|
1556
|
+
} else {
|
|
1557
|
+
if (removeCount > 0) {
|
|
1558
|
+
this.#emitEvictions(items.slice(0, removeCount));
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
this.#oldCache = new Map(items.slice(removeCount));
|
|
1562
|
+
this.#cache = new Map();
|
|
1563
|
+
this.#size = 0;
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
this.#maxSize = newSize;
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
* keys() {
|
|
1570
|
+
for (const [key] of this) {
|
|
1571
|
+
yield key;
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
|
|
1575
|
+
* values() {
|
|
1576
|
+
for (const [, value] of this) {
|
|
1577
|
+
yield value;
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
* [Symbol.iterator]() {
|
|
1582
|
+
for (const item of this.#cache) {
|
|
1583
|
+
const [key, value] = item;
|
|
1584
|
+
const deleted = this.#deleteIfExpired(key, value);
|
|
1585
|
+
if (deleted === false) {
|
|
1586
|
+
yield [key, value.value];
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
for (const item of this.#oldCache) {
|
|
1591
|
+
const [key, value] = item;
|
|
1592
|
+
if (!this.#cache.has(key)) {
|
|
1593
|
+
const deleted = this.#deleteIfExpired(key, value);
|
|
1594
|
+
if (deleted === false) {
|
|
1595
|
+
yield [key, value.value];
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
* entriesDescending() {
|
|
1602
|
+
let items = [...this.#cache];
|
|
1603
|
+
for (let i = items.length - 1; i >= 0; --i) {
|
|
1604
|
+
const item = items[i];
|
|
1605
|
+
const [key, value] = item;
|
|
1606
|
+
const deleted = this.#deleteIfExpired(key, value);
|
|
1607
|
+
if (deleted === false) {
|
|
1608
|
+
yield [key, value.value];
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
items = [...this.#oldCache];
|
|
1613
|
+
for (let i = items.length - 1; i >= 0; --i) {
|
|
1614
|
+
const item = items[i];
|
|
1615
|
+
const [key, value] = item;
|
|
1616
|
+
if (!this.#cache.has(key)) {
|
|
1617
|
+
const deleted = this.#deleteIfExpired(key, value);
|
|
1618
|
+
if (deleted === false) {
|
|
1619
|
+
yield [key, value.value];
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1625
|
+
* entriesAscending() {
|
|
1626
|
+
for (const [key, value] of this.#entriesAscending()) {
|
|
1627
|
+
yield [key, value.value];
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1631
|
+
get size() {
|
|
1632
|
+
if (!this.#size) {
|
|
1633
|
+
return this.#oldCache.size;
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
let oldCacheSize = 0;
|
|
1637
|
+
for (const key of this.#oldCache.keys()) {
|
|
1638
|
+
if (!this.#cache.has(key)) {
|
|
1639
|
+
oldCacheSize++;
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
return Math.min(this.#size + oldCacheSize, this.#maxSize);
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
get maxSize() {
|
|
1647
|
+
return this.#maxSize;
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
entries() {
|
|
1651
|
+
return this.entriesAscending();
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
forEach(callbackFunction, thisArgument = this) {
|
|
1655
|
+
for (const [key, value] of this.entriesAscending()) {
|
|
1656
|
+
callbackFunction.call(thisArgument, value, key, this);
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
get [Symbol.toStringTag]() {
|
|
1661
|
+
return JSON.stringify([...this.entriesAscending()]);
|
|
1662
|
+
}
|
|
1234
1663
|
}
|
|
1235
1664
|
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
return multiPolygonArea(geometry.coordinates);
|
|
1665
|
+
let tileCache = null;
|
|
1666
|
+
function getTileCache() {
|
|
1667
|
+
if (!tileCache) {
|
|
1668
|
+
tileCache = new QuickLRU({ maxSize: config.tileCacheSize });
|
|
1241
1669
|
}
|
|
1242
|
-
return
|
|
1670
|
+
return tileCache;
|
|
1243
1671
|
}
|
|
1244
|
-
function
|
|
1245
|
-
const
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1672
|
+
async function bufferToPixelDataBrowser(buff) {
|
|
1673
|
+
const blob = new Blob([buff]);
|
|
1674
|
+
const imageBitmap = await createImageBitmap(blob);
|
|
1675
|
+
const canvas = document.createElement("canvas");
|
|
1676
|
+
const ctx = canvas.getContext("2d");
|
|
1677
|
+
canvas.width = imageBitmap.width;
|
|
1678
|
+
canvas.height = imageBitmap.height;
|
|
1679
|
+
ctx.drawImage(imageBitmap, 0, 0);
|
|
1680
|
+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
1681
|
+
return {
|
|
1682
|
+
pixels: imageData.data,
|
|
1683
|
+
width: canvas.width,
|
|
1684
|
+
height: canvas.height,
|
|
1685
|
+
components: imageData.data.length / (canvas.width * canvas.height)
|
|
1686
|
+
};
|
|
1251
1687
|
}
|
|
1252
|
-
function
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
for (const ring of coords) {
|
|
1256
|
-
total += ringArea(ring);
|
|
1688
|
+
function getBufferToPixelDataParser() {
|
|
1689
|
+
if (config.bufferToPixelData) {
|
|
1690
|
+
return config.bufferToPixelData;
|
|
1257
1691
|
}
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
function ringArea(coords) {
|
|
1261
|
-
let p1;
|
|
1262
|
-
let p2;
|
|
1263
|
-
let p3;
|
|
1264
|
-
let lowerIndex;
|
|
1265
|
-
let middleIndex;
|
|
1266
|
-
let upperIndex;
|
|
1267
|
-
let i;
|
|
1268
|
-
let total = 0;
|
|
1269
|
-
const coordsLength = coords.length;
|
|
1270
|
-
if (coordsLength > 2) {
|
|
1271
|
-
for (i = 0; i < coordsLength; i++) {
|
|
1272
|
-
if (i === coordsLength - 2) {
|
|
1273
|
-
lowerIndex = coordsLength - 2;
|
|
1274
|
-
middleIndex = coordsLength - 1;
|
|
1275
|
-
upperIndex = 0;
|
|
1276
|
-
} else if (i === coordsLength - 1) {
|
|
1277
|
-
lowerIndex = coordsLength - 1;
|
|
1278
|
-
middleIndex = 0;
|
|
1279
|
-
upperIndex = 1;
|
|
1280
|
-
} else {
|
|
1281
|
-
lowerIndex = i;
|
|
1282
|
-
middleIndex = i + 1;
|
|
1283
|
-
upperIndex = i + 2;
|
|
1284
|
-
}
|
|
1285
|
-
p1 = coords[lowerIndex];
|
|
1286
|
-
p2 = coords[middleIndex];
|
|
1287
|
-
p3 = coords[upperIndex];
|
|
1288
|
-
total += (degToRad(p3[0]) - degToRad(p1[0])) * Math.sin(degToRad(p2[1]));
|
|
1289
|
-
}
|
|
1290
|
-
total = total * earthRadius * earthRadius / 2;
|
|
1692
|
+
if (typeof window !== "undefined") {
|
|
1693
|
+
return bufferToPixelDataBrowser;
|
|
1291
1694
|
}
|
|
1292
|
-
|
|
1695
|
+
throw new Error(
|
|
1696
|
+
"An image file buffer to pixel data parser is necessary. Specify it in `config.bufferToPixelData`"
|
|
1697
|
+
);
|
|
1293
1698
|
}
|
|
1294
1699
|
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1700
|
+
const terrainTileJsonURL = "tiles/terrain-rgb-v2/tiles.json";
|
|
1701
|
+
let terrainTileJson = null;
|
|
1702
|
+
const customMessages = {
|
|
1703
|
+
403: "Key is missing, invalid or restricted"
|
|
1704
|
+
};
|
|
1705
|
+
async function fetchTerrainTileJson(apiKey) {
|
|
1706
|
+
const endpoint = new URL(terrainTileJsonURL, defaults.maptilerApiURL);
|
|
1707
|
+
endpoint.searchParams.set("key", apiKey);
|
|
1708
|
+
const urlWithParams = endpoint.toString();
|
|
1709
|
+
const res = await callFetch(urlWithParams);
|
|
1710
|
+
if (res.ok) {
|
|
1711
|
+
terrainTileJson = await res.json();
|
|
1712
|
+
return terrainTileJson;
|
|
1713
|
+
} else {
|
|
1714
|
+
if (!res.ok) {
|
|
1715
|
+
throw new ServiceError(res, customMessages[res.status] ?? "");
|
|
1302
1716
|
}
|
|
1303
|
-
prevCoord = coordinate;
|
|
1304
1717
|
}
|
|
1305
|
-
return distance;
|
|
1306
|
-
}
|
|
1307
|
-
function pointDistance(from, to) {
|
|
1308
|
-
const { pow, sin, cos, sqrt, atan2 } = Math;
|
|
1309
|
-
const dLat = degToRad(to[1] - from[1]);
|
|
1310
|
-
const dLon = degToRad(to[0] - from[0]);
|
|
1311
|
-
const lat1 = degToRad(from[1]);
|
|
1312
|
-
const lat2 = degToRad(to[1]);
|
|
1313
|
-
const a = pow(sin(dLat / 2), 2) + pow(sin(dLon / 2), 2) * cos(lat1) * cos(lat2);
|
|
1314
|
-
return 2 * atan2(sqrt(a), sqrt(1 - a)) * earthRadius;
|
|
1315
|
-
}
|
|
1316
|
-
|
|
1317
|
-
function tileCover(coordinates, zoom, tileSize) {
|
|
1318
|
-
const tileHash = /* @__PURE__ */ new Map();
|
|
1319
|
-
const tiles = [];
|
|
1320
|
-
const coords = [];
|
|
1321
|
-
const resolution = getZoomLevelResolution(coordinates[0][1], zoom, tileSize);
|
|
1322
|
-
const samples = sampleProfileLine(coordinates, resolution);
|
|
1323
|
-
for (const sample of samples) {
|
|
1324
|
-
const coordinate = sample;
|
|
1325
|
-
const [tileX, tileY] = webMercProjection.llToTile(coordinate, zoom, tileSize);
|
|
1326
|
-
const id = xyzToTileID(tileX, tileY, zoom);
|
|
1327
|
-
const tile = { id, x: tileX, y: tileY, z: zoom };
|
|
1328
|
-
tileHash.set(id, tile);
|
|
1329
|
-
coords.push({ coordinate, tile });
|
|
1330
|
-
}
|
|
1331
|
-
for (const tile of tileHash.values())
|
|
1332
|
-
tiles.push(tile);
|
|
1333
|
-
return { coords, tiles };
|
|
1334
1718
|
}
|
|
1335
|
-
function
|
|
1336
|
-
const
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1719
|
+
async function at(position, options = {}) {
|
|
1720
|
+
const apiKey = options.apiKey ?? config.apiKey;
|
|
1721
|
+
if (!terrainTileJson) {
|
|
1722
|
+
await fetchTerrainTileJson(apiKey);
|
|
1723
|
+
}
|
|
1724
|
+
const maxZoom = terrainTileJson.maxzoom;
|
|
1725
|
+
let zoom = ~~(options.zoom ?? maxZoom);
|
|
1726
|
+
if (zoom > maxZoom || zoom < 0) {
|
|
1727
|
+
zoom = maxZoom;
|
|
1728
|
+
}
|
|
1729
|
+
const tileIndex = math.wgs84ToTileIndex(position, zoom, false);
|
|
1730
|
+
const tileX = ~~tileIndex[0];
|
|
1731
|
+
const tileY = ~~tileIndex[1];
|
|
1732
|
+
if (!terrainTileJson.tiles.length) {
|
|
1733
|
+
throw new Error("Terrain tileJSON tile list is empty.");
|
|
1734
|
+
}
|
|
1735
|
+
const tileID = `terrain_${zoom.toString()}_${tileX.toString()}_${tileY.toString()}`;
|
|
1736
|
+
let tilePixelData;
|
|
1737
|
+
const cache = getTileCache();
|
|
1738
|
+
if (cache.has(tileID)) {
|
|
1739
|
+
tilePixelData = cache.get(tileID);
|
|
1740
|
+
} else {
|
|
1741
|
+
const tileURL = terrainTileJson.tiles[0].replace("{x}", tileX.toString()).replace("{y}", tileY.toString()).replace("{z}", zoom.toString());
|
|
1742
|
+
const tileRes = await callFetch(tileURL);
|
|
1743
|
+
if (!tileRes.ok) {
|
|
1744
|
+
throw new ServiceError(tileRes, customMessages[tileRes.status] ?? "");
|
|
1354
1745
|
}
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1746
|
+
const tileBuff = await tileRes.arrayBuffer();
|
|
1747
|
+
const tileParser = getBufferToPixelDataParser();
|
|
1748
|
+
tilePixelData = await tileParser(tileBuff);
|
|
1749
|
+
cache.set(tileID, tilePixelData);
|
|
1750
|
+
}
|
|
1751
|
+
const pixelX = ~~(tilePixelData.width * (tileIndex[0] % 1));
|
|
1752
|
+
const pixelY = ~~(tilePixelData.height * (tileIndex[1] % 1));
|
|
1753
|
+
const pixelDataIndex = (pixelY * tilePixelData.width + pixelX) * tilePixelData.components;
|
|
1754
|
+
const R = tilePixelData.pixels[pixelDataIndex];
|
|
1755
|
+
const G = tilePixelData.pixels[pixelDataIndex + 1];
|
|
1756
|
+
const B = tilePixelData.pixels[pixelDataIndex + 2];
|
|
1757
|
+
const elevation2 = -1e4 + (R * 256 * 256 + G * 256 + B) * 0.1;
|
|
1758
|
+
return [position[0], position[1], elevation2];
|
|
1358
1759
|
}
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
const
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1760
|
+
async function batch(positions, options = {}) {
|
|
1761
|
+
const apiKey = options.apiKey ?? config.apiKey;
|
|
1762
|
+
if (!terrainTileJson) {
|
|
1763
|
+
await fetchTerrainTileJson(apiKey);
|
|
1764
|
+
}
|
|
1765
|
+
const tileParser = getBufferToPixelDataParser();
|
|
1766
|
+
const tileURLSchema = terrainTileJson.tiles[0];
|
|
1767
|
+
const cache = getTileCache();
|
|
1768
|
+
const maxZoom = terrainTileJson.maxzoom;
|
|
1769
|
+
let zoom = ~~(options.zoom ?? maxZoom);
|
|
1770
|
+
if (zoom > maxZoom || zoom < 0) {
|
|
1771
|
+
zoom = maxZoom;
|
|
1772
|
+
}
|
|
1773
|
+
const tileIndicesFloats = positions.map(
|
|
1774
|
+
(position) => math.wgs84ToTileIndex(position, zoom, false)
|
|
1371
1775
|
);
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
}
|
|
1379
|
-
|
|
1380
|
-
async function profileLineString(path, options) {
|
|
1381
|
-
const coordinates = "geometry" in path ? path.geometry.coordinates : path.coordinates;
|
|
1382
|
-
const { coords, tiles } = tileCover(
|
|
1383
|
-
coordinates,
|
|
1384
|
-
options.zoom ?? 13,
|
|
1385
|
-
options.tileSize ?? 512
|
|
1776
|
+
const tileIndicesInteger = tileIndicesFloats.map((index) => [
|
|
1777
|
+
~~index[0],
|
|
1778
|
+
~~index[1]
|
|
1779
|
+
]);
|
|
1780
|
+
const tileIDs = tileIndicesInteger.map(
|
|
1781
|
+
(index) => `terrain_${zoom.toString()}_${index[0].toString()}_${index[1].toString()}`
|
|
1386
1782
|
);
|
|
1387
|
-
const
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
options.elevationParser
|
|
1783
|
+
const uniqueTilesToFetch = Array.from(
|
|
1784
|
+
new Set(tileIDs.filter((tileID) => !cache.has(tileID)))
|
|
1785
|
+
).map((tileID) => tileID.split("_").slice(1));
|
|
1786
|
+
const tileURLs = uniqueTilesToFetch.map(
|
|
1787
|
+
(zxy) => tileURLSchema.replace("{x}", zxy[1].toString()).replace("{y}", zxy[2].toString()).replace("{z}", zxy[0].toString())
|
|
1393
1788
|
);
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
tileCache.set(tile.id, res);
|
|
1408
|
-
return res;
|
|
1409
|
-
}).catch((err) => {
|
|
1410
|
-
console.error(err);
|
|
1411
|
-
return void 0;
|
|
1412
|
-
})
|
|
1789
|
+
const promisesFetchTiles = tileURLs.map((url) => callFetch(url));
|
|
1790
|
+
const resTiles = await Promise.allSettled(promisesFetchTiles);
|
|
1791
|
+
const fulfilledRes = resTiles.map(
|
|
1792
|
+
(el) => el.status === "fulfilled" ? el.value : null
|
|
1793
|
+
).filter((res) => res);
|
|
1794
|
+
const fulfilledRButNotOkRes = fulfilledRes.filter((res) => !res.ok);
|
|
1795
|
+
if (fulfilledRes.length !== promisesFetchTiles.length) {
|
|
1796
|
+
throw new Error("Some tiles could not be fetched.");
|
|
1797
|
+
}
|
|
1798
|
+
if (fulfilledRButNotOkRes.length) {
|
|
1799
|
+
throw new ServiceError(
|
|
1800
|
+
fulfilledRButNotOkRes[0],
|
|
1801
|
+
customMessages[fulfilledRButNotOkRes[0].status] ?? ""
|
|
1413
1802
|
);
|
|
1414
1803
|
}
|
|
1415
|
-
await Promise.
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1804
|
+
const tileArrayBuffers = await Promise.all(
|
|
1805
|
+
fulfilledRes.map((res) => res.arrayBuffer())
|
|
1806
|
+
);
|
|
1807
|
+
if (!tileArrayBuffers.every((buff) => buff.byteLength > 0)) {
|
|
1808
|
+
throw new Error("Some tiles are not available.");
|
|
1809
|
+
}
|
|
1810
|
+
const tilePixelDatas = await Promise.all(
|
|
1811
|
+
tileArrayBuffers.map((buff) => tileParser(buff))
|
|
1812
|
+
);
|
|
1813
|
+
tilePixelDatas.forEach((tilePixelData, i) => {
|
|
1814
|
+
const zxy = uniqueTilesToFetch[i];
|
|
1815
|
+
const tileID = `terrain_${zxy[0].toString()}_${zxy[1].toString()}_${zxy[2].toString()}`;
|
|
1816
|
+
cache.set(tileID, tilePixelData);
|
|
1817
|
+
});
|
|
1818
|
+
const elevatedPositions = positions.map((position, i) => {
|
|
1819
|
+
const tileID = tileIDs[i];
|
|
1820
|
+
const tileIndexFloat = tileIndicesFloats[i];
|
|
1821
|
+
const tilePixelData = cache.get(tileID);
|
|
1822
|
+
const pixelX = Math.min(
|
|
1823
|
+
Math.round(tilePixelData.width * (tileIndexFloat[0] % 1)),
|
|
1824
|
+
tilePixelData.width - 1
|
|
1434
1825
|
);
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1826
|
+
const pixelY = Math.min(
|
|
1827
|
+
Math.round(tilePixelData.height * (tileIndexFloat[1] % 1)),
|
|
1828
|
+
tilePixelData.height - 1
|
|
1829
|
+
);
|
|
1830
|
+
const pixelDataIndex = (pixelY * tilePixelData.width + pixelX) * tilePixelData.components;
|
|
1831
|
+
const R = tilePixelData.pixels[pixelDataIndex];
|
|
1832
|
+
const G = tilePixelData.pixels[pixelDataIndex + 1];
|
|
1833
|
+
const B = tilePixelData.pixels[pixelDataIndex + 2];
|
|
1834
|
+
const elevation2 = -1e4 + (R * 256 * 256 + G * 256 + B) * 0.1;
|
|
1835
|
+
return [position[0], position[1], ~~(elevation2 * 1e3) / 1e3];
|
|
1836
|
+
});
|
|
1837
|
+
if (options.smoothingKernelSize) {
|
|
1838
|
+
const kernelSize = ~~(options.smoothingKernelSize / 2) * 2 + 1;
|
|
1839
|
+
const elevations = elevatedPositions.map((pos) => pos[2]);
|
|
1840
|
+
const kernelSpan = ~~(kernelSize / 2);
|
|
1841
|
+
for (let i = kernelSpan; i < elevations.length - kernelSpan - 1; i += 1) {
|
|
1842
|
+
let sum = 0;
|
|
1843
|
+
for (let j = 0; j < kernelSize; j += 1) {
|
|
1844
|
+
const elev = elevations[i - kernelSpan + j];
|
|
1845
|
+
sum += elev;
|
|
1846
|
+
}
|
|
1847
|
+
sum /= kernelSize;
|
|
1848
|
+
elevatedPositions[i][2] = sum;
|
|
1443
1849
|
}
|
|
1444
|
-
prevCoord = coordinate;
|
|
1445
1850
|
}
|
|
1446
|
-
return
|
|
1851
|
+
return elevatedPositions;
|
|
1447
1852
|
}
|
|
1448
|
-
function
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
maxElevation = point.elevation;
|
|
1457
|
-
totalElevation += point.elevation;
|
|
1458
|
-
}
|
|
1459
|
-
return {
|
|
1460
|
-
distance: points[points.length - 1].distance,
|
|
1461
|
-
minElevation,
|
|
1462
|
-
maxElevation,
|
|
1463
|
-
avgElevation: totalElevation / points.length,
|
|
1464
|
-
startElevation: points[0].elevation,
|
|
1465
|
-
endElevation: points[points.length - 1].elevation,
|
|
1466
|
-
points
|
|
1467
|
-
};
|
|
1468
|
-
}
|
|
1469
|
-
function smoothElevation(points) {
|
|
1470
|
-
const newPoints = [];
|
|
1471
|
-
let prevPoint;
|
|
1472
|
-
for (let i = 0; i < points.length; i++) {
|
|
1473
|
-
const point = points[i];
|
|
1474
|
-
const nextPoint = points[i + 1] ?? points[i];
|
|
1475
|
-
if (prevPoint === void 0) {
|
|
1476
|
-
newPoints.push(point);
|
|
1477
|
-
} else {
|
|
1478
|
-
const newPoint = {
|
|
1479
|
-
distance: point.distance,
|
|
1480
|
-
elevation: (prevPoint.elevation + point.elevation + nextPoint.elevation) / 3,
|
|
1481
|
-
coordinate: point.coordinate,
|
|
1482
|
-
tile: point.tile
|
|
1483
|
-
};
|
|
1484
|
-
newPoints.push(newPoint);
|
|
1485
|
-
}
|
|
1486
|
-
prevPoint = point;
|
|
1487
|
-
}
|
|
1488
|
-
return newPoints;
|
|
1853
|
+
async function fromLineString(ls, options = {}) {
|
|
1854
|
+
if (ls.type !== "LineString") {
|
|
1855
|
+
throw new Error("The provided object is not a GeoJSON LineString");
|
|
1856
|
+
}
|
|
1857
|
+
const clone = structuredClone(ls);
|
|
1858
|
+
const elevatedPositions = await batch(clone.coordinates, options);
|
|
1859
|
+
clone.coordinates = elevatedPositions;
|
|
1860
|
+
return clone;
|
|
1489
1861
|
}
|
|
1490
|
-
function
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
return
|
|
1862
|
+
async function fromMultiLineString(ls, options = {}) {
|
|
1863
|
+
if (ls.type !== "MultiLineString") {
|
|
1864
|
+
throw new Error("The provided object is not a GeoJSON MultiLineString");
|
|
1865
|
+
}
|
|
1866
|
+
const clone = structuredClone(ls);
|
|
1867
|
+
const multiLengths = clone.coordinates.map((poss) => poss.length);
|
|
1868
|
+
const flattenPositions = clone.coordinates.flat();
|
|
1869
|
+
const flattenPositionsElevated = await batch(flattenPositions, options);
|
|
1870
|
+
const result = [];
|
|
1871
|
+
let index = 0;
|
|
1872
|
+
for (const length of multiLengths) {
|
|
1873
|
+
result.push(flattenPositionsElevated.slice(index, index + length));
|
|
1874
|
+
index += length;
|
|
1875
|
+
}
|
|
1876
|
+
clone.coordinates = result;
|
|
1877
|
+
return clone;
|
|
1506
1878
|
}
|
|
1879
|
+
const elevation = {
|
|
1880
|
+
at,
|
|
1881
|
+
batch,
|
|
1882
|
+
fromLineString,
|
|
1883
|
+
fromMultiLineString
|
|
1884
|
+
};
|
|
1507
1885
|
|
|
1508
1886
|
exports.ClientConfig = ClientConfig;
|
|
1509
1887
|
exports.LanguageGeocoding = LanguageGeocoding;
|
|
@@ -1511,25 +1889,21 @@ exports.MapStyle = MapStyle;
|
|
|
1511
1889
|
exports.MapStyleVariant = MapStyleVariant;
|
|
1512
1890
|
exports.ReferenceMapStyle = ReferenceMapStyle;
|
|
1513
1891
|
exports.ServiceError = ServiceError;
|
|
1514
|
-
exports.
|
|
1892
|
+
exports.bufferToPixelDataBrowser = bufferToPixelDataBrowser;
|
|
1893
|
+
exports.circumferenceAtLatitude = circumferenceAtLatitude;
|
|
1515
1894
|
exports.config = config;
|
|
1516
1895
|
exports.coordinates = coordinates;
|
|
1517
1896
|
exports.data = data;
|
|
1518
|
-
exports.
|
|
1519
|
-
exports.degToRad = degToRad;
|
|
1520
|
-
exports.earthRadius = earthRadius;
|
|
1897
|
+
exports.elevation = elevation;
|
|
1521
1898
|
exports.expandMapStyle = expandMapStyle;
|
|
1522
1899
|
exports.geocoding = geocoding;
|
|
1523
1900
|
exports.geolocation = geolocation;
|
|
1524
|
-
exports.
|
|
1525
|
-
exports.
|
|
1526
|
-
exports.
|
|
1901
|
+
exports.getAutoLanguageGeocoding = getAutoLanguageGeocoding;
|
|
1902
|
+
exports.getBufferToPixelDataParser = getBufferToPixelDataParser;
|
|
1903
|
+
exports.getTileCache = getTileCache;
|
|
1527
1904
|
exports.mapStylePresetList = mapStylePresetList;
|
|
1528
|
-
exports.
|
|
1529
|
-
exports.
|
|
1530
|
-
exports.polygonArea = polygonArea;
|
|
1531
|
-
exports.profileLineString = profileLineString;
|
|
1532
|
-
exports.radToDeg = radToDeg;
|
|
1905
|
+
exports.math = math;
|
|
1906
|
+
exports.misc = misc;
|
|
1533
1907
|
exports.staticMaps = staticMaps;
|
|
1534
|
-
exports.
|
|
1908
|
+
exports.styleToStyle = styleToStyle;
|
|
1535
1909
|
//# sourceMappingURL=maptiler-client.cjs.map
|