@carto/api-client 0.4.2 → 0.4.4

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/build/api/query.d.ts +1 -1
  3. package/build/api-client.cjs +1136 -984
  4. package/build/api-client.cjs.map +1 -1
  5. package/build/api-client.modern.js +1014 -869
  6. package/build/api-client.modern.js.map +1 -1
  7. package/build/index.d.ts +1 -1
  8. package/build/models/model.d.ts +7 -1
  9. package/build/sources/boundary-query-source.d.ts +2 -1
  10. package/build/sources/boundary-table-source.d.ts +2 -1
  11. package/build/sources/h3-query-source.d.ts +2 -1
  12. package/build/sources/h3-table-source.d.ts +2 -1
  13. package/build/sources/h3-tileset-source.d.ts +2 -1
  14. package/build/sources/index.d.ts +13 -14
  15. package/build/sources/quadbin-query-source.d.ts +2 -1
  16. package/build/sources/quadbin-table-source.d.ts +2 -1
  17. package/build/sources/quadbin-tileset-source.d.ts +2 -1
  18. package/build/sources/raster-source.d.ts +2 -1
  19. package/build/sources/types.d.ts +48 -37
  20. package/build/sources/vector-query-source.d.ts +2 -1
  21. package/build/sources/vector-table-source.d.ts +2 -1
  22. package/build/sources/vector-tileset-source.d.ts +2 -1
  23. package/build/spatial-index.d.ts +8 -0
  24. package/build/utils.d.ts +1 -1
  25. package/build/widget-sources/types.d.ts +9 -1
  26. package/build/widget-sources/widget-base-source.d.ts +3 -3
  27. package/package.json +1 -1
  28. package/src/api/query.ts +1 -2
  29. package/src/index.ts +1 -36
  30. package/src/models/model.ts +47 -24
  31. package/src/sources/boundary-query-source.ts +4 -2
  32. package/src/sources/boundary-table-source.ts +4 -2
  33. package/src/sources/h3-query-source.ts +10 -2
  34. package/src/sources/h3-table-source.ts +9 -2
  35. package/src/sources/h3-tileset-source.ts +4 -2
  36. package/src/sources/index.ts +54 -24
  37. package/src/sources/quadbin-query-source.ts +10 -2
  38. package/src/sources/quadbin-table-source.ts +10 -2
  39. package/src/sources/quadbin-tileset-source.ts +4 -2
  40. package/src/sources/raster-source.ts +4 -2
  41. package/src/sources/types.ts +54 -40
  42. package/src/sources/vector-query-source.ts +13 -2
  43. package/src/sources/vector-table-source.ts +14 -2
  44. package/src/sources/vector-tileset-source.ts +4 -2
  45. package/src/spatial-index.ts +111 -0
  46. package/src/utils.ts +1 -1
  47. package/src/widget-sources/types.ts +10 -1
  48. package/src/widget-sources/widget-base-source.ts +183 -23
@@ -29,9 +29,11 @@ type UrlParameters = {
29
29
  filters?: Record<string, unknown>;
30
30
  };
31
31
 
