@carto/api-client 0.5.0-alpha.0 → 0.5.0-alpha.2

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.
@@ -764,6 +764,91 @@ const boundaryTableSource = async function boundaryTableSource(options) {
764
764
  return baseSource('boundary', options, urlParameters);
765
765
  };
766
766
 
767
+ const DEFAULT_TILE_SIZE = 512;
768
+ const QUADBIN_ZOOM_MAX_OFFSET = 4;
769
+ function getSpatialFiltersResolution(source, viewState) {
770
+ var _source$dataResolutio, _source$aggregationRe;
771
+ const dataResolution = (_source$dataResolutio = source.dataResolution) != null ? _source$dataResolutio : Number.MAX_VALUE;
772
+ const aggregationResLevel = (_source$aggregationRe = source.aggregationResLevel) != null ? _source$aggregationRe : source.spatialDataType === 'h3' ? DEFAULT_AGGREGATION_RES_LEVEL_H3 : DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN;
773
+ const aggregationResLevelOffset = Math.max(0, Math.floor(aggregationResLevel));
774
+ const currentZoomInt = Math.ceil(viewState.zoom);
775
+ if (source.spatialDataType === 'h3') {
776
+ var _maxH3SpatialFiltersR, _maxH3SpatialFiltersR2;
777
+ const tileSize = DEFAULT_TILE_SIZE;
778
+ const maxResolutionForZoom = (_maxH3SpatialFiltersR = (_maxH3SpatialFiltersR2 = maxH3SpatialFiltersResolutions.find(([zoom]) => zoom === currentZoomInt)) == null ? void 0 : _maxH3SpatialFiltersR2[1]) != null ? _maxH3SpatialFiltersR : Math.max(0, currentZoomInt - 3);
779
+ const maxSpatialFiltersResolution = maxResolutionForZoom ? Math.min(dataResolution, maxResolutionForZoom) : dataResolution;
780
+ const hexagonResolution = getHexagonResolution(viewState, tileSize) + aggregationResLevelOffset;
781
+ return Math.min(hexagonResolution, maxSpatialFiltersResolution);
782
+ }
783
+ if (source.spatialDataType === 'quadbin') {
784
+ const maxResolutionForZoom = currentZoomInt + QUADBIN_ZOOM_MAX_OFFSET;
785
+ const maxSpatialFiltersResolution = Math.min(dataResolution, maxResolutionForZoom);
786
+ const quadsResolution = Math.floor(viewState.zoom) + aggregationResLevelOffset;
787
+ return Math.min(quadsResolution, maxSpatialFiltersResolution);
788
+ }
789
+ return undefined;
790
+ }
791
+ const maxH3SpatialFiltersResolutions = [[20, 14], [19, 13], [18, 12], [17, 11], [16, 10], [15, 9], [14, 8], [13, 7], [12, 7], [11, 7], [10, 6], [9, 6], [8, 5], [7, 4], [6, 4], [5, 3], [4, 2], [3, 1], [2, 1], [1, 0]];
792
+ // stolen from https://github.com/visgl/deck.gl/blob/master/modules/carto/src/layers/h3-tileset-2d.ts
793
+ // Relative scale factor (0 = no biasing, 2 = a few hexagons cover view)
794
+ const BIAS = 2;
795
+ // Resolution conversion function. Takes a WebMercatorViewport and returns
796
+ // a H3 resolution such that the screen space size of the hexagons is
797
+ // similar
798
+ function getHexagonResolution(viewport, tileSize) {
799
+ // Difference in given tile size compared to deck's internal 512px tile size,
800
+ // expressed as an offset to the viewport zoom.
801
+ const zoomOffset = Math.log2(tileSize / DEFAULT_TILE_SIZE);
802
+ const hexagonScaleFactor = 2 / 3 * (viewport.zoom - zoomOffset);
803
+ const latitudeScaleFactor = Math.log(1 / Math.cos(Math.PI * viewport.latitude / 180));
804
+ // Clip and bias
805
+ return Math.max(0, Math.floor(hexagonScaleFactor + latitudeScaleFactor - BIAS));
806
+ }
807
+
808
+ /**
809
+ * Source for Widget API requests on a data source defined by a SQL query.
810
+ *
811
+ * Abstract class. Use {@link WidgetQuerySource} or {@link WidgetTableSource}.
812
+ */
813
+ class WidgetSource {
814
+ constructor(props) {
815
+ this.props = void 0;
816
+ this.props = _extends({}, WidgetSource.defaultProps, props);
817
+ }
818
+ _getModelSource(owner) {
819
+ const props = this.props;
820
+ return {
821
+ apiVersion: props.apiVersion,
822
+ apiBaseUrl: props.apiBaseUrl,
823
+ clientId: props.clientId,
824
+ accessToken: props.accessToken,
825
+ connectionName: props.connectionName,
826
+ filters: getApplicableFilters(owner, props.filters),
827
+ filtersLogicalOperator: props.filtersLogicalOperator,
828
+ spatialDataType: props.spatialDataType,
829
+ spatialDataColumn: props.spatialDataColumn,
830
+ dataResolution: props.dataResolution
831
+ };
832
+ }
833
+ _getSpatialFiltersResolution(source, spatialFilter, referenceViewState) {
834
+ // spatialFiltersResolution applies only to spatial index sources.
835
+ if (!spatialFilter || source.spatialDataType === 'geo') {
836
+ return;
837
+ }
838
+ if (!referenceViewState) {
839
+ throw new Error('Missing required option, "spatialIndexReferenceViewState".');
840
+ }
841
+ return getSpatialFiltersResolution(source, referenceViewState);
842
+ }
843
+ }
844
+ WidgetSource.defaultProps = {
845
+ apiVersion: ApiVersion.V3,
846
+ apiBaseUrl: DEFAULT_API_BASE_URL,
847
+ clientId: getClient(),
848
+ filters: {},
849
+ filtersLogicalOperator: 'and'
850
+ };
851
+
767
852
  /**
768
853
  * Return more descriptive error from API
769
854
  * @internalRemarks Source: @carto/react-api
@@ -928,91 +1013,6 @@ function objectToURLSearchParams(object) {
928
1013
  return params;
929
1014
  }
930
1015
 
931
- const DEFAULT_TILE_SIZE = 512;
932
- const QUADBIN_ZOOM_MAX_OFFSET = 4;
933
- function getSpatialFiltersResolution(source, viewState) {
934
- var _source$dataResolutio, _source$aggregationRe;
935
- const dataResolution = (_source$dataResolutio = source.dataResolution) != null ? _source$dataResolutio : Number.MAX_VALUE;
936
- const aggregationResLevel = (_source$aggregationRe = source.aggregationResLevel) != null ? _source$aggregationRe : source.spatialDataType === 'h3' ? DEFAULT_AGGREGATION_RES_LEVEL_H3 : DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN;
937
- const aggregationResLevelOffset = Math.max(0, Math.floor(aggregationResLevel));
938
- const currentZoomInt = Math.ceil(viewState.zoom);
939
- if (source.spatialDataType === 'h3') {
940
- var _maxH3SpatialFiltersR, _maxH3SpatialFiltersR2;
941
- const tileSize = DEFAULT_TILE_SIZE;
942
- const maxResolutionForZoom = (_maxH3SpatialFiltersR = (_maxH3SpatialFiltersR2 = maxH3SpatialFiltersResolutions.find(([zoom]) => zoom === currentZoomInt)) == null ? void 0 : _maxH3SpatialFiltersR2[1]) != null ? _maxH3SpatialFiltersR : Math.max(0, currentZoomInt - 3);
943
- const maxSpatialFiltersResolution = maxResolutionForZoom ? Math.min(dataResolution, maxResolutionForZoom) : dataResolution;
944
- const hexagonResolution = getHexagonResolution(viewState, tileSize) + aggregationResLevelOffset;
945
- return Math.min(hexagonResolution, maxSpatialFiltersResolution);
946
- }
947
- if (source.spatialDataType === 'quadbin') {
948
- const maxResolutionForZoom = currentZoomInt + QUADBIN_ZOOM_MAX_OFFSET;
949
- const maxSpatialFiltersResolution = Math.min(dataResolution, maxResolutionForZoom);
950
- const quadsResolution = Math.floor(viewState.zoom) + aggregationResLevelOffset;
951
- return Math.min(quadsResolution, maxSpatialFiltersResolution);
952
- }
953
- return undefined;
954
- }
955
- const maxH3SpatialFiltersResolutions = [[20, 14], [19, 13], [18, 12], [17, 11], [16, 10], [15, 9], [14, 8], [13, 7], [12, 7], [11, 7], [10, 6], [9, 6], [8, 5], [7, 4], [6, 4], [5, 3], [4, 2], [3, 1], [2, 1], [1, 0]];
956
- // stolen from https://github.com/visgl/deck.gl/blob/master/modules/carto/src/layers/h3-tileset-2d.ts
957
- // Relative scale factor (0 = no biasing, 2 = a few hexagons cover view)
958
- const BIAS = 2;
959
- // Resolution conversion function. Takes a WebMercatorViewport and returns
960
- // a H3 resolution such that the screen space size of the hexagons is
961
- // similar
962
- function getHexagonResolution(viewport, tileSize) {
963
- // Difference in given tile size compared to deck's internal 512px tile size,
964
- // expressed as an offset to the viewport zoom.
965
- const zoomOffset = Math.log2(tileSize / DEFAULT_TILE_SIZE);
966
- const hexagonScaleFactor = 2 / 3 * (viewport.zoom - zoomOffset);
967
- const latitudeScaleFactor = Math.log(1 / Math.cos(Math.PI * viewport.latitude / 180));
968
- // Clip and bias
969
- return Math.max(0, Math.floor(hexagonScaleFactor + latitudeScaleFactor - BIAS));
970
- }
971
-
972
- /**
973
- * Source for Widget API requests on a data source defined by a SQL query.
974
- *
975
- * Abstract class. Use {@link WidgetQuerySource} or {@link WidgetTableSource}.
976
- */
977
- class WidgetSource {
978
- constructor(props) {
979
- this.props = void 0;
980
- this.props = _extends({}, WidgetSource.defaultProps, props);
981
- }
982
- _getModelSource(owner) {
983
- const props = this.props;
984
- return {
985
- apiVersion: props.apiVersion,
986
- apiBaseUrl: props.apiBaseUrl,
987
- clientId: props.clientId,
988
- accessToken: props.accessToken,
989
- connectionName: props.connectionName,
990
- filters: getApplicableFilters(owner, props.filters),
991
- filtersLogicalOperator: props.filtersLogicalOperator,
992
- spatialDataType: props.spatialDataType,
993
- spatialDataColumn: props.spatialDataColumn,
994
- dataResolution: props.dataResolution
995
- };
996
- }
997
- _getSpatialFiltersResolution(source, spatialFilter, referenceViewState) {
998
- // spatialFiltersResolution applies only to spatial index sources.
999
- if (!spatialFilter || source.spatialDataType === 'geo') {
1000
- return;
1001
- }
1002
- if (!referenceViewState) {
1003
- throw new Error('Missing required option, "spatialIndexReferenceViewState".');
1004
- }
1005
- return getSpatialFiltersResolution(source, referenceViewState);
1006
- }
1007
- }
1008
- WidgetSource.defaultProps = {
1009
- apiVersion: ApiVersion.V3,
1010
- apiBaseUrl: DEFAULT_API_BASE_URL,
1011
- clientId: getClient(),
1012
- filters: {},
1013
- filtersLogicalOperator: 'and'
1014
- };
1015
-
1016
1016
  const _excluded$1 = ["filterOwner", "spatialFilter", "spatialFiltersMode", "spatialIndexReferenceViewState", "abortController"],
1017
1017
  _excluded2 = ["filterOwner", "spatialFilter", "spatialFiltersMode", "spatialIndexReferenceViewState", "abortController"],
1018
1018
  _excluded3 = ["filterOwner", "spatialFilter", "spatialFiltersMode", "spatialIndexReferenceViewState", "abortController", "operationExp"],
@@ -2998,6 +2998,7 @@ const _excluded = ["filterOwner", "spatialFilter", "abortController"];
2998
2998
  class WidgetTilesetSource extends WidgetSource {
2999
2999
  constructor(...args) {
3000
3000
  super(...args);
3001
+ this._tiles = [];
3001
3002
  this._features = [];
3002
3003
  }
3003
3004
  getModelSource(owner) {
@@ -3006,15 +3007,25 @@ class WidgetTilesetSource extends WidgetSource {
3006
3007
  data: this.props.tableName
3007
3008
  });
3008
3009
  }
3009
- /** Loads features as a list of tiles (typically provided by deck.gl). */
3010
- loadTiles({
3011
- tiles,
3010
+ /**
3011
+ * Loads features as a list of tiles (typically provided by deck.gl).
3012
+ * After tiles are loaded, {@link extractTileFeatures} must be called
3013
+ * before computing statistics on the tiles.
3014
+ */
3015
+ loadTiles(tiles) {
3016
+ this._tiles = tiles;
3017
+ }
3018
+ /**
3019
+ * Extracts feature data from tiles previously loaded with {@link loadTiles}.
3020
+ * Must be called before computing statistics on tiles.
3021
+ */
3022
+ extractTileFeatures({
3012
3023
  spatialFilter,
3013
3024
  uniqueIdProperty,
3014
3025
  options
3015
3026
  }) {
3016
3027
  this._features = tileFeatures({
3017
- tiles,
3028
+ tiles: this._tiles,
3018
3029
  options,
3019
3030
  spatialFilter,
3020
3031
  uniqueIdProperty,
@@ -3251,10 +3262,11 @@ const h3QuerySource = async function h3QuerySource(options) {
3251
3262
  queryParameters,
3252
3263
  filters
3253
3264
  } = options;
3265
+ const spatialDataType = 'h3';
3254
3266
  const urlParameters = {
3255
3267
  aggregationExp,
3256
3268
  spatialDataColumn,
3257
- spatialDataType: 'h3',
3269
+ spatialDataType,
3258
3270
  q: sqlQuery
3259
3271
  };
3260
3272
  if (aggregationResLevel) {
@@ -3268,9 +3280,9 @@ const h3QuerySource = async function h3QuerySource(options) {
3268
3280
  }
3269
3281
  return baseSource('query', options, urlParameters).then(result => _extends({}, result, {
3270
3282
  widgetSource: new WidgetQuerySource(_extends({}, options, {
3271
- // NOTE: passing redundant spatialDataColumn here to apply the default value 'h3'
3283
+ // NOTE: Parameters with default values above must be explicitly passed here.
3272
3284
  spatialDataColumn,
3273
- spatialDataType: 'h3'
3285
+ spatialDataType
3274
3286
  }))
3275
3287
  }));
3276
3288
  };
@@ -3283,11 +3295,12 @@ const h3TableSource = async function h3TableSource(options) {
3283
3295
  tableName,
3284
3296
  filters
3285
3297
  } = options;
3298
+ const spatialDataType = 'h3';
3286
3299
  const urlParameters = {
3287
3300
  aggregationExp,
3288
3301
  name: tableName,
3289
3302
  spatialDataColumn,
3290
- spatialDataType: 'h3'
3303
+ spatialDataType
3291
3304
  };
3292
3305
  if (aggregationResLevel) {
3293
3306
  urlParameters.aggregationResLevel = String(aggregationResLevel);
@@ -3297,9 +3310,9 @@ const h3TableSource = async function h3TableSource(options) {
3297
3310
  }
3298
3311
  return baseSource('table', options, urlParameters).then(result => _extends({}, result, {
3299
3312
  widgetSource: new WidgetTableSource(_extends({}, options, {
3300
- // NOTE: passing redundant spatialDataColumn here to apply the default value 'h3'
3313
+ // NOTE: Parameters with default values above must be explicitly passed here.
3301
3314
  spatialDataColumn,
3302
- spatialDataType: 'h3'
3315
+ spatialDataType
3303
3316
  }))
3304
3317
  }));
3305
3318
  };
@@ -3352,11 +3365,12 @@ const quadbinQuerySource = async function quadbinQuerySource(options) {
3352
3365
  queryParameters,
3353
3366
  filters
3354
3367
  } = options;
3368
+ const spatialDataType = 'quadbin';
3355
3369
  const urlParameters = {
3356
3370
  aggregationExp,
3357
3371
  q: sqlQuery,
3358
3372
  spatialDataColumn,
3359
- spatialDataType: 'quadbin'
3373
+ spatialDataType
3360
3374
  };
3361
3375
  if (aggregationResLevel) {
3362
3376
  urlParameters.aggregationResLevel = String(aggregationResLevel);
@@ -3369,9 +3383,9 @@ const quadbinQuerySource = async function quadbinQuerySource(options) {
3369
3383
  }
3370
3384
  return baseSource('query', options, urlParameters).then(result => _extends({}, result, {
3371
3385
  widgetSource: new WidgetQuerySource(_extends({}, options, {
3372
- // NOTE: passing redundant spatialDataColumn here to apply the default value 'quadbin'
3386
+ // NOTE: Parameters with default values above must be explicitly passed here.
3373
3387
  spatialDataColumn,
3374
- spatialDataType: 'quadbin'
3388
+ spatialDataType
3375
3389
  }))
3376
3390
  }));
3377
3391
  };
@@ -3384,11 +3398,12 @@ const quadbinTableSource = async function quadbinTableSource(options) {
3384
3398
  tableName,
3385
3399
  filters
3386
3400
  } = options;
3401
+ const spatialDataType = 'quadbin';
3387
3402
  const urlParameters = {
3388
3403
  aggregationExp,
3389
3404
  name: tableName,
3390
3405
  spatialDataColumn,
3391
- spatialDataType: 'quadbin'
3406
+ spatialDataType
3392
3407
  };
3393
3408
  if (aggregationResLevel) {
3394
3409
  urlParameters.aggregationResLevel = String(aggregationResLevel);
@@ -3398,9 +3413,9 @@ const quadbinTableSource = async function quadbinTableSource(options) {
3398
3413
  }
3399
3414
  return baseSource('table', options, urlParameters).then(result => _extends({}, result, {
3400
3415
  widgetSource: new WidgetTableSource(_extends({}, options, {
3401
- // NOTE: passing redundant spatialDataColumn here to apply the default value 'quadbin'
3416
+ // NOTE: Parameters with default values above must be explicitly passed here.
3402
3417
  spatialDataColumn,
3403
- spatialDataType: 'quadbin'
3418
+ spatialDataType
3404
3419
  }))
3405
3420
  }));
3406
3421
  };
@@ -3432,9 +3447,10 @@ const vectorQuerySource = async function vectorQuerySource(options) {
3432
3447
  queryParameters,
3433
3448
  aggregationExp
3434
3449
  } = options;
3450
+ const spatialDataType = 'geo';
3435
3451
  const urlParameters = {
3436
3452
  spatialDataColumn,
3437
- spatialDataType: 'geo',
3453
+ spatialDataType,
3438
3454
  tileResolution: tileResolution.toString(),
3439
3455
  q: sqlQuery
3440
3456
  };
@@ -3452,8 +3468,10 @@ const vectorQuerySource = async function vectorQuerySource(options) {
3452
3468
  }
3453
3469
  return baseSource('query', options, urlParameters).then(result => _extends({}, result, {
3454
3470
  widgetSource: new WidgetQuerySource(_extends({}, options, {
3471
+ // NOTE: Parameters with default values above must be explicitly passed here.
3455
3472
  spatialDataColumn,
3456
- spatialDataType: 'geo'
3473
+ spatialDataType,
3474
+ tileResolution
3457
3475
  }))
3458
3476
  }));
3459
3477
  };
@@ -3467,10 +3485,11 @@ const vectorTableSource = async function vectorTableSource(options) {
3467
3485
  tileResolution = DEFAULT_TILE_RESOLUTION,
3468
3486
  aggregationExp
3469
3487
  } = options;
3488
+ const spatialDataType = 'geo';
3470
3489
  const urlParameters = {
3471
3490
  name: tableName,
3472
3491
  spatialDataColumn,
3473
- spatialDataType: 'geo',
3492
+ spatialDataType,
3474
3493
  tileResolution: tileResolution.toString()
3475
3494
  };
3476
3495
  if (columns) {
@@ -3484,8 +3503,10 @@ const vectorTableSource = async function vectorTableSource(options) {
3484
3503
  }
3485
3504
  return baseSource('table', options, urlParameters).then(result => _extends({}, result, {
3486
3505
  widgetSource: new WidgetTableSource(_extends({}, options, {
3506
+ // NOTE: Parameters with default values above must be explicitly passed here.
3487
3507
  spatialDataColumn,
3488
- spatialDataType: 'geo'
3508
+ spatialDataType,
3509
+ tileResolution
3489
3510
  }))
3490
3511
  }));
3491
3512
  };
@@ -3549,5 +3570,5 @@ const query = async function query(options) {
3549
3570
  });
3550
3571
  };
3551
3572
 
3552
- export { ApiVersion, CartoAPIError, DEFAULT_API_BASE_URL, FEATURE_GEOM_PROPERTY, FilterType, Provider, SOURCE_DEFAULTS, SpatialIndex, TileFormat, WidgetQuerySource, WidgetRemoteSource, WidgetTableSource, WidgetTilesetSource, addFilter, aggregate, aggregationFunctions, applyFilters, applySorting, boundaryQuerySource, boundaryTableSource, buildBinaryFeatureFilter, buildFeatureFilter, buildPublicMapUrl, buildStatsUrl, clearFilters, createPolygonSpatialFilter, createViewportSpatialFilter, filterFunctions, geojsonFeatures, getClient, getFilter, groupValuesByColumn, groupValuesByDateColumn, h3QuerySource, h3TableSource, h3TilesetSource, hasFilter, histogram, makeIntervalComplete, quadbinQuerySource, quadbinTableSource, quadbinTilesetSource, query, rasterSource, removeFilter, requestWithParameters, scatterPlot, setClient, tileFeatures, tileFeaturesGeometries, tileFeaturesSpatialIndex, transformToTileCoords, vectorQuerySource, vectorTableSource, vectorTilesetSource };
3573
+ export { ApiVersion, CartoAPIError, DEFAULT_API_BASE_URL, FEATURE_GEOM_PROPERTY, FilterType, Provider, SOURCE_DEFAULTS, SpatialIndex, TileFormat, WidgetQuerySource, WidgetRemoteSource, WidgetSource, WidgetTableSource, WidgetTilesetSource, addFilter, aggregate, aggregationFunctions, applyFilters, applySorting, boundaryQuerySource, boundaryTableSource, buildBinaryFeatureFilter, buildFeatureFilter, buildPublicMapUrl, buildStatsUrl, clearFilters, createPolygonSpatialFilter, createViewportSpatialFilter, filterFunctions, geojsonFeatures, getClient, getFilter, groupValuesByColumn, groupValuesByDateColumn, h3QuerySource, h3TableSource, h3TilesetSource, hasFilter, histogram, makeIntervalComplete, quadbinQuerySource, quadbinTableSource, quadbinTilesetSource, query, rasterSource, removeFilter, requestWithParameters, scatterPlot, setClient, tileFeatures, tileFeaturesGeometries, tileFeaturesSpatialIndex, transformToTileCoords, vectorQuerySource, vectorTableSource, vectorTilesetSource };
3553
3574
  //# sourceMappingURL=api-client.modern.js.map