@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.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@
4
4
 
5
5
  ## 0.4
6
6
 
7
+ ### 0.4.5
8
+
9
+ - types: Export TileResolution and SpatialFilterPolyfillMode types (#63)
10
+ - fix: Propagate spatialDataColumn and tileResolution defaults to widget source (#64)
11
+
7
12
  ### 0.4.4
8
13
 
9
14
  - feat: Add support for spatial index types (H3, quadbin) in Widget APIs
@@ -796,6 +796,95 @@ const boundaryTableSource = function (options) {
796
796
  }
797
797
  };
798
798
 
799
+ const DEFAULT_TILE_SIZE = 512;
800
+ const QUADBIN_ZOOM_MAX_OFFSET = 4;
801
+ function getSpatialFiltersResolution(source, viewState) {
802
+ const dataResolution = source.dataResolution ?? Number.MAX_VALUE;
803
+ const aggregationResLevel = source.aggregationResLevel ?? (source.spatialDataType === 'h3' ? DEFAULT_AGGREGATION_RES_LEVEL_H3 : DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN);
804
+ const aggregationResLevelOffset = Math.max(0, Math.floor(aggregationResLevel));
805
+ const currentZoomInt = Math.ceil(viewState.zoom);
806
+ if (source.spatialDataType === 'h3') {
807
+ const tileSize = DEFAULT_TILE_SIZE;
808
+ const maxResolutionForZoom = maxH3SpatialFiltersResolutions.find(_ref => {
809
+ let [zoom] = _ref;
810
+ return zoom === currentZoomInt;
811
+ })?.[1] ?? Math.max(0, currentZoomInt - 3);
812
+ const maxSpatialFiltersResolution = maxResolutionForZoom ? Math.min(dataResolution, maxResolutionForZoom) : dataResolution;
813
+ const hexagonResolution = getHexagonResolution(viewState, tileSize) + aggregationResLevelOffset;
814
+ return Math.min(hexagonResolution, maxSpatialFiltersResolution);
815
+ }
816
+ if (source.spatialDataType === 'quadbin') {
817
+ const maxResolutionForZoom = currentZoomInt + QUADBIN_ZOOM_MAX_OFFSET;
818
+ const maxSpatialFiltersResolution = Math.min(dataResolution, maxResolutionForZoom);
819
+ const quadsResolution = Math.floor(viewState.zoom) + aggregationResLevelOffset;
820
+ return Math.min(quadsResolution, maxSpatialFiltersResolution);
821
+ }
822
+ return undefined;
823
+ }
824
+ 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]];
825
+ // stolen from https://github.com/visgl/deck.gl/blob/master/modules/carto/src/layers/h3-tileset-2d.ts
826
+ // Relative scale factor (0 = no biasing, 2 = a few hexagons cover view)
827
+ const BIAS = 2;
828
+ // Resolution conversion function. Takes a WebMercatorViewport and returns
829
+ // a H3 resolution such that the screen space size of the hexagons is
830
+ // similar
831
+ function getHexagonResolution(viewport, tileSize) {
832
+ // Difference in given tile size compared to deck's internal 512px tile size,
833
+ // expressed as an offset to the viewport zoom.
834
+ const zoomOffset = Math.log2(tileSize / DEFAULT_TILE_SIZE);
835
+ const hexagonScaleFactor = 2 / 3 * (viewport.zoom - zoomOffset);
836
+ const latitudeScaleFactor = Math.log(1 / Math.cos(Math.PI * viewport.latitude / 180));
837
+ // Clip and bias
838
+ return Math.max(0, Math.floor(hexagonScaleFactor + latitudeScaleFactor - BIAS));
839
+ }
840
+
841
+ /**
842
+ * Source for Widget API requests on a data source defined by a SQL query.
843
+ *
844
+ * Abstract class. Use {@link WidgetQuerySource} or {@link WidgetTableSource}.
845
+ */
846
+ class WidgetSource {
847
+ constructor(props) {
848
+ this.props = void 0;
849
+ this.props = {
850
+ ...WidgetSource.defaultProps,
851
+ ...props
852
+ };
853
+ }
854
+ _getModelSource(owner) {
855
+ const props = this.props;
856
+ return {
857
+ apiVersion: props.apiVersion,
858
+ apiBaseUrl: props.apiBaseUrl,
859
+ clientId: props.clientId,
860
+ accessToken: props.accessToken,
861
+ connectionName: props.connectionName,
862
+ filters: getApplicableFilters(owner, props.filters),
863
+ filtersLogicalOperator: props.filtersLogicalOperator,
864
+ spatialDataType: props.spatialDataType,
865
+ spatialDataColumn: props.spatialDataColumn,
866
+ dataResolution: props.dataResolution
867
+ };
868
+ }
869
+ _getSpatialFiltersResolution(source, spatialFilter, referenceViewState) {
870
+ // spatialFiltersResolution applies only to spatial index sources.
871
+ if (!spatialFilter || source.spatialDataType === 'geo') {
872
+ return;
873
+ }
874
+ if (!referenceViewState) {
875
+ throw new Error('Missing required option, "spatialIndexReferenceViewState".');
876
+ }
877
+ return getSpatialFiltersResolution(source, referenceViewState);
878
+ }
879
+ }
880
+ WidgetSource.defaultProps = {
881
+ apiVersion: exports.ApiVersion.V3,
882
+ apiBaseUrl: DEFAULT_API_BASE_URL,
883
+ clientId: getClient(),
884
+ filters: {},
885
+ filtersLogicalOperator: 'and'
886
+ };
887
+
799
888
  /**
800
889
  * Return more descriptive error from API
801
890
  * @internalRemarks Source: @carto/react-api
@@ -990,95 +1079,6 @@ function objectToURLSearchParams(object) {
990
1079
  return params;
991
1080
  }
992
1081
 
993
- const DEFAULT_TILE_SIZE = 512;
994
- const QUADBIN_ZOOM_MAX_OFFSET = 4;
995
- function getSpatialFiltersResolution(source, viewState) {
996
- const dataResolution = source.dataResolution ?? Number.MAX_VALUE;
997
- const aggregationResLevel = source.aggregationResLevel ?? (source.spatialDataType === 'h3' ? DEFAULT_AGGREGATION_RES_LEVEL_H3 : DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN);
998
- const aggregationResLevelOffset = Math.max(0, Math.floor(aggregationResLevel));
999
- const currentZoomInt = Math.ceil(viewState.zoom);
1000
- if (source.spatialDataType === 'h3') {
1001
- const tileSize = DEFAULT_TILE_SIZE;
1002
- const maxResolutionForZoom = maxH3SpatialFiltersResolutions.find(_ref => {
1003
- let [zoom] = _ref;
1004
- return zoom === currentZoomInt;
1005
- })?.[1] ?? Math.max(0, currentZoomInt - 3);
1006
- const maxSpatialFiltersResolution = maxResolutionForZoom ? Math.min(dataResolution, maxResolutionForZoom) : dataResolution;
1007
- const hexagonResolution = getHexagonResolution(viewState, tileSize) + aggregationResLevelOffset;
1008
- return Math.min(hexagonResolution, maxSpatialFiltersResolution);
1009
- }
1010
- if (source.spatialDataType === 'quadbin') {
1011
- const maxResolutionForZoom = currentZoomInt + QUADBIN_ZOOM_MAX_OFFSET;
1012
- const maxSpatialFiltersResolution = Math.min(dataResolution, maxResolutionForZoom);
1013
- const quadsResolution = Math.floor(viewState.zoom) + aggregationResLevelOffset;
1014
- return Math.min(quadsResolution, maxSpatialFiltersResolution);
1015
- }
1016
- return undefined;
1017
- }
1018
- 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]];
1019
- // stolen from https://github.com/visgl/deck.gl/blob/master/modules/carto/src/layers/h3-tileset-2d.ts
1020
- // Relative scale factor (0 = no biasing, 2 = a few hexagons cover view)
1021
- const BIAS = 2;
1022
- // Resolution conversion function. Takes a WebMercatorViewport and returns
1023
- // a H3 resolution such that the screen space size of the hexagons is
1024
- // similar
1025
- function getHexagonResolution(viewport, tileSize) {
1026
- // Difference in given tile size compared to deck's internal 512px tile size,
1027
- // expressed as an offset to the viewport zoom.
1028
- const zoomOffset = Math.log2(tileSize / DEFAULT_TILE_SIZE);
1029
- const hexagonScaleFactor = 2 / 3 * (viewport.zoom - zoomOffset);
1030
- const latitudeScaleFactor = Math.log(1 / Math.cos(Math.PI * viewport.latitude / 180));
1031
- // Clip and bias
1032
- return Math.max(0, Math.floor(hexagonScaleFactor + latitudeScaleFactor - BIAS));
1033
- }
1034
-
1035
- /**
1036
- * Source for Widget API requests on a data source defined by a SQL query.
1037
- *
1038
- * Abstract class. Use {@link WidgetQuerySource} or {@link WidgetTableSource}.
1039
- */
1040
- class WidgetSource {
1041
- constructor(props) {
1042
- this.props = void 0;
1043
- this.props = {
1044
- ...WidgetSource.defaultProps,
1045
- ...props
1046
- };
1047
- }
1048
- _getModelSource(owner) {
1049
- const props = this.props;
1050
- return {
1051
- apiVersion: props.apiVersion,
1052
- apiBaseUrl: props.apiBaseUrl,
1053
- clientId: props.clientId,
1054
- accessToken: props.accessToken,
1055
- connectionName: props.connectionName,
1056
- filters: getApplicableFilters(owner, props.filters),
1057
- filtersLogicalOperator: props.filtersLogicalOperator,
1058
- spatialDataType: props.spatialDataType,
1059
- spatialDataColumn: props.spatialDataColumn,
1060
- dataResolution: props.dataResolution
1061
- };
1062
- }
1063
- _getSpatialFiltersResolution(source, spatialFilter, referenceViewState) {
1064
- // spatialFiltersResolution applies only to spatial index sources.
1065
- if (!spatialFilter || source.spatialDataType === 'geo') {
1066
- return;
1067
- }
1068
- if (!referenceViewState) {
1069
- throw new Error('Missing required option, "spatialIndexReferenceViewState".');
1070
- }
1071
- return getSpatialFiltersResolution(source, referenceViewState);
1072
- }
1073
- }
1074
- WidgetSource.defaultProps = {
1075
- apiVersion: exports.ApiVersion.V3,
1076
- apiBaseUrl: DEFAULT_API_BASE_URL,
1077
- clientId: getClient(),
1078
- filters: {},
1079
- filtersLogicalOperator: 'and'
1080
- };
1081
-
1082
1082
  /**
1083
1083
  * Source for Widget API requests.
1084
1084
  *
@@ -3152,6 +3152,7 @@ function scatterPlot(_ref) {
3152
3152
  class WidgetTilesetSource extends WidgetSource {
3153
3153
  constructor() {
3154
3154
  super(...arguments);
3155
+ this._tiles = [];
3155
3156
  this._features = [];
3156
3157
  }
3157
3158
  getModelSource(owner) {
@@ -3161,16 +3162,26 @@ class WidgetTilesetSource extends WidgetSource {
3161
3162
  data: this.props.tableName
3162
3163
  };
3163
3164
  }
3164
- /** Loads features as a list of tiles (typically provided by deck.gl). */
3165
- loadTiles(_ref) {
3165
+ /**
3166
+ * Loads features as a list of tiles (typically provided by deck.gl).
3167
+ * After tiles are loaded, {@link extractTileFeatures} must be called
3168
+ * before computing statistics on the tiles.
3169
+ */
3170
+ loadTiles(tiles) {
3171
+ this._tiles = tiles;
3172
+ }
3173
+ /**
3174
+ * Extracts feature data from tiles previously loaded with {@link loadTiles}.
3175
+ * Must be called before computing statistics on tiles.
3176
+ */
3177
+ extractTileFeatures(_ref) {
3166
3178
  let {
3167
- tiles,
3168
3179
  spatialFilter,
3169
3180
  uniqueIdProperty,
3170
3181
  options
3171
3182
  } = _ref;
3172
3183
  this._features = tileFeatures({
3173
- tiles,
3184
+ tiles: this._tiles,
3174
3185
  options,
3175
3186
  spatialFilter,
3176
3187
  uniqueIdProperty,
@@ -3461,10 +3472,11 @@ const h3QuerySource = function (options) {
3461
3472
  queryParameters,
3462
3473
  filters
3463
3474
  } = options;
3475
+ const spatialDataType = 'h3';
3464
3476
  const urlParameters = {
3465
3477
  aggregationExp,
3466
3478
  spatialDataColumn,
3467
- spatialDataType: 'h3',
3479
+ spatialDataType,
3468
3480
  q: sqlQuery
3469
3481
  };
3470
3482
  if (aggregationResLevel) {
@@ -3480,9 +3492,9 @@ const h3QuerySource = function (options) {
3480
3492
  ...result,
3481
3493
  widgetSource: new WidgetQuerySource({
3482
3494
  ...options,
3483
- // NOTE: passing redundant spatialDataColumn here to apply the default value 'h3'
3495
+ // NOTE: Parameters with default values above must be explicitly passed here.
3484
3496
  spatialDataColumn,
3485
- spatialDataType: 'h3'
3497
+ spatialDataType
3486
3498
  })
3487
3499
  })));
3488
3500
  } catch (e) {
@@ -3503,11 +3515,12 @@ const h3TableSource = function (options) {
3503
3515
  tableName,
3504
3516
  filters
3505
3517
  } = options;
3518
+ const spatialDataType = 'h3';
3506
3519
  const urlParameters = {
3507
3520
  aggregationExp,
3508
3521
  name: tableName,
3509
3522
  spatialDataColumn,
3510
- spatialDataType: 'h3'
3523
+ spatialDataType
3511
3524
  };
3512
3525
  if (aggregationResLevel) {
3513
3526
  urlParameters.aggregationResLevel = String(aggregationResLevel);
@@ -3519,9 +3532,9 @@ const h3TableSource = function (options) {
3519
3532
  ...result,
3520
3533
  widgetSource: new WidgetTableSource({
3521
3534
  ...options,
3522
- // NOTE: passing redundant spatialDataColumn here to apply the default value 'h3'
3535
+ // NOTE: Parameters with default values above must be explicitly passed here.
3523
3536
  spatialDataColumn,
3524
- spatialDataType: 'h3'
3537
+ spatialDataType
3525
3538
  })
3526
3539
  })));
