@carto/api-client 0.4.3 → 0.4.5-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.
@@ -16,21 +16,20 @@ import {
16
16
  TableResponse,
17
17
  TimeSeriesRequestOptions,
18
18
  TimeSeriesResponse,
19
+ ViewState,
19
20
  } from './types.js';
20
- import {FilterLogicalOperator, Filter} from '../types.js';
21
+ import {FilterLogicalOperator, Filter, SpatialFilter} from '../types.js';
21
22
  import {getApplicableFilters, normalizeObjectKeys} from '../utils.js';
22
23
  import {getClient} from '../client.js';
23
24
  import {ModelSource} from '../models/model.js';
24
25
  import {SourceOptions} from '../sources/index.js';
25
26
  import {ApiVersion, DEFAULT_API_BASE_URL} from '../constants.js';
26
- import {
27
- DEFAULT_GEO_COLUMN,
28
- DEFAULT_TILE_RESOLUTION,
29
- } from '../constants-internal.js';
27
+ import {DEFAULT_TILE_RESOLUTION} from '../constants-internal.js';
28
+ import {getSpatialFiltersResolution} from '../spatial-index.js';
29
+ import {AggregationOptions} from '../sources/types.js';
30
30
 
31
31
  export interface WidgetBaseSourceProps extends Omit<SourceOptions, 'filters'> {
32
32
  apiVersion?: ApiVersion;
33
- geoColumn?: string;
34
33
  filters?: Record<string, Filter>;
35
34
  filtersLogicalOperator?: FilterLogicalOperator;
36
35
  }
@@ -51,7 +50,6 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
51
50
  clientId: getClient(),
52
51
  filters: {},
53
52
  filtersLogicalOperator: 'and',
54
- geoColumn: DEFAULT_GEO_COLUMN,
55
53
  };
56
54
 
57
55
  constructor(props: Props) {
@@ -78,10 +76,31 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
78
76
  connectionName: props.connectionName,
79
77
  filters: getApplicableFilters(owner, props.filters),
80
78
  filtersLogicalOperator: props.filtersLogicalOperator,
81
- geoColumn: props.geoColumn,
79
+ spatialDataType: props.spatialDataType,
80
+ spatialDataColumn: props.spatialDataColumn,
81
+ dataResolution: (props as Partial<AggregationOptions>).dataResolution,
82
82
  };
83
83
  }
84
84
 
85
+ protected _getSpatialFiltersResolution(
86
+ source: Omit<ModelSource, 'type' | 'data'>,
87
+ spatialFilter?: SpatialFilter,
88
+ referenceViewState?: ViewState
89
+ ): number | undefined {
90
+ // spatialFiltersResolution applies only to spatial index sources.
91
+ if (!spatialFilter || source.spatialDataType === 'geo') {
92
+ return;
93
+ }
94
+
95
+ if (!referenceViewState) {
96
+ throw new Error(
97
+ 'Missing required option, "spatialIndexReferenceViewState".'
98
+ );
99
+ }
100
+
101
+ return getSpatialFiltersResolution(source, referenceViewState);
102
+ }
103
+
85
104
  /****************************************************************************
86
105
  * CATEGORIES
87
106
  */
