@maptiler/client 1.0.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/.eslintrc.cjs +6 -0
- package/LICENSE +29 -0
- package/dist/maptiler-client.cjs +692 -0
- package/dist/maptiler-client.cjs.map +1 -0
- package/dist/maptiler-client.d.ts +702 -0
- package/dist/maptiler-client.min.mjs +1 -0
- package/dist/maptiler-client.mjs +680 -0
- package/dist/maptiler-client.mjs.map +1 -0
- package/dist/maptiler-client.umd.js +698 -0
- package/dist/maptiler-client.umd.js.map +1 -0
- package/dist/maptiler-client.umd.min.js +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +85 -0
- package/docs/assets/main.js +54 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1257 -0
- package/docs/classes/ClientConfig.html +122 -0
- package/docs/classes/ServiceError.html +93 -0
- package/docs/images/JS-logo.svg +4 -0
- package/docs/images/TS-logo.svg +6 -0
- package/docs/images/maptiler-client-logo.afdesign +0 -0
- package/docs/images/maptiler-client-logo.svg +75 -0
- package/docs/images/maptiler-logo.svg +23 -0
- 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 +280 -0
- package/docs/modules.html +119 -0
- package/docs/types/AutomaticStaticMapOptions.html +80 -0
- package/docs/types/BoundedStaticMapOptions.html +80 -0
- package/docs/types/CenteredStaticMapOptions.html +80 -0
- package/docs/types/CoordinateExport.html +85 -0
- package/docs/types/CoordinateGrid.html +83 -0
- package/docs/types/CoordinateId.html +85 -0
- package/docs/types/CoordinateSearch.html +107 -0
- package/docs/types/CoordinateSearchResult.html +89 -0
- package/docs/types/CoordinateTransformResult.html +90 -0
- package/docs/types/CoordinateTransformation.html +103 -0
- package/docs/types/Coordinates.html +78 -0
- package/docs/types/CoordinatesSearchOptions.html +97 -0
- package/docs/types/CoordinatesTransformOptions.html +99 -0
- package/docs/types/FeatureHierarchy.html +89 -0
- package/docs/types/FetchFunction.html +93 -0
- package/docs/types/GeocodingFeature.html +78 -0
- package/docs/types/GeocodingOptions.html +97 -0
- package/docs/types/GeocodingSearchResult.html +95 -0
- package/docs/types/GeolocationInfoOptions.html +87 -0
- package/docs/types/GeolocationResult.html +150 -0
- package/docs/types/GetDataOptions.html +87 -0
- package/docs/types/LanguageGeocodingString.html +80 -0
- package/docs/types/ReverseGeocodingOptions.html +89 -0
- package/docs/types/StaticMapBaseOptions.html +176 -0
- package/docs/types/StaticMapMarker.html +80 -0
- package/docs/types/XYZ.html +87 -0
- package/docs/variables/LanguageGeocoding.html +193 -0
- package/docs/variables/config.html +80 -0
- package/docs/variables/coordinates-1.html +124 -0
- package/docs/variables/data.html +103 -0
- package/docs/variables/geocoding.html +239 -0
- package/docs/variables/geolocation.html +101 -0
- package/docs/variables/staticMaps.html +147 -0
- package/docsmd/.nojekyll +1 -0
- package/docsmd/README.md +756 -0
- package/docsmd/classes/ClientConfig.md +90 -0
- package/docsmd/classes/ServiceError.md +52 -0
- package/docsmd/images/JS-logo.svg +4 -0
- package/docsmd/images/TS-logo.svg +6 -0
- package/docsmd/images/maptiler-client-logo.afdesign +0 -0
- package/docsmd/images/maptiler-client-logo.svg +75 -0
- package/docsmd/images/maptiler-logo.svg +23 -0
- package/docsmd/images/screenshots/static-bounded-europe-1024.png +0 -0
- package/docsmd/images/screenshots/static-bounded-europe-2048.png +0 -0
- package/docsmd/images/screenshots/static-bounded-portugal-1024x2048.png +0 -0
- package/docsmd/images/screenshots/static-bounded-portugal-2048x2048.png +0 -0
- package/docsmd/images/screenshots/static-with-path.png +0 -0
- package/examples/test-browser-es.html +102 -0
- package/examples/test-browser-umd.html +89 -0
- package/examples/test-node.js +52 -0
- package/images/JS-logo.svg +4 -0
- package/images/TS-logo.svg +6 -0
- package/images/maptiler-client-logo.afdesign +0 -0
- package/images/maptiler-client-logo.svg +75 -0
- package/images/maptiler-logo.svg +23 -0
- 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/package.json +69 -0
- package/readme.md +287 -0
- package/rollup.config.js +142 -0
- package/src/callFetch.ts +18 -0
- package/src/config.ts +65 -0
- package/src/defaults.ts +11 -0
- package/src/index.ts +90 -0
- package/src/language.ts +87 -0
- package/src/services/ServiceError.ts +10 -0
- package/src/services/coordinates.ts +258 -0
- package/src/services/data.ts +63 -0
- package/src/services/geocoding.ts +239 -0
- package/src/services/geolocation.ts +139 -0
- package/src/services/simplify.ts +87 -0
- package/src/services/staticMaps.ts +462 -0
- package/tsconfig.json +10 -0
- package/typedoc.json +10 -0
package/src/config.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export type FetchFunction = (url: string, options: object) => Promise<any>;
|
|
2
|
+
|
|
3
|
+
function tryGettingFetch() {
|
|
4
|
+
// this is browser, fetch exists
|
|
5
|
+
if (typeof self !== "undefined") {
|
|
6
|
+
return fetch.bind(self);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (typeof global !== "undefined" && global.fetch) {
|
|
10
|
+
return global.fetch;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The configuration object definition
|
|
18
|
+
*/
|
|
19
|
+
class ClientConfig {
|
|
20
|
+
/**
|
|
21
|
+
* MapTiler Cloud API key
|
|
22
|
+
*/
|
|
23
|
+
private _apiKey = "";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The fetch function. To be set if in Node < 18, otherwise
|
|
27
|
+
* will be automatically resolved.
|
|
28
|
+
*/
|
|
29
|
+
private _fetch: FetchFunction | null = tryGettingFetch();
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Set the MapTiler Cloud API key
|
|
33
|
+
*/
|
|
34
|
+
set apiKey(k: string) {
|
|
35
|
+
this._apiKey = k;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Get the MapTiler Cloud API key
|
|
40
|
+
*/
|
|
41
|
+
get apiKey(): string {
|
|
42
|
+
return this._apiKey;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Set a the custom fetch function to replace the default one
|
|
47
|
+
*/
|
|
48
|
+
set fetch(f: FetchFunction) {
|
|
49
|
+
this._fetch = f;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Get the fetch fucntion
|
|
54
|
+
*/
|
|
55
|
+
get fetch(): FetchFunction | null {
|
|
56
|
+
return this._fetch;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Configuration object
|
|
62
|
+
*/
|
|
63
|
+
const config = new ClientConfig();
|
|
64
|
+
|
|
65
|
+
export { ClientConfig, config };
|
package/src/defaults.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { BBox, Position } from "geojson";
|
|
2
|
+
import { ClientConfig, config } from "./config";
|
|
3
|
+
import type { FetchFunction } from "./config";
|
|
4
|
+
import { LanguageGeocoding, LanguageGeocodingString } from "./language";
|
|
5
|
+
|
|
6
|
+
// Importing services
|
|
7
|
+
import { geocoding } from "./services/geocoding";
|
|
8
|
+
import type {
|
|
9
|
+
GeocodingOptions,
|
|
10
|
+
ReverseGeocodingOptions,
|
|
11
|
+
GeocodingSearchResult,
|
|
12
|
+
GeocodingFeature,
|
|
13
|
+
FeatureHierarchy,
|
|
14
|
+
Coordinates,
|
|
15
|
+
} from "./services/geocoding";
|
|
16
|
+
import { geolocation } from "./services/geolocation";
|
|
17
|
+
import type {
|
|
18
|
+
GeolocationInfoOptions,
|
|
19
|
+
GeolocationResult,
|
|
20
|
+
} from "./services/geolocation";
|
|
21
|
+
import { coordinates } from "./services/coordinates";
|
|
22
|
+
import type {
|
|
23
|
+
CoordinatesSearchOptions,
|
|
24
|
+
CoordinatesTransformOptions,
|
|
25
|
+
CoordinateTransformResult,
|
|
26
|
+
CoordinateSearchResult,
|
|
27
|
+
CoordinateSearch,
|
|
28
|
+
XYZ,
|
|
29
|
+
CoordinateTransformation,
|
|
30
|
+
CoordinateId,
|
|
31
|
+
CoordinateExport,
|
|
32
|
+
CoordinateGrid,
|
|
33
|
+
} from "./services/coordinates";
|
|
34
|
+
import { data } from "./services/data";
|
|
35
|
+
import type { GetDataOptions } from "./services/data";
|
|
36
|
+
import { staticMaps } from "./services/staticMaps";
|
|
37
|
+
import type {
|
|
38
|
+
StaticMapMarker,
|
|
39
|
+
StaticMapBaseOptions,
|
|
40
|
+
CenteredStaticMapOptions,
|
|
41
|
+
AutomaticStaticMapOptions,
|
|
42
|
+
BoundedStaticMapOptions,
|
|
43
|
+
} from "./services/staticMaps";
|
|
44
|
+
|
|
45
|
+
import { ServiceError } from "./services/ServiceError";
|
|
46
|
+
|
|
47
|
+
// Exporting types
|
|
48
|
+
export type {
|
|
49
|
+
Position,
|
|
50
|
+
BBox,
|
|
51
|
+
GeocodingOptions,
|
|
52
|
+
ReverseGeocodingOptions,
|
|
53
|
+
GeocodingSearchResult,
|
|
54
|
+
GeocodingFeature,
|
|
55
|
+
CoordinateExport,
|
|
56
|
+
CoordinateGrid,
|
|
57
|
+
CoordinateTransformation,
|
|
58
|
+
CoordinateId,
|
|
59
|
+
FeatureHierarchy,
|
|
60
|
+
Coordinates,
|
|
61
|
+
CoordinatesTransformOptions,
|
|
62
|
+
CoordinateTransformResult,
|
|
63
|
+
CoordinateSearchResult,
|
|
64
|
+
XYZ,
|
|
65
|
+
CoordinateSearch,
|
|
66
|
+
CoordinatesSearchOptions,
|
|
67
|
+
StaticMapMarker,
|
|
68
|
+
StaticMapBaseOptions,
|
|
69
|
+
CenteredStaticMapOptions,
|
|
70
|
+
BoundedStaticMapOptions,
|
|
71
|
+
AutomaticStaticMapOptions,
|
|
72
|
+
FetchFunction,
|
|
73
|
+
LanguageGeocodingString,
|
|
74
|
+
GetDataOptions,
|
|
75
|
+
GeolocationInfoOptions,
|
|
76
|
+
GeolocationResult,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// Exporting classes, objects, functions, etc.
|
|
80
|
+
export {
|
|
81
|
+
config,
|
|
82
|
+
ClientConfig,
|
|
83
|
+
ServiceError,
|
|
84
|
+
geocoding,
|
|
85
|
+
geolocation,
|
|
86
|
+
coordinates,
|
|
87
|
+
data,
|
|
88
|
+
staticMaps,
|
|
89
|
+
LanguageGeocoding,
|
|
90
|
+
};
|
package/src/language.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Languages. Note that not all the languages of this list are available but the compatibility list may be expanded in the future.
|
|
3
|
+
*/
|
|
4
|
+
const LanguageGeocoding = {
|
|
5
|
+
AUTO: "auto",
|
|
6
|
+
ALBANIAN: "sq",
|
|
7
|
+
ARABIC: "ar",
|
|
8
|
+
ARMENIAN: "hy",
|
|
9
|
+
AZERBAIJANI: "az",
|
|
10
|
+
BELORUSSIAN: "be",
|
|
11
|
+
BOSNIAN: "bs",
|
|
12
|
+
BRETON: "br",
|
|
13
|
+
BULGARIAN: "bg",
|
|
14
|
+
CATALAN: "ca",
|
|
15
|
+
CHINESE: "zh",
|
|
16
|
+
CROATIAN: "hr",
|
|
17
|
+
CZECH: "cs",
|
|
18
|
+
DANISH: "da",
|
|
19
|
+
DUTCH: "nl",
|
|
20
|
+
ENGLISH: "en",
|
|
21
|
+
ESPERANTO: "eo",
|
|
22
|
+
ESTONIAN: "et",
|
|
23
|
+
FINNISH: "fi",
|
|
24
|
+
FRENCH: "fr",
|
|
25
|
+
FRISIAN: "fy",
|
|
26
|
+
GEORGIAN: "ka",
|
|
27
|
+
GERMAN: "de",
|
|
28
|
+
GREEK: "el",
|
|
29
|
+
HEBREW: "he",
|
|
30
|
+
HUNGARIAN: "hu",
|
|
31
|
+
ICELANDIC: "is",
|
|
32
|
+
IRISH: "ga",
|
|
33
|
+
ITALIAN: "it",
|
|
34
|
+
JAPANESE: "ja",
|
|
35
|
+
KANNADA: "kn",
|
|
36
|
+
KAZAKH: "kk",
|
|
37
|
+
KOREAN: "ko",
|
|
38
|
+
ROMAN_LATIN: "la",
|
|
39
|
+
LATVIAN: "lv",
|
|
40
|
+
LITHUANIAN: "lt",
|
|
41
|
+
LUXEMBOURGISH: "lb",
|
|
42
|
+
MACEDONIAN: "mk",
|
|
43
|
+
MALTESE: "mt",
|
|
44
|
+
NORWEGIAN: "no",
|
|
45
|
+
POLISH: "pl",
|
|
46
|
+
PORTUGUESE: "pt",
|
|
47
|
+
ROMANIAN: "ro",
|
|
48
|
+
ROMANSH: "rm",
|
|
49
|
+
RUSSIAN: "ru",
|
|
50
|
+
SCOTTISH_GAELIC: "gd",
|
|
51
|
+
SERBIAN_CYRILLIC: "sr",
|
|
52
|
+
SLOVAK: "sk",
|
|
53
|
+
SLOVENE: "sl",
|
|
54
|
+
SPANISH: "es",
|
|
55
|
+
SWEDISH: "sv",
|
|
56
|
+
THAI: "th",
|
|
57
|
+
TURKISH: "tr",
|
|
58
|
+
UKRAINIAN: "uk",
|
|
59
|
+
WELSH: "cy",
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const languageCodeSet = new Set(Object.values(LanguageGeocoding));
|
|
63
|
+
|
|
64
|
+
type Values<T> = T[keyof T];
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Built-in languages values as strings
|
|
68
|
+
*/
|
|
69
|
+
type LanguageGeocodingString = Values<typeof LanguageGeocoding>;
|
|
70
|
+
|
|
71
|
+
function getAutoLanguageGeocoding(): LanguageGeocodingString {
|
|
72
|
+
if (typeof navigator === "undefined") {
|
|
73
|
+
return Intl.DateTimeFormat()
|
|
74
|
+
.resolvedOptions()
|
|
75
|
+
.locale.split("-")[0] as LanguageGeocodingString;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const canditatelangs = Array.from(
|
|
79
|
+
new Set(navigator.languages.map((l) => l.split("-")[0]))
|
|
80
|
+
).filter((l) => languageCodeSet.has(l as LanguageGeocodingString));
|
|
81
|
+
|
|
82
|
+
return canditatelangs.length
|
|
83
|
+
? (canditatelangs[0] as LanguageGeocodingString)
|
|
84
|
+
: LanguageGeocoding.ENGLISH;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { LanguageGeocoding, LanguageGeocodingString, getAutoLanguageGeocoding };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A ServiceError is an Error that includes the HTTP response details
|
|
3
|
+
*/
|
|
4
|
+
export class ServiceError extends Error {
|
|
5
|
+
constructor(public res: Response, customMessage = "") {
|
|
6
|
+
super(
|
|
7
|
+
`Call to enpoint ${res.url} failed with the status code ${res.status}. ${customMessage}`
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { BBox, Position } 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
|
+
export type CoordinatesSearchOptions = {
|
|
12
|
+
/**
|
|
13
|
+
* Custom mapTiler Cloud API key to use instead of the one in global `config`
|
|
14
|
+
*/
|
|
15
|
+
apiKey?: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Maximum number of results returned (default: 10)
|
|
19
|
+
*/
|
|
20
|
+
limit?: number;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Show detailed transformations for each CRS (default: false)
|
|
24
|
+
*/
|
|
25
|
+
transformations?: boolean;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Show exports in WKT and Proj4 notations (default: false)
|
|
29
|
+
*/
|
|
30
|
+
exports?: boolean;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type CoordinateId = {
|
|
34
|
+
authority: string;
|
|
35
|
+
code: BigInteger;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type CoordinateExport = {
|
|
39
|
+
proj4: string;
|
|
40
|
+
wkt: string;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type CoordinateGrid = {
|
|
44
|
+
path: string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type CoordinateTransformation = {
|
|
48
|
+
id: CoordinateId;
|
|
49
|
+
name: string;
|
|
50
|
+
reversible: boolean;
|
|
51
|
+
usable: boolean;
|
|
52
|
+
deprecated: boolean;
|
|
53
|
+
grids: Array<CoordinateGrid>;
|
|
54
|
+
accuracy?: number;
|
|
55
|
+
area?: string;
|
|
56
|
+
bbox?: BBox;
|
|
57
|
+
target_crs?: CoordinateId;
|
|
58
|
+
unit?: string;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type CoordinateSearch = {
|
|
62
|
+
id: CoordinateId;
|
|
63
|
+
|
|
64
|
+
name: string;
|
|
65
|
+
|
|
66
|
+
kind: string;
|
|
67
|
+
|
|
68
|
+
deprecated: boolean;
|
|
69
|
+
|
|
70
|
+
transformations?: Array<CoordinateTransformation | number>;
|
|
71
|
+
|
|
72
|
+
accuracy?: number;
|
|
73
|
+
|
|
74
|
+
unit?: string;
|
|
75
|
+
|
|
76
|
+
area?: string;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Bounding box of the resource in [min_lon, min_lat, max_lon, max_lat] order.
|
|
80
|
+
*/
|
|
81
|
+
bbox?: BBox;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Most suitable transformation for this CRS.
|
|
85
|
+
*/
|
|
86
|
+
default_transformation?: any;
|
|
87
|
+
|
|
88
|
+
exports: CoordinateExport;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export type CoordinateSearchResult = {
|
|
92
|
+
/**
|
|
93
|
+
* The coordinate search results
|
|
94
|
+
*/
|
|
95
|
+
results: Array<CoordinateSearch>;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The number of results
|
|
99
|
+
*/
|
|
100
|
+
total: number;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Search information about coordinate systems using MapTiler API.
|
|
105
|
+
* Learn more on the MapTiler API reference page: https://docs.maptiler.com/cloud/api/coordinates/#search-coordinate-systems
|
|
106
|
+
* @param query Can be any kind of CRS by name or code
|
|
107
|
+
* @param options
|
|
108
|
+
* @returns
|
|
109
|
+
*/
|
|
110
|
+
async function search(
|
|
111
|
+
query: string,
|
|
112
|
+
options: CoordinatesSearchOptions = {}
|
|
113
|
+
): Promise<CoordinateSearchResult> {
|
|
114
|
+
if (typeof query !== "string" || query.trim().length === 0) {
|
|
115
|
+
throw new Error("The query must be a non-empty string");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const endpoint = new URL(
|
|
119
|
+
`coordinates/search/${query}.json`,
|
|
120
|
+
defaults.maptilerApiURL
|
|
121
|
+
);
|
|
122
|
+
endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
|
|
123
|
+
|
|
124
|
+
if ("limit" in options) {
|
|
125
|
+
endpoint.searchParams.set("limit", options.limit.toString());
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if ("transformations" in options) {
|
|
129
|
+
endpoint.searchParams.set(
|
|
130
|
+
"transformations",
|
|
131
|
+
options.transformations.toString()
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if ("exports" in options) {
|
|
136
|
+
endpoint.searchParams.set("exports", options.exports.toString());
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const urlWithParams = endpoint.toString();
|
|
140
|
+
const res = await callFetch(urlWithParams);
|
|
141
|
+
|
|
142
|
+
if (!res.ok) {
|
|
143
|
+
throw new ServiceError(
|
|
144
|
+
res,
|
|
145
|
+
res.status in customMessages ? customMessages[res.status] : ""
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const obj = await res.json();
|
|
150
|
+
return obj as CoordinateSearchResult;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export type XYZ = {
|
|
154
|
+
x?: number;
|
|
155
|
+
y?: number;
|
|
156
|
+
z?: number;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export type CoordinateTransformResult = {
|
|
160
|
+
results: Array<XYZ>;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Transformations are selected using given ops parameter.
|
|
164
|
+
* If no parameter is given, auto strategy is used.
|
|
165
|
+
* If given, it may try to use a listed transformation,
|
|
166
|
+
* then fallback to towgs84 patching, and finally boundcrs.
|
|
167
|
+
*/
|
|
168
|
+
transformer_selection_strategy: string;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Options that can be provided when transforming a coordinate from one CRS to another.
|
|
173
|
+
*/
|
|
174
|
+
export type CoordinatesTransformOptions = {
|
|
175
|
+
/**
|
|
176
|
+
* Custom mapTiler Cloud API key to use instead of the one in global `config`
|
|
177
|
+
*/
|
|
178
|
+
apiKey?: string;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Source coordinate reference system (default: 4326)
|
|
182
|
+
*/
|
|
183
|
+
sourceCrs?: number;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Target coordinate reference system (default: 4326)
|
|
187
|
+
*/
|
|
188
|
+
targetCrs?: number;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* List of codes of operations
|
|
192
|
+
*/
|
|
193
|
+
operations?: number | Array<number>;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Transforms coordinates from a source reference system to a target reference system using MapTiler API.
|
|
198
|
+
* Learn more on the MapTiler API reference page: https://docs.maptiler.com/cloud/api/coordinates/#transform-coordinates
|
|
199
|
+
* @param positions
|
|
200
|
+
* @param options
|
|
201
|
+
* @returns
|
|
202
|
+
*/
|
|
203
|
+
async function transform(
|
|
204
|
+
positions: Position | Array<Position>,
|
|
205
|
+
options: CoordinatesTransformOptions = {}
|
|
206
|
+
): Promise<CoordinateTransformResult> {
|
|
207
|
+
const coordinatesStr = (Array.isArray(positions[0]) ? positions : [positions])
|
|
208
|
+
.map((coord) => `${coord[0]},${coord[1]}`)
|
|
209
|
+
.join(";");
|
|
210
|
+
|
|
211
|
+
const endpoint = new URL(
|
|
212
|
+
`coordinates/transform/${coordinatesStr}.json`,
|
|
213
|
+
defaults.maptilerApiURL
|
|
214
|
+
);
|
|
215
|
+
endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
|
|
216
|
+
|
|
217
|
+
if ("sourceCrs" in options) {
|
|
218
|
+
endpoint.searchParams.set("s_srs", options.sourceCrs.toString());
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if ("targetCrs" in options) {
|
|
222
|
+
endpoint.searchParams.set("t_srs", options.targetCrs.toString());
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if ("operations" in options) {
|
|
226
|
+
endpoint.searchParams.set(
|
|
227
|
+
"ops",
|
|
228
|
+
(Array.isArray(options.operations)
|
|
229
|
+
? options.operations
|
|
230
|
+
: [options.operations]
|
|
231
|
+
).join("|")
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const urlWithParams = endpoint.toString();
|
|
236
|
+
const res = await callFetch(urlWithParams);
|
|
237
|
+
|
|
238
|
+
if (!res.ok) {
|
|
239
|
+
throw new ServiceError(
|
|
240
|
+
res,
|
|
241
|
+
res.status in customMessages ? customMessages[res.status] : ""
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const obj = await res.json();
|
|
246
|
+
return obj as CoordinateTransformResult;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* The **coordinate** namespace contains asynchronous functions to call the [MapTiler Coordinate API](https://docs.maptiler.com/cloud/api/coordinates/).
|
|
251
|
+
* The goal of the **Coordinate API* is query information about spatial coordinate reference system (CRS) as well as to transform coordinates from one CRS to another.
|
|
252
|
+
*/
|
|
253
|
+
const coordinates = {
|
|
254
|
+
search,
|
|
255
|
+
transform,
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
export { coordinates };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { FeatureCollection } 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 GetDataOptions = {
|
|
15
|
+
/**
|
|
16
|
+
* Custom mapTiler Cloud API key to use instead of the one in global `config`
|
|
17
|
+
*/
|
|
18
|
+
apiKey?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Get user data and returns it as GeoJSON using the MapTiler API.
|
|
23
|
+
* Learn more on the MapTiler API reference page: https://docs.maptiler.com/cloud/api/data/#geojson
|
|
24
|
+
* @param dataId
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
async function get(
|
|
28
|
+
dataId: string,
|
|
29
|
+
options: GetDataOptions = {}
|
|
30
|
+
): Promise<FeatureCollection> {
|
|
31
|
+
if (typeof dataId !== "string" || dataId.trim().length === 0) {
|
|
32
|
+
throw new Error("The data ID must be a non-empty string");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const endpoint = new URL(
|
|
36
|
+
`data/${encodeURIComponent(dataId)}/features.json`,
|
|
37
|
+
defaults.maptilerApiURL
|
|
38
|
+
);
|
|
39
|
+
endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
|
|
40
|
+
const urlWithParams = endpoint.toString();
|
|
41
|
+
|
|
42
|
+
const res = await callFetch(urlWithParams);
|
|
43
|
+
|
|
44
|
+
if (!res.ok) {
|
|
45
|
+
throw new ServiceError(
|
|
46
|
+
res,
|
|
47
|
+
res.status in customMessages ? customMessages[res.status] : ""
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const obj = await res.json();
|
|
52
|
+
return obj;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The **data** namespace contains an asynchronous function to call the [MapTiler Data API](https://docs.maptiler.com/cloud/api/data/).
|
|
57
|
+
* The **Data API** provides a way to retrieve user data in GeoJSON format.
|
|
58
|
+
*/
|
|
59
|
+
const data = {
|
|
60
|
+
get,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export { data };
|