@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.
Files changed (118) hide show
  1. package/.github/pull_request_template.md +11 -0
  2. package/.github/workflows/format-lint.yml +24 -0
  3. package/CHANGELOG.md +25 -0
  4. package/dist/maptiler-client.cjs +587 -332
  5. package/dist/maptiler-client.cjs.map +1 -1
  6. package/dist/maptiler-client.d.ts +291 -12
  7. package/dist/maptiler-client.min.mjs +1 -1
  8. package/dist/maptiler-client.mjs +580 -332
  9. package/dist/maptiler-client.mjs.map +1 -1
  10. package/package.json +29 -20
  11. package/readme.md +134 -2
  12. package/rollup.config.js +3 -3
  13. package/tsconfig.json +1 -1
  14. package/dist/maptiler-client.umd.js +0 -1368
  15. package/dist/maptiler-client.umd.js.map +0 -1
  16. package/dist/maptiler-client.umd.min.js +0 -1
  17. package/docs/.nojekyll +0 -1
  18. package/docs/assets/custom.css +0 -118
  19. package/docs/assets/highlight.css +0 -85
  20. package/docs/assets/main.js +0 -58
  21. package/docs/assets/search.js +0 -1
  22. package/docs/assets/style.css +0 -1280
  23. package/docs/classes/config.ClientConfig.html +0 -132
  24. package/docs/classes/services_ServiceError.ServiceError.html +0 -103
  25. package/docs/functions/language.getAutoLanguageGeocoding.html +0 -60
  26. package/docs/functions/services_simplify.default.html +0 -65
  27. package/docs/images/JS-logo.svg +0 -4
  28. package/docs/images/TS-logo.svg +0 -6
  29. package/docs/images/maptiler-client-logo.afdesign +0 -0
  30. package/docs/images/maptiler-client-logo.svg +0 -75
  31. package/docs/images/maptiler-logo-256.png +0 -0
  32. package/docs/images/maptiler-logo.svg +0 -23
  33. package/docs/images/screenshots/static-bounded-europe-1024.png +0 -0
  34. package/docs/images/screenshots/static-bounded-europe-2048.png +0 -0
  35. package/docs/images/screenshots/static-bounded-portugal-1024x2048.png +0 -0
  36. package/docs/images/screenshots/static-bounded-portugal-2048x2048.png +0 -0
  37. package/docs/images/screenshots/static-with-path.png +0 -0
  38. package/docs/index.html +0 -340
  39. package/docs/modules/config.html +0 -71
  40. package/docs/modules/language.html +0 -71
  41. package/docs/modules/services_ServiceError.html +0 -61
  42. package/docs/modules/services_coordinates.html +0 -84
  43. package/docs/modules/services_data.html +0 -66
  44. package/docs/modules/services_geocoding.html +0 -82
  45. package/docs/modules/services_geolocation.html +0 -68
  46. package/docs/modules/services_simplify.html +0 -61
  47. package/docs/modules/services_staticMaps.html +0 -74
  48. package/docs/modules.html +0 -59
  49. package/docs/types/config.FetchFunction.html +0 -71
  50. package/docs/types/language.LanguageGeocodingString.html +0 -58
  51. package/docs/types/services_coordinates.CoordinateExport.html +0 -71
  52. package/docs/types/services_coordinates.CoordinateGrid.html +0 -69
  53. package/docs/types/services_coordinates.CoordinateId.html +0 -71
  54. package/docs/types/services_coordinates.CoordinateSearch.html +0 -93
  55. package/docs/types/services_coordinates.CoordinateSearchResult.html +0 -75
  56. package/docs/types/services_coordinates.CoordinateTransformResult.html +0 -76
  57. package/docs/types/services_coordinates.CoordinateTransformation.html +0 -89
  58. package/docs/types/services_coordinates.CoordinatesSearchOptions.html +0 -83
  59. package/docs/types/services_coordinates.CoordinatesTransformOptions.html +0 -85
  60. package/docs/types/services_coordinates.XYZ.html +0 -73
  61. package/docs/types/services_data.GetDataOptions.html +0 -64
  62. package/docs/types/services_geocoding.ByIdGeocodingOptions.html +0 -63
  63. package/docs/types/services_geocoding.CommonForwardAndReverseGeocodingOptions.html +0 -63
  64. package/docs/types/services_geocoding.Coordinates.html +0 -63
  65. package/docs/types/services_geocoding.FeatureHierarchy.html +0 -63
  66. package/docs/types/services_geocoding.GeocodingFeature.html +0 -63
  67. package/docs/types/services_geocoding.GeocodingOptions.html +0 -63
  68. package/docs/types/services_geocoding.GeocodingSearchResult.html +0 -80
  69. package/docs/types/services_geocoding.LanguageGeocodingOptions.html +0 -70
  70. package/docs/types/services_geocoding.ReverseGeocodingOptions.html +0 -63
  71. package/docs/types/services_geolocation.GeolocationInfoOptions.html +0 -65
  72. package/docs/types/services_geolocation.GeolocationResult.html +0 -128
  73. package/docs/types/services_staticMaps.AutomaticStaticMapOptions.html +0 -61
  74. package/docs/types/services_staticMaps.BoundedStaticMapOptions.html +0 -61
  75. package/docs/types/services_staticMaps.CenteredStaticMapOptions.html +0 -61
  76. package/docs/types/services_staticMaps.StaticMapBaseOptions.html +0 -157
  77. package/docs/types/services_staticMaps.StaticMapMarker.html +0 -61
  78. package/docs/variables/config.config.html +0 -58
  79. package/docs/variables/language.LanguageGeocoding.html +0 -171
  80. package/docs/variables/services_coordinates.coordinates.html +0 -110
  81. package/docs/variables/services_data.data.html +0 -80
  82. package/docs/variables/services_geocoding.geocoding.html +0 -265
  83. package/docs/variables/services_geolocation.geolocation.html +0 -79
  84. package/docs/variables/services_staticMaps.staticMaps.html +0 -128
  85. package/docsmd/.nojekyll +0 -1
  86. package/docsmd/README.md +0 -873
  87. package/docsmd/assets/custom.css +0 -118
  88. package/docsmd/classes/ClientConfig.md +0 -90
  89. package/docsmd/classes/MapStyleVariant.md +0 -234
  90. package/docsmd/classes/ReferenceMapStyle.md +0 -169
  91. package/docsmd/classes/ServiceError.md +0 -52
  92. package/examples/test-browser-es.html +0 -103
  93. package/examples/test-browser-umd.html +0 -90
  94. package/examples/test-node.js +0 -52
  95. package/images/JS-logo.svg +0 -4
  96. package/images/TS-logo.svg +0 -6
  97. package/images/maptiler-client-logo.afdesign +0 -0
  98. package/images/maptiler-client-logo.svg +0 -75
  99. package/images/maptiler-logo-256.png +0 -0
  100. package/images/maptiler-logo.svg +0 -23
  101. package/images/screenshots/static-bounded-europe-1024.png +0 -0
  102. package/images/screenshots/static-bounded-europe-2048.png +0 -0
  103. package/images/screenshots/static-bounded-portugal-1024x2048.png +0 -0
  104. package/images/screenshots/static-bounded-portugal-2048x2048.png +0 -0
  105. package/images/screenshots/static-with-path.png +0 -0
  106. package/src/callFetch.ts +0 -18
  107. package/src/config.ts +0 -65
  108. package/src/defaults.ts +0 -11
  109. package/src/index.ts +0 -111
  110. package/src/language.ts +0 -87
  111. package/src/mapstyle.ts +0 -968
  112. package/src/services/ServiceError.ts +0 -10
  113. package/src/services/coordinates.ts +0 -258
  114. package/src/services/data.ts +0 -63
  115. package/src/services/geocoding.ts +0 -458
  116. package/src/services/geolocation.ts +0 -139
  117. package/src/services/simplify.ts +0 -87
  118. package/src/services/staticMaps.ts +0 -463
