@deck.gl/carto 8.9.7 → 8.9.8

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 (150) hide show
  1. package/dist/dist.dev.js +774 -114
  2. package/dist/es5/api/layer-map.js +9 -15
  3. package/dist/es5/api/layer-map.js.map +1 -1
  4. package/dist/es5/api/maps-api-common.js +2 -1
  5. package/dist/es5/api/maps-api-common.js.map +1 -1
  6. package/dist/es5/api/maps-v3-client.js +17 -6
  7. package/dist/es5/api/maps-v3-client.js.map +1 -1
  8. package/dist/es5/api/parseMap.js +1 -1
  9. package/dist/es5/api/parseMap.js.map +1 -1
  10. package/dist/es5/index.js +8 -0
  11. package/dist/es5/index.js.map +1 -1
  12. package/dist/es5/layers/carto-layer.js +1 -1
  13. package/dist/es5/layers/carto-layer.js.map +1 -1
  14. package/dist/es5/layers/h3-tile-layer.js +2 -1
  15. package/dist/es5/layers/h3-tile-layer.js.map +1 -1
  16. package/dist/es5/layers/quadbin-tile-layer.js +2 -1
  17. package/dist/es5/layers/quadbin-tile-layer.js.map +1 -1
  18. package/dist/es5/layers/quadbin-utils.js +14 -4
  19. package/dist/es5/layers/quadbin-utils.js.map +1 -1
  20. package/dist/es5/layers/raster-layer-vertex.glsl.js +9 -0
  21. package/dist/es5/layers/raster-layer-vertex.glsl.js.map +1 -0
  22. package/dist/es5/layers/raster-layer.js +176 -0
  23. package/dist/es5/layers/raster-layer.js.map +1 -0
  24. package/dist/es5/layers/raster-tile-layer.js +114 -0
  25. package/dist/es5/layers/raster-tile-layer.js.map +1 -0
  26. package/dist/es5/layers/schema/carto-raster-tile-loader.js +80 -0
  27. package/dist/es5/layers/schema/carto-raster-tile-loader.js.map +1 -0
  28. package/dist/es5/layers/schema/carto-raster-tile.js +87 -0
  29. package/dist/es5/layers/schema/carto-raster-tile.js.map +1 -0
  30. package/dist/es5/layers/schema/carto-spatial-tile.js +1 -1
  31. package/dist/es5/layers/schema/carto-spatial-tile.js.map +1 -1
  32. package/dist/es5/layers/schema/carto-tile.js +2 -2
  33. package/dist/es5/layers/schema/carto-tile.js.map +1 -1
  34. package/dist/es5/layers/schema/fast-pbf.js +3 -11
  35. package/dist/es5/layers/schema/fast-pbf.js.map +1 -1
  36. package/dist/es5/layers/spatial-index-tile-layer.js +3 -41
  37. package/dist/es5/layers/spatial-index-tile-layer.js.map +1 -1
  38. package/dist/es5/utils.js +18 -0
  39. package/dist/es5/utils.js.map +1 -1
  40. package/dist/esm/api/layer-map.js +9 -21
  41. package/dist/esm/api/layer-map.js.map +1 -1
  42. package/dist/esm/api/maps-api-common.js +2 -1
  43. package/dist/esm/api/maps-api-common.js.map +1 -1
  44. package/dist/esm/api/maps-v3-client.js +9 -1
  45. package/dist/esm/api/maps-v3-client.js.map +1 -1
  46. package/dist/esm/api/parseMap.js +1 -1
  47. package/dist/esm/api/parseMap.js.map +1 -1
  48. package/dist/esm/index.js +1 -0
  49. package/dist/esm/index.js.map +1 -1
  50. package/dist/esm/layers/carto-layer.js +1 -1
  51. package/dist/esm/layers/carto-layer.js.map +1 -1
  52. package/dist/esm/layers/h3-tile-layer.js +2 -1
  53. package/dist/esm/layers/h3-tile-layer.js.map +1 -1
  54. package/dist/esm/layers/quadbin-tile-layer.js +2 -1
  55. package/dist/esm/layers/quadbin-tile-layer.js.map +1 -1
  56. package/dist/esm/layers/quadbin-utils.js +7 -4
  57. package/dist/esm/layers/quadbin-utils.js.map +1 -1
  58. package/dist/esm/layers/raster-layer-vertex.glsl.js +2 -0
  59. package/dist/esm/layers/raster-layer-vertex.glsl.js.map +1 -0
  60. package/dist/esm/layers/raster-layer.js +111 -0
  61. package/dist/esm/layers/raster-layer.js.map +1 -0
  62. package/dist/esm/layers/raster-tile-layer.js +67 -0
  63. package/dist/esm/layers/raster-tile-layer.js.map +1 -0
  64. package/dist/esm/layers/schema/carto-raster-tile-loader.js +46 -0
  65. package/dist/esm/layers/schema/carto-raster-tile-loader.js.map +1 -0
  66. package/dist/esm/layers/schema/carto-raster-tile.js +51 -0
  67. package/dist/esm/layers/schema/carto-raster-tile.js.map +1 -0
  68. package/dist/esm/layers/schema/carto-spatial-tile.js +2 -2
  69. package/dist/esm/layers/schema/carto-spatial-tile.js.map +1 -1
  70. package/dist/esm/layers/schema/carto-tile.js +3 -3
  71. package/dist/esm/layers/schema/carto-tile.js.map +1 -1
  72. package/dist/esm/layers/schema/fast-pbf.js +2 -8
  73. package/dist/esm/layers/schema/fast-pbf.js.map +1 -1
  74. package/dist/esm/layers/spatial-index-tile-layer.js +3 -41
  75. package/dist/esm/layers/spatial-index-tile-layer.js.map +1 -1
  76. package/dist/esm/utils.js +20 -0
  77. package/dist/esm/utils.js.map +1 -1
  78. package/dist.min.js +100 -23
  79. package/package.json +3 -3
  80. package/src/api/layer-map.ts +12 -19
  81. package/src/api/maps-api-common.ts +2 -1
  82. package/src/api/maps-v3-client.ts +6 -1
  83. package/src/api/parseMap.ts +1 -1
  84. package/src/index.ts +1 -0
  85. package/src/layers/carto-layer.ts +1 -1
  86. package/src/layers/h3-tile-layer.ts +2 -3
  87. package/src/layers/quadbin-tile-layer.ts +2 -3
  88. package/src/layers/quadbin-utils.ts +9 -5
  89. package/src/layers/raster-layer-vertex.glsl.ts +98 -0
  90. package/src/layers/raster-layer.ts +146 -0
  91. package/src/layers/raster-tile-layer.ts +63 -0
  92. package/src/layers/schema/carto-raster-tile-loader.ts +42 -0
  93. package/src/layers/schema/carto-raster-tile.ts +46 -0
  94. package/src/layers/schema/carto-spatial-tile.ts +2 -2
  95. package/src/layers/schema/carto-tile.ts +3 -3
  96. package/src/layers/schema/fast-pbf.ts +3 -9
  97. package/src/layers/spatial-index-tile-layer.ts +3 -38
  98. package/src/utils.ts +22 -0
  99. package/typed/api/layer-map.d.ts +2 -1
  100. package/typed/api/layer-map.d.ts.map +1 -1
  101. package/typed/api/maps-api-common.d.ts +1 -0
  102. package/typed/api/maps-api-common.d.ts.map +1 -1
  103. package/typed/api/maps-v3-client.d.ts.map +1 -1
  104. package/typed/index.d.ts +1 -0
  105. package/typed/index.d.ts.map +1 -1
  106. package/typed/layers/h3-tile-layer.d.ts.map +1 -1
  107. package/typed/layers/quadbin-tile-layer.d.ts.map +1 -1
  108. package/typed/layers/quadbin-utils.d.ts +1 -0
  109. package/typed/layers/quadbin-utils.d.ts.map +1 -1
  110. package/typed/layers/raster-layer-vertex.glsl.d.ts +3 -0
  111. package/typed/layers/raster-layer-vertex.glsl.d.ts.map +1 -0
  112. package/typed/layers/raster-layer.d.ts +19 -0
  113. package/typed/layers/raster-layer.d.ts.map +1 -0
  114. package/typed/layers/raster-tile-layer.d.ts +24 -0
  115. package/typed/layers/raster-tile-layer.d.ts.map +1 -0
  116. package/typed/layers/schema/carto-raster-tile-loader.d.ts +13 -0
  117. package/typed/layers/schema/carto-raster-tile-loader.d.ts.map +1 -0
  118. package/typed/layers/schema/carto-raster-tile.d.ts +9 -0
  119. package/typed/layers/schema/carto-raster-tile.d.ts.map +1 -0
  120. package/typed/layers/schema/carto-spa-tile.d.ts +57 -0
  121. package/typed/layers/schema/carto-spa-tile.d.ts.map +1 -0
  122. package/typed/layers/schema/fast-pbf.d.ts +1 -2
  123. package/typed/layers/schema/fast-pbf.d.ts.map +1 -1
  124. package/typed/layers/spatial-index-tile-layer.d.ts +1 -2
  125. package/typed/layers/spatial-index-tile-layer.d.ts.map +1 -1
  126. package/typed/utils.d.ts +5 -0
  127. package/typed/utils.d.ts.map +1 -1
  128. package/typed/api/index.js +0 -3
  129. package/typed/api/layer-map.js +0 -264
  130. package/typed/api/maps-api-common.js +0 -50
  131. package/typed/api/maps-client.js +0 -125
  132. package/typed/api/maps-v3-client.js +0 -304
  133. package/typed/api/parseMap.js +0 -178
  134. package/typed/basemap.js +0 -9
  135. package/typed/config.js +0 -35
  136. package/typed/index.js +0 -10
  137. package/typed/layers/carto-layer.js +0 -173
  138. package/typed/layers/carto-tile-layer.js +0 -105
  139. package/typed/layers/h3-tile-layer.js +0 -47
  140. package/typed/layers/h3-tileset-2d.js +0 -85
  141. package/typed/layers/quadkey-tile-layer.js +0 -42
  142. package/typed/layers/quadkey-tileset-2d.js +0 -36
  143. package/typed/layers/schema/carto-tile.js +0 -161
  144. package/typed/layers/spatial-index-tile-layer.js +0 -40
  145. package/typed/style/color-bins-style.js +0 -12
  146. package/typed/style/color-categories-style.js +0 -16
  147. package/typed/style/color-continuous-style.js +0 -12
  148. package/typed/style/palette.js +0 -62
  149. package/typed/style/utils.js +0 -15
  150. package/typed/utils.js +0 -4
@@ -1,264 +0,0 @@
1
- import { deviation, extent, groupSort, median, variance } from 'd3-array';
2
- import { rgb } from 'd3-color';
3
- import { scaleLinear, scaleOrdinal, scaleLog, scalePoint, scaleQuantile, scaleQuantize, scaleSqrt, scaleThreshold } from 'd3-scale';
4
- import { format as d3Format } from 'd3-format';
5
- import moment from 'moment-timezone';
6
- import { CPUGridLayer, HeatmapLayer, HexagonLayer } from '@deck.gl/aggregation-layers';
7
- import { GeoJsonLayer } from '@deck.gl/layers';
8
- import { H3HexagonLayer, MVTLayer } from '@deck.gl/geo-layers';
9
- import CartoTileLayer from '../layers/carto-tile-layer';
10
- import { TILE_FORMATS } from './maps-api-common';
11
- import { assert } from '../utils';
12
- const SCALE_FUNCS = {
13
- linear: scaleLinear,
14
- ordinal: scaleOrdinal,
15
- log: scaleLog,
16
- point: scalePoint,
17
- quantile: scaleQuantile,
18
- quantize: scaleQuantize,
19
- sqrt: scaleSqrt,
20
- custom: scaleThreshold
21
- };
22
- const UNKNOWN_COLOR = '#868d91';
23
- export const AGGREGATION = {
24
- average: 'MEAN',
25
- maximum: 'MAX',
26
- minimum: 'MIN',
27
- sum: 'SUM'
28
- };
29
- export const OPACITY_MAP = {
30
- getFillColor: 'opacity',
31
- getLineColor: 'strokeOpacity',
32
- getTextColor: 'opacity'
33
- };
34
- const AGGREGATION_FUNC = {
35
- 'count unique': (values, accessor) => groupSort(values, v => v.length, accessor).length,
36
- median,
37
- // Unfortunately mode() is only available in d3-array@3+ which is ESM only
38
- mode: (values, accessor) => groupSort(values, v => v.length, accessor).pop(),
39
- stddev: deviation,
40
- variance
41
- };
42
- const hexToRGBA = c => {
43
- const { r, g, b, opacity } = rgb(c);
44
- return [r, g, b, 255 * opacity];
45
- };
46
- // Kepler -> Deck.gl
47
- const sharedPropMap = {
48
- color: 'getFillColor',
49
- isVisible: 'visible',
50
- label: 'cartoLabel',
51
- textLabel: {
52
- alignment: 'getTextAlignmentBaseline',
53
- anchor: 'getTextAnchor',
54
- color: 'getTextColor',
55
- size: 'getTextSize'
56
- },
57
- visConfig: {
58
- enable3d: 'extruded',
59
- elevationScale: 'elevationScale',
60
- filled: 'filled',
61
- strokeColor: 'getLineColor',
62
- stroked: 'stroked',
63
- thickness: 'getLineWidth',
64
- radius: 'getPointRadius',
65
- wireframe: 'wireframe'
66
- }
67
- };
68
- const aggregationVisConfig = {
69
- colorAggregation: x => ({ colorAggregation: AGGREGATION[x] || AGGREGATION.sum }),
70
- colorRange: x => ({ colorRange: x.colors.map(hexToRGBA) }),
71
- coverage: 'coverage',
72
- elevationPercentile: ['elevationLowerPercentile', 'elevationUpperPercentile'],
73
- percentile: ['lowerPercentile', 'upperPercentile']
74
- };
75
- const defaultProps = {
76
- lineMiterLimit: 2,
77
- lineWidthUnits: 'pixels',
78
- pointRadiusUnits: 'pixels',
79
- rounded: true,
80
- wrapLongitude: false
81
- };
82
- function mergePropMaps(a = {}, b = {}) {
83
- return { ...a, ...b, visConfig: { ...a.visConfig, ...b.visConfig } };
84
- }
85
- export function getLayer(type, config, dataset) {
86
- if (type === 'mvt' || type === 'tileset') {
87
- return getTileLayer(dataset);
88
- }
89
- const geoColumn = dataset?.geoColumn;
90
- const getPosition = d => d[geoColumn].coordinates;
91
- const hexagonId = config.columns?.hex_id;
92
- const layerTypeDefs = {
93
- point: {
94
- Layer: GeoJsonLayer,
95
- propMap: { visConfig: { outline: 'stroked' } }
96
- },
97
- geojson: {
98
- Layer: GeoJsonLayer
99
- },
100
- grid: {
101
- Layer: CPUGridLayer,
102
- propMap: { visConfig: { ...aggregationVisConfig, worldUnitSize: x => ({ cellSize: 1000 * x }) } },
103
- defaultProps: { getPosition }
104
- },
105
- heatmap: {
106
- Layer: HeatmapLayer,
107
- propMap: { visConfig: { ...aggregationVisConfig, radius: 'radiusPixels' } },
108
- defaultProps: { getPosition }
109
- },
110
- hexagon: {
111
- Layer: HexagonLayer,
112
- propMap: { visConfig: { ...aggregationVisConfig, worldUnitSize: x => ({ radius: 1000 * x }) } },
113
- defaultProps: { getPosition }
114
- },
115
- hexagonId: {
116
- Layer: H3HexagonLayer,
117
- propMap: { visConfig: { coverage: 'coverage' } },
118
- defaultProps: { getHexagon: d => d[hexagonId], stroked: false }
119
- }
120
- };
121
- const layer = layerTypeDefs[type];
122
- assert(layer, `Unsupported layer type: ${type}`);
123
- return {
124
- ...layer,
125
- propMap: mergePropMaps(sharedPropMap, layer.propMap),
126
- defaultProps: { ...defaultProps, ...layer.defaultProps }
127
- };
128
- }
129
- function getTileLayer(dataset) {
130
- const { data: { tiles: [tileUrl] } } = dataset;
131
- /* global URL */
132
- const formatTiles = new URL(tileUrl).searchParams.get('formatTiles') || TILE_FORMATS.MVT;
133
- return {
134
- Layer: formatTiles === TILE_FORMATS.MVT ? MVTLayer : CartoTileLayer,
135
- propMap: sharedPropMap,
136
- defaultProps: {
137
- ...defaultProps,
138
- uniqueIdProperty: 'geoid',
139
- formatTiles
140
- }
141
- };
142
- }
143
- function domainFromAttribute(attribute, scaleType) {
144
- if (scaleType === 'ordinal' || scaleType === 'point') {
145
- return attribute.categories.map(c => c.category).filter(c => c !== undefined && c !== null);
146
- }
147
- let { min } = attribute;
148
- if (scaleType === 'log' && min === 0) {
149
- min = 1e-5;
150
- }
151
- return [min, attribute.max];
152
- }
153
- function domainFromValues(values, scaleType) {
154
- if (scaleType === 'ordinal') {
155
- return groupSort(values, g => -g.length, d => d);
156
- }
157
- else if (scaleType === 'quantile') {
158
- return values.sort((a, b) => a - b);
159
- }
160
- else if (scaleType === 'log') {
161
- const [d0, d1] = extent(values);
162
- return [d0 === 0 ? 1e-5 : d0, d1];
163
- }
164
- return extent(values);
165
- }
166
- function calculateDomain(data, name, scaleType) {
167
- if (data.tilestats) {
168
- // Tileset data type
169
- const { attributes } = data.tilestats.layers[0];
170
- const attribute = attributes.find(a => a.attribute === name);
171
- return domainFromAttribute(attribute, scaleType);
172
- }
173
- else if (data.features) {
174
- // GeoJSON data type
175
- const values = data.features.map(({ properties }) => properties[name]);
176
- return domainFromValues(values, scaleType);
177
- }
178
- else if (Array.isArray(data) && data[0][name] !== undefined) {
179
- // JSON data type
180
- const values = data.map(properties => properties[name]);
181
- return domainFromValues(values, scaleType);
182
- }
183
- return [0, 1];
184
- }
185
- function normalizeAccessor(accessor, data) {
186
- if (data.features || data.tilestats) {
187
- return ({ properties }) => {
188
- return accessor(properties);
189
- };
190
- }
191
- return accessor;
192
- }
193
- export function opacityToAlpha(opacity) {
194
- return opacity !== undefined ? Math.round(255 * Math.pow(opacity, 1 / 2.2)) : 255;
195
- }
196
- export function getColorValueAccessor({ name }, colorAggregation, data) {
197
- const aggregator = AGGREGATION_FUNC[colorAggregation];
198
- const accessor = values => aggregator(values, p => p[name]);
199
- return normalizeAccessor(accessor, data);
200
- }
201
- export function getColorAccessor({ name }, scaleType, { colors, colorMap }, opacity, data) {
202
- const scale = SCALE_FUNCS[scaleType]();
203
- let domain = [];
204
- let scaleColor = [];
205
- if (Array.isArray(colorMap)) {
206
- colorMap.forEach(([value, color]) => {
207
- domain.push(value);
208
- scaleColor.push(color);
209
- });
210
- }
211
- else {
212
- domain = calculateDomain(data, name, scaleType);
213
- scaleColor = colors;
214
- }
215
- if (scaleType === 'ordinal') {
216
- domain = domain.slice(0, scaleColor.length);
217
- }
218
- scale.domain(domain);
219
- scale.range(scaleColor);
220
- scale.unknown(UNKNOWN_COLOR);
221
- const alpha = opacityToAlpha(opacity);
222
- const accessor = properties => {
223
- const propertyValue = properties[name];
224
- const { r, g, b } = rgb(scale(propertyValue));
225
- return [r, g, b, propertyValue === null ? 0 : alpha];
226
- };
227
- return normalizeAccessor(accessor, data);
228
- }
229
- export function getSizeAccessor({ name }, scaleType, range, data) {
230
- const scale = SCALE_FUNCS[scaleType]();
231
- scale.domain(calculateDomain(data, name, scaleType));
232
- scale.range(range);
233
- const accessor = properties => {
234
- return scale(properties[name]);
235
- };
236
- return normalizeAccessor(accessor, data);
237
- }
238
- const FORMATS = {
239
- date: s => moment.utc(s).format('MM/DD/YY HH:mm:ssa'),
240
- integer: d3Format('i'),
241
- float: d3Format('.5f'),
242
- timestamp: s => moment.utc(s).format('X'),
243
- default: String
244
- };
245
- export function getTextAccessor({ name, type }, data) {
246
- const format = FORMATS[type] || FORMATS.default;
247
- const accessor = properties => {
248
- return format(properties[name]);
249
- };
250
- return normalizeAccessor(accessor, data);
251
- }
252
- export function getTextPixelOffsetAccessor({ alignment, anchor, size }, radius) {
253
- const padding = 20;
254
- const signX = anchor === 'middle' ? 0 : anchor === 'start' ? 1 : -1;
255
- const signY = alignment === 'center' ? 0 : alignment === 'bottom' ? 1 : -1;
256
- const sizeOffset = alignment === 'center' ? 0 : size;
257
- const calculateOffset = r => [signX * (r + padding), signY * (r + padding + sizeOffset)];
258
- return typeof radius === 'function'
259
- ? d => {
260
- return calculateOffset(radius(d));
261
- }
262
- : calculateOffset(radius);
263
- }
264
- export { domainFromValues as _domainFromValues };
@@ -1,50 +0,0 @@
1
- export const DEFAULT_USER_COMPONENT_IN_URL = '{user}';
2
- export const DEFAULT_REGION_COMPONENT_IN_URL = '{region}';
3
- export const API_VERSIONS = {
4
- V1: 'v1',
5
- V2: 'v2',
6
- V3: 'v3'
7
- };
8
- export const DEFAULT_MAPS_URL_FORMAT = {
9
- [API_VERSIONS.V1]: `https://${DEFAULT_USER_COMPONENT_IN_URL}.carto.com/api/v1/map`,
10
- [API_VERSIONS.V2]: `https://maps-api-v2.${DEFAULT_REGION_COMPONENT_IN_URL}.carto.com/user/${DEFAULT_USER_COMPONENT_IN_URL}`
11
- };
12
- export const MAP_TYPES = {
13
- QUERY: 'query',
14
- TABLE: 'table',
15
- TILESET: 'tileset'
16
- };
17
- export const COLUMNS_SUPPORT = [MAP_TYPES.TABLE];
18
- export const GEO_COLUMN_SUPPORT = [MAP_TYPES.QUERY, MAP_TYPES.TABLE];
19
- // AVAILABLE FORMATS
20
- export const FORMATS = {
21
- GEOJSON: 'geojson',
22
- NDJSON: 'ndjson',
23
- TILEJSON: 'tilejson',
24
- JSON: 'json'
25
- };
26
- // AVAILABLE FORMATS FOR TILES
27
- export const TILE_FORMATS = {
28
- MVT: 'mvt',
29
- JSON: 'json',
30
- GEOJSON: 'geojson',
31
- BINARY: 'binary'
32
- };
33
- export var SchemaFieldType;
34
- (function (SchemaFieldType) {
35
- SchemaFieldType["Number"] = "number";
36
- SchemaFieldType["Bigint"] = "bigint";
37
- SchemaFieldType["String"] = "string";
38
- SchemaFieldType["Geometry"] = "geometry";
39
- SchemaFieldType["Timestamp"] = "timestamp";
40
- SchemaFieldType["Object"] = "object";
41
- SchemaFieldType["Boolean"] = "boolean";
42
- SchemaFieldType["Variant"] = "variant";
43
- SchemaFieldType["Unknown"] = "unknown";
44
- })(SchemaFieldType || (SchemaFieldType = {}));
45
- /**
46
- * Simple encode parameter
47
- */
48
- export function encodeParameter(name, value) {
49
- return `${name}=${encodeURIComponent(value)}`;
50
- }
@@ -1,125 +0,0 @@
1
- /**
2
- * Maps API Client for Maps API v1 and Maps API v2
3
- */
4
- import { defaultClassicCredentials, defaultCloudNativeCredentials, getDefaultCredentials } from '../config';
5
- import { API_VERSIONS, DEFAULT_MAPS_URL_FORMAT, DEFAULT_REGION_COMPONENT_IN_URL, DEFAULT_USER_COMPONENT_IN_URL, encodeParameter, MAP_TYPES } from './maps-api-common';
6
- export const CONNECTIONS = {
7
- BIGQUERY: 'bigquery',
8
- CARTO: 'carto'
9
- };
10
- const BUFFER_SIZE = 16;
11
- const TILE_EXTENT = 4096;
12
- /**
13
- * Obtain a TileJson from Maps API v1 and v2
14
- */
15
- export async function getDataV2({ type, source, credentials }) {
16
- const defaultCredentials = getDefaultCredentials();
17
- const apiVersion = credentials?.apiVersion || defaultCredentials.apiVersion;
18
- // Only pick up default credentials if they have been defined for
19
- // correct API version
20
- const localCreds = {
21
- ...(apiVersion === API_VERSIONS.V3 ? defaultCloudNativeCredentials : defaultClassicCredentials),
22
- mapsUrl: DEFAULT_MAPS_URL_FORMAT[apiVersion],
23
- ...(defaultCredentials.apiVersion === apiVersion && defaultCredentials),
24
- ...credentials
25
- };
26
- let url;
27
- const connection = type === MAP_TYPES.TILESET ? CONNECTIONS.BIGQUERY : CONNECTIONS.CARTO;
28
- switch (apiVersion) {
29
- case API_VERSIONS.V1:
30
- // Maps API v1
31
- const mapConfig = createMapConfig(source);
32
- url = buildURLMapsAPIv1({ mapConfig, credentials: localCreds });
33
- const layergroup = await request({ url, credentials: localCreds });
34
- return layergroup.metadata.tilejson.vector;
35
- case API_VERSIONS.V2:
36
- // Maps API v2
37
- url = buildURLMapsAPIv2({ connection, type, source, credentials: localCreds });
38
- return await request({ url, credentials: localCreds });
39
- default:
40
- throw new Error(`Invalid maps API version. It should be ${API_VERSIONS.V1} or ${API_VERSIONS.V2}`);
41
- }
42
- }
43
- /**
44
- * Request against Maps API
45
- */
46
- async function request({ url, credentials }) {
47
- let response;
48
- try {
49
- /* global fetch */
50
- response = await fetch(url, {
51
- headers: {
52
- Accept: 'application/json'
53
- }
54
- });
55
- }
56
- catch (error) {
57
- throw new Error(`Failed to connect to Maps API: ${error}`);
58
- }
59
- const json = await response.json();
60
- if (!response.ok) {
61
- dealWithError({ response, json, credentials });
62
- }
63
- return json;
64
- }
65
- /**
66
- * Display proper message from Maps API error
67
- */
68
- function dealWithError({ response, json, credentials }) {
69
- switch (response.status) {
70
- case 401:
71
- throw new Error(`Unauthorized access to Maps API: invalid combination of user ('${credentials.username}') and apiKey ('${credentials.apiKey}')`);
72
- case 403:
73
- throw new Error(`Unauthorized access to dataset: the provided apiKey('${credentials.apiKey}') doesn't provide access to the requested data`);
74
- default:
75
- const e = credentials.apiVersion === API_VERSIONS.V1 ? JSON.stringify(json.errors) : json.error;
76
- throw new Error(e);
77
- }
78
- }
79
- function initURLParameters(credentials) {
80
- const encodedApiKey = encodeParameter('api_key', credentials.apiKey);
81
- const encodedClient = encodeParameter('client', 'deck-gl-carto');
82
- return [encodedApiKey, encodedClient];
83
- }
84
- /**
85
- * Build a URL with all required parameters
86
- */
87
- function buildURLMapsAPIv1({ mapConfig, credentials }) {
88
- const parameters = initURLParameters(credentials);
89
- const cfg = JSON.stringify(mapConfig);
90
- return `${mapsUrl(credentials)}?${parameters.join('&')}&${encodeParameter('config', cfg)}`;
91
- }
92
- function buildURLMapsAPIv2({ connection, type, source, credentials }) {
93
- const parameters = initURLParameters(credentials);
94
- // Query type is mapped to 'sql' at maps api v1
95
- const mapsApiType = type === MAP_TYPES.QUERY ? 'sql' : type;
96
- let url = `${mapsUrl(credentials)}/${connection}/${mapsApiType}?`;
97
- url += `${encodeParameter('source', source)}&format=tilejson&${parameters.join('&')}`;
98
- return url;
99
- }
100
- /**
101
- * Prepare a url valid for the specified user
102
- */
103
- function mapsUrl(credentials) {
104
- return credentials.mapsUrl
105
- .replace(DEFAULT_USER_COMPONENT_IN_URL, credentials.username)
106
- .replace(DEFAULT_REGION_COMPONENT_IN_URL, credentials.region);
107
- }
108
- function createMapConfig(sql) {
109
- return {
110
- version: '1.3.1',
111
- buffersize: {
112
- mvt: BUFFER_SIZE
113
- },
114
- layers: [
115
- {
116
- type: 'mapnik',
117
- options: {
118
- sql,
119
- // eslint-disable-next-line camelcase
120
- vector_extent: TILE_EXTENT
121
- }
122
- }
123
- ]
124
- };
125
- }