@@ -93,14 +112,32 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
93
112
  async getCategories(
94
113
  options: CategoryRequestOptions
95
114
  ): Promise<CategoryResponse> {
96
- const {filterOwner, spatialFilter, abortController, ...params} = options;
115
+ const {
116
+ filterOwner,
117
+ spatialFilter,
118
+ spatialFiltersMode,
119
+ spatialIndexReferenceViewState,
120
+ abortController,
121
+ ...params
122
+ } = options;
97
123
  const {column, operation, operationColumn} = params;
124
+ const source = this.getModelSource(filterOwner);
125
+ const spatialFiltersResolution = this._getSpatialFiltersResolution(
126
+ source,
127
+ spatialFilter,
128
+ spatialIndexReferenceViewState
129
+ );
98
130
 
99
131
  type CategoriesModelResponse = {rows: {name: string; value: number}[]};
100
132
 
101
133
  return executeModel({
102
134
  model: 'category',
103
- source: {...this.getModelSource(filterOwner), spatialFilter},
135
+ source: {
136
+ ...source,
137
+ spatialFiltersResolution,
138
+ spatialFiltersMode,
139
+ spatialFilter,
140
+ },
104
141
  params: {
105
142
  column,
106
143
  operation,
@@ -125,14 +162,32 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
125
162
  async getFeatures(
126
163
  options: FeaturesRequestOptions
127
164
  ): Promise<FeaturesResponse> {
128
- const {filterOwner, spatialFilter, abortController, ...params} = options;
165
+ const {
166
+ filterOwner,
167
+ spatialFilter,
168
+ spatialFiltersMode,
169
+ spatialIndexReferenceViewState,
170
+ abortController,
171
+ ...params
172
+ } = options;
129
173
  const {columns, dataType, featureIds, z, limit, tileResolution} = params;
174
+ const source = this.getModelSource(filterOwner);
175
+ const spatialFiltersResolution = this._getSpatialFiltersResolution(
176
+ source,
177
+ spatialFilter,
178
+ spatialIndexReferenceViewState
179
+ );
130
180
 
131
181
  type FeaturesModelResponse = {rows: Record<string, unknown>[]};
132
182
 
133
183
  return executeModel({
134
184
  model: 'pick',
135
- source: {...this.getModelSource(filterOwner), spatialFilter},
185
+ source: {
186
+ ...source,
187
+ spatialFiltersResolution,
188
+ spatialFiltersMode,
189
+ spatialFilter,
190
+ },
136
191
  params: {
137
192
  columns,
138
193
  dataType,
@@ -158,17 +213,30 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
158
213
  const {
159
214
  filterOwner,
160
215
  spatialFilter,
216
+ spatialFiltersMode,
217
+ spatialIndexReferenceViewState,
161
218
  abortController,
162
219
  operationExp,
163
220
  ...params
164
221
  } = options;
165
222
  const {column, operation} = params;
223
+ const source = this.getModelSource(filterOwner);
224
+ const spatialFiltersResolution = this._getSpatialFiltersResolution(
225
+ source,
226
+ spatialFilter,
227
+ spatialIndexReferenceViewState
228
+ );
166
229
 
167
230
  type FormulaModelResponse = {rows: {value: number}[]};
168
231
 
169
232
  return executeModel({
170
233
  model: 'formula',
171
- source: {...this.getModelSource(filterOwner), spatialFilter},
234
+ source: {
235
+ ...source,
236
+ spatialFiltersResolution,
237
+ spatialFiltersMode,
238
+ spatialFilter,
239
+ },
172
240
  params: {column: column ?? '*', operation, operationExp},
173
241
  opts: {abortController},
174
242
  }).then((res: FormulaModelResponse) => normalizeObjectKeys(res.rows[0]));
@@ -185,14 +253,32 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
185
253
  async getHistogram(
186
254
  options: HistogramRequestOptions
187
255
  ): Promise<HistogramResponse> {
188
- const {filterOwner, spatialFilter, abortController, ...params} = options;
256
+ const {
257
+ filterOwner,
258
+ spatialFilter,
259
+ spatialFiltersMode,
260
+ spatialIndexReferenceViewState,
261
+ abortController,
262
+ ...params
263
+ } = options;
189
264
  const {column, operation, ticks} = params;
265
+ const source = this.getModelSource(filterOwner);
266
+ const spatialFiltersResolution = this._getSpatialFiltersResolution(
267
+ source,
268
+ spatialFilter,
269
+ spatialIndexReferenceViewState
270
+ );
190
271
 
191
272
  type HistogramModelResponse = {rows: {tick: number; value: number}[]};
192
273
 
193
274
  const data = await executeModel({
194
275
  model: 'histogram',
195
- source: {...this.getModelSource(filterOwner), spatialFilter},
276
+ source: {
277
+ ...source,
278
+ spatialFiltersResolution,
279
+ spatialFiltersMode,
280
+ spatialFilter,
281
+ },
196
282
  params: {column, operation, ticks},
197
283
  opts: {abortController},
198
284
  }).then((res: HistogramModelResponse) => normalizeObjectKeys(res.rows));
@@ -220,14 +306,32 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
220
306
  * or rendering a range slider UI for filtering.
221
307
  */
222
308
  async getRange(options: RangeRequestOptions): Promise<RangeResponse> {
223
- const {filterOwner, spatialFilter, abortController, ...params} = options;
309
+ const {
310
+ filterOwner,
311
+ spatialFilter,
312
+ spatialFiltersMode,
313
+ spatialIndexReferenceViewState,
314
+ abortController,
315
+ ...params
316
+ } = options;
224
317
  const {column} = params;
318
+ const source = this.getModelSource(filterOwner);
319
+ const spatialFiltersResolution = this._getSpatialFiltersResolution(
320
+ source,
321
+ spatialFilter,
322
+ spatialIndexReferenceViewState
323
+ );
225
324
 
226
325
  type RangeModelResponse = {rows: {min: number; max: number}[]};
227
326
 
228
327
  return executeModel({
229
328
  model: 'range',
230
- source: {...this.getModelSource(filterOwner), spatialFilter},
329
+ source: {
330
+ ...source,
331
+ spatialFiltersResolution,
332
+ spatialFiltersMode,
333
+ spatialFilter,
334
+ },
231
335
  params: {column},
232
336
  opts: {abortController},
233
337
  }).then((res: RangeModelResponse) => normalizeObjectKeys(res.rows[0]));
@@ -242,10 +346,24 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
242
346
  * values. Suitable for rendering scatter plots.
243
347
  */
244
348
  async getScatter(options: ScatterRequestOptions): Promise<ScatterResponse> {
245
- const {filterOwner, spatialFilter, abortController, ...params} = options;
349
+ const {
350
+ filterOwner,
351
+ spatialFilter,
352
+ spatialFiltersMode,
353
+ spatialIndexReferenceViewState,
354
+ abortController,
355
+ ...params
356
+ } = options;
246
357
  const {xAxisColumn, xAxisJoinOperation, yAxisColumn, yAxisJoinOperation} =
247
358
  params;
248
359
 
360
+ const source = this.getModelSource(filterOwner);
361
+ const spatialFiltersResolution = this._getSpatialFiltersResolution(
362
+ source,
363
+ spatialFilter,
364
+ spatialIndexReferenceViewState
365
+ );
366
+
249
367
  // Make sure this is sync with the same constant in cloud-native/maps-api
250
368
  const HARD_LIMIT = 500;
251
369
 
@@ -253,7 +371,12 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
253
371
 
254
372
  return executeModel({
255
373
  model: 'scatterplot',
256
- source: {...this.getModelSource(filterOwner), spatialFilter},
374
+ source: {
375
+ ...source,
376
+ spatialFiltersResolution,
377
+ spatialFiltersMode,
378
+ spatialFilter,
379
+ },
257
380
  params: {
258
381
  xAxisColumn,
259
382
  xAxisJoinOperation,
@@ -276,8 +399,21 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
276
399
  * sorting. Suitable for displaying tables and lists.
277
400
  */
278
401
  async getTable(options: TableRequestOptions): Promise<TableResponse> {
279
- const {filterOwner, spatialFilter, abortController, ...params} = options;
402
+ const {
403
+ filterOwner,
404
+ spatialFilter,
405
+ spatialFiltersMode,
406
+ spatialIndexReferenceViewState,
407
+ abortController,
408
+ ...params
409
+ } = options;
280
410
  const {columns, sortBy, sortDirection, offset = 0, limit = 10} = params;
411
+ const source = this.getModelSource(filterOwner);
412
+ const spatialFiltersResolution = this._getSpatialFiltersResolution(
413
+ source,
414
+ spatialFilter,
415
+ spatialIndexReferenceViewState
416
+ );
281
417
 
282
418
  type TableModelResponse = {
283
419
  rows: Record<string, number | string>[];
@@ -286,7 +422,12 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
286
422
 
287
423
  return executeModel({
288
424
  model: 'table',
289
- source: {...this.getModelSource(filterOwner), spatialFilter},
425
+ source: {
426
+ ...source,
427
+ spatialFiltersResolution,
428
+ spatialFiltersMode,
429
+ spatialFilter,
430
+ },
290
431
  params: {
291
432
  column: columns,
292
433
  sortBy,
@@ -313,7 +454,14 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
313
454
  async getTimeSeries(
314
455
  options: TimeSeriesRequestOptions
315
456
  ): Promise<TimeSeriesResponse> {
316
- const {filterOwner, abortController, spatialFilter, ...params} = options;
457
+ const {
458
+ filterOwner,
459
+ abortController,
460
+ spatialFilter,
461
+ spatialFiltersMode,
462
+ spatialIndexReferenceViewState,
463
+ ...params
464
+ } = options;
317
465
  const {
318
466
  column,
319
467
  operationColumn,
@@ -326,6 +474,13 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
326
474
  splitByCategoryValues,
327
475
  } = params;
328
476
 
477
+ const source = this.getModelSource(filterOwner);
478
+ const spatialFiltersResolution = this._getSpatialFiltersResolution(
479
+ source,
480
+ spatialFilter,
481
+ spatialIndexReferenceViewState
482
+ );
483
+
329
484
  type TimeSeriesModelResponse = {
330
485
  rows: {name: string; value: number}[];
331
486
  metadata: {categories: string[]};
@@ -333,7 +488,12 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
333
488
 
334
489
  return executeModel({
335
490
  model: 'timeseries',
336
- source: {...this.getModelSource(filterOwner), spatialFilter},
491
+ source: {
492
+ ...source,
493
+ spatialFiltersResolution,
494
+ spatialFiltersMode,
495
+ spatialFilter,
496
+ },
337
497
  params: {
338
498
  column,
339
499
  stepSize,