@maptiler/client 1.6.0 → 1.8.0
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 +25 -0
- package/dist/maptiler-client.cjs +587 -332
- package/dist/maptiler-client.cjs.map +1 -1
- package/dist/maptiler-client.d.ts +291 -12
- package/dist/maptiler-client.min.mjs +1 -1
- package/dist/maptiler-client.mjs +580 -332
- package/dist/maptiler-client.mjs.map +1 -1
- package/package.json +29 -20
- package/readme.md +134 -2
- package/rollup.config.js +3 -3
- package/tsconfig.json +1 -1
- package/dist/maptiler-client.umd.js +0 -1368
- package/dist/maptiler-client.umd.js.map +0 -1
- package/dist/maptiler-client.umd.min.js +0 -1
- package/docs/.nojekyll +0 -1
- package/docs/assets/custom.css +0 -118
- package/docs/assets/highlight.css +0 -85
- package/docs/assets/main.js +0 -58
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1280
- package/docs/classes/config.ClientConfig.html +0 -132
- package/docs/classes/services_ServiceError.ServiceError.html +0 -103
- package/docs/functions/language.getAutoLanguageGeocoding.html +0 -60
- package/docs/functions/services_simplify.default.html +0 -65
- package/docs/images/JS-logo.svg +0 -4
- package/docs/images/TS-logo.svg +0 -6
- package/docs/images/maptiler-client-logo.afdesign +0 -0
- package/docs/images/maptiler-client-logo.svg +0 -75
- package/docs/images/maptiler-logo-256.png +0 -0
- package/docs/images/maptiler-logo.svg +0 -23
- package/docs/images/screenshots/static-bounded-europe-1024.png +0 -0
- package/docs/images/screenshots/static-bounded-europe-2048.png +0 -0
- package/docs/images/screenshots/static-bounded-portugal-1024x2048.png +0 -0
- package/docs/images/screenshots/static-bounded-portugal-2048x2048.png +0 -0
- package/docs/images/screenshots/static-with-path.png +0 -0
- package/docs/index.html +0 -340
- package/docs/modules/config.html +0 -71
- package/docs/modules/language.html +0 -71
- package/docs/modules/services_ServiceError.html +0 -61
- package/docs/modules/services_coordinates.html +0 -84
- package/docs/modules/services_data.html +0 -66
- package/docs/modules/services_geocoding.html +0 -82
- package/docs/modules/services_geolocation.html +0 -68
- package/docs/modules/services_simplify.html +0 -61
- package/docs/modules/services_staticMaps.html +0 -74
- package/docs/modules.html +0 -59
- package/docs/types/config.FetchFunction.html +0 -71
- package/docs/types/language.LanguageGeocodingString.html +0 -58
- package/docs/types/services_coordinates.CoordinateExport.html +0 -71
- package/docs/types/services_coordinates.CoordinateGrid.html +0 -69
- package/docs/types/services_coordinates.CoordinateId.html +0 -71
- package/docs/types/services_coordinates.CoordinateSearch.html +0 -93
- package/docs/types/services_coordinates.CoordinateSearchResult.html +0 -75
- package/docs/types/services_coordinates.CoordinateTransformResult.html +0 -76
- package/docs/types/services_coordinates.CoordinateTransformation.html +0 -89
- package/docs/types/services_coordinates.CoordinatesSearchOptions.html +0 -83
- package/docs/types/services_coordinates.CoordinatesTransformOptions.html +0 -85
- package/docs/types/services_coordinates.XYZ.html +0 -73
- package/docs/types/services_data.GetDataOptions.html +0 -64
- package/docs/types/services_geocoding.ByIdGeocodingOptions.html +0 -63
- package/docs/types/services_geocoding.CommonForwardAndReverseGeocodingOptions.html +0 -63
- package/docs/types/services_geocoding.Coordinates.html +0 -63
- package/docs/types/services_geocoding.FeatureHierarchy.html +0 -63
- package/docs/types/services_geocoding.GeocodingFeature.html +0 -63
- package/docs/types/services_geocoding.GeocodingOptions.html +0 -63
- package/docs/types/services_geocoding.GeocodingSearchResult.html +0 -80
- package/docs/types/services_geocoding.LanguageGeocodingOptions.html +0 -70
- package/docs/types/services_geocoding.ReverseGeocodingOptions.html +0 -63
- package/docs/types/services_geolocation.GeolocationInfoOptions.html +0 -65
- package/docs/types/services_geolocation.GeolocationResult.html +0 -128
- package/docs/types/services_staticMaps.AutomaticStaticMapOptions.html +0 -61
- package/docs/types/services_staticMaps.BoundedStaticMapOptions.html +0 -61
- package/docs/types/services_staticMaps.CenteredStaticMapOptions.html +0 -61
- package/docs/types/services_staticMaps.StaticMapBaseOptions.html +0 -157
- package/docs/types/services_staticMaps.StaticMapMarker.html +0 -61
- package/docs/variables/config.config.html +0 -58
- package/docs/variables/language.LanguageGeocoding.html +0 -171
- package/docs/variables/services_coordinates.coordinates.html +0 -110
- package/docs/variables/services_data.data.html +0 -80
- package/docs/variables/services_geocoding.geocoding.html +0 -265
- package/docs/variables/services_geolocation.geolocation.html +0 -79
- package/docs/variables/services_staticMaps.staticMaps.html +0 -128
- package/docsmd/.nojekyll +0 -1
- package/docsmd/README.md +0 -873
- package/docsmd/assets/custom.css +0 -118
- package/docsmd/classes/ClientConfig.md +0 -90
- package/docsmd/classes/MapStyleVariant.md +0 -234
- package/docsmd/classes/ReferenceMapStyle.md +0 -169
- package/docsmd/classes/ServiceError.md +0 -52
- package/examples/test-browser-es.html +0 -103
- package/examples/test-browser-umd.html +0 -90
- package/examples/test-node.js +0 -52
- package/images/JS-logo.svg +0 -4
- package/images/TS-logo.svg +0 -6
- package/images/maptiler-client-logo.afdesign +0 -0
- package/images/maptiler-client-logo.svg +0 -75
- package/images/maptiler-logo-256.png +0 -0
- package/images/maptiler-logo.svg +0 -23
- package/images/screenshots/static-bounded-europe-1024.png +0 -0
- package/images/screenshots/static-bounded-europe-2048.png +0 -0
- package/images/screenshots/static-bounded-portugal-1024x2048.png +0 -0
- package/images/screenshots/static-bounded-portugal-2048x2048.png +0 -0
- package/images/screenshots/static-with-path.png +0 -0
- package/src/callFetch.ts +0 -18
- package/src/config.ts +0 -65
- package/src/defaults.ts +0 -11
- package/src/index.ts +0 -111
- package/src/language.ts +0 -87
- package/src/mapstyle.ts +0 -968
- package/src/services/ServiceError.ts +0 -10
- package/src/services/coordinates.ts +0 -258
- package/src/services/data.ts +0 -63
- package/src/services/geocoding.ts +0 -458
- 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.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import QuickLRU from 'quick-lru';
|
|
2
|
+
|
|
1
3
|
function tryGettingFetch() {
|
|
2
4
|
if (typeof self !== "undefined") {
|
|
3
5
|
return fetch.bind(self);
|
|
@@ -18,6 +20,10 @@ class ClientConfig {
|
|
|
18
20
|
* will be automatically resolved.
|
|
19
21
|
*/
|
|
20
22
|
this._fetch = tryGettingFetch();
|
|
23
|
+
/**
|
|
24
|
+
* Number of tiles to keep in cache
|
|
25
|
+
*/
|
|
26
|
+
this.tileCacheSize = 200;
|
|
21
27
|
}
|
|
22
28
|
/**
|
|
23
29
|
* Set the MapTiler Cloud API key
|
|
@@ -114,40 +120,18 @@ function getAutoLanguageGeocoding() {
|
|
|
114
120
|
return canditatelangs.length ? canditatelangs[0] : LanguageGeocoding.ENGLISH;
|
|
115
121
|
}
|
|
116
122
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
} catch (e) {
|
|
130
|
-
reject(e);
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
134
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
135
|
-
});
|
|
136
|
-
};
|
|
137
|
-
function callFetch(_0) {
|
|
138
|
-
return __async$4(this, arguments, function* (resource, options = {}) {
|
|
139
|
-
if (config.fetch === null) {
|
|
140
|
-
throw new Error(
|
|
141
|
-
"The fetch function was not found. If on NodeJS < 18 please specify the fetch function with config.fetch"
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
if (new URL(resource).searchParams.get("key").trim() === "") {
|
|
145
|
-
throw new Error(
|
|
146
|
-
"The MapTiler Cloud API key is missing. Set it in `config.apiKey` or get one for free at https://maptiler.com"
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
return config.fetch(resource, options);
|
|
150
|
-
});
|
|
123
|
+
async function callFetch(resource, options = {}) {
|
|
124
|
+
if (config.fetch === null) {
|
|
125
|
+
throw new Error(
|
|
126
|
+
"The fetch function was not found. If on NodeJS < 18 please specify the fetch function with config.fetch"
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
if (new URL(resource).searchParams.get("key").trim() === "") {
|
|
130
|
+
throw new Error(
|
|
131
|
+
"The MapTiler Cloud API key is missing. Set it in `config.apiKey` or get one for free at https://maptiler.com"
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
return config.fetch(resource, options);
|
|
151
135
|
}
|
|
152
136
|
|
|
153
137
|
const defaults = {
|
|
@@ -165,37 +149,18 @@ class ServiceError extends Error {
|
|
|
165
149
|
}
|
|
166
150
|
}
|
|
167
151
|
|
|
168
|
-
|
|
169
|
-
return new Promise((resolve, reject) => {
|
|
170
|
-
var fulfilled = (value) => {
|
|
171
|
-
try {
|
|
172
|
-
step(generator.next(value));
|
|
173
|
-
} catch (e) {
|
|
174
|
-
reject(e);
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
var rejected = (value) => {
|
|
178
|
-
try {
|
|
179
|
-
step(generator.throw(value));
|
|
180
|
-
} catch (e) {
|
|
181
|
-
reject(e);
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
185
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
186
|
-
});
|
|
187
|
-
};
|
|
188
|
-
const customMessages$3 = {
|
|
152
|
+
const customMessages$4 = {
|
|
189
153
|
400: "Query too long / Invalid parameters",
|
|
190
154
|
403: "Key is missing, invalid or restricted"
|
|
191
155
|
};
|
|
192
156
|
function addLanguageGeocodingOptions(searchParams, options) {
|
|
193
|
-
|
|
157
|
+
const { language } = options;
|
|
158
|
+
if (language == void 0) {
|
|
194
159
|
return;
|
|
195
160
|
}
|
|
196
161
|
const languages = Array.from(
|
|
197
162
|
new Set(
|
|
198
|
-
(Array.isArray(
|
|
163
|
+
(Array.isArray(language) ? language : [language]).map(
|
|
199
164
|
(lang) => lang === LanguageGeocoding.AUTO ? getAutoLanguageGeocoding() : lang
|
|
200
165
|
)
|
|
201
166
|
)
|
|
@@ -203,304 +168,228 @@ function addLanguageGeocodingOptions(searchParams, options) {
|
|
|
203
168
|
searchParams.set("language", languages);
|
|
204
169
|
}
|
|
205
170
|
function addCommonForwardAndReverseGeocodingOptions(searchParams, options) {
|
|
206
|
-
|
|
207
|
-
searchParams.set("key",
|
|
208
|
-
if (
|
|
209
|
-
searchParams.set("limit", String(
|
|
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(","));
|
|
210
178
|
}
|
|
211
|
-
if (
|
|
212
|
-
searchParams.set("
|
|
179
|
+
if (excludeTypes != void 0) {
|
|
180
|
+
searchParams.set("excludeTypes", String(excludeTypes));
|
|
213
181
|
}
|
|
214
182
|
addLanguageGeocodingOptions(searchParams, options);
|
|
215
183
|
}
|
|
216
184
|
function addForwardGeocodingOptions(searchParams, options) {
|
|
217
185
|
addCommonForwardAndReverseGeocodingOptions(searchParams, options);
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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
|
+
);
|
|
223
195
|
}
|
|
224
|
-
if (
|
|
225
|
-
searchParams.set("country",
|
|
196
|
+
if (country != void 0) {
|
|
197
|
+
searchParams.set("country", country.join(","));
|
|
226
198
|
}
|
|
227
|
-
if (
|
|
228
|
-
searchParams.set("fuzzyMatch",
|
|
199
|
+
if (fuzzyMatch != void 0) {
|
|
200
|
+
searchParams.set("fuzzyMatch", fuzzyMatch ? "true" : "false");
|
|
229
201
|
}
|
|
230
|
-
if (
|
|
231
|
-
searchParams.set("autocomplete",
|
|
202
|
+
if (autocomplete != void 0) {
|
|
203
|
+
searchParams.set("autocomplete", autocomplete ? "true" : "false");
|
|
232
204
|
}
|
|
233
205
|
}
|
|
234
|
-
function forward(
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
const obj = yield res.json();
|
|
252
|
-
return obj;
|
|
253
|
-
});
|
|
206
|
+
async function forward(query, options = {}) {
|
|
207
|
+
if (typeof query !== "string" || query.trim().length === 0) {
|
|
208
|
+
throw new Error("The query must be a non-empty string");
|
|
209
|
+
}
|
|
210
|
+
const endpoint = new URL(
|
|
211
|
+
`geocoding/${encodeURIComponent(query)}.json`,
|
|
212
|
+
defaults.maptilerApiURL
|
|
213
|
+
);
|
|
214
|
+
const { searchParams } = endpoint;
|
|
215
|
+
addForwardGeocodingOptions(searchParams, options);
|
|
216
|
+
const urlWithParams = endpoint.toString();
|
|
217
|
+
const res = await callFetch(urlWithParams);
|
|
218
|
+
if (!res.ok) {
|
|
219
|
+
throw new ServiceError(res, customMessages$4[res.status] ?? "");
|
|
220
|
+
}
|
|
221
|
+
const obj = await res.json();
|
|
222
|
+
return obj;
|
|
254
223
|
}
|
|
255
|
-
function reverse(
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const obj = yield res.json();
|
|
272
|
-
return obj;
|
|
273
|
-
});
|
|
224
|
+
async function reverse(position, options = {}) {
|
|
225
|
+
if (!Array.isArray(position) || position.length < 2) {
|
|
226
|
+
throw new Error("The position must be an array of form [lng, lat].");
|
|
227
|
+
}
|
|
228
|
+
const endpoint = new URL(
|
|
229
|
+
`geocoding/${position[0]},${position[1]}.json`,
|
|
230
|
+
defaults.maptilerApiURL
|
|
231
|
+
);
|
|
232
|
+
addCommonForwardAndReverseGeocodingOptions(endpoint.searchParams, options);
|
|
233
|
+
const urlWithParams = endpoint.toString();
|
|
234
|
+
const res = await callFetch(urlWithParams);
|
|
235
|
+
if (!res.ok) {
|
|
236
|
+
throw new ServiceError(res, customMessages$4[res.status] ?? "");
|
|
237
|
+
}
|
|
238
|
+
const obj = await res.json();
|
|
239
|
+
return obj;
|
|
274
240
|
}
|
|
275
|
-
function byId(
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
return obj;
|
|
287
|
-
});
|
|
241
|
+
async function byId(id, options = {}) {
|
|
242
|
+
const endpoint = new URL(`geocoding/${id}.json`, defaults.maptilerApiURL);
|
|
243
|
+
endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
|
|
244
|
+
addLanguageGeocodingOptions(endpoint.searchParams, options);
|
|
245
|
+
const urlWithParams = endpoint.toString();
|
|
246
|
+
const res = await callFetch(urlWithParams);
|
|
247
|
+
if (!res.ok) {
|
|
248
|
+
throw new ServiceError(res, customMessages$4[res.status] ?? "");
|
|
249
|
+
}
|
|
250
|
+
const obj = await res.json();
|
|
251
|
+
return obj;
|
|
288
252
|
}
|
|
289
|
-
function batch(
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
const obj = yield res.json();
|
|
308
|
-
return queries.length === 1 ? [obj] : obj;
|
|
309
|
-
});
|
|
253
|
+
async function batch$1(queries, options = {}) {
|
|
254
|
+
if (!queries.length) {
|
|
255
|
+
return [];
|
|
256
|
+
}
|
|
257
|
+
const joinedQuery = queries.map((query) => encodeURIComponent(query)).join(";");
|
|
258
|
+
const endpoint = new URL(
|
|
259
|
+
`geocoding/${joinedQuery}.json`,
|
|
260
|
+
defaults.maptilerApiURL
|
|
261
|
+
);
|
|
262
|
+
const { searchParams } = endpoint;
|
|
263
|
+
addForwardGeocodingOptions(searchParams, options);
|
|
264
|
+
const urlWithParams = endpoint.toString();
|
|
265
|
+
const res = await callFetch(urlWithParams);
|
|
266
|
+
if (!res.ok) {
|
|
267
|
+
throw new ServiceError(res, customMessages$4[res.status] ?? "");
|
|
268
|
+
}
|
|
269
|
+
const obj = await res.json();
|
|
270
|
+
return queries.length === 1 ? [obj] : obj;
|
|
310
271
|
}
|
|
311
272
|
const geocoding = {
|
|
312
273
|
forward,
|
|
313
274
|
reverse,
|
|
314
275
|
byId,
|
|
315
|
-
batch,
|
|
276
|
+
batch: batch$1,
|
|
316
277
|
language: LanguageGeocoding
|
|
317
278
|
};
|
|
318
279
|
|
|
319
|
-
|
|
320
|
-
return new Promise((resolve, reject) => {
|
|
321
|
-
var fulfilled = (value) => {
|
|
322
|
-
try {
|
|
323
|
-
step(generator.next(value));
|
|
324
|
-
} catch (e) {
|
|
325
|
-
reject(e);
|
|
326
|
-
}
|
|
327
|
-
};
|
|
328
|
-
var rejected = (value) => {
|
|
329
|
-
try {
|
|
330
|
-
step(generator.throw(value));
|
|
331
|
-
} catch (e) {
|
|
332
|
-
reject(e);
|
|
333
|
-
}
|
|
334
|
-
};
|
|
335
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
336
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
337
|
-
});
|
|
338
|
-
};
|
|
339
|
-
const customMessages$2 = {
|
|
280
|
+
const customMessages$3 = {
|
|
340
281
|
403: "Key is missing, invalid or restricted"
|
|
341
282
|
};
|
|
342
|
-
function info() {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
const obj = yield res.json();
|
|
356
|
-
return obj;
|
|
357
|
-
});
|
|
283
|
+
async function info(options = {}) {
|
|
284
|
+
const endpoint = new URL(`geolocation/ip.json`, defaults.maptilerApiURL);
|
|
285
|
+
endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
|
|
286
|
+
const urlWithParams = endpoint.toString();
|
|
287
|
+
const res = await callFetch(urlWithParams);
|
|
288
|
+
if (!res.ok) {
|
|
289
|
+
throw new ServiceError(
|
|
290
|
+
res,
|
|
291
|
+
res.status in customMessages$3 ? customMessages$3[res.status] : ""
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
const obj = await res.json();
|
|
295
|
+
return obj;
|
|
358
296
|
}
|
|
359
297
|
const geolocation = {
|
|
360
298
|
info
|
|
361
299
|
};
|
|
362
300
|
|
|
363
|
-
|
|
364
|
-
return new Promise((resolve, reject) => {
|
|
365
|
-
var fulfilled = (value) => {
|
|
366
|
-
try {
|
|
367
|
-
step(generator.next(value));
|
|
368
|
-
} catch (e) {
|
|
369
|
-
reject(e);
|
|
370
|
-
}
|
|
371
|
-
};
|
|
372
|
-
var rejected = (value) => {
|
|
373
|
-
try {
|
|
374
|
-
step(generator.throw(value));
|
|
375
|
-
} catch (e) {
|
|
376
|
-
reject(e);
|
|
377
|
-
}
|
|
378
|
-
};
|
|
379
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
380
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
381
|
-
});
|
|
382
|
-
};
|
|
383
|
-
const customMessages$1 = {
|
|
301
|
+
const customMessages$2 = {
|
|
384
302
|
403: "Key is missing, invalid or restricted"
|
|
385
303
|
};
|
|
386
|
-
function search(
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
304
|
+
async function search(query, options = {}) {
|
|
305
|
+
if (typeof query !== "string" || query.trim().length === 0) {
|
|
306
|
+
throw new Error("The query must be a non-empty string");
|
|
307
|
+
}
|
|
308
|
+
const endpoint = new URL(
|
|
309
|
+
`coordinates/search/${query}.json`,
|
|
310
|
+
defaults.maptilerApiURL
|
|
311
|
+
);
|
|
312
|
+
endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
|
|
313
|
+
if ("limit" in options) {
|
|
314
|
+
endpoint.searchParams.set("limit", options.limit.toString());
|
|
315
|
+
}
|
|
316
|
+
if ("transformations" in options) {
|
|
317
|
+
endpoint.searchParams.set(
|
|
318
|
+
"transformations",
|
|
319
|
+
options.transformations.toString()
|
|
395
320
|
);
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
const res = yield callFetch(urlWithParams);
|
|
411
|
-
if (!res.ok) {
|
|
412
|
-
throw new ServiceError(
|
|
413
|
-
res,
|
|
414
|
-
res.status in customMessages$1 ? customMessages$1[res.status] : ""
|
|
415
|
-
);
|
|
416
|
-
}
|
|
417
|
-
const obj = yield res.json();
|
|
418
|
-
return obj;
|
|
419
|
-
});
|
|
321
|
+
}
|
|
322
|
+
if ("exports" in options) {
|
|
323
|
+
endpoint.searchParams.set("exports", options.exports.toString());
|
|
324
|
+
}
|
|
325
|
+
const urlWithParams = endpoint.toString();
|
|
326
|
+
const res = await callFetch(urlWithParams);
|
|
327
|
+
if (!res.ok) {
|
|
328
|
+
throw new ServiceError(
|
|
329
|
+
res,
|
|
330
|
+
res.status in customMessages$2 ? customMessages$2[res.status] : ""
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
const obj = await res.json();
|
|
334
|
+
return obj;
|
|
420
335
|
}
|
|
421
|
-
function transform(
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
336
|
+
async function transform(positions, options = {}) {
|
|
337
|
+
const coordinatesStr = (Array.isArray(positions[0]) ? positions : [positions]).map((coord) => `${coord[0]},${coord[1]}`).join(";");
|
|
338
|
+
const endpoint = new URL(
|
|
339
|
+
`coordinates/transform/${coordinatesStr}.json`,
|
|
340
|
+
defaults.maptilerApiURL
|
|
341
|
+
);
|
|
342
|
+
endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
|
|
343
|
+
if ("sourceCrs" in options) {
|
|
344
|
+
endpoint.searchParams.set("s_srs", options.sourceCrs.toString());
|
|
345
|
+
}
|
|
346
|
+
if ("targetCrs" in options) {
|
|
347
|
+
endpoint.searchParams.set("t_srs", options.targetCrs.toString());
|
|
348
|
+
}
|
|
349
|
+
if ("operations" in options) {
|
|
350
|
+
endpoint.searchParams.set(
|
|
351
|
+
"ops",
|
|
352
|
+
(Array.isArray(options.operations) ? options.operations : [options.operations]).join("|")
|
|
428
353
|
);
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
);
|
|
441
|
-
}
|
|
442
|
-
const urlWithParams = endpoint.toString();
|
|
443
|
-
const res = yield callFetch(urlWithParams);
|
|
444
|
-
if (!res.ok) {
|
|
445
|
-
throw new ServiceError(
|
|
446
|
-
res,
|
|
447
|
-
res.status in customMessages$1 ? customMessages$1[res.status] : ""
|
|
448
|
-
);
|
|
449
|
-
}
|
|
450
|
-
const obj = yield res.json();
|
|
451
|
-
return obj;
|
|
452
|
-
});
|
|
354
|
+
}
|
|
355
|
+
const urlWithParams = endpoint.toString();
|
|
356
|
+
const res = await callFetch(urlWithParams);
|
|
357
|
+
if (!res.ok) {
|
|
358
|
+
throw new ServiceError(
|
|
359
|
+
res,
|
|
360
|
+
res.status in customMessages$2 ? customMessages$2[res.status] : ""
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
const obj = await res.json();
|
|
364
|
+
return obj;
|
|
453
365
|
}
|
|
454
366
|
const coordinates = {
|
|
455
367
|
search,
|
|
456
368
|
transform
|
|
457
369
|
};
|
|
458
370
|
|
|
459
|
-
|
|
460
|
-
return new Promise((resolve, reject) => {
|
|
461
|
-
var fulfilled = (value) => {
|
|
462
|
-
try {
|
|
463
|
-
step(generator.next(value));
|
|
464
|
-
} catch (e) {
|
|
465
|
-
reject(e);
|
|
466
|
-
}
|
|
467
|
-
};
|
|
468
|
-
var rejected = (value) => {
|
|
469
|
-
try {
|
|
470
|
-
step(generator.throw(value));
|
|
471
|
-
} catch (e) {
|
|
472
|
-
reject(e);
|
|
473
|
-
}
|
|
474
|
-
};
|
|
475
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
476
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
477
|
-
});
|
|
478
|
-
};
|
|
479
|
-
const customMessages = {
|
|
371
|
+
const customMessages$1 = {
|
|
480
372
|
403: "Key is missing, invalid or restricted"
|
|
481
373
|
};
|
|
482
|
-
function get(
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
374
|
+
async function get(dataId, options = {}) {
|
|
375
|
+
if (typeof dataId !== "string" || dataId.trim().length === 0) {
|
|
376
|
+
throw new Error("The data ID must be a non-empty string");
|
|
377
|
+
}
|
|
378
|
+
const endpoint = new URL(
|
|
379
|
+
`data/${encodeURIComponent(dataId)}/features.json`,
|
|
380
|
+
defaults.maptilerApiURL
|
|
381
|
+
);
|
|
382
|
+
endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
|
|
383
|
+
const urlWithParams = endpoint.toString();
|
|
384
|
+
const res = await callFetch(urlWithParams);
|
|
385
|
+
if (!res.ok) {
|
|
386
|
+
throw new ServiceError(
|
|
387
|
+
res,
|
|
388
|
+
res.status in customMessages$1 ? customMessages$1[res.status] : ""
|
|
491
389
|
);
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
if (!res.ok) {
|
|
496
|
-
throw new ServiceError(
|
|
497
|
-
res,
|
|
498
|
-
res.status in customMessages ? customMessages[res.status] : ""
|
|
499
|
-
);
|
|
500
|
-
}
|
|
501
|
-
const obj = yield res.json();
|
|
502
|
-
return obj;
|
|
503
|
-
});
|
|
390
|
+
}
|
|
391
|
+
const obj = await res.json();
|
|
392
|
+
return obj;
|
|
504
393
|
}
|
|
505
394
|
const data = {
|
|
506
395
|
get
|
|
@@ -1103,6 +992,36 @@ function styleToStyle(style) {
|
|
|
1103
992
|
}
|
|
1104
993
|
const MapStyle = buildMapStyles();
|
|
1105
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
|
+
}
|
|
1106
1025
|
function getSqSegDist(p, p1, p2) {
|
|
1107
1026
|
let x = p1[0], y = p1[1], dx = p2[0] - x, dy = p2[1] - y;
|
|
1108
1027
|
if (dx !== 0 || dy !== 0) {
|
|
@@ -1153,6 +1072,10 @@ function simplify(points, tolerance) {
|
|
|
1153
1072
|
const simplePoints = simplifyDouglasPeucker(points, sqTolerance);
|
|
1154
1073
|
return simplePoints;
|
|
1155
1074
|
}
|
|
1075
|
+
const misc = {
|
|
1076
|
+
extractLineStrings,
|
|
1077
|
+
simplify
|
|
1078
|
+
};
|
|
1156
1079
|
|
|
1157
1080
|
function staticMapMarkerToString(marker, includeColor = true) {
|
|
1158
1081
|
let str = `${marker[0]},${marker[1]}`;
|
|
@@ -1166,19 +1089,18 @@ function simplifyAndStringify(path, maxNbChar = 3e3) {
|
|
|
1166
1089
|
let tolerance = 5e-6;
|
|
1167
1090
|
const toleranceStep = 1e-5;
|
|
1168
1091
|
while (str.length > maxNbChar) {
|
|
1169
|
-
const simplerPath = simplify(path, tolerance);
|
|
1092
|
+
const simplerPath = misc.simplify(path, tolerance);
|
|
1170
1093
|
str = simplerPath.map((point) => `${point[0]},${point[1]}`).join("|");
|
|
1171
1094
|
tolerance += toleranceStep;
|
|
1172
1095
|
}
|
|
1173
1096
|
return str;
|
|
1174
1097
|
}
|
|
1175
1098
|
function centered(center, zoom, options = {}) {
|
|
1176
|
-
var _a, _b, _c, _d, _e;
|
|
1177
1099
|
const style = styleToStyle(options.style);
|
|
1178
1100
|
const scale = options.hiDPI ? "@2x" : "";
|
|
1179
|
-
const format =
|
|
1180
|
-
let width = ~~(
|
|
1181
|
-
let height = ~~(
|
|
1101
|
+
const format = options.format ?? "png";
|
|
1102
|
+
let width = ~~(options.width ?? 1024);
|
|
1103
|
+
let height = ~~(options.height ?? 1024);
|
|
1182
1104
|
if (options.hiDPI) {
|
|
1183
1105
|
width = ~~(width / 2);
|
|
1184
1106
|
height = ~~(height / 2);
|
|
@@ -1208,7 +1130,7 @@ function centered(center, zoom, options = {}) {
|
|
|
1208
1130
|
}
|
|
1209
1131
|
if ("path" in options) {
|
|
1210
1132
|
let pathStr = "";
|
|
1211
|
-
pathStr += `fill:${
|
|
1133
|
+
pathStr += `fill:${options.pathFillColor ?? "none"}|`;
|
|
1212
1134
|
if ("pathStrokeColor" in options) {
|
|
1213
1135
|
pathStr += `stroke:${options.pathStrokeColor}|`;
|
|
1214
1136
|
}
|
|
@@ -1219,16 +1141,15 @@ function centered(center, zoom, options = {}) {
|
|
|
1219
1141
|
pathStr += simplifyAndStringify(options.path);
|
|
1220
1142
|
endpoint.searchParams.set("path", pathStr);
|
|
1221
1143
|
}
|
|
1222
|
-
endpoint.searchParams.set("key",
|
|
1144
|
+
endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
|
|
1223
1145
|
return endpoint.toString();
|
|
1224
1146
|
}
|
|
1225
1147
|
function bounded(boundingBox, options = {}) {
|
|
1226
|
-
var _a, _b, _c, _d, _e;
|
|
1227
1148
|
const style = styleToStyle(options.style);
|
|
1228
1149
|
const scale = options.hiDPI ? "@2x" : "";
|
|
1229
|
-
const format =
|
|
1230
|
-
let width = ~~(
|
|
1231
|
-
let height = ~~(
|
|
1150
|
+
const format = options.format ?? "png";
|
|
1151
|
+
let width = ~~(options.width ?? 1024);
|
|
1152
|
+
let height = ~~(options.height ?? 1024);
|
|
1232
1153
|
if (options.hiDPI) {
|
|
1233
1154
|
width = ~~(width / 2);
|
|
1234
1155
|
height = ~~(height / 2);
|
|
@@ -1261,7 +1182,7 @@ function bounded(boundingBox, options = {}) {
|
|
|
1261
1182
|
}
|
|
1262
1183
|
if ("path" in options) {
|
|
1263
1184
|
let pathStr = "";
|
|
1264
|
-
pathStr += `fill:${
|
|
1185
|
+
pathStr += `fill:${options.pathFillColor ?? "none"}|`;
|
|
1265
1186
|
if ("pathStrokeColor" in options) {
|
|
1266
1187
|
pathStr += `stroke:${options.pathStrokeColor}|`;
|
|
1267
1188
|
}
|
|
@@ -1272,11 +1193,10 @@ function bounded(boundingBox, options = {}) {
|
|
|
1272
1193
|
pathStr += simplifyAndStringify(options.path);
|
|
1273
1194
|
endpoint.searchParams.set("path", pathStr);
|
|
1274
1195
|
}
|
|
1275
|
-
endpoint.searchParams.set("key",
|
|
1196
|
+
endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
|
|
1276
1197
|
return endpoint.toString();
|
|
1277
1198
|
}
|
|
1278
1199
|
function automatic(options = {}) {
|
|
1279
|
-
var _a, _b, _c, _d, _e;
|
|
1280
1200
|
if (!("markers" in options) && !("path" in options)) {
|
|
1281
1201
|
throw new Error(
|
|
1282
1202
|
"Automatic static maps require markers and/or path to be created."
|
|
@@ -1284,9 +1204,9 @@ function automatic(options = {}) {
|
|
|
1284
1204
|
}
|
|
1285
1205
|
const style = styleToStyle(options.style);
|
|
1286
1206
|
const scale = options.hiDPI ? "@2x" : "";
|
|
1287
|
-
const format =
|
|
1288
|
-
let width = ~~(
|
|
1289
|
-
let height = ~~(
|
|
1207
|
+
const format = options.format ?? "png";
|
|
1208
|
+
let width = ~~(options.width ?? 1024);
|
|
1209
|
+
let height = ~~(options.height ?? 1024);
|
|
1290
1210
|
if (options.hiDPI) {
|
|
1291
1211
|
width = ~~(width / 2);
|
|
1292
1212
|
height = ~~(height / 2);
|
|
@@ -1321,7 +1241,7 @@ function automatic(options = {}) {
|
|
|
1321
1241
|
}
|
|
1322
1242
|
if ("path" in options) {
|
|
1323
1243
|
let pathStr = "";
|
|
1324
|
-
pathStr += `fill:${
|
|
1244
|
+
pathStr += `fill:${options.pathFillColor ?? "none"}|`;
|
|
1325
1245
|
if ("pathStrokeColor" in options) {
|
|
1326
1246
|
pathStr += `stroke:${options.pathStrokeColor}|`;
|
|
1327
1247
|
}
|
|
@@ -1332,7 +1252,7 @@ function automatic(options = {}) {
|
|
|
1332
1252
|
pathStr += simplifyAndStringify(options.path);
|
|
1333
1253
|
endpoint.searchParams.set("path", pathStr);
|
|
1334
1254
|
}
|
|
1335
|
-
endpoint.searchParams.set("key",
|
|
1255
|
+
endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
|
|
1336
1256
|
return endpoint.toString();
|
|
1337
1257
|
}
|
|
1338
1258
|
const staticMaps = {
|
|
@@ -1341,5 +1261,333 @@ const staticMaps = {
|
|
|
1341
1261
|
automatic
|
|
1342
1262
|
};
|
|
1343
1263
|
|
|
1344
|
-
|
|
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])];
|
|
1288
|
+
}
|
|
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;
|
|
1296
|
+
}
|
|
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) {
|
|
1333
|
+
return radians * 180 / Math.PI;
|
|
1334
|
+
}
|
|
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)];
|
|
1350
|
+
}
|
|
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
|
+
let tileCache = null;
|
|
1372
|
+
function getTileCache() {
|
|
1373
|
+
if (!tileCache) {
|
|
1374
|
+
tileCache = new QuickLRU({ maxSize: config.tileCacheSize });
|
|
1375
|
+
}
|
|
1376
|
+
return tileCache;
|
|
1377
|
+
}
|
|
1378
|
+
async function bufferToPixelDataBrowser(buff) {
|
|
1379
|
+
const blob = new Blob([buff]);
|
|
1380
|
+
const imageBitmap = await createImageBitmap(blob);
|
|
1381
|
+
const canvas = document.createElement("canvas");
|
|
1382
|
+
const ctx = canvas.getContext("2d");
|
|
1383
|
+
canvas.width = imageBitmap.width;
|
|
1384
|
+
canvas.height = imageBitmap.height;
|
|
1385
|
+
ctx.drawImage(imageBitmap, 0, 0);
|
|
1386
|
+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
1387
|
+
return {
|
|
1388
|
+
pixels: imageData.data,
|
|
1389
|
+
width: canvas.width,
|
|
1390
|
+
height: canvas.height,
|
|
1391
|
+
components: imageData.data.length / (canvas.width * canvas.height)
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
function getBufferToPixelDataParser() {
|
|
1395
|
+
if (config.bufferToPixelData) {
|
|
1396
|
+
return config.bufferToPixelData;
|
|
1397
|
+
}
|
|
1398
|
+
if (typeof window !== "undefined") {
|
|
1399
|
+
return bufferToPixelDataBrowser;
|
|
1400
|
+
}
|
|
1401
|
+
throw new Error(
|
|
1402
|
+
"An image file buffer to pixel data parser is necessary. Specify it in `config.bufferToPixelData`"
|
|
1403
|
+
);
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
const terrainTileJsonURL = "tiles/terrain-rgb-v2/tiles.json";
|
|
1407
|
+
let terrainTileJson = null;
|
|
1408
|
+
const customMessages = {
|
|
1409
|
+
403: "Key is missing, invalid or restricted"
|
|
1410
|
+
};
|
|
1411
|
+
async function fetchTerrainTileJson(apiKey) {
|
|
1412
|
+
const endpoint = new URL(terrainTileJsonURL, defaults.maptilerApiURL);
|
|
1413
|
+
endpoint.searchParams.set("key", apiKey);
|
|
1414
|
+
const urlWithParams = endpoint.toString();
|
|
1415
|
+
const res = await callFetch(urlWithParams);
|
|
1416
|
+
if (res.ok) {
|
|
1417
|
+
terrainTileJson = await res.json();
|
|
1418
|
+
return terrainTileJson;
|
|
1419
|
+
} else {
|
|
1420
|
+
if (!res.ok) {
|
|
1421
|
+
throw new ServiceError(res, customMessages[res.status] ?? "");
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
async function at(position, options = {}) {
|
|
1426
|
+
const apiKey = options.apiKey ?? config.apiKey;
|
|
1427
|
+
if (!terrainTileJson) {
|
|
1428
|
+
await fetchTerrainTileJson(apiKey);
|
|
1429
|
+
}
|
|
1430
|
+
const maxZoom = terrainTileJson.maxzoom;
|
|
1431
|
+
let zoom = ~~(options.zoom ?? maxZoom);
|
|
1432
|
+
if (zoom > maxZoom || zoom < 0) {
|
|
1433
|
+
zoom = maxZoom;
|
|
1434
|
+
}
|
|
1435
|
+
const tileIndex = math.wgs84ToTileIndex(position, zoom, false);
|
|
1436
|
+
const tileX = ~~tileIndex[0];
|
|
1437
|
+
const tileY = ~~tileIndex[1];
|
|
1438
|
+
if (!terrainTileJson.tiles.length) {
|
|
1439
|
+
throw new Error("Terrain tileJSON tile list is empty.");
|
|
1440
|
+
}
|
|
1441
|
+
const tileID = `terrain_${zoom.toString()}_${tileX.toString()}_${tileY.toString()}`;
|
|
1442
|
+
let tilePixelData;
|
|
1443
|
+
const cache = getTileCache();
|
|
1444
|
+
if (cache.has(tileID)) {
|
|
1445
|
+
tilePixelData = cache.get(tileID);
|
|
1446
|
+
} else {
|
|
1447
|
+
const tileURL = terrainTileJson.tiles[0].replace("{x}", tileX.toString()).replace("{y}", tileY.toString()).replace("{z}", zoom.toString());
|
|
1448
|
+
const tileRes = await callFetch(tileURL);
|
|
1449
|
+
if (!tileRes.ok) {
|
|
1450
|
+
throw new ServiceError(tileRes, customMessages[tileRes.status] ?? "");
|
|
1451
|
+
}
|
|
1452
|
+
const tileBuff = await tileRes.arrayBuffer();
|
|
1453
|
+
const tileParser = getBufferToPixelDataParser();
|
|
1454
|
+
tilePixelData = await tileParser(tileBuff);
|
|
1455
|
+
cache.set(tileID, tilePixelData);
|
|
1456
|
+
}
|
|
1457
|
+
const pixelX = ~~(tilePixelData.width * (tileIndex[0] % 1));
|
|
1458
|
+
const pixelY = ~~(tilePixelData.height * (tileIndex[1] % 1));
|
|
1459
|
+
const pixelDataIndex = (pixelY * tilePixelData.width + pixelX) * tilePixelData.components;
|
|
1460
|
+
const R = tilePixelData.pixels[pixelDataIndex];
|
|
1461
|
+
const G = tilePixelData.pixels[pixelDataIndex + 1];
|
|
1462
|
+
const B = tilePixelData.pixels[pixelDataIndex + 2];
|
|
1463
|
+
const elevation2 = -1e4 + (R * 256 * 256 + G * 256 + B) * 0.1;
|
|
1464
|
+
return [position[0], position[1], elevation2];
|
|
1465
|
+
}
|
|
1466
|
+
async function batch(positions, options = {}) {
|
|
1467
|
+
const apiKey = options.apiKey ?? config.apiKey;
|
|
1468
|
+
if (!terrainTileJson) {
|
|
1469
|
+
await fetchTerrainTileJson(apiKey);
|
|
1470
|
+
}
|
|
1471
|
+
const tileParser = getBufferToPixelDataParser();
|
|
1472
|
+
const tileURLSchema = terrainTileJson.tiles[0];
|
|
1473
|
+
const cache = getTileCache();
|
|
1474
|
+
const maxZoom = terrainTileJson.maxzoom;
|
|
1475
|
+
let zoom = ~~(options.zoom ?? maxZoom);
|
|
1476
|
+
if (zoom > maxZoom || zoom < 0) {
|
|
1477
|
+
zoom = maxZoom;
|
|
1478
|
+
}
|
|
1479
|
+
const tileIndicesFloats = positions.map(
|
|
1480
|
+
(position) => math.wgs84ToTileIndex(position, zoom, false)
|
|
1481
|
+
);
|
|
1482
|
+
const tileIndicesInteger = tileIndicesFloats.map((index) => [
|
|
1483
|
+
~~index[0],
|
|
1484
|
+
~~index[1]
|
|
1485
|
+
]);
|
|
1486
|
+
const tileIDs = tileIndicesInteger.map(
|
|
1487
|
+
(index) => `terrain_${zoom.toString()}_${index[0].toString()}_${index[1].toString()}`
|
|
1488
|
+
);
|
|
1489
|
+
const uniqueTilesToFetch = Array.from(
|
|
1490
|
+
new Set(tileIDs.filter((tileID) => !cache.has(tileID)))
|
|
1491
|
+
).map((tileID) => tileID.split("_").slice(1));
|
|
1492
|
+
const tileURLs = uniqueTilesToFetch.map(
|
|
1493
|
+
(zxy) => tileURLSchema.replace("{x}", zxy[1].toString()).replace("{y}", zxy[2].toString()).replace("{z}", zxy[0].toString())
|
|
1494
|
+
);
|
|
1495
|
+
const promisesFetchTiles = tileURLs.map((url) => callFetch(url));
|
|
1496
|
+
const resTiles = await Promise.allSettled(promisesFetchTiles);
|
|
1497
|
+
const fulfilledRes = resTiles.map(
|
|
1498
|
+
(el) => el.status === "fulfilled" ? el.value : null
|
|
1499
|
+
).filter((res) => res);
|
|
1500
|
+
const fulfilledRButNotOkRes = fulfilledRes.filter((res) => !res.ok);
|
|
1501
|
+
if (fulfilledRes.length !== promisesFetchTiles.length) {
|
|
1502
|
+
throw new Error("Some tiles could not be fetched.");
|
|
1503
|
+
}
|
|
1504
|
+
if (fulfilledRButNotOkRes.length) {
|
|
1505
|
+
throw new ServiceError(
|
|
1506
|
+
fulfilledRButNotOkRes[0],
|
|
1507
|
+
customMessages[fulfilledRButNotOkRes[0].status] ?? ""
|
|
1508
|
+
);
|
|
1509
|
+
}
|
|
1510
|
+
const tileArrayBuffers = await Promise.all(
|
|
1511
|
+
fulfilledRes.map((res) => res.arrayBuffer())
|
|
1512
|
+
);
|
|
1513
|
+
if (!tileArrayBuffers.every((buff) => buff.byteLength > 0)) {
|
|
1514
|
+
throw new Error("Some tiles are not available.");
|
|
1515
|
+
}
|
|
1516
|
+
const tilePixelDatas = await Promise.all(
|
|
1517
|
+
tileArrayBuffers.map((buff) => tileParser(buff))
|
|
1518
|
+
);
|
|
1519
|
+
tilePixelDatas.forEach((tilePixelData, i) => {
|
|
1520
|
+
const zxy = uniqueTilesToFetch[i];
|
|
1521
|
+
const tileID = `terrain_${zxy[0].toString()}_${zxy[1].toString()}_${zxy[2].toString()}`;
|
|
1522
|
+
cache.set(tileID, tilePixelData);
|
|
1523
|
+
});
|
|
1524
|
+
const elevatedPositions = positions.map((position, i) => {
|
|
1525
|
+
const tileID = tileIDs[i];
|
|
1526
|
+
const tileIndexFloat = tileIndicesFloats[i];
|
|
1527
|
+
const tilePixelData = cache.get(tileID);
|
|
1528
|
+
const pixelX = Math.min(
|
|
1529
|
+
Math.round(tilePixelData.width * (tileIndexFloat[0] % 1)),
|
|
1530
|
+
tilePixelData.width - 1
|
|
1531
|
+
);
|
|
1532
|
+
const pixelY = Math.min(
|
|
1533
|
+
Math.round(tilePixelData.height * (tileIndexFloat[1] % 1)),
|
|
1534
|
+
tilePixelData.height - 1
|
|
1535
|
+
);
|
|
1536
|
+
const pixelDataIndex = (pixelY * tilePixelData.width + pixelX) * tilePixelData.components;
|
|
1537
|
+
const R = tilePixelData.pixels[pixelDataIndex];
|
|
1538
|
+
const G = tilePixelData.pixels[pixelDataIndex + 1];
|
|
1539
|
+
const B = tilePixelData.pixels[pixelDataIndex + 2];
|
|
1540
|
+
const elevation2 = -1e4 + (R * 256 * 256 + G * 256 + B) * 0.1;
|
|
1541
|
+
return [position[0], position[1], ~~(elevation2 * 1e3) / 1e3];
|
|
1542
|
+
});
|
|
1543
|
+
if (options.smoothingKernelSize) {
|
|
1544
|
+
const kernelSize = ~~(options.smoothingKernelSize / 2) * 2 + 1;
|
|
1545
|
+
const elevations = elevatedPositions.map((pos) => pos[2]);
|
|
1546
|
+
const kernelSpan = ~~(kernelSize / 2);
|
|
1547
|
+
for (let i = kernelSpan; i < elevations.length - kernelSpan - 1; i += 1) {
|
|
1548
|
+
let sum = 0;
|
|
1549
|
+
for (let j = 0; j < kernelSize; j += 1) {
|
|
1550
|
+
const elev = elevations[i - kernelSpan + j];
|
|
1551
|
+
sum += elev;
|
|
1552
|
+
}
|
|
1553
|
+
sum /= kernelSize;
|
|
1554
|
+
elevatedPositions[i][2] = sum;
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
return elevatedPositions;
|
|
1558
|
+
}
|
|
1559
|
+
async function fromLineString(ls, options = {}) {
|
|
1560
|
+
if (ls.type !== "LineString") {
|
|
1561
|
+
throw new Error("The provided object is not a GeoJSON LineString");
|
|
1562
|
+
}
|
|
1563
|
+
const clone = structuredClone(ls);
|
|
1564
|
+
const elevatedPositions = await batch(clone.coordinates, options);
|
|
1565
|
+
clone.coordinates = elevatedPositions;
|
|
1566
|
+
return clone;
|
|
1567
|
+
}
|
|
1568
|
+
async function fromMultiLineString(ls, options = {}) {
|
|
1569
|
+
if (ls.type !== "MultiLineString") {
|
|
1570
|
+
throw new Error("The provided object is not a GeoJSON MultiLineString");
|
|
1571
|
+
}
|
|
1572
|
+
const clone = structuredClone(ls);
|
|
1573
|
+
const multiLengths = clone.coordinates.map((poss) => poss.length);
|
|
1574
|
+
const flattenPositions = clone.coordinates.flat();
|
|
1575
|
+
const flattenPositionsElevated = await batch(flattenPositions, options);
|
|
1576
|
+
const result = [];
|
|
1577
|
+
let index = 0;
|
|
1578
|
+
for (const length of multiLengths) {
|
|
1579
|
+
result.push(flattenPositionsElevated.slice(index, index + length));
|
|
1580
|
+
index += length;
|
|
1581
|
+
}
|
|
1582
|
+
clone.coordinates = result;
|
|
1583
|
+
return clone;
|
|
1584
|
+
}
|
|
1585
|
+
const elevation = {
|
|
1586
|
+
at,
|
|
1587
|
+
batch,
|
|
1588
|
+
fromLineString,
|
|
1589
|
+
fromMultiLineString
|
|
1590
|
+
};
|
|
1591
|
+
|
|
1592
|
+
export { ClientConfig, LanguageGeocoding, MapStyle, MapStyleVariant, ReferenceMapStyle, ServiceError, bufferToPixelDataBrowser, circumferenceAtLatitude, config, coordinates, data, elevation, expandMapStyle, geocoding, geolocation, getAutoLanguageGeocoding, getBufferToPixelDataParser, getTileCache, mapStylePresetList, math, misc, staticMaps, styleToStyle };
|
|
1345
1593
|
//# sourceMappingURL=maptiler-client.mjs.map
|