32
+ export type H3TableSourceResponse = TilejsonResult & WidgetTableSourceResult;
33
+
32
34
  export const h3TableSource = async function (
33
35
  options: H3TableSourceOptions
34
- ): Promise<TilejsonResult & WidgetTableSourceResult> {
36
+ ): Promise<H3TableSourceResponse> {
35
37
  const {
36
38
  aggregationExp,
37
39
  aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_H3,
@@ -55,7 +57,12 @@ export const h3TableSource = async function (
55
57
  return baseSource<UrlParameters>('table', options, urlParameters).then(
56
58
  (result) => ({
57
59
  ...(result as TilejsonResult),
58
- widgetSource: new WidgetTableSource(options),
60
+ widgetSource: new WidgetTableSource({
61
+ ...options,
62
+ // NOTE: passing redundant spatialDataColumn here to apply the default value 'h3'
63
+ spatialDataColumn,
64
+ spatialDataType: 'h3',
65
+ }),
59
66
  })
60
67
  );
61
68
  };
@@ -12,9 +12,11 @@ import type {
12
12
  export type H3TilesetSourceOptions = SourceOptions & TilesetSourceOptions;
13
13
  type UrlParameters = {name: string};
14
14
 
15
+ export type H3TilesetSourceResponse = TilejsonResult;
16
+
15
17
  export const h3TilesetSource = async function (
16
18
  options: H3TilesetSourceOptions
17
- ): Promise<TilejsonResult> {
19
+ ): Promise<H3TilesetSourceResponse> {
18
20
  const {tableName} = options;
19
21
  const urlParameters: UrlParameters = {name: tableName};
20
22
 
@@ -22,5 +24,5 @@ export const h3TilesetSource = async function (
22
24
  'tileset',
23
25
  options,
24
26
  urlParameters
25
- ) as Promise<TilejsonResult>;
27
+ ) as Promise<H3TilesetSourceResponse>;
26
28
  };
@@ -4,56 +4,86 @@
4
4
 
5
5
  export {SOURCE_DEFAULTS} from './base-source';
6
6
  export type {
7
- VectorLayer,
8
- RasterMetadataBandStats,
9
- RasterBandColorinterp,
10
- RasterMetadataBand,
11
- RasterMetadata,
12
- TilejsonResult,
13
7
  GeojsonResult,
14
8
  JsonResult,
15
9
  QueryResult,
10
+ QuerySourceOptions,
11
+ RasterBandColorinterp,
12
+ RasterMetadata,
13
+ RasterMetadataBand,
14
+ RasterMetadataBandStats,
15
+ SourceOptions,
16
+ TableSourceOptions,
17
+ TilejsonResult,
18
+ TilesetSourceOptions,
19
+ VectorLayer,
16
20
  } from './types';
17
21
 
18
22
  export {boundaryQuerySource} from './boundary-query-source';
19
- export type {BoundaryQuerySourceOptions} from './boundary-query-source';
23
+ export type {
24
+ BoundaryQuerySourceOptions,
25
+ BoundaryQuerySourceResponse,
26
+ } from './boundary-query-source';
20
27
 
21
28
  export {boundaryTableSource} from './boundary-table-source';
22
- export type {BoundaryTableSourceOptions} from './boundary-table-source';
29
+ export type {
30
+ BoundaryTableSourceOptions,
31
+ BoundaryTableSourceResponse,
32
+ } from './boundary-table-source';
23
33
 
24
34
  export {h3QuerySource} from './h3-query-source';
25
- export type {H3QuerySourceOptions} from './h3-query-source';
35
+ export type {
36
+ H3QuerySourceOptions,
37
+ H3QuerySourceResponse,
38
+ } from './h3-query-source';
26
39
 
27
40
  export {h3TableSource} from './h3-table-source';
28
- export type {H3TableSourceOptions} from './h3-table-source';
41
+ export type {
42
+ H3TableSourceOptions,
43
+ H3TableSourceResponse,
44
+ } from './h3-table-source';
29
45
 
30
46
  export {h3TilesetSource} from './h3-tileset-source';
31
- export type {H3TilesetSourceOptions} from './h3-tileset-source';
47
+ export type {
48
+ H3TilesetSourceOptions,
49
+ H3TilesetSourceResponse,
50
+ } from './h3-tileset-source';
32
51
 
33
52
  export {rasterSource} from './raster-source';
34
- export type {RasterSourceOptions} from './raster-source';
53
+ export type {RasterSourceOptions, RasterSourceResponse} from './raster-source';
35
54
 
36
55
  export {quadbinQuerySource} from './quadbin-query-source';
37
- export type {QuadbinQuerySourceOptions} from './quadbin-query-source';
56
+ export type {
57
+ QuadbinQuerySourceOptions,
58
+ QuadbinQuerySourceResponse,
59
+ } from './quadbin-query-source';
38
60
 
39
61
  export {quadbinTableSource} from './quadbin-table-source';
40
- export type {QuadbinTableSourceOptions} from './quadbin-table-source';
62
+ export type {
63
+ QuadbinTableSourceOptions,
64
+ QuadbinTableSourceResponse,
65
+ } from './quadbin-table-source';
41
66
 
42
67
  export {quadbinTilesetSource} from './quadbin-tileset-source';
43
- export type {QuadbinTilesetSourceOptions} from './quadbin-tileset-source';
68
+ export type {
69
+ QuadbinTilesetSourceOptions,
70
+ QuadbinTilesetSourceResponse,
71
+ } from './quadbin-tileset-source';
44
72
 
45
73
  export {vectorQuerySource} from './vector-query-source';
46
- export type {VectorQuerySourceOptions} from './vector-query-source';
74
+ export type {
75
+ VectorQuerySourceOptions,
76
+ VectorQuerySourceResponse,
77
+ } from './vector-query-source';
47
78
 
48
79
  export {vectorTableSource} from './vector-table-source';
49
- export type {VectorTableSourceOptions} from './vector-table-source';
80
+ export type {
81
+ VectorTableSourceOptions,
82
+ VectorTableSourceResponse,
83
+ } from './vector-table-source';
50
84
 
51
85
  export {vectorTilesetSource} from './vector-tileset-source';
52
- export type {VectorTilesetSourceOptions} from './vector-tileset-source';
53
-
54
86
  export type {
55
- SourceOptions,
56
- QuerySourceOptions,
57
- TableSourceOptions,
58
- TilesetSourceOptions,
59
- } from './types';
87
+ VectorTilesetSourceOptions,
88
+ VectorTilesetSourceResponse,
89
+ } from './vector-tileset-source';
@@ -30,9 +30,12 @@ type UrlParameters = {
30
30
  filters?: Record<string, unknown>;
31
31
  };
32
32
 
33
+ export type QuadbinQuerySourceResponse = TilejsonResult &
34
+ WidgetQuerySourceResult;
35
+
33
36
  export const quadbinQuerySource = async function (
34
37
  options: QuadbinQuerySourceOptions
35
- ): Promise<TilejsonResult & WidgetQuerySourceResult> {
38
+ ): Promise<QuadbinQuerySourceResponse> {
36
39
  const {
37
40
  aggregationExp,
38
41
  aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN,
@@ -60,7 +63,12 @@ export const quadbinQuerySource = async function (
60
63
  return baseSource<UrlParameters>('query', options, urlParameters).then(
61
64
  (result) => ({
62
65
  ...(result as TilejsonResult),
63
- widgetSource: new WidgetQuerySource(options),
66
+ widgetSource: new WidgetQuerySource({
67
+ ...options,
68
+ // NOTE: passing redundant spatialDataColumn here to apply the default value 'quadbin'
69
+ spatialDataColumn,
70
+ spatialDataType: 'quadbin',
71
+ }),
64
72
  })
65
73
  );
66
74
  };
@@ -29,9 +29,12 @@ type UrlParameters = {
29
29
  filters?: Record<string, unknown>;
30
30
  };
31
31
 
32
+ export type QuadbinTableSourceResponse = TilejsonResult &
33
+ WidgetTableSourceResult;
34
+
32
35
  export const quadbinTableSource = async function (
33
36
  options: QuadbinTableSourceOptions
34
- ): Promise<TilejsonResult & WidgetTableSourceResult> {
37
+ ): Promise<QuadbinTableSourceResponse> {
35
38
  const {
36
39
  aggregationExp,
37
40
  aggregationResLevel = DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN,
@@ -56,7 +59,12 @@ export const quadbinTableSource = async function (
56
59
  return baseSource<UrlParameters>('table', options, urlParameters).then(
57
60
  (result) => ({
58
61
  ...(result as TilejsonResult),
59
- widgetSource: new WidgetTableSource(options),
62
+ widgetSource: new WidgetTableSource({
63
+ ...options,
64
+ // NOTE: passing redundant spatialDataColumn here to apply the default value 'quadbin'
65
+ spatialDataColumn,
66
+ spatialDataType: 'quadbin',
67
+ }),
60
68
  })
61
69
  );
62
70
  };
@@ -12,9 +12,11 @@ import type {
12
12
  export type QuadbinTilesetSourceOptions = SourceOptions & TilesetSourceOptions;
13
13
  type UrlParameters = {name: string};
14
14
 
15
+ export type QuadbinTilesetSourceResponse = TilejsonResult;
16
+
15
17
  export const quadbinTilesetSource = async function (
16
18
  options: QuadbinTilesetSourceOptions
17
- ): Promise<TilejsonResult> {
19
+ ): Promise<QuadbinTilesetSourceResponse> {
18
20
  const {tableName} = options;
19
21
  const urlParameters: UrlParameters = {name: tableName};
20
22
 
@@ -22,5 +24,5 @@ export const quadbinTilesetSource = async function (
22
24
  'tileset',
23
25
  options,
24
26
  urlParameters
25
- ) as Promise<TilejsonResult>;
27
+ ) as Promise<QuadbinTilesetSourceResponse>;
26
28
  };
@@ -18,9 +18,11 @@ type UrlParameters = {
18
18
  filters?: Record<string, unknown>;
19
19
  };
20
20
 
21
+ export type RasterSourceResponse = TilejsonResult;
22
+
21
23
  export const rasterSource = async function (
22
24
  options: RasterSourceOptions
23
- ): Promise<TilejsonResult> {
25
+ ): Promise<RasterSourceResponse> {
24
26
  const {tableName, filters} = options;
25
27
  const urlParameters: UrlParameters = {name: tableName};
26
28
  if (filters) {
@@ -30,5 +32,5 @@ export const rasterSource = async function (
30
32
  'raster',
31
33
  options,
32
34
  urlParameters
33
- ) as Promise<TilejsonResult>;
35
+ ) as Promise<RasterSourceResponse>;
34
36
  };
@@ -48,6 +48,33 @@ export type SourceOptionalOptions = {
48
48
  */
49
49
  maxLengthURL?: number;
50
50
 
51
+ /**
52
+ * The column name and the type of geospatial support.
53
+ *
54
+ * If not present, defaults to `'geom'` for generic queries, `'quadbin'` for Quadbin sources and `'h3'` for H3 sources.
55
+ */
56
+ spatialDataColumn?: string;
57
+
58
+ /**
59
+ * The type of geospatial support. Defaults to `'geo'`.
60
+ */
61
+ spatialDataType?: SpatialDataType;
62
+
63
+ /**
64
+ * Relative resolution of a tile. Higher values increase density and data size. At `tileResolution = 1`, tile geometry is
65
+ * quantized to a 1024x1024 grid. Increasing or decreasing the resolution will increase or decrease the dimensions of
66
+ * the quantization grid proportionately.
67
+ *
68
+ * Supported `tileResolution` values, with corresponding grid sizes:
69
+ *
70
+ * - 0.25: 256x256
71
+ * - 0.5: 512x512
72
+ * - 1: 1024x1024
73
+ * - 2: 2048x2048
74
+ * - 4: 4096x4096
75
+ */
76
+ tileResolution?: TileResolution;
77
+
51
78
  /**
52
79
  * By default, local in-memory caching is enabled.
53
80
  */
@@ -89,6 +116,11 @@ export type AggregationOptions = {
89
116
  * @default 6 for quadbin and 4 for h3 sources
90
117
  */
91
118
  aggregationResLevel?: number;
119
+
120
+ /**
121
+ * Original resolution of the spatial index data as stored in the DW
122
+ */
123
+ dataResolution?: number;
92
124
  };
93
125
 
94
126
  export type FilterOptions = {
@@ -99,31 +131,9 @@ export type FilterOptions = {
99
131
  };
100
132
 
101
133
  export type QuerySourceOptions = {
102
- /**
103
- * The column name and the type of geospatial support.
104
- *
105
- * If not present, defaults to `'geom'` for generic queries, `'quadbin'` for Quadbin sources and `'h3'` for H3 sources.
106
- */
107
- spatialDataColumn?: string;
108
-
109
- /** SQL query. */
134
+ /** Full SQL query with query paremeter placeholders (if any). */
110
135
  sqlQuery: string;
111
136
 
112
- /**
113
- * Relative resolution of a tile. Higher values increase density and data size. At `tileResolution = 1`, tile geometry is
114
- * quantized to a 1024x1024 grid. Increasing or decreasing the resolution will increase or decrease the dimensions of
115
- * the quantization grid proportionately.
116
- *
117
- * Supported `tileResolution` values, with corresponding grid sizes:
118
- *
119
- * - 0.25: 256x256
120
- * - 0.5: 512x512
121
- * - 1: 1024x1024
122
- * - 2: 2048x2048
123
- * - 4: 4096x4096
124
- */
125
- tileResolution?: TileResolution;
126
-
127
137
  /**
128
138
  * Values for named or positional paramteres in the query.
129
139
  *
@@ -149,6 +159,15 @@ export type QuerySourceOptions = {
149
159
  * ```
150
160
  */
151
161
  queryParameters?: QueryParameters;
162
+
163
+ /**
164
+ * Comma-separated aggregation expressions. If assigned on a vector source, source is grouped by geometry and then aggregated.
165
+ *
166
+ * Example:
167
+ *
168
+ * 1 as value, avg(rev) as average_revenue
169
+ */
170
+ aggregationExp?: string;
152
171
  };
153
172
 
154
173
  export type TableSourceOptions = {
@@ -158,26 +177,13 @@ export type TableSourceOptions = {
158
177
  tableName: string;
159
178
 
160
179
  /**
161
- * The column name and the type of geospatial support.
162
- *
163
- * If not present, defaults to `'geom'` for generic tables, `'quadbin'` for Quadbin sources and `'h3'` for H3 sources.
164
- */
165
- spatialDataColumn?: string;
166
-
167
- /**
168
- * Relative resolution of a tile. Higher values increase density and data size. At `tileResolution = 1`, tile geometry is
169
- * quantized to a 1024x1024 grid. Increasing or decreasing the resolution will increase or decrease the dimensions of
170
- * the quantization grid proportionately.
180
+ * Comma-separated aggregation expressions. If assigned on a vector source, source is grouped by geometry and then aggregated.
171
181
  *
172
- * Supported `tileResolution` values, with corresponding grid sizes:
182
+ * Example:
173
183
  *
174
- * - 0.25: 256x256
175
- * - 0.5: 512x512
176
- * - 1: 1024x1024
177
- * - 2: 2048x2048
178
- * - 4: 4096x4096
184
+ * 1 as value, avg(rev) as average_revenue
179
185
  */
180
- tileResolution?: TileResolution;
186
+ aggregationExp?: string;
181
187
  };
182
188
 
183
189
  export type TilesetSourceOptions = {
@@ -198,6 +204,14 @@ export type ColumnsOption = {
198
204
 
199
205
  export type SpatialDataType = 'geo' | 'h3' | 'quadbin';
200
206
 
207
+ /**
208
+ * Strategy used for covering spatial filter geometry with spatial indexes.
209
+ * See https://docs.carto.com/data-and-analysis/analytics-toolbox-for-bigquery/sql-reference/quadbin#quadbin_polyfill_mode
210
+ * or https://docs.carto.com/data-and-analysis/analytics-toolbox-for-bigquery/sql-reference/h3#h3_polyfill_mode for more information.
211
+ * @internalRemarks Source: cloud-native maps-api
212
+ * */
213
+ export type SpatialFilterPolyfillMode = 'center' | 'intersects' | 'contains';
214
+
201
215
  export type TilejsonMapInstantiation = MapInstantiation & {
202
216
  tilejson: {url: string[]};
203
217
  };
@@ -31,11 +31,15 @@ type UrlParameters = {
31
31
  tileResolution?: string;
32
32
  q: string;
33
33
  queryParameters?: Record<string, unknown> | unknown[];
34
+ aggregationExp?: string;
34
35
  };
35
36
 
37
+ export type VectorQuerySourceResponse = TilejsonResult &
38
+ WidgetQuerySourceResult;
39
+
36
40
  export const vectorQuerySource = async function (
37
41
  options: VectorQuerySourceOptions
38
- ): Promise<TilejsonResult & WidgetQuerySourceResult> {
42
+ ): Promise<VectorQuerySourceResponse> {
39
43
  const {
40
44
  columns,
41
45
  filters,
@@ -43,6 +47,7 @@ export const vectorQuerySource = async function (
43
47
  sqlQuery,
44
48
  tileResolution = DEFAULT_TILE_RESOLUTION,
45
49
  queryParameters,
50
+ aggregationExp,
46
51
  } = options;
47
52
 
48
53
  const urlParameters: UrlParameters = {
@@ -61,10 +66,16 @@ export const vectorQuerySource = async function (
61
66
  if (queryParameters) {
62
67
  urlParameters.queryParameters = queryParameters;
63
68
  }
69
+ if (aggregationExp) {
70
+ urlParameters.aggregationExp = aggregationExp;
71
+ }
64
72
  return baseSource<UrlParameters>('query', options, urlParameters).then(
65
73
  (result) => ({
66
74
  ...(result as TilejsonResult),
67
- widgetSource: new WidgetQuerySource(options),
75
+ widgetSource: new WidgetQuerySource({
76
+ ...options,
77
+ spatialDataType: 'geo',
78
+ }),
68
79
  })
69
80
  );
70
81
  };
@@ -22,6 +22,7 @@ export type VectorTableSourceOptions = SourceOptions &
22
22
  TableSourceOptions &
23
23
  FilterOptions &
24
24
  ColumnsOption;
25
+
25
26
  type UrlParameters = {
26
27
  columns?: string;
27
28
  filters?: Record<string, unknown>;
@@ -29,17 +30,22 @@ type UrlParameters = {
29
30
  spatialDataColumn?: string;
30
31
  tileResolution?: string;
31
32
  name: string;
33
+ aggregationExp?: string;
32
34
  };
33
35
 
36
+ export type VectorTableSourceResponse = TilejsonResult &
37
+ WidgetTableSourceResult;
38
+
34
39
  export const vectorTableSource = async function (
35
40
  options: VectorTableSourceOptions
36
- ): Promise<TilejsonResult & WidgetTableSourceResult> {
41
+ ): Promise<VectorTableSourceResponse> {
37
42
  const {
38
43
  columns,
39
44
  filters,
40
45
  spatialDataColumn = 'geom',
41
46
  tableName,
42
47
  tileResolution = DEFAULT_TILE_RESOLUTION,
48
+ aggregationExp,
43
49
  } = options;
44
50
 
45
51
  const urlParameters: UrlParameters = {
@@ -55,10 +61,16 @@ export const vectorTableSource = async function (
55
61
  if (filters) {
56
62
  urlParameters.filters = filters;
57
63
  }
64
+ if (aggregationExp) {
65
+ urlParameters.aggregationExp = aggregationExp;
66
+ }
58
67
  return baseSource<UrlParameters>('table', options, urlParameters).then(
59
68
  (result) => ({
60
69
  ...(result as TilejsonResult),
61
- widgetSource: new WidgetTableSource(options),
70
+ widgetSource: new WidgetTableSource({
71
+ ...options,
72
+ spatialDataType: 'geo',
73
+ }),
62
74
  })
63
75
  );
64
76
  };
@@ -12,9 +12,11 @@ import type {
12
12
  export type VectorTilesetSourceOptions = SourceOptions & TilesetSourceOptions;
13
13
  type UrlParameters = {name: string};
14
14
 
15
+ export type VectorTilesetSourceResponse = TilejsonResult;
16
+
15
17
  export const vectorTilesetSource = async function (
16
18
  options: VectorTilesetSourceOptions
17
- ): Promise<TilejsonResult> {
19
+ ): Promise<VectorTilesetSourceResponse> {
18
20
  const {tableName} = options;
19
21
  const urlParameters: UrlParameters = {name: tableName};
20
22
 
@@ -22,5 +24,5 @@ export const vectorTilesetSource = async function (
22
24
  'tileset',
23
25
  options,
24
26
  urlParameters
25
- ) as Promise<TilejsonResult>;
27
+ ) as Promise<VectorTilesetSourceResponse>;
26
28
  };
@@ -0,0 +1,111 @@
1
+ import {
2
+ DEFAULT_AGGREGATION_RES_LEVEL_H3,
3
+ DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN,
4
+ } from './constants-internal';
5
+ import type {ModelSource} from './models/model';
6
+ import type {AggregationOptions} from './sources/types';
7
+ import {assert} from './utils';
8
+ import type {ViewState} from './widget-sources';
9
+
10
+ const DEFAULT_TILE_SIZE = 512;
11
+ const QUADBIN_ZOOM_MAX_OFFSET = 4;
12
+
13
+ export function getSpatialFiltersResolution(
14
+ source: Partial<ModelSource & AggregationOptions>,
15
+ viewState: ViewState
16
+ ): number | undefined {
17
+ const dataResolution = source.dataResolution ?? Number.MAX_VALUE;
18
+
19
+ const aggregationResLevel =
20
+ source.aggregationResLevel ??
21
+ (source.spatialDataType === 'h3'
22
+ ? DEFAULT_AGGREGATION_RES_LEVEL_H3
23
+ : DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN);
24
+
25
+ const aggregationResLevelOffset = Math.max(
26
+ 0,
27
+ Math.floor(aggregationResLevel)
28
+ );
29
+
30
+ const currentZoomInt = Math.ceil(viewState.zoom);
31
+ if (source.spatialDataType === 'h3') {
32
+ const tileSize = DEFAULT_TILE_SIZE;
33
+ const maxResolutionForZoom =
34
+ maxH3SpatialFiltersResolutions.find(
35
+ ([zoom]) => zoom === currentZoomInt
36
+ )?.[1] ?? Math.max(0, currentZoomInt - 3);
37
+
38
+ const maxSpatialFiltersResolution = maxResolutionForZoom
39
+ ? Math.min(dataResolution, maxResolutionForZoom)
40
+ : dataResolution;
41
+
42
+ const hexagonResolution =
43
+ getHexagonResolution(viewState, tileSize) + aggregationResLevelOffset;
44
+
45
+ return Math.min(hexagonResolution, maxSpatialFiltersResolution);
46
+ }
47
+
48
+ if (source.spatialDataType === 'quadbin') {
49
+ const maxResolutionForZoom = currentZoomInt + QUADBIN_ZOOM_MAX_OFFSET;
50
+ const maxSpatialFiltersResolution = Math.min(
51
+ dataResolution,
52
+ maxResolutionForZoom
53
+ );
54
+
55
+ const quadsResolution =
56
+ Math.floor(viewState.zoom) + aggregationResLevelOffset;
57
+ return Math.min(quadsResolution, maxSpatialFiltersResolution);
58
+ }
59
+
60
+ return undefined;
61
+ }
62
+
63
+ const maxH3SpatialFiltersResolutions = [
64
+ [20, 14],
65
+ [19, 13],
66
+ [18, 12],
67
+ [17, 11],
68
+ [16, 10],
69
+ [15, 9],
70
+ [14, 8],
71
+ [13, 7],
72
+ [12, 7],
73
+ [11, 7],
74
+ [10, 6],
75
+ [9, 6],
76
+ [8, 5],
77
+ [7, 4],
78
+ [6, 4],
79
+ [5, 3],
80
+ [4, 2],
81
+ [3, 1],
82
+ [2, 1],
83
+ [1, 0],
84
+ ];
85
+
86
+ // stolen from https://github.com/visgl/deck.gl/blob/master/modules/carto/src/layers/h3-tileset-2d.ts
87
+
88
+ // Relative scale factor (0 = no biasing, 2 = a few hexagons cover view)
89
+ const BIAS = 2;
90
+
91
+ // Resolution conversion function. Takes a WebMercatorViewport and returns
92
+ // a H3 resolution such that the screen space size of the hexagons is
93
+ // similar
94
+ export function getHexagonResolution(
95
+ viewport: {zoom: number; latitude: number},
96
+ tileSize: number
97
+ ): number {
98
+ // Difference in given tile size compared to deck's internal 512px tile size,
99
+ // expressed as an offset to the viewport zoom.
100
+ const zoomOffset = Math.log2(tileSize / DEFAULT_TILE_SIZE);
101
+ const hexagonScaleFactor = (2 / 3) * (viewport.zoom - zoomOffset);
102
+ const latitudeScaleFactor = Math.log(
103
+ 1 / Math.cos((Math.PI * viewport.latitude) / 180)
104
+ );
105
+
106
+ // Clip and bias
107
+ return Math.max(
108
+ 0,
109
+ Math.floor(hexagonScaleFactor + latitudeScaleFactor - BIAS)
110
+ );
111
+ }
package/src/utils.ts CHANGED
@@ -57,7 +57,7 @@ export function normalizeObjectKeys<T, R extends Row<T>>(el: R): R {
57
57
  }
58
58
 
59
59
  /** @internalRemarks Source: @carto/react-core */
60
- export function assert(condition: unknown, message: string) {
60
+ export function assert(condition: unknown, message: string): asserts condition {
61
61
  if (!condition) {
62
62
  throw new Error(message);
63
63
  }
@@ -1,4 +1,4 @@
1
- import {TileResolution} from '../sources/types';
1
+ import {SpatialFilterPolyfillMode, TileResolution} from '../sources/types';
2
2
  import {
3
3
  GroupDateType,
4
4
  SortColumnType,
@@ -10,9 +10,18 @@ import {
10
10
  * WIDGET API REQUESTS
11
11
  */
12
12
 
13
+ export interface ViewState {
14
+ zoom: number;
15
+ latitude: number;
16
+ longitude: number;
17
+ }
18
+
13
19
  /** Common options for {@link WidgetBaseSource} requests. */
14
20
  interface BaseRequestOptions {
15
21
  spatialFilter?: SpatialFilter;
22
+ spatialFiltersMode?: SpatialFilterPolyfillMode;
23
+ /** Required for table- and query-based spatial index sources (H3, Quadbin). */
24
+ spatialIndexReferenceViewState?: ViewState;
16
25
  abortController?: AbortController;
17
26
  filterOwner?: string;
18
27
  }