3527
3540
  } catch (e) {
@@ -3595,11 +3608,12 @@ const quadbinQuerySource = function (options) {
3595
3608
  queryParameters,
3596
3609
  filters
3597
3610
  } = options;
3611
+ const spatialDataType = 'quadbin';
3598
3612
  const urlParameters = {
3599
3613
  aggregationExp,
3600
3614
  q: sqlQuery,
3601
3615
  spatialDataColumn,
3602
- spatialDataType: 'quadbin'
3616
+ spatialDataType
3603
3617
  };
3604
3618
  if (aggregationResLevel) {
3605
3619
  urlParameters.aggregationResLevel = String(aggregationResLevel);
@@ -3614,9 +3628,9 @@ const quadbinQuerySource = function (options) {
3614
3628
  ...result,
3615
3629
  widgetSource: new WidgetQuerySource({
3616
3630
  ...options,
3617
- // NOTE: passing redundant spatialDataColumn here to apply the default value 'quadbin'
3631
+ // NOTE: Parameters with default values above must be explicitly passed here.
3618
3632
  spatialDataColumn,
3619
- spatialDataType: 'quadbin'
3633
+ spatialDataType
3620
3634
  })
3621
3635
  })));
3622
3636
  } catch (e) {
@@ -3637,11 +3651,12 @@ const quadbinTableSource = function (options) {
3637
3651
  tableName,
3638
3652
  filters
3639
3653
  } = options;
3654
+ const spatialDataType = 'quadbin';
3640
3655
  const urlParameters = {
3641
3656
  aggregationExp,
3642
3657
  name: tableName,
3643
3658
  spatialDataColumn,
3644
- spatialDataType: 'quadbin'
3659
+ spatialDataType
3645
3660
  };
3646
3661
  if (aggregationResLevel) {
3647
3662
  urlParameters.aggregationResLevel = String(aggregationResLevel);
@@ -3653,9 +3668,9 @@ const quadbinTableSource = function (options) {
3653
3668
  ...result,
3654
3669
  widgetSource: new WidgetTableSource({
3655
3670
  ...options,
3656
- // NOTE: passing redundant spatialDataColumn here to apply the default value 'quadbin'
3671
+ // NOTE: Parameters with default values above must be explicitly passed here.
3657
3672
  spatialDataColumn,
3658
- spatialDataType: 'quadbin'
3673
+ spatialDataType
3659
3674
  })
3660
3675
  })));
