@carto/api-client 0.5.0-alpha.1 → 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.
@@ -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
  *
@@ -3881,6 +3881,7 @@ exports.FEATURE_GEOM_PROPERTY = FEATURE_GEOM_PROPERTY;
3881
3881
  exports.SOURCE_DEFAULTS = SOURCE_DEFAULTS;
3882
3882
  exports.WidgetQuerySource = WidgetQuerySource;
3883
3883
  exports.WidgetRemoteSource = WidgetRemoteSource;
3884
+ exports.WidgetSource = WidgetSource;
3884
3885
  exports.WidgetTableSource = WidgetTableSource;
3885
3886
  exports.WidgetTilesetSource = WidgetTilesetSource;
3886
3887
  exports.addFilter = addFilter;