@carto/api-client 0.4.3 → 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.
- package/CHANGELOG.md +4 -0
- package/build/api/query.d.ts +1 -1
- package/build/api-client.cjs +179 -35
- package/build/api-client.cjs.map +1 -1
- package/build/api-client.modern.js +181 -44
- package/build/api-client.modern.js.map +1 -1
- package/build/models/model.d.ts +7 -1
- package/build/sources/types.d.ts +36 -41
- package/build/spatial-index.d.ts +8 -0
- package/build/utils.d.ts +1 -1
- package/build/widget-sources/types.d.ts +9 -1
- package/build/widget-sources/widget-base-source.d.ts +3 -3
- package/package.json +1 -1
- package/src/api/query.ts +1 -2
- package/src/models/model.ts +47 -24
- package/src/sources/h3-query-source.ts +7 -1
- package/src/sources/h3-table-source.ts +6 -1
- package/src/sources/quadbin-query-source.ts +6 -1
- package/src/sources/quadbin-table-source.ts +6 -1
- package/src/sources/types.ts +41 -45
- package/src/sources/vector-query-source.ts +4 -1
- package/src/sources/vector-table-source.ts +4 -1
- package/src/spatial-index.ts +111 -0
- package/src/utils.ts +1 -1
- package/src/widget-sources/types.ts +10 -1
- package/src/widget-sources/widget-base-source.ts +183 -23
package/build/models/model.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Filter, FilterLogicalOperator, MapType, QueryParameters, SpatialFilter } from '../types.js';
|
|
2
2
|
import { ModelRequestOptions } from './common.js';
|
|
3
3
|
import { ApiVersion } from '../constants.js';
|
|
4
|
+
import { SpatialDataType, SpatialFilterPolyfillMode } from '../sources/types.js';
|
|
4
5
|
/** @internalRemarks Source: @carto/react-api */
|
|
5
6
|
declare const AVAILABLE_MODELS: readonly ["category", "histogram", "formula", "pick", "timeseries", "range", "scatterplot", "table"];
|
|
6
7
|
export type Model = (typeof AVAILABLE_MODELS)[number];
|
|
@@ -14,9 +15,14 @@ export interface ModelSource {
|
|
|
14
15
|
data: string;
|
|
15
16
|
filters?: Record<string, Filter>;
|
|
16
17
|
filtersLogicalOperator?: FilterLogicalOperator;
|
|
17
|
-
geoColumn?: string;
|
|
18
18
|
spatialFilter?: SpatialFilter;
|
|
19
19
|
queryParameters?: QueryParameters;
|
|
20
|
+
spatialDataColumn?: string;
|
|
21
|
+
spatialDataType?: SpatialDataType;
|
|
22
|
+
spatialFiltersResolution?: number;
|
|
23
|
+
spatialFiltersMode?: SpatialFilterPolyfillMode;
|
|
24
|
+
/** original resolution of the spatial index data as stored in the DW */
|
|
25
|
+
dataResolution?: number;
|
|
20
26
|
}
|
|
21
27
|
/**
|
|
22
28
|
* Execute a SQL model request.
|
package/build/sources/types.d.ts
CHANGED
|
@@ -38,6 +38,30 @@ export type SourceOptionalOptions = {
|
|
|
38
38
|
* @default {@link DEFAULT_MAX_LENGTH_URL}
|
|
39
39
|
*/
|
|
40
40
|
maxLengthURL?: number;
|
|
41
|
+
/**
|
|
42
|
+
* The column name and the type of geospatial support.
|
|
43
|
+
*
|
|
44
|
+
* If not present, defaults to `'geom'` for generic queries, `'quadbin'` for Quadbin sources and `'h3'` for H3 sources.
|
|
45
|
+
*/
|
|
46
|
+
spatialDataColumn?: string;
|
|
47
|
+
/**
|
|
48
|
+
* The type of geospatial support. Defaults to `'geo'`.
|
|
49
|
+
*/
|
|
50
|
+
spatialDataType?: SpatialDataType;
|
|
51
|
+
/**
|
|
52
|
+
* Relative resolution of a tile. Higher values increase density and data size. At `tileResolution = 1`, tile geometry is
|
|
53
|
+
* quantized to a 1024x1024 grid. Increasing or decreasing the resolution will increase or decrease the dimensions of
|
|
54
|
+
* the quantization grid proportionately.
|
|
55
|
+
*
|
|
56
|
+
* Supported `tileResolution` values, with corresponding grid sizes:
|
|
57
|
+
*
|
|
58
|
+
* - 0.25: 256x256
|
|
59
|
+
* - 0.5: 512x512
|
|
60
|
+
* - 1: 1024x1024
|
|
61
|
+
* - 2: 2048x2048
|
|
62
|
+
* - 4: 4096x4096
|
|
63
|
+
*/
|
|
64
|
+
tileResolution?: TileResolution;
|
|
41
65
|
/**
|
|
42
66
|
* By default, local in-memory caching is enabled.
|
|
43
67
|
*/
|
|
@@ -73,6 +97,10 @@ export type AggregationOptions = {
|
|
|
73
97
|
* @default 6 for quadbin and 4 for h3 sources
|
|
74
98
|
*/
|
|
75
99
|
aggregationResLevel?: number;
|
|
100
|
+
/**
|
|
101
|
+
* Original resolution of the spatial index data as stored in the DW
|
|
102
|
+
*/
|
|
103
|
+
dataResolution?: number;
|
|
76
104
|
};
|
|
77
105
|
export type FilterOptions = {
|
|
78
106
|
/**
|
|
@@ -81,28 +109,8 @@ export type FilterOptions = {
|
|
|
81
109
|
filters?: Filters;
|
|
82
110
|
};
|
|
83
111
|
export type QuerySourceOptions = {
|
|
84
|
-
/**
|
|
85
|
-
* The column name and the type of geospatial support.
|
|
86
|
-
*
|
|
87
|
-
* If not present, defaults to `'geom'` for generic queries, `'quadbin'` for Quadbin sources and `'h3'` for H3 sources.
|
|
88
|
-
*/
|
|
89
|
-
spatialDataColumn?: string;
|
|
90
|
-
/** SQL query. */
|
|
112
|
+
/** Full SQL query with query paremeter placeholders (if any). */
|
|
91
113
|
sqlQuery: string;
|
|
92
|
-
/**
|
|
93
|
-
* Relative resolution of a tile. Higher values increase density and data size. At `tileResolution = 1`, tile geometry is
|
|
94
|
-
* quantized to a 1024x1024 grid. Increasing or decreasing the resolution will increase or decrease the dimensions of
|
|
95
|
-
* the quantization grid proportionately.
|
|
96
|
-
*
|
|
97
|
-
* Supported `tileResolution` values, with corresponding grid sizes:
|
|
98
|
-
*
|
|
99
|
-
* - 0.25: 256x256
|
|
100
|
-
* - 0.5: 512x512
|
|
101
|
-
* - 1: 1024x1024
|
|
102
|
-
* - 2: 2048x2048
|
|
103
|
-
* - 4: 4096x4096
|
|
104
|
-
*/
|
|
105
|
-
tileResolution?: TileResolution;
|
|
106
114
|
/**
|
|
107
115
|
* Values for named or positional paramteres in the query.
|
|
108
116
|
*
|
|
@@ -142,26 +150,6 @@ export type TableSourceOptions = {
|
|
|
142
150
|
* Fully qualified name of table.
|
|
143
151
|
*/
|
|
144
152
|
tableName: string;
|
|
145
|
-
/**
|
|
146
|
-
* The column name and the type of geospatial support.
|
|
147
|
-
*
|
|
148
|
-
* If not present, defaults to `'geom'` for generic tables, `'quadbin'` for Quadbin sources and `'h3'` for H3 sources.
|
|
149
|
-
*/
|
|
150
|
-
spatialDataColumn?: string;
|
|
151
|
-
/**
|
|
152
|
-
* Relative resolution of a tile. Higher values increase density and data size. At `tileResolution = 1`, tile geometry is
|
|
153
|
-
* quantized to a 1024x1024 grid. Increasing or decreasing the resolution will increase or decrease the dimensions of
|
|
154
|
-
* the quantization grid proportionately.
|
|
155
|
-
*
|
|
156
|
-
* Supported `tileResolution` values, with corresponding grid sizes:
|
|
157
|
-
*
|
|
158
|
-
* - 0.25: 256x256
|
|
159
|
-
* - 0.5: 512x512
|
|
160
|
-
* - 1: 1024x1024
|
|
161
|
-
* - 2: 2048x2048
|
|
162
|
-
* - 4: 4096x4096
|
|
163
|
-
*/
|
|
164
|
-
tileResolution?: TileResolution;
|
|
165
153
|
/**
|
|
166
154
|
* Comma-separated aggregation expressions. If assigned on a vector source, source is grouped by geometry and then aggregated.
|
|
167
155
|
*
|
|
@@ -186,6 +174,13 @@ export type ColumnsOption = {
|
|
|
186
174
|
columns?: string[];
|
|
187
175
|
};
|
|
188
176
|
export type SpatialDataType = 'geo' | 'h3' | 'quadbin';
|
|
177
|
+
/**
|
|
178
|
+
* Strategy used for covering spatial filter geometry with spatial indexes.
|
|
179
|
+
* See https://docs.carto.com/data-and-analysis/analytics-toolbox-for-bigquery/sql-reference/quadbin#quadbin_polyfill_mode
|
|
180
|
+
* or https://docs.carto.com/data-and-analysis/analytics-toolbox-for-bigquery/sql-reference/h3#h3_polyfill_mode for more information.
|
|
181
|
+
* @internalRemarks Source: cloud-native maps-api
|
|
182
|
+
* */
|
|
183
|
+
export type SpatialFilterPolyfillMode = 'center' | 'intersects' | 'contains';
|
|
189
184
|
export type TilejsonMapInstantiation = MapInstantiation & {
|
|
190
185
|
tilejson: {
|
|
191
186
|
url: string[];
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ModelSource } from './models/model';
|
|
2
|
+
import type { AggregationOptions } from './sources/types';
|
|
3
|
+
import type { ViewState } from './widget-sources';
|
|
4
|
+
export declare function getSpatialFiltersResolution(source: Partial<ModelSource & AggregationOptions>, viewState: ViewState): number | undefined;
|
|
5
|
+
export declare function getHexagonResolution(viewport: {
|
|
6
|
+
zoom: number;
|
|
7
|
+
latitude: number;
|
|
8
|
+
}, tileSize: number): number;
|
package/build/utils.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ type Row<T> = Record<string, T> | Record<string, T>[] | T[] | T;
|
|
|
14
14
|
*/
|
|
15
15
|
export declare function normalizeObjectKeys<T, R extends Row<T>>(el: R): R;
|
|
16
16
|
/** @internalRemarks Source: @carto/react-core */
|
|
17
|
-
export declare function assert(condition: unknown, message: string):
|
|
17
|
+
export declare function assert(condition: unknown, message: string): asserts condition;
|
|
18
18
|
/**
|
|
19
19
|
* @internalRemarks Source: @carto/react-core
|
|
20
20
|
* @internal
|
|
@@ -1,11 +1,19 @@
|
|
|
1
|
-
import { TileResolution } from '../sources/types';
|
|
1
|
+
import { SpatialFilterPolyfillMode, TileResolution } from '../sources/types';
|
|
2
2
|
import { GroupDateType, SortColumnType, SortDirection, SpatialFilter } from '../types';
|
|
3
3
|
/******************************************************************************
|
|
4
4
|
* WIDGET API REQUESTS
|
|
5
5
|
*/
|
|
6
|
+
export interface ViewState {
|
|
7
|
+
zoom: number;
|
|
8
|
+
latitude: number;
|
|
9
|
+
longitude: number;
|
|
10
|
+
}
|
|
6
11
|
/** Common options for {@link WidgetBaseSource} requests. */
|
|
7
12
|
interface BaseRequestOptions {
|
|
8
13
|
spatialFilter?: SpatialFilter;
|
|
14
|
+
spatialFiltersMode?: SpatialFilterPolyfillMode;
|
|
15
|
+
/** Required for table- and query-based spatial index sources (H3, Quadbin). */
|
|
16
|
+
spatialIndexReferenceViewState?: ViewState;
|
|
9
17
|
abortController?: AbortController;
|
|
10
18
|
filterOwner?: string;
|
|
11
19
|
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { CategoryRequestOptions, CategoryResponse, FeaturesRequestOptions, FeaturesResponse, FormulaRequestOptions, FormulaResponse, HistogramRequestOptions, HistogramResponse, RangeRequestOptions, RangeResponse, ScatterRequestOptions, ScatterResponse, TableRequestOptions, TableResponse, TimeSeriesRequestOptions, TimeSeriesResponse } from './types.js';
|
|
2
|
-
import { FilterLogicalOperator, Filter } from '../types.js';
|
|
1
|
+
import { CategoryRequestOptions, CategoryResponse, FeaturesRequestOptions, FeaturesResponse, FormulaRequestOptions, FormulaResponse, HistogramRequestOptions, HistogramResponse, RangeRequestOptions, RangeResponse, ScatterRequestOptions, ScatterResponse, TableRequestOptions, TableResponse, TimeSeriesRequestOptions, TimeSeriesResponse, ViewState } from './types.js';
|
|
2
|
+
import { FilterLogicalOperator, Filter, SpatialFilter } from '../types.js';
|
|
3
3
|
import { ModelSource } from '../models/model.js';
|
|
4
4
|
import { SourceOptions } from '../sources/index.js';
|
|
5
5
|
import { ApiVersion } from '../constants.js';
|
|
6
6
|
export interface WidgetBaseSourceProps extends Omit<SourceOptions, 'filters'> {
|
|
7
7
|
apiVersion?: ApiVersion;
|
|
8
|
-
geoColumn?: string;
|
|
9
8
|
filters?: Record<string, Filter>;
|
|
10
9
|
filtersLogicalOperator?: FilterLogicalOperator;
|
|
11
10
|
}
|
|
@@ -27,6 +26,7 @@ export declare abstract class WidgetBaseSource<Props extends WidgetBaseSourcePro
|
|
|
27
26
|
*/
|
|
28
27
|
protected abstract getModelSource(owner: string | undefined): ModelSource;
|
|
29
28
|
protected _getModelSource(owner?: string): Omit<ModelSource, 'type' | 'data'>;
|
|
29
|
+
protected _getSpatialFiltersResolution(source: Omit<ModelSource, 'type' | 'data'>, spatialFilter?: SpatialFilter, referenceViewState?: ViewState): number | undefined;
|
|
30
30
|
/****************************************************************************
|
|
31
31
|
* CATEGORIES
|
|
32
32
|
*/
|
package/package.json
CHANGED
package/src/api/query.ts
CHANGED
|
@@ -12,8 +12,7 @@ import {buildQueryUrl} from './endpoints';
|
|
|
12
12
|
import {requestWithParameters} from './request-with-parameters';
|
|
13
13
|
import {APIErrorContext} from './carto-api-error';
|
|
14
14
|
|
|
15
|
-
export type QueryOptions = SourceOptions &
|
|
16
|
-
Omit<QuerySourceOptions, 'spatialDataColumn'>;
|
|
15
|
+
export type QueryOptions = SourceOptions & QuerySourceOptions;
|
|
17
16
|
type UrlParameters = {q: string; queryParameters?: string};
|
|
18
17
|
|
|
19
18
|
export const query = async function (
|
package/src/models/model.ts
CHANGED
|
@@ -7,9 +7,10 @@ import {
|
|
|
7
7
|
SpatialFilter,
|
|
8
8
|
} from '../types.js';
|
|
9
9
|
import {$TODO} from '../types-internal.js';
|
|
10
|
-
import {assert} from '../utils.js';
|
|
10
|
+
import {assert, isPureObject} from '../utils.js';
|
|
11
11
|
import {ModelRequestOptions, makeCall} from './common.js';
|
|
12
12
|
import {ApiVersion} from '../constants.js';
|
|
13
|
+
import {SpatialDataType, SpatialFilterPolyfillMode} from '../sources/types.js';
|
|
13
14
|
|
|
14
15
|
/** @internalRemarks Source: @carto/react-api */
|
|
15
16
|
const AVAILABLE_MODELS = [
|
|
@@ -35,9 +36,14 @@ export interface ModelSource {
|
|
|
35
36
|
data: string;
|
|
36
37
|
filters?: Record<string, Filter>;
|
|
37
38
|
filtersLogicalOperator?: FilterLogicalOperator;
|
|
38
|
-
geoColumn?: string;
|
|
39
39
|
spatialFilter?: SpatialFilter;
|
|
40
40
|
queryParameters?: QueryParameters;
|
|
41
|
+
spatialDataColumn?: string;
|
|
42
|
+
spatialDataType?: SpatialDataType;
|
|
43
|
+
spatialFiltersResolution?: number;
|
|
44
|
+
spatialFiltersMode?: SpatialFilterPolyfillMode;
|
|
45
|
+
/** original resolution of the spatial index data as stored in the DW */
|
|
46
|
+
dataResolution?: number;
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
const {V3} = ApiVersion;
|
|
@@ -79,50 +85,51 @@ export function executeModel(props: {
|
|
|
79
85
|
data,
|
|
80
86
|
filters,
|
|
81
87
|
filtersLogicalOperator = 'and',
|
|
82
|
-
|
|
88
|
+
spatialDataType = 'geo',
|
|
89
|
+
spatialFiltersMode = 'intersects',
|
|
90
|
+
spatialFiltersResolution = 0,
|
|
83
91
|
} = source;
|
|
84
92
|
|
|
85
|
-
const
|
|
86
|
-
? JSON.stringify(source.queryParameters)
|
|
87
|
-
: '';
|
|
88
|
-
|
|
89
|
-
const queryParams: Record<string, string> = {
|
|
93
|
+
const queryParams: Record<string, unknown> = {
|
|
90
94
|
type,
|
|
91
95
|
client: clientId,
|
|
92
96
|
source: data,
|
|
93
|
-
params
|
|
94
|
-
queryParameters,
|
|
95
|
-
filters
|
|
97
|
+
params,
|
|
98
|
+
queryParameters: source.queryParameters || '',
|
|
99
|
+
filters,
|
|
96
100
|
filtersLogicalOperator,
|
|
97
101
|
};
|
|
98
102
|
|
|
103
|
+
const spatialDataColumn = source.spatialDataColumn || DEFAULT_GEO_COLUMN;
|
|
104
|
+
|
|
99
105
|
// Picking Model API requires 'spatialDataColumn'.
|
|
100
106
|
if (model === 'pick') {
|
|
101
|
-
queryParams.spatialDataColumn =
|
|
107
|
+
queryParams.spatialDataColumn = spatialDataColumn;
|
|
102
108
|
}
|
|
103
109
|
|
|
104
|
-
// API supports multiple filters, we apply it only to
|
|
110
|
+
// API supports multiple filters, we apply it only to spatialDataColumn
|
|
105
111
|
const spatialFilters = source.spatialFilter
|
|
106
|
-
? {[
|
|
112
|
+
? {[spatialDataColumn]: source.spatialFilter}
|
|
107
113
|
: undefined;
|
|
108
114
|
|
|
109
115
|
if (spatialFilters) {
|
|
110
|
-
queryParams.spatialFilters =
|
|
116
|
+
queryParams.spatialFilters = spatialFilters;
|
|
117
|
+
queryParams.spatialDataColumn = spatialDataColumn;
|
|
118
|
+
queryParams.spatialDataType = spatialDataType;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (spatialDataType !== 'geo') {
|
|
122
|
+
if (spatialFiltersResolution > 0) {
|
|
123
|
+
queryParams.spatialFiltersResolution = spatialFiltersResolution;
|
|
124
|
+
}
|
|
125
|
+
queryParams.spatialFiltersMode = spatialFiltersMode;
|
|
111
126
|
}
|
|
112
127
|
|
|
113
128
|
const urlWithSearchParams =
|
|
114
|
-
url + '?' +
|
|
129
|
+
url + '?' + objectToURLSearchParams(queryParams).toString();
|
|
115
130
|
const isGet = urlWithSearchParams.length <= REQUEST_GET_MAX_URL_LENGTH;
|
|
116
131
|
if (isGet) {
|
|
117
132
|
url = urlWithSearchParams;
|
|
118
|
-
} else {
|
|
119
|
-
// undo the JSON.stringify, @TODO find a better pattern
|
|
120
|
-
queryParams.params = params as $TODO;
|
|
121
|
-
queryParams.filters = filters as $TODO;
|
|
122
|
-
queryParams.queryParameters = source.queryParameters as $TODO;
|
|
123
|
-
if (spatialFilters) {
|
|
124
|
-
queryParams.spatialFilters = spatialFilters as $TODO;
|
|
125
|
-
}
|
|
126
133
|
}
|
|
127
134
|
return makeCall({
|
|
128
135
|
url,
|
|
@@ -134,3 +141,19 @@ export function executeModel(props: {
|
|
|
134
141
|
},
|
|
135
142
|
});
|
|
136
143
|
}
|
|
144
|
+
|
|
145
|
+
function objectToURLSearchParams(object: Record<string, unknown>) {
|
|
146
|
+
const params = new URLSearchParams();
|
|
147
|
+
for (const key in object) {
|
|
148
|
+
if (isPureObject(object[key])) {
|
|
149
|
+
params.append(key, JSON.stringify(object[key]));
|
|
150
|
+
} else if (Array.isArray(object[key])) {
|
|
151
|
+
params.append(key, JSON.stringify(object[key]));
|
|
152
|
+
} else if (object[key] === null) {
|
|
153
|
+
params.append(key, 'null');
|
|
154
|
+
} else if (object[key] !== undefined) {
|
|
155
|
+
params.append(key, String(object[key]));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return params;
|
|
159
|
+
}
|
|
@@ -19,6 +19,7 @@ export type H3QuerySourceOptions = SourceOptions &
|
|
|
19
19
|
QuerySourceOptions &
|
|
20
20
|
AggregationOptions &
|
|
21
21
|
FilterOptions;
|
|
22
|
+
|
|
22
23
|
type UrlParameters = {
|
|
23
24
|
aggregationExp: string;
|
|
24
25
|
aggregationResLevel?: string;
|
|
@@ -61,7 +62,12 @@ export const h3QuerySource = async function (
|
|
|
61
62
|
return baseSource<UrlParameters>('query', options, urlParameters).then(
|
|
62
63
|
(result) => ({
|
|
63
64
|
...(result as TilejsonResult),
|
|
64
|
-
widgetSource: new WidgetQuerySource(
|
|
65
|
+
widgetSource: new WidgetQuerySource({
|
|
66
|
+
...options,
|
|
67
|
+
// NOTE: passing redundant spatialDataColumn here to apply the default value 'h3'
|
|
68
|
+
spatialDataColumn,
|
|
69
|
+
spatialDataType: 'h3',
|
|
70
|
+
}),
|
|
65
71
|
})
|
|
66
72
|
);
|
|
67
73
|
};
|
|
@@ -57,7 +57,12 @@ export const h3TableSource = async function (
|
|
|
57
57
|
return baseSource<UrlParameters>('table', options, urlParameters).then(
|
|
58
58
|
(result) => ({
|
|
59
59
|
...(result as TilejsonResult),
|
|
60
|
-
widgetSource: new WidgetTableSource(
|
|
60
|
+
widgetSource: new WidgetTableSource({
|
|
61
|
+
...options,
|
|
62
|
+
// NOTE: passing redundant spatialDataColumn here to apply the default value 'h3'
|
|
63
|
+
spatialDataColumn,
|
|
64
|
+
spatialDataType: 'h3',
|
|
65
|
+
}),
|
|
61
66
|
})
|
|
62
67
|
);
|
|
63
68
|
};
|
|
@@ -63,7 +63,12 @@ export const quadbinQuerySource = async function (
|
|
|
63
63
|
return baseSource<UrlParameters>('query', options, urlParameters).then(
|
|
64
64
|
(result) => ({
|
|
65
65
|
...(result as TilejsonResult),
|
|
66
|
-
widgetSource: new WidgetQuerySource(
|
|
66
|
+
widgetSource: new WidgetQuerySource({
|
|
67
|
+
...options,
|
|
68
|
+
// NOTE: passing redundant spatialDataColumn here to apply the default value 'quadbin'
|
|
69
|
+
spatialDataColumn,
|
|
70
|
+
spatialDataType: 'quadbin',
|
|
71
|
+
}),
|
|
67
72
|
})
|
|
68
73
|
);
|
|
69
74
|
};
|
|
@@ -59,7 +59,12 @@ export const quadbinTableSource = async function (
|
|
|
59
59
|
return baseSource<UrlParameters>('table', options, urlParameters).then(
|
|
60
60
|
(result) => ({
|
|
61
61
|
...(result as TilejsonResult),
|
|
62
|
-
widgetSource: new WidgetTableSource(
|
|
62
|
+
widgetSource: new WidgetTableSource({
|
|
63
|
+
...options,
|
|
64
|
+
// NOTE: passing redundant spatialDataColumn here to apply the default value 'quadbin'
|
|
65
|
+
spatialDataColumn,
|
|
66
|
+
spatialDataType: 'quadbin',
|
|
67
|
+
}),
|
|
63
68
|
})
|
|
64
69
|
);
|
|
65
70
|
};
|
package/src/sources/types.ts
CHANGED
|
@@ -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
|
*
|
|
@@ -166,28 +176,6 @@ export type TableSourceOptions = {
|
|
|
166
176
|
*/
|
|
167
177
|
tableName: string;
|
|
168
178
|
|
|
169
|
-
/**
|
|
170
|
-
* The column name and the type of geospatial support.
|
|
171
|
-
*
|
|
172
|
-
* If not present, defaults to `'geom'` for generic tables, `'quadbin'` for Quadbin sources and `'h3'` for H3 sources.
|
|
173
|
-
*/
|
|
174
|
-
spatialDataColumn?: string;
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Relative resolution of a tile. Higher values increase density and data size. At `tileResolution = 1`, tile geometry is
|
|
178
|
-
* quantized to a 1024x1024 grid. Increasing or decreasing the resolution will increase or decrease the dimensions of
|
|
179
|
-
* the quantization grid proportionately.
|
|
180
|
-
*
|
|
181
|
-
* Supported `tileResolution` values, with corresponding grid sizes:
|
|
182
|
-
*
|
|
183
|
-
* - 0.25: 256x256
|
|
184
|
-
* - 0.5: 512x512
|
|
185
|
-
* - 1: 1024x1024
|
|
186
|
-
* - 2: 2048x2048
|
|
187
|
-
* - 4: 4096x4096
|
|
188
|
-
*/
|
|
189
|
-
tileResolution?: TileResolution;
|
|
190
|
-
|
|
191
179
|
/**
|
|
192
180
|
* Comma-separated aggregation expressions. If assigned on a vector source, source is grouped by geometry and then aggregated.
|
|
193
181
|
*
|
|
@@ -216,6 +204,14 @@ export type ColumnsOption = {
|
|
|
216
204
|
|
|
217
205
|
export type SpatialDataType = 'geo' | 'h3' | 'quadbin';
|
|
218
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
|
+
|
|
219
215
|
export type TilejsonMapInstantiation = MapInstantiation & {
|
|
220
216
|
tilejson: {url: string[]};
|
|
221
217
|
};
|
|
@@ -72,7 +72,10 @@ export const vectorQuerySource = async function (
|
|
|
72
72
|
return baseSource<UrlParameters>('query', options, urlParameters).then(
|
|
73
73
|
(result) => ({
|
|
74
74
|
...(result as TilejsonResult),
|
|
75
|
-
widgetSource: new WidgetQuerySource(
|
|
75
|
+
widgetSource: new WidgetQuerySource({
|
|
76
|
+
...options,
|
|
77
|
+
spatialDataType: 'geo',
|
|
78
|
+
}),
|
|
76
79
|
})
|
|
77
80
|
);
|
|
78
81
|
};
|
|
@@ -67,7 +67,10 @@ export const vectorTableSource = async function (
|
|
|
67
67
|
return baseSource<UrlParameters>('table', options, urlParameters).then(
|
|
68
68
|
(result) => ({
|
|
69
69
|
...(result as TilejsonResult),
|
|
70
|
-
widgetSource: new WidgetTableSource(
|
|
70
|
+
widgetSource: new WidgetTableSource({
|
|
71
|
+
...options,
|
|
72
|
+
spatialDataType: 'geo',
|
|
73
|
+
}),
|
|
71
74
|
})
|
|
72
75
|
);
|
|
73
76
|
};
|
|
@@ -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
|
}
|