@@ -1,458 +0,0 @@
1
- import { BBox, Feature, Geometry, Position } from "geojson";
2
- import { callFetch } from "../callFetch";
3
- import { config } from "../config";
4
- import { defaults } from "../defaults";
5
-
6
- import {
7
- getAutoLanguageGeocoding,
8
- LanguageGeocoding,
9
- LanguageGeocodingString,
10
- } from "../language";
11
- import { ServiceError } from "./ServiceError";
12
-
13
- const customMessages = {
14
- 400: "Query too long / Invalid parameters",
15
- 403: "Key is missing, invalid or restricted",
16
- };
17
-
18
- export type LanguageGeocodingOptions = {
19
- /**
20
- * Prefer results in specific language. It’s possible to specify multiple values.
21
- */
22
- language?: LanguageGeocodingString | Array<LanguageGeocodingString>;
23
- };
24
-
25
- export type CommonForwardAndReverseGeocodingOptions =
26
- LanguageGeocodingOptions & {
27
- /**
28
- * Custom MapTiler Cloud API key to use instead of the one in global `config`
29
- */
30
- apiKey?: string;
31
-
32
- /**
33
- * Maximum number of results to show. Must be between 1 and 10. Default is 5 for forward and 1 for reverse geocoding.
34
- */
35
- limit?: number;
36
-
37
- /**
38
- * Filter of feature types to return. If not specified, all available feature types are returned.
39
- */
40
- types?: (
41
- | "country"
42
- | "region"
43
- | "subregion"
44
- | "county"
45
- | "joint_municipality"
46
- | "joint_submunicipality"
47
- | "municipality"
48
- | "municipal_district"
49
- | "locality"
50
- | "neighbourhood"
51
- | "place"
52
- | "postal_code"
53
- | "address"
54
- | "poi"
55
- )[];
56
- };
57
-
58
- export type GeocodingOptions = CommonForwardAndReverseGeocodingOptions & {
59
- /**
60
- * Only search for results in the specified area.
61
- */
62
- bbox?: BBox;
63
-
64
- /**
65
- * Prefer results close to a specific location.
66
- */
67
- proximity?: Position;
68
-
69
- /**
70
- * Limit search to specific country/countries specified as list of Alpha-2 ISO 3166-1 codes.
71
- */
72
- country?: string[];
73
-
74
- /**
75
- * Set to `false` to disable fuzzy (typo-tolerant) search. Default is `true`.
76
- */
77
- fuzzyMatch?: boolean;
78
-
79
- /**
80
- * Set to `true` to use autocomplete, `false` to disable it.
81
- * Default (`undefined`) is to combine autocomplete with non-autocomplete results.
82
- */
83
- autocomplete?: boolean;
84
- };
85
-
86
- export type ReverseGeocodingOptions = CommonForwardAndReverseGeocodingOptions;
87
-
88
- export type ByIdGeocodingOptions = LanguageGeocodingOptions;
89
-
90
- export type Coordinates = Position;
91
-
92
- type FeatureProperties = {
93
- /**
94
- * External reference of the feature used for debugging purposes
95
- */
96
- ref: string;
97
-
98
- /**
99
- * ISO 3166-1 alpha-2 country code of the feature
100
- */
101
- country_code: string;
102
-
103
- /**
104
- * (experimental) Kind of the feature
105
- */
106
- kind?:
107
- | "road"
108
- | "road_relation"
109
- | "admin_area"
110
- | "place"
111
- | "street"
112
- | "virtual_street";
113
-
114
- /**
115
- * (experimental) Value of place=* tag from OpenStreetMap feature if kind=place
116
- */
117
- "osm:place_type"?: string;
118
-
119
- /**
120
- * (experimental) Feature tags from OpenStreetMap. Only available for `poi` type.
121
- */
122
- "osm:tags"?: Record<string, string>;
123
-
124
- /**
125
- * Array of POI categories. Only available for `poi` type.
126
- */
127
- categories?: string[];
128
- };
129
-
130
- type FeatureBase = {
131
- /**
132
- * Unique feature ID
133
- */
134
- id: string;
135
-
136
- /**
137
- * Localized feature name
138
- */
139
- text: string;
140
-
141
- /**
142
- * Query's primary ISO 639-1 language code
143
- */
144
- language?: string;
145
-
146
- /**
147
- * A string analogous to the `text` field that matches the query in the requested language.
148
- * This field is only returned when multiple languages are requested using the `language` parameter, and will be present for each requested language.
149
- */
150
- [text: `text_${string}`]: string;
151
-
152
- /**
153
- * A ISO 639-1 query's fallback language code.
154
- * This field is only returned when multiple languages are requested using the `language` parameter, and will be present for each requested language.
155
- */
156
- [language: `language_${string}`]: string;
157
- };
158
-
159
- export type FeatureHierarchy = FeatureProperties & FeatureBase;
160
-
161
- export type GeocodingFeature = Feature<Geometry, FeatureProperties> &
162
- FeatureBase & {
163
- /**
164
- * Bounding box of the original feature as [w, s, e, n] array
165
- */
166
- bbox: BBox;
167
-
168
- /**
169
- * A [lon, lat] array of the original feature centeroid
170
- */
171
- center: Coordinates;
172
-
173
- /**
174
- * Formatted (including the hierarchy) and localized feature full name
175
- */
176
- place_name: string;
177
-
178
- /**
179
- * A string analogous to the `place_name` field that matches the query in the requested language.
180
- * This field is only returned when multiple languages are requested using the `language` parameter, and will be present for each requested language.
181
- */
182
- [key: `place_name_${string}`]: string;
183
-
184
- /**
185
- * An array of feature types describing the feature.
186
- * Currently each feature has only single type but this may change in the future.
187
- */
188
- place_type: string[];
189
-
190
- /**
191
- * Localized type of the place name, matches `place_type` property
192
- */
193
- place_type_name: string[];
194
-
195
- /**
196
- * Feature hierarchy
197
- */
198
- context?: Array<FeatureHierarchy>;
199
-
200
- /**
201
- * Address number, if applicable
202
- */
203
- address?: string;
204
-
205
- /**
206
- * Indicates how well the returned feature matches the user's query on a scale from 0 to 1.
207
- * 0 means the result does not match the query text at all, while 1 means the result fully matches the query text.
208
- * You can use the relevance property to remove results that don't fully match the query.
209
- */
210
- relevance: number;
211
- };
212
-
213
- export type GeocodingSearchResult = {
214
- type: "FeatureCollection";
215
-
216
- /**
217
- * Array of features found
218
- */
219
- features: Array<GeocodingFeature>;
220
-
221
- /**
222
- * Tokenized search query
223
- */
224
- query: Array<string>;
225
-
226
- /**
227
- * Attribution of the result
228
- */
229
- attribution: string;
230
- };
231
-
232
- function addLanguageGeocodingOptions(
233
- searchParams: URLSearchParams,
234
- options: LanguageGeocodingOptions
235
- ) {
236
- if (options.language == undefined) {
237
- return;
238
- }
239
-
240
- const languages = Array.from(
241
- new Set(
242
- (Array.isArray(options.language)
243
- ? options.language
244
- : [options.language]
245
- ).map((lang) =>
246
- lang === LanguageGeocoding.AUTO ? getAutoLanguageGeocoding() : lang
247
- )
248
- )
249
- ).join(",");
250
-
251
- searchParams.set("language", languages);
252
- }
253
-
254
- function addCommonForwardAndReverseGeocodingOptions(
255
- searchParams: URLSearchParams,
256
- options: CommonForwardAndReverseGeocodingOptions
257
- ) {
258
- searchParams.set("key", options.apiKey ?? config.apiKey);
259
-
260
- if (options.limit != undefined) {
261
- searchParams.set("limit", String(options.limit));
262
- }
263
-
264
- if (options.types != undefined) {
265
- searchParams.set("types", options.types.join(","));
266
- }
267
-
268
- addLanguageGeocodingOptions(searchParams, options);
269
- }
270
-
271
- function addForwardGeocodingOptions(
272
- searchParams: URLSearchParams,
273
- options: GeocodingOptions
274
- ) {
275
- addCommonForwardAndReverseGeocodingOptions(searchParams, options);
276
-
277
- if (options.bbox != undefined) {
278
- searchParams.set("bbox", options.bbox.join(","));
279
- }
280
-
281
- if (options.proximity != undefined) {
282
- searchParams.set("proximity", options.proximity.join(","));
283
- }
284
-
285
- if (options.country != undefined) {
286
- searchParams.set("country", options.country.join(","));
287
- }
288
-
289
- if (options.fuzzyMatch != undefined) {
290
- searchParams.set("fuzzyMatch", options.fuzzyMatch ? "true" : "false");
291
- }
292
-
293
- if (options.autocomplete != undefined) {
294
- searchParams.set("autocomplete", options.autocomplete ? "true" : "false");
295
- }
296
- }
297
-
298
- /**
299
- * Performs a forward geocoding query to MapTiler API.
300
- * Providing a human readable place name (of a city, country, street, etc.), the function returns
301
- * a list of candidate locations including longitude and latitude.
302
- * Learn more on the MapTiler API reference page: https://docs.maptiler.com/cloud/api/geocoding/#search-by-name-forward
303
- * @param query
304
- * @param options
305
- * @returns
306
- */
307
- async function forward(
308
- query: string,
309
- options: GeocodingOptions = {}
310
- ): Promise<GeocodingSearchResult> {
311
- if (typeof query !== "string" || query.trim().length === 0) {
312
- throw new Error("The query must be a non-empty string");
313
- }
314
-
315
- const endpoint = new URL(
316
- `geocoding/${encodeURIComponent(query)}.json`,
317
- defaults.maptilerApiURL
318
- );
319
-
320
- const { searchParams } = endpoint;
321
-
322
- addForwardGeocodingOptions(searchParams, options);
323
-
324
- const urlWithParams = endpoint.toString();
325
-
326
- const res = await callFetch(urlWithParams);
327
-
328
- if (!res.ok) {
329
- throw new ServiceError(res, customMessages[res.status] ?? "");
330
- }
331
-
332
- const obj: GeocodingSearchResult = await res.json();
333
-
334
- return obj;
335
- }
336
-
337
- /**
338
- * Perform a reverse geocoding query to MapTiler API.
339
- * Providing a longitude and latitude, this function returns a set of human readable information about this place (country, city, street, etc.)
340
- * Learn more on the MapTiler API reference page: https://docs.maptiler.com/cloud/api/geocoding/#search-by-coordinates-reverse
341
- * @param position
342
- * @param options
343
- * @returns
344
- */
345
- async function reverse(
346
- position: Position,
347
- options: ReverseGeocodingOptions = {}
348
- ): Promise<GeocodingSearchResult> {
349
- if (!Array.isArray(position) || position.length < 2) {
350
- throw new Error("The position must be an array of form [lng, lat].");
351
- }
352
-
353
- const endpoint = new URL(
354
- `geocoding/${position[0]},${position[1]}.json`,
355
- defaults.maptilerApiURL
356
- );
357
-
358
- addCommonForwardAndReverseGeocodingOptions(endpoint.searchParams, options);
359
-
360
- const urlWithParams = endpoint.toString();
361
-
362
- const res = await callFetch(urlWithParams);
363
-
364
- if (!res.ok) {
365
- throw new ServiceError(res, customMessages[res.status] ?? "");
366
- }
367
-
368
- const obj: GeocodingSearchResult = await res.json();
369
-
370
- return obj;
371
- }
372
-
373
- /**
374
- * Perform a geocoding query to MapTiler API to obtain fature by its ID.
375
- * Providing a feature ID, this function returns a feature which includes its full geometry.
376
- * Note that the feature ID is not stable and it changes when the database is re-indexed.
377
- * Learn more on the MapTiler API reference page: https://docs.maptiler.com/cloud/api/geocoding/#search-by-feature-id
378
- * @param id
379
- * @param options
380
- * @returns
381
- */
382
- async function byId(
383
- id: string,
384
- options: ByIdGeocodingOptions = {}
385
- ): Promise<GeocodingSearchResult> {
386
- const endpoint = new URL(`geocoding/${id}.json`, defaults.maptilerApiURL);
387
-
388
- addLanguageGeocodingOptions(endpoint.searchParams, options);
389
-
390
- const urlWithParams = endpoint.toString();
391
-
392
- const res = await callFetch(urlWithParams);
393
-
394
- if (!res.ok) {
395
- throw new ServiceError(res, customMessages[res.status] ?? "");
396
- }
397
-
398
- const obj: GeocodingSearchResult = await res.json();
399
-
400
- return obj;
401
- }
402
-
403
- /**
404
- * Perform a batch geocoding query to MapTiler API.
405
- * Provide multiple queries in the array. Each query can be forward, reverse or by feature ID.
406
- * Learn more on the MapTiler API reference page: https://docs.maptiler.com/cloud/api/geocoding/#batch-geocoding
407
- * @param queries
408
- * @param options
409
- * @returns
410
- */
411
- async function batch(
412
- queries: string[],
413
- options: GeocodingOptions = {}
414
- ): Promise<GeocodingSearchResult[]> {
415
- if (!queries.length) {
416
- return [];
417
- }
418
-
419
- const joinedQuery = queries
420
- .map((query) => encodeURIComponent(query))
421
- .join(";");
422
-
423
- const endpoint = new URL(
424
- `geocoding/${joinedQuery}.json`,
425
- defaults.maptilerApiURL
426
- );
427
-
428
- const { searchParams } = endpoint;
429
-
430
- addForwardGeocodingOptions(searchParams, options);
431
-
432
- const urlWithParams = endpoint.toString();
433
-
434
- const res = await callFetch(urlWithParams);
435
-
436
- if (!res.ok) {
437
- throw new ServiceError(res, customMessages[res.status] ?? "");
438
- }
439
-
440
- const obj = await res.json();
441
-
442
- return queries.length === 1 ? [obj] : obj;
443
- }
444
-
445
- /**
446
- * The **geocoding** namespace contains asynchronous functions to call the [MapTiler Geocoding API](https://docs.maptiler.com/cloud/api/geocoding/).
447
- * The **Geocoding API** provides ways to get geographic coordinates from a human-readable search query of a place (forward geocoding)
448
- * and to get the location details (country, city, street, etc.) from a geographic coordinate (reverse geocoding);
449
- */
450
- const geocoding = {
451
- forward,
452
- reverse,
453
- byId,
454
- batch,
455
- language: LanguageGeocoding,
456
- };
457
-
458
- export { geocoding };
@@ -1,139 +0,0 @@
1
- import { BBox } from "geojson";
2
- import { callFetch } from "../callFetch";
3
- import { config } from "../config";
4
- import { defaults } from "../defaults";
5
- import { ServiceError } from "./ServiceError";
6
-
7
- const customMessages = {
8
- 403: "Key is missing, invalid or restricted",
9
- };
10
-
11
- /**
12
- * Options that can be provided to get user data.
13
- */
14
- export type GeolocationInfoOptions = {
15
- /**
16
- * Custom MapTiler Cloud API key to use instead of the one in global `config`
17
- */
18
- apiKey?: string;
19
- };
20
-
21
- export type GeolocationResult = {
22
- /**
23
- * Name of the country
24
- * Example: Switzerland
25
- */
26
- country?: string;
27
-
28
- /**
29
- * Two-letter code of the country ISO 3166-1 alpha-2 codes
30
- * Example: CH
31
- */
32
- country_code?: string;
33
-
34
- /**
35
- * Bounds of the country in WGS84 degrees [west, south, east, north].
36
- * Example: [5.95538,45.818852,10.490936,47.809357]
37
- */
38
- country_bounds?: BBox;
39
-
40
- /**
41
- * Official country languages in ISO 639-1 format. ISO 639-1 codes
42
- * Example: ["de","fr","it"]
43
- */
44
- country_languages?: Array<string>;
45
-
46
- /**
47
- * Name of the continent
48
- * Example: Europe
49
- */
50
- continent?: string;
51
-
52
- /**
53
- * Two-letter code of the continent
54
- * Example: EU
55
- */
56
- continent_code?: string;
57
-
58
- /**
59
- * Indicated whether the country is part of the European Union.
60
- */
61
- eu?: boolean;
62
-
63
- /**
64
- * Name of the city
65
- * Example: Zurich
66
- */
67
- city?: string;
68
-
69
- /**
70
- * Latitude of the location
71
- * Example: 47.36667
72
- */
73
- latitude?: number;
74
-
75
- /**
76
- * Longitude of the location
77
- * Example: 8.55
78
- */
79
- longitude?: number;
80
-
81
- /**
82
- * Postal code
83
- * Example: 8000
84
- */
85
- postal?: string;
86
-
87
- /**
88
- * If known, the ISO 3166-2 name for the first level region. ISO 3166-2 codes
89
- * Example: Zurich
90
- */
91
- region?: string;
92
-
93
- /**
94
- * If known, the ISO 3166-2 code for the first level region. ISO 3166-2 codes
95
- * Example: ZH
96
- */
97
- region_code?: string;
98
-
99
- /**
100
- * Name of the timezone
101
- * Example: Europe/Zurich
102
- */
103
- timezone?: string;
104
- };
105
-
106
- /**
107
- * Looks up geolocation details from IP address using MapTiler API.
108
- * Learn more on the MapTiler API reference page: https://docs.maptiler.com/cloud/api/geolocation/#ip-geolocation
109
- * @returns
110
- */
111
- async function info(
112
- options: GeolocationInfoOptions = {}
113
- ): Promise<GeolocationResult> {
114
- const endpoint = new URL(`geolocation/ip.json`, defaults.maptilerApiURL);
115
- endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
116
- const urlWithParams = endpoint.toString();
117
-
118
- const res = await callFetch(urlWithParams);
119
-
120
- if (!res.ok) {
121
- throw new ServiceError(
122
- res,
123
- res.status in customMessages ? customMessages[res.status] : ""
124
- );
125
- }
126
-
127
- const obj = await res.json();
128
- return obj as GeolocationResult;
129
- }
130
-
131
- /**
132
- * The **geolocation** namespace contains an asynchronous function to call the [MapTiler Geolocation API](https://docs.maptiler.com/cloud/api/geolocation/).
133
- * The **Geolocation API** provides a way to retrieve the IP address as well as geographic informations of a machine performing the query (most likely: a user)
134
- */
135
- const geolocation = {
136
- info,
137
- };
138
-
139
- export { geolocation };
@@ -1,87 +0,0 @@
1
- /**\
2
- * This code is borrowed from https://github.com/mourner/simplify-js and reworked a little.
3
- */
4
-
5
- import { Position } from "geojson";
6
-
7
- // square distance from a point to a segment
8
- function getSqSegDist(p: Position, p1: Position, p2: Position): number {
9
- let x = p1[0],
10
- y = p1[1],
11
- dx = p2[0] - x,
12
- dy = p2[1] - y;
13
-
14
- if (dx !== 0 || dy !== 0) {
15
- const t = ((p[0] - x) * dx + (p[1] - y) * dy) / (dx * dx + dy * dy);
16
-
17
- if (t > 1) {
18
- x = p2[0];
19
- y = p2[1];
20
- } else if (t > 0) {
21
- x += dx * t;
22
- y += dy * t;
23
- }
24
- }
25
-
26
- dx = p[0] - x;
27
- dy = p[1] - y;
28
-
29
- return dx * dx + dy * dy;
30
- }
31
-
32
- function simplifyDPStep(
33
- points: Array<Position>,
34
- first: number,
35
- last: number,
36
- sqTolerance: number,
37
- simplified: Array<Position>
38
- ) {
39
- let maxSqDist = sqTolerance,
40
- index;
41
-
42
- for (let i = first + 1; i < last; i++) {
43
- const sqDist = getSqSegDist(points[i], points[first], points[last]);
44
-
45
- if (sqDist > maxSqDist) {
46
- index = i;
47
- maxSqDist = sqDist;
48
- }
49
- }
50
-
51
- if (maxSqDist > sqTolerance) {
52
- if (index - first > 1) {
53
- simplifyDPStep(points, first, index, sqTolerance, simplified);
54
- }
55
- simplified.push(points[index]);
56
-
57
- if (last - index > 1) {
58
- simplifyDPStep(points, index, last, sqTolerance, simplified);
59
- }
60
- }
61
- }
62
-
63
- // simplification using Ramer-Douglas-Peucker algorithm
64
- function simplifyDouglasPeucker(
65
- points: Array<Position>,
66
- sqTolerance: number
67
- ): Array<Position> {
68
- const last = points.length - 1;
69
- const simplified = [points[0]];
70
- simplifyDPStep(points, 0, last, sqTolerance, simplified);
71
- simplified.push(points[last]);
72
- return simplified;
73
- }
74
-
75
- // both algorithms combined for awesome performance
76
- export default function simplify(
77
- points: Array<Position>,
78
- tolerance: number
79
- ): Array<Position> {
80
- if (points.length <= 2) {
81
- return points;
82
- }
83
-
84
- const sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1;
85
- const simplePoints = simplifyDouglasPeucker(points, sqTolerance);
86
- return simplePoints;
87
- }