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

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 (93) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/build/api/query.d.ts +1 -1
  3. package/build/api-client.cjs +2388 -261
  4. package/build/api-client.cjs.map +1 -1
  5. package/build/api-client.modern.js +2237 -262
  6. package/build/api-client.modern.js.map +1 -1
  7. package/build/constants.d.ts +22 -0
  8. package/build/filters/Filter.d.ts +13 -0
  9. package/build/filters/FilterTypes.d.ts +3 -0
  10. package/build/filters/geosjonFeatures.d.ts +8 -0
  11. package/build/filters/index.d.ts +6 -0
  12. package/build/filters/tileFeatures.d.ts +20 -0
  13. package/build/filters/tileFeaturesGeometries.d.ts +13 -0
  14. package/build/filters/tileFeaturesSpatialIndex.d.ts +10 -0
  15. package/build/index.d.ts +4 -0
  16. package/build/models/index.d.ts +1 -1
  17. package/build/models/model.d.ts +7 -1
  18. package/build/operations/aggregation.d.ts +8 -0
  19. package/build/operations/applySorting.d.ts +20 -0
  20. package/build/operations/groupBy.d.ts +15 -0
  21. package/build/operations/groupByDate.d.ts +11 -0
  22. package/build/operations/histogram.d.ts +13 -0
  23. package/build/operations/index.d.ts +6 -0
  24. package/build/operations/scatterPlot.d.ts +14 -0
  25. package/build/sources/h3-tileset-source.d.ts +2 -1
  26. package/build/sources/index.d.ts +1 -1
  27. package/build/sources/quadbin-tileset-source.d.ts +2 -1
  28. package/build/sources/types.d.ts +36 -41
  29. package/build/sources/vector-tileset-source.d.ts +2 -1
  30. package/build/spatial-index.d.ts +8 -0
  31. package/build/types-internal.d.ts +4 -0
  32. package/build/types.d.ts +61 -1
  33. package/build/utils/dateUtils.d.ts +10 -0
  34. package/build/utils/getTileFormat.d.ts +3 -0
  35. package/build/utils/makeIntervalComplete.d.ts +2 -0
  36. package/build/utils/transformTileCoordsToWGS84.d.ts +8 -0
  37. package/build/utils/transformToTileCoords.d.ts +9 -0
  38. package/build/utils.d.ts +1 -1
  39. package/build/widget-sources/index.d.ts +2 -1
  40. package/build/widget-sources/types.d.ts +40 -23
  41. package/build/widget-sources/widget-query-source.d.ts +2 -2
  42. package/build/widget-sources/widget-remote-source.d.ts +18 -0
  43. package/build/widget-sources/{widget-base-source.d.ts → widget-source.d.ts} +16 -41
  44. package/build/widget-sources/widget-table-source.d.ts +2 -2
  45. package/build/widget-sources/widget-tileset-source.d.ts +67 -0
  46. package/package.json +36 -35
  47. package/src/api/query.ts +1 -2
  48. package/src/constants.ts +25 -0
  49. package/src/filters/Filter.ts +169 -0
  50. package/src/filters/FilterTypes.ts +109 -0
  51. package/src/filters/geosjonFeatures.ts +32 -0
  52. package/src/filters/index.ts +6 -0
  53. package/src/filters/tileFeatures.ts +56 -0
  54. package/src/filters/tileFeaturesGeometries.ts +444 -0
  55. package/src/filters/tileFeaturesSpatialIndex.ts +119 -0
  56. package/src/index.ts +6 -0
  57. package/src/models/index.ts +1 -1
  58. package/src/models/model.ts +47 -24
  59. package/src/operations/aggregation.ts +154 -0
  60. package/src/operations/applySorting.ts +109 -0
  61. package/src/operations/groupBy.ts +59 -0
  62. package/src/operations/groupByDate.ts +98 -0
  63. package/src/operations/histogram.ts +66 -0
  64. package/src/operations/index.ts +6 -0
  65. package/src/operations/scatterPlot.ts +50 -0
  66. package/src/sources/h3-query-source.ts +7 -1
  67. package/src/sources/h3-table-source.ts +6 -1
  68. package/src/sources/h3-tileset-source.ts +18 -6
  69. package/src/sources/index.ts +1 -1
  70. package/src/sources/quadbin-query-source.ts +6 -1
  71. package/src/sources/quadbin-table-source.ts +6 -1
  72. package/src/sources/quadbin-tileset-source.ts +18 -6
  73. package/src/sources/raster-source.ts +1 -0
  74. package/src/sources/types.ts +41 -45
  75. package/src/sources/vector-query-source.ts +10 -3
  76. package/src/sources/vector-table-source.ts +10 -3
  77. package/src/sources/vector-tileset-source.ts +19 -6
  78. package/src/spatial-index.ts +111 -0
  79. package/src/types-internal.ts +6 -0
  80. package/src/types.ts +60 -2
  81. package/src/utils/dateUtils.ts +28 -0
  82. package/src/utils/getTileFormat.ts +9 -0
  83. package/src/utils/makeIntervalComplete.ts +17 -0
  84. package/src/utils/transformTileCoordsToWGS84.ts +77 -0
  85. package/src/utils/transformToTileCoords.ts +85 -0
  86. package/src/utils.ts +9 -6
  87. package/src/widget-sources/index.ts +2 -1
  88. package/src/widget-sources/types.ts +42 -23
  89. package/src/widget-sources/widget-query-source.ts +6 -3
  90. package/src/widget-sources/{widget-base-source.ts → widget-remote-source.ts} +169 -144
  91. package/src/widget-sources/widget-source.ts +160 -0
  92. package/src/widget-sources/widget-table-source.ts +6 -3
  93. package/src/widget-sources/widget-tileset-source.ts +396 -0
