@carto/api-client 0.5.0-alpha.9 → 0.5.1-alpha.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/CHANGELOG.md +30 -1
- package/build/api-client.cjs +15241 -3358
- package/build/api-client.cjs.map +1 -0
- package/build/api-client.d.cts +528 -181
- package/build/api-client.d.ts +528 -181
- package/build/api-client.js +14685 -2911
- package/build/api-client.js.map +1 -0
- package/build/worker.js +5117 -619
- package/build/worker.js.map +1 -0
- package/package.json +49 -32
- package/src/api/query.ts +2 -1
- package/src/constants-internal.ts +10 -0
- package/src/constants.ts +5 -1
- package/src/deck/get-data-filter-extension-props.ts +27 -9
- package/src/fetch-map/basemap-styles.ts +159 -0
- package/src/fetch-map/basemap.ts +120 -0
- package/src/fetch-map/fetch-map.ts +331 -0
- package/src/fetch-map/index.ts +13 -0
- package/src/fetch-map/layer-map.ts +461 -0
- package/src/fetch-map/parse-map.ts +425 -0
- package/src/fetch-map/source.ts +233 -0
- package/src/fetch-map/types.ts +268 -0
- package/src/fetch-map/utils.ts +69 -0
- package/src/filters/tileFeatures.ts +27 -10
- package/src/filters/tileFeaturesRaster.ts +122 -0
- package/src/index.ts +1 -0
- package/src/models/model.ts +0 -7
- package/src/sources/base-source.ts +4 -2
- package/src/sources/h3-tileset-source.ts +1 -1
- package/src/sources/quadbin-tileset-source.ts +1 -1
- package/src/sources/raster-source.ts +18 -5
- package/src/sources/types.ts +15 -8
- package/src/sources/vector-tileset-source.ts +1 -1
- package/src/spatial-index.ts +3 -84
- package/src/types.ts +16 -2
- package/src/widget-sources/index.ts +1 -0
- package/src/widget-sources/types.ts +1 -3
- package/src/widget-sources/widget-raster-source.ts +14 -0
- package/src/widget-sources/widget-remote-source.ts +16 -91
- package/src/widget-sources/widget-source.ts +9 -25
- package/src/widget-sources/widget-tileset-source-impl.ts +16 -19
- package/src/widget-sources/widget-tileset-source.ts +24 -21
- package/src/workers/widget-tileset-worker.ts +1 -1
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import {TilejsonResult} from '../sources/index.js';
|
|
2
|
+
|
|
3
|
+
import {DEFAULT_API_BASE_URL} from '../constants.js';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
APIErrorContext,
|
|
7
|
+
CartoAPIError,
|
|
8
|
+
buildPublicMapUrl,
|
|
9
|
+
buildStatsUrl,
|
|
10
|
+
requestWithParameters,
|
|
11
|
+
} from '../api/index.js';
|
|
12
|
+
|
|
13
|
+
import {ParseMapResult, parseMap} from './parse-map.js';
|
|
14
|
+
import {assert} from '../utils.js';
|
|
15
|
+
import type {Basemap, Dataset, KeplerMapConfig} from './types.js';
|
|
16
|
+
import {fetchBasemapProps} from './basemap.js';
|
|
17
|
+
import {configureSource} from './source.js';
|
|
18
|
+
import {Filters} from '../types.js';
|
|
19
|
+
import {isRemoteCalculationSupported} from './utils.js';
|
|
20
|
+
|
|
21
|
+
/* global clearInterval, setInterval, URL */
|
|
22
|
+
async function _fetchMapDataset(
|
|
23
|
+
dataset: Dataset,
|
|
24
|
+
filters: Filters,
|
|
25
|
+
context: _FetchMapContext
|
|
26
|
+
) {
|
|
27
|
+
const {connectionName} = dataset;
|
|
28
|
+
const cache: {value?: number} = {};
|
|
29
|
+
const configuredSource = configureSource({
|
|
30
|
+
dataset,
|
|
31
|
+
filters: isRemoteCalculationSupported(dataset) ? filters : undefined,
|
|
32
|
+
options: {
|
|
33
|
+
...context,
|
|
34
|
+
connection: connectionName,
|
|
35
|
+
headers: context.headers,
|
|
36
|
+
accessToken: context.accessToken!,
|
|
37
|
+
apiBaseUrl: context.apiBaseUrl,
|
|
38
|
+
maxLengthURL: context.maxLengthURL,
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
dataset.data = await configuredSource;
|
|
42
|
+
|
|
43
|
+
let cacheChanged = true;
|
|
44
|
+
if (cache.value) {
|
|
45
|
+
cacheChanged = dataset.cache !== cache.value;
|
|
46
|
+
dataset.cache = cache.value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return cacheChanged;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function _fetchTilestats(
|
|
53
|
+
attribute: string,
|
|
54
|
+
dataset: Dataset,
|
|
55
|
+
context: _FetchMapContext
|
|
56
|
+
) {
|
|
57
|
+
const {connectionName, data, id, source, type, queryParameters} = dataset;
|
|
58
|
+
const {apiBaseUrl} = context;
|
|
59
|
+
const errorContext: APIErrorContext = {
|
|
60
|
+
requestType: 'Tile stats',
|
|
61
|
+
connection: connectionName,
|
|
62
|
+
type,
|
|
63
|
+
source,
|
|
64
|
+
};
|
|
65
|
+
if (!('tilestats' in data)) {
|
|
66
|
+
throw new CartoAPIError(
|
|
67
|
+
new Error(`Invalid dataset for tilestats: ${id}`),
|
|
68
|
+
errorContext
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const baseUrl = buildStatsUrl({attribute, apiBaseUrl, ...dataset});
|
|
73
|
+
const client = new URLSearchParams(data.tiles[0]).get('client');
|
|
74
|
+
const headers = {Authorization: `Bearer ${context.accessToken}`};
|
|
75
|
+
const parameters: Record<string, string> = {};
|
|
76
|
+
if (client) {
|
|
77
|
+
parameters.client = client;
|
|
78
|
+
}
|
|
79
|
+
if (type === 'query') {
|
|
80
|
+
parameters.q = source;
|
|
81
|
+
if (queryParameters) {
|
|
82
|
+
parameters.queryParameters = JSON.stringify(queryParameters);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const stats = await requestWithParameters({
|
|
86
|
+
baseUrl,
|
|
87
|
+
headers,
|
|
88
|
+
parameters,
|
|
89
|
+
errorContext,
|
|
90
|
+
maxLengthURL: context.maxLengthURL,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Replace tilestats for attribute with value from API
|
|
94
|
+
const {attributes} = data.tilestats.layers[0];
|
|
95
|
+
const index = attributes.findIndex((d) => d.attribute === attribute);
|
|
96
|
+
attributes[index] = stats;
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function fillInMapDatasets(
|
|
101
|
+
{datasets, keplerMapConfig}: {datasets: Dataset[]; keplerMapConfig: any},
|
|
102
|
+
context: _FetchMapContext
|
|
103
|
+
) {
|
|
104
|
+
const {filters} = keplerMapConfig.config as KeplerMapConfig;
|
|
105
|
+
const promises = datasets.map((dataset) =>
|
|
106
|
+
_fetchMapDataset(dataset, filters[dataset.id], context)
|
|
107
|
+
);
|
|
108
|
+
return await Promise.all(promises);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function fillInTileStats(
|
|
112
|
+
{datasets, keplerMapConfig}: {datasets: Dataset[]; keplerMapConfig: any},
|
|
113
|
+
context: _FetchMapContext
|
|
114
|
+
) {
|
|
115
|
+
const attributes: {attribute: string; dataset: any}[] = [];
|
|
116
|
+
const {layers} = keplerMapConfig.config.visState;
|
|
117
|
+
for (const layer of layers) {
|
|
118
|
+
for (const channel of Object.keys(layer.visualChannels)) {
|
|
119
|
+
const attribute = layer.visualChannels[channel]?.name;
|
|
120
|
+
if (attribute) {
|
|
121
|
+
const dataset = datasets.find((d) => d.id === layer.config.dataId);
|
|
122
|
+
if (
|
|
123
|
+
dataset &&
|
|
124
|
+
dataset.type !== 'tileset' &&
|
|
125
|
+
(dataset.data as TilejsonResult).tilestats
|
|
126
|
+
) {
|
|
127
|
+
// Only fetch stats for QUERY & TABLE map types
|
|
128
|
+
attributes.push({attribute, dataset});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// Remove duplicates to avoid repeated requests
|
|
134
|
+
const filteredAttributes: {attribute: string; dataset: any}[] = [];
|
|
135
|
+
for (const a of attributes) {
|
|
136
|
+
if (
|
|
137
|
+
!filteredAttributes.find(
|
|
138
|
+
({attribute, dataset}) =>
|
|
139
|
+
attribute === a.attribute && dataset === a.dataset
|
|
140
|
+
)
|
|
141
|
+
) {
|
|
142
|
+
filteredAttributes.push(a);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const promises = filteredAttributes.map(({attribute, dataset}) =>
|
|
147
|
+
_fetchTilestats(attribute, dataset, context)
|
|
148
|
+
);
|
|
149
|
+
return await Promise.all(promises);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export type FetchMapOptions = {
|
|
153
|
+
/**
|
|
154
|
+
* CARTO platform access token. Only required for private maps.
|
|
155
|
+
*/
|
|
156
|
+
accessToken?: string;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Base URL of the CARTO Maps API.
|
|
160
|
+
*
|
|
161
|
+
* Example for account located in EU-west region: `https://gcp-eu-west1.api.carto.com`
|
|
162
|
+
*
|
|
163
|
+
* @default https://gcp-us-east1.api.carto.com
|
|
164
|
+
*/
|
|
165
|
+
apiBaseUrl?: string;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Identifier of map created in CARTO Builder.
|
|
169
|
+
*/
|
|
170
|
+
cartoMapId: string;
|
|
171
|
+
clientId?: string;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Custom HTTP headers added to map instantiation and data requests.
|
|
175
|
+
*/
|
|
176
|
+
headers?: Record<string, string>;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Interval in seconds at which to autoRefresh the data. If provided, `onNewData` must also be provided.
|
|
180
|
+
*/
|
|
181
|
+
autoRefresh?: number;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Callback function that will be invoked whenever data in layers is changed. If provided, `autoRefresh` must also be provided.
|
|
185
|
+
*/
|
|
186
|
+
onNewData?: (map: any) => void;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Maximum URL character length. Above this limit, requests use POST.
|
|
190
|
+
* Used to avoid browser and CDN limits.
|
|
191
|
+
* @default {@link DEFAULT_MAX_LENGTH_URL}
|
|
192
|
+
*/
|
|
193
|
+
maxLengthURL?: number;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Context reused while fetching and updating a map with fetchMap().
|
|
198
|
+
*/
|
|
199
|
+
type _FetchMapContext = {apiBaseUrl: string} & Pick<
|
|
200
|
+
FetchMapOptions,
|
|
201
|
+
'accessToken' | 'clientId' | 'headers' | 'maxLengthURL'
|
|
202
|
+
>;
|
|
203
|
+
|
|
204
|
+
export type FetchMapResult = ParseMapResult & {
|
|
205
|
+
/**
|
|
206
|
+
* Basemap properties.
|
|
207
|
+
*/
|
|
208
|
+
basemap: Basemap | null;
|
|
209
|
+
stopAutoRefresh?: () => void;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
export async function fetchMap({
|
|
213
|
+
accessToken,
|
|
214
|
+
apiBaseUrl = DEFAULT_API_BASE_URL,
|
|
215
|
+
cartoMapId,
|
|
216
|
+
clientId,
|
|
217
|
+
headers,
|
|
218
|
+
autoRefresh,
|
|
219
|
+
onNewData,
|
|
220
|
+
maxLengthURL,
|
|
221
|
+
}: FetchMapOptions): Promise<FetchMapResult> {
|
|
222
|
+
assert(
|
|
223
|
+
cartoMapId,
|
|
224
|
+
'Must define CARTO map id: fetchMap({cartoMapId: "XXXX-XXXX-XXXX"})'
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
if (accessToken) {
|
|
228
|
+
headers = {Authorization: `Bearer ${accessToken}`, ...headers};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (autoRefresh || onNewData) {
|
|
232
|
+
assert(onNewData, 'Must define `onNewData` when using autoRefresh');
|
|
233
|
+
assert(typeof onNewData === 'function', '`onNewData` must be a function');
|
|
234
|
+
assert(
|
|
235
|
+
typeof autoRefresh === 'number' && autoRefresh > 0,
|
|
236
|
+
'`autoRefresh` must be a positive number'
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const baseUrl = buildPublicMapUrl({apiBaseUrl, cartoMapId});
|
|
241
|
+
const errorContext: APIErrorContext = {
|
|
242
|
+
requestType: 'Public map',
|
|
243
|
+
mapId: cartoMapId,
|
|
244
|
+
};
|
|
245
|
+
const map = await requestWithParameters({
|
|
246
|
+
baseUrl,
|
|
247
|
+
headers,
|
|
248
|
+
errorContext,
|
|
249
|
+
maxLengthURL,
|
|
250
|
+
});
|
|
251
|
+
const context: _FetchMapContext = {
|
|
252
|
+
accessToken: map.token || accessToken,
|
|
253
|
+
apiBaseUrl,
|
|
254
|
+
clientId,
|
|
255
|
+
headers,
|
|
256
|
+
maxLengthURL,
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
// Periodically check if the data has changed. Note that this
|
|
260
|
+
// will not update when a map is published.
|
|
261
|
+
let stopAutoRefresh: (() => void) | undefined;
|
|
262
|
+
if (autoRefresh) {
|
|
263
|
+
const intervalId = setInterval(async () => {
|
|
264
|
+
const changed = await fillInMapDatasets(map, {
|
|
265
|
+
...context,
|
|
266
|
+
headers: {
|
|
267
|
+
...headers,
|
|
268
|
+
'If-Modified-Since': new Date().toUTCString(),
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
if (onNewData && changed.some((v) => v === true)) {
|
|
272
|
+
onNewData(parseMap(map));
|
|
273
|
+
}
|
|
274
|
+
}, autoRefresh * 1000);
|
|
275
|
+
stopAutoRefresh = () => {
|
|
276
|
+
clearInterval(intervalId);
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const geojsonLayers = map.keplerMapConfig.config.visState.layers.filter(
|
|
281
|
+
({type}: {type: string}) => type === 'geojson' || type === 'point'
|
|
282
|
+
);
|
|
283
|
+
const geojsonDatasetIds = geojsonLayers.map(
|
|
284
|
+
({config}: {config: any}) => config.dataId
|
|
285
|
+
);
|
|
286
|
+
map.datasets.forEach((dataset: any) => {
|
|
287
|
+
if (geojsonDatasetIds.includes(dataset.id)) {
|
|
288
|
+
const {config} = geojsonLayers.find(
|
|
289
|
+
({config}: {config: any}) => config.dataId === dataset.id
|
|
290
|
+
);
|
|
291
|
+
dataset.format = 'geojson';
|
|
292
|
+
// Support for very old maps. geoColumn was not stored in the past
|
|
293
|
+
if (!dataset.geoColumn && config.columns.geojson) {
|
|
294
|
+
dataset.geoColumn = config.columns.geojson;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
const [basemap] = await Promise.all([
|
|
300
|
+
fetchBasemapProps({config: map.keplerMapConfig.config, errorContext}),
|
|
301
|
+
|
|
302
|
+
// Mutates map.datasets so that dataset.data contains data
|
|
303
|
+
fillInMapDatasets(map, context),
|
|
304
|
+
]);
|
|
305
|
+
|
|
306
|
+
// Mutates attributes in visualChannels to contain tile stats
|
|
307
|
+
await fillInTileStats(map, context);
|
|
308
|
+
|
|
309
|
+
const out = {...parseMap(map), basemap, ...{stopAutoRefresh}};
|
|
310
|
+
|
|
311
|
+
const textLayers = out.layers.filter((layer: any) => {
|
|
312
|
+
const pointType = layer.props?.pointType || '';
|
|
313
|
+
return pointType.includes('text');
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
/* global FontFace, window, document */
|
|
317
|
+
if (
|
|
318
|
+
textLayers.length &&
|
|
319
|
+
window.FontFace &&
|
|
320
|
+
!document.fonts.check('12px Inter')
|
|
321
|
+
) {
|
|
322
|
+
// Fetch font needed for labels
|
|
323
|
+
const font = new FontFace(
|
|
324
|
+
'Inter',
|
|
325
|
+
'url(https://fonts.gstatic.com/s/inter/v12/UcC73FwrK3iLTeHuS_fvQtMwCp50KnMa1ZL7W0Q5nw.woff2)'
|
|
326
|
+
);
|
|
327
|
+
await font.load().then((f) => document.fonts.add(f));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return out as FetchMapResult;
|
|
331
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export {default as BASEMAP} from './basemap-styles.js';
|
|
2
|
+
export {fetchMap} from './fetch-map.js';
|
|
3
|
+
export type {FetchMapOptions, FetchMapResult} from './fetch-map.js';
|
|
4
|
+
export type {
|
|
5
|
+
Basemap,
|
|
6
|
+
MapLibreBasemap,
|
|
7
|
+
GoogleBasemap,
|
|
8
|
+
KeplerMapConfig,
|
|
9
|
+
} from './types.js';
|
|
10
|
+
|
|
11
|
+
export * from './basemap.js';
|
|
12
|
+
export * from './layer-map.js';
|
|
13
|
+
export * from './parse-map.js';
|