3661
3676
  } catch (e) {
@@ -3704,9 +3719,10 @@ const vectorQuerySource = function (options) {
3704
3719
  queryParameters,
3705
3720
  aggregationExp
3706
3721
  } = options;
3722
+ const spatialDataType = 'geo';
3707
3723
  const urlParameters = {
3708
3724
  spatialDataColumn,
3709
- spatialDataType: 'geo',
3725
+ spatialDataType,
3710
3726
  tileResolution: tileResolution.toString(),
3711
3727
  q: sqlQuery
3712
3728
  };
@@ -3726,8 +3742,10 @@ const vectorQuerySource = function (options) {
3726
3742
  ...result,
3727
3743
  widgetSource: new WidgetQuerySource({
3728
3744
  ...options,
3745
+ // NOTE: Parameters with default values above must be explicitly passed here.
3729
3746
  spatialDataColumn,
3730
- spatialDataType: 'geo'
3747
+ spatialDataType,
3748
+ tileResolution
3731
3749
  })
3732
3750
  })));
3733
3751
  } catch (e) {
@@ -3749,10 +3767,11 @@ const vectorTableSource = function (options) {
3749
3767
  tileResolution = DEFAULT_TILE_RESOLUTION,
3750
3768
  aggregationExp
3751
3769
  } = options;
3770
+ const spatialDataType = 'geo';
3752
3771
  const urlParameters = {
3753
3772
  name: tableName,
3754
3773
  spatialDataColumn,
3755
- spatialDataType: 'geo',
3774
+ spatialDataType,
3756
3775
  tileResolution: tileResolution.toString()
3757
3776
  };
3758
3777
  if (columns) {
@@ -3768,8 +3787,10 @@ const vectorTableSource = function (options) {
3768
3787
  ...result,
3769
3788
  widgetSource: new WidgetTableSource({
3770
3789
  ...options,
3790
+ // NOTE: Parameters with default values above must be explicitly passed here.
3771
3791
  spatialDataColumn,
3772
- spatialDataType: 'geo'
3792
+ spatialDataType,
3793
+ tileResolution
3773
3794
  })
3774
3795
  })));
3775
3796
  } catch (e) {
@@ -3860,6 +3881,7 @@ exports.FEATURE_GEOM_PROPERTY = FEATURE_GEOM_PROPERTY;
3860
3881
  exports.SOURCE_DEFAULTS = SOURCE_DEFAULTS;
3861
3882
  exports.WidgetQuerySource = WidgetQuerySource;
3862
3883
  exports.WidgetRemoteSource = WidgetRemoteSource;
3884
+ exports.WidgetSource = WidgetSource;
3863
3885
  exports.WidgetTableSource = WidgetTableSource;
3864
3886
  exports.WidgetTilesetSource = WidgetTilesetSource;
3865
3887
  exports.addFilter = addFilter;