@@ -0,0 +1,85 @@
1
+ import {lngLatToWorld} from '@math.gl/web-mercator';
2
+ import {BBox, GeoJsonGeometryTypes, Geometry, Position} from 'geojson';
3
+
4
+ type TransformFn = (coordinates: any[], bbox: Position[]) => any[];
5
+
6
+ const TRANSFORM_FN: Record<
7
+ Exclude<GeoJsonGeometryTypes, 'GeometryCollection'>,
8
+ TransformFn
9
+ > = {
10
+ Point: transformPoint,
11
+ MultiPoint: transformMultiPoint,
12
+ LineString: transformLineString,
13
+ MultiLineString: transformMultiLineString,
14
+ Polygon: transformPolygon,
15
+ MultiPolygon: transformMultiPolygon,
16
+ };
17
+
18
+ /**
19
+ * Transform WGS84 coordinates to tile coords.
20
+ * It's the inverse of deck.gl coordinate-transform (https://github.com/visgl/deck.gl/blob/master/modules/geo-layers/src/mvt-layer/coordinate-transform.js)
21
+ *
22
+ * @param geometry - any valid geojson geometry
23
+ * @param bbox - geojson bbox
24
+ */
25
+ export function transformToTileCoords<T extends Geometry>(
26
+ geometry: T,
27
+ bbox: BBox
28
+ ): T {
29
+ const [west, south, east, north] = bbox;
30
+ const nw = projectFlat([west, north]);
31
+ const se = projectFlat([east, south]);
32
+ const projectedBbox = [nw, se];
33
+
34
+ if (geometry.type === 'GeometryCollection') {
35
+ throw new Error('Unsupported geometry type GeometryCollection');
36
+ }
37
+
38
+ const transformFn = TRANSFORM_FN[geometry.type];
39
+ const coordinates = transformFn(geometry.coordinates, projectedBbox);
40
+ return {...geometry, coordinates};
41
+ }
42
+
43
+ function transformPoint([pointX, pointY]: Position, [nw, se]: Position[]) {
44
+ const x = inverseLerp(nw[0], se[0], pointX);
45
+ const y = inverseLerp(nw[1], se[1], pointY);
46
+
47
+ return [x, y];
48
+ }
49
+
50
+ function getPoints(geometry: Position[], bbox: Position[]) {
51
+ return geometry.map((g) => transformPoint(projectFlat(g), bbox));
52
+ }
53
+
54
+ function transformMultiPoint(multiPoint: Position[], bbox: Position[]) {
55
+ return getPoints(multiPoint, bbox);
56
+ }
57
+
58
+ function transformLineString(line: Position[], bbox: Position[]) {
59
+ return getPoints(line, bbox);
60
+ }
61
+
62
+ function transformMultiLineString(
63
+ multiLineString: Position[][],
64
+ bbox: Position[]
65
+ ) {
66
+ return multiLineString.map((lineString) =>
67
+ transformLineString(lineString, bbox)
68
+ );
69
+ }
70
+
71
+ function transformPolygon(polygon: Position[][], bbox: Position[]) {
72
+ return polygon.map((polygonRing) => getPoints(polygonRing, bbox));
73
+ }
74
+
75
+ function transformMultiPolygon(multiPolygon: Position[][][], bbox: Position[]) {
76
+ return multiPolygon.map((polygon) => transformPolygon(polygon, bbox));
77
+ }
78
+
79
+ function projectFlat(xyz: Position): Position {
80
+ return lngLatToWorld(xyz);
81
+ }
82
+
83
+ function inverseLerp(a: number, b: number, x: number): number {
84
+ return (x - a) / (b - a);
85
+ }
package/src/utils.ts CHANGED
@@ -49,15 +49,18 @@ export function normalizeObjectKeys<T, R extends Row<T>>(el: R): R {
49
49
  return el;
50
50
  }
51
51
 
52
- return Object.entries(el as Record<string, T>).reduce((acc, [key, value]) => {
53
- acc[key.toLowerCase()] =
54
- typeof value === 'object' && value ? normalizeObjectKeys(value) : value;
55
- return acc;
56
- }, {} as Record<string, T>) as R;
52
+ return Object.entries(el as Record<string, T>).reduce(
53
+ (acc, [key, value]) => {
54
+ acc[key.toLowerCase()] =
55
+ typeof value === 'object' && value ? normalizeObjectKeys(value) : value;
56
+ return acc;
57
+ },
58
+ {} as Record<string, T>
59
+ ) as R;
57
60
  }
58
61
 
59
62
  /** @internalRemarks Source: @carto/react-core */
60
- export function assert(condition: unknown, message: string) {
63
+ export function assert(condition: unknown, message: string): asserts condition {
61
64
  if (!condition) {
62
65
  throw new Error(message);
63
66
  }
@@ -1,4 +1,5 @@
1
- export * from './widget-base-source.js';
2
1
  export * from './widget-query-source.js';
2
+ export * from './widget-remote-source.js';
3
3
  export * from './widget-table-source.js';
4
+ export * from './widget-tileset-source.js';
4
5
  export * from './types.js';
@@ -1,31 +1,43 @@
1
- import {TileResolution} from '../sources/types';
1
+ import {SpatialFilterPolyfillMode, TileResolution} from '../sources/types';
2
2
  import {
3
3
  GroupDateType,
4
4
  SortColumnType,
5
5
  SortDirection,
6
6
  SpatialFilter,
7
7
  } from '../types';
8
+ import type {WidgetRemoteSource} from './widget-remote-source';
8
9
 
9
10
  /******************************************************************************
10
11
  * WIDGET API REQUESTS
11
12
  */
12
13
 
13
- /** Common options for {@link WidgetBaseSource} requests. */
14
+ export interface ViewState {
15
+ zoom: number;
16
+ latitude: number;
17
+ longitude: number;
18
+ }
19
+
20
+ /** Common options for {@link WidgetRemoteSource} requests. */
14
21
  interface BaseRequestOptions {
15
22
  spatialFilter?: SpatialFilter;
23
+ spatialFiltersMode?: SpatialFilterPolyfillMode;
24
+ /** Required for table- and query-based spatial index sources (H3, Quadbin). */
25
+ spatialIndexReferenceViewState?: ViewState;
16
26
  abortController?: AbortController;
17
27
  filterOwner?: string;
18
28
  }
19
29
 
20
- /** Options for {@link WidgetBaseSource#getCategories}. */
30
+ /** Options for {@link WidgetRemoteSource#getCategories}. */
21
31
  export interface CategoryRequestOptions extends BaseRequestOptions {
22
32
  column: string;
23
33
  operation?: 'count' | 'avg' | 'min' | 'max' | 'sum';
24
34
  operationColumn?: string;
35
+ /** Local only. */
36
+ joinOperation?: 'count' | 'avg' | 'min' | 'max' | 'sum';
25
37
  }
26
38
 
27
39
  /**
28
- * Options for {@link WidgetBaseSource#getFeatures}.
40
+ * Options for {@link WidgetRemoteSource#getFeatures}.
29
41
  * @experimental
30
42
  * @internal
31
43
  */
@@ -67,26 +79,29 @@ export interface FeaturesRequestOptions extends BaseRequestOptions {
67
79
  tileResolution?: TileResolution;
68
80
  }
69
81
 
70
- /** Options for {@link WidgetBaseSource#getFormula}. */
82
+ /** Options for {@link WidgetRemoteSource#getFormula}. */
71
83
  export interface FormulaRequestOptions extends BaseRequestOptions {
72
84
  column: string;
73
- operation?: 'count' | 'avg' | 'min' | 'max' | 'sum';
85
+ operation?: 'count' | 'avg' | 'min' | 'max' | 'sum' | 'custom';
74
86
  operationExp?: string;
87
+ joinOperation?: 'count' | 'avg' | 'min' | 'max' | 'sum';
75
88
  }
76
89
 
77
- /** Options for {@link WidgetBaseSource#getHistogram}. */
90
+ /** Options for {@link WidgetRemoteSource#getHistogram}. */
78
91
  export interface HistogramRequestOptions extends BaseRequestOptions {
79
92
  column: string;
80
93
  ticks: number[];
81
94
  operation?: 'count' | 'avg' | 'min' | 'max' | 'sum';
95
+ /** Local only. */
96
+ joinOperation?: 'count' | 'avg' | 'min' | 'max' | 'sum';
82
97
  }
83
98
 
84
- /** Options for {@link WidgetBaseSource#getRange}. */
99
+ /** Options for {@link WidgetRemoteSource#getRange}. */
85
100
  export interface RangeRequestOptions extends BaseRequestOptions {
86
101
  column: string;
87
102
  }
88
103
 
89
- /** Options for {@link WidgetBaseSource#getScatter}. */
104
+ /** Options for {@link WidgetRemoteSource#getScatter}. */
90
105
  export interface ScatterRequestOptions extends BaseRequestOptions {
91
106
  xAxisColumn: string;
92
107
  xAxisJoinOperation?: 'count' | 'avg' | 'min' | 'max' | 'sum';
@@ -94,7 +109,7 @@ export interface ScatterRequestOptions extends BaseRequestOptions {
94
109
  yAxisJoinOperation?: 'count' | 'avg' | 'min' | 'max' | 'sum';
95
110
  }
96
111
 
97
- /** Options for {@link WidgetBaseSource#getTable}. */
112
+ /** Options for {@link WidgetRemoteSource#getTable}. */
98
113
  export interface TableRequestOptions extends BaseRequestOptions {
99
114
  columns: string[];
100
115
  sortBy?: string;
@@ -102,12 +117,16 @@ export interface TableRequestOptions extends BaseRequestOptions {
102
117
  sortByColumnType?: SortColumnType;
103
118
  offset?: number;
104
119
  limit?: number;
120
+ /** Local only. */
121
+ searchFilterColumn?: string;
122
+ /** Local only. */
123
+ searchFilterText?: string;
105
124
  }
106
125
 
107
- /** Options for {@link WidgetBaseSource#getTimeSeries}. */
126
+ /** Options for {@link WidgetRemoteSource#getTimeSeries}. */
108
127
  export interface TimeSeriesRequestOptions extends BaseRequestOptions {
109
128
  column: string;
110
- stepSize?: GroupDateType;
129
+ stepSize: GroupDateType;
111
130
  stepMultiplier?: number;
112
131
  operation?: 'count' | 'avg' | 'min' | 'max' | 'sum';
113
132
  operationColumn?: string;
@@ -122,35 +141,35 @@ export interface TimeSeriesRequestOptions extends BaseRequestOptions {
122
141
  */
123
142
 
124
143
  /**
125
- * Response from {@link WidgetBaseSource#getFeatures}.
144
+ * Response from {@link WidgetRemoteSource#getFeatures}.
126
145
  * @experimental
127
146
  * @internal
128
147
  */
129
148
  export type FeaturesResponse = {rows: Record<string, unknown>[]};
130
149
 
131
- /** Response from {@link WidgetBaseSource#getFormula}. */
132
- export type FormulaResponse = {value: number};
150
+ /** Response from {@link WidgetRemoteSource#getFormula}. */
151
+ export type FormulaResponse = {value: number | null};
133
152
 
134
- /** Response from {@link WidgetBaseSource#getCategories}. */
153
+ /** Response from {@link WidgetRemoteSource#getCategories}. */
135
154
  export type CategoryResponse = {name: string; value: number}[];
136
155
 
137
- /** Response from {@link WidgetBaseSource#getRange}. */
138
- export type RangeResponse = {min: number; max: number};
156
+ /** Response from {@link WidgetRemoteSource#getRange}. */
157
+ export type RangeResponse = {min: number; max: number} | null;
139
158
 
140
- /** Response from {@link WidgetBaseSource#getTable}. */
159
+ /** Response from {@link WidgetRemoteSource#getTable}. */
141
160
  export type TableResponse = {
142
161
  totalCount: number;
143
162
  rows: Record<string, number | string>[];
144
163
  };
145
164
 
146
- /** Response from {@link WidgetBaseSource#getScatter}. */
165
+ /** Response from {@link WidgetRemoteSource#getScatter}. */
147
166
  export type ScatterResponse = [number, number][];
148
167
 
149
- /** Response from {@link WidgetBaseSource#getTimeSeries}. */
168
+ /** Response from {@link WidgetRemoteSource#getTimeSeries}. */
150
169
  export type TimeSeriesResponse = {
151
170
  rows: {name: string; value: number}[];
152
- categories: string[];
171
+ categories?: string[];
153
172
  };
154
173
 
155
- /** Response from {@link WidgetBaseSource#getHistogram}. */
174
+ /** Response from {@link WidgetRemoteSource#getHistogram}. */
156
175
  export type HistogramResponse = number[];
@@ -3,7 +3,10 @@ import {
3
3
  QuadbinQuerySourceOptions,
4
4
  VectorQuerySourceOptions,
5
5
  } from '../sources/index.js';
6
- import {WidgetBaseSource, WidgetBaseSourceProps} from './widget-base-source.js';
6
+ import {
7
+ WidgetRemoteSource,
8
+ WidgetRemoteSourceProps,
9
+ } from './widget-remote-source.js';
7
10
  import {ModelSource} from '../models/model.js';
8
11
 
9
12
  type LayerQuerySourceOptions =
@@ -35,8 +38,8 @@ export type WidgetQuerySourceResult = {widgetSource: WidgetQuerySource};
35
38
  * const { widgetSource } = await data;
36
39
  * ```
37
40
  */
38
- export class WidgetQuerySource extends WidgetBaseSource<
39
- LayerQuerySourceOptions & WidgetBaseSourceProps
41
+ export class WidgetQuerySource extends WidgetRemoteSource<
42
+ LayerQuerySourceOptions & WidgetRemoteSourceProps
40
43
  > {
41
44
  protected override getModelSource(owner: string): ModelSource {
42
45
  return {