@deck.gl/carto 8.8.9 → 8.8.11

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 (96) hide show
  1. package/dist/dist.dev.js +1747 -1555
  2. package/dist/es5/api/layer-map.js +104 -9
  3. package/dist/es5/api/layer-map.js.map +1 -1
  4. package/dist/es5/api/maps-v3-client.js +8 -8
  5. package/dist/es5/api/maps-v3-client.js.map +1 -1
  6. package/dist/es5/api/parseMap.js +38 -4
  7. package/dist/es5/api/parseMap.js.map +1 -1
  8. package/dist/es5/api/types.js +2 -0
  9. package/dist/es5/api/types.js.map +1 -0
  10. package/dist/es5/layers/carto-tile-layer.js +33 -107
  11. package/dist/es5/layers/carto-tile-layer.js.map +1 -1
  12. package/dist/es5/layers/schema/carto-spatial-tile-loader.js +6 -30
  13. package/dist/es5/layers/schema/carto-spatial-tile-loader.js.map +1 -1
  14. package/dist/es5/layers/schema/carto-tile.js +30 -6
  15. package/dist/es5/layers/schema/carto-tile.js.map +1 -1
  16. package/dist/es5/layers/schema/carto-vector-tile-loader.js +81 -0
  17. package/dist/es5/layers/schema/carto-vector-tile-loader.js.map +1 -0
  18. package/dist/es5/layers/schema/tile-loader-utils.js +34 -0
  19. package/dist/es5/layers/schema/tile-loader-utils.js.map +1 -0
  20. package/dist/es5/layers/spatial-index-tile-layer.js +40 -0
  21. package/dist/es5/layers/spatial-index-tile-layer.js.map +1 -1
  22. package/dist/esm/api/layer-map.js +85 -9
  23. package/dist/esm/api/layer-map.js.map +1 -1
  24. package/dist/esm/api/maps-v3-client.js +3 -2
  25. package/dist/esm/api/maps-v3-client.js.map +1 -1
  26. package/dist/esm/api/parseMap.js +41 -5
  27. package/dist/esm/api/parseMap.js.map +1 -1
  28. package/dist/esm/api/types.js +2 -0
  29. package/dist/esm/api/types.js.map +1 -0
  30. package/dist/esm/layers/carto-tile-layer.js +31 -82
  31. package/dist/esm/layers/carto-tile-layer.js.map +1 -1
  32. package/dist/esm/layers/schema/carto-spatial-tile-loader.js +4 -27
  33. package/dist/esm/layers/schema/carto-spatial-tile-loader.js.map +1 -1
  34. package/dist/esm/layers/schema/carto-tile.js +22 -6
  35. package/dist/esm/layers/schema/carto-tile.js.map +1 -1
  36. package/dist/esm/layers/schema/carto-vector-tile-loader.js +40 -0
  37. package/dist/esm/layers/schema/carto-vector-tile-loader.js.map +1 -0
  38. package/dist/esm/layers/schema/tile-loader-utils.js +23 -0
  39. package/dist/esm/layers/schema/tile-loader-utils.js.map +1 -0
  40. package/dist/esm/layers/spatial-index-tile-layer.js +40 -1
  41. package/dist/esm/layers/spatial-index-tile-layer.js.map +1 -1
  42. package/dist.min.js +138 -138
  43. package/package.json +2 -2
  44. package/src/api/layer-map.ts +103 -17
  45. package/src/api/maps-v3-client.ts +3 -3
  46. package/src/api/parseMap.ts +80 -15
  47. package/src/api/types.ts +108 -0
  48. package/src/layers/carto-tile-layer.ts +32 -82
  49. package/src/layers/schema/carto-spatial-tile-loader.ts +4 -27
  50. package/src/layers/schema/carto-tile.ts +31 -6
  51. package/src/layers/schema/carto-vector-tile-loader.ts +39 -0
  52. package/src/layers/schema/tile-loader-utils.ts +22 -0
  53. package/src/layers/spatial-index-tile-layer.ts +37 -1
  54. package/typed/api/layer-map.d.ts +10 -13
  55. package/typed/api/layer-map.d.ts.map +1 -1
  56. package/typed/api/parseMap.d.ts.map +1 -1
  57. package/typed/api/types.d.ts +87 -0
  58. package/typed/api/types.d.ts.map +1 -0
  59. package/typed/layers/carto-tile-layer.d.ts +2 -0
  60. package/typed/layers/carto-tile-layer.d.ts.map +1 -1
  61. package/typed/layers/schema/carto-spatial-tile-loader.d.ts.map +1 -1
  62. package/typed/layers/schema/carto-tile.d.ts +4 -0
  63. package/typed/layers/schema/carto-tile.d.ts.map +1 -1
  64. package/typed/layers/schema/carto-vector-tile-loader.d.ts +4 -0
  65. package/typed/layers/schema/carto-vector-tile-loader.d.ts.map +1 -0
  66. package/typed/layers/schema/tile-loader-utils.d.ts +5 -0
  67. package/typed/layers/schema/tile-loader-utils.d.ts.map +1 -0
  68. package/typed/layers/spatial-index-tile-layer.d.ts +2 -1
  69. package/typed/layers/spatial-index-tile-layer.d.ts.map +1 -1
  70. package/typed/api/index.js +0 -3
  71. package/typed/api/layer-map.js +0 -264
  72. package/typed/api/maps-api-common.js +0 -50
  73. package/typed/api/maps-client.js +0 -125
  74. package/typed/api/maps-v3-client.js +0 -304
  75. package/typed/api/parseMap.js +0 -178
  76. package/typed/basemap.js +0 -9
  77. package/typed/config.js +0 -35
  78. package/typed/index.js +0 -10
  79. package/typed/layers/carto-layer.js +0 -173
  80. package/typed/layers/carto-tile-layer.js +0 -105
  81. package/typed/layers/h3-tile-layer.js +0 -47
  82. package/typed/layers/h3-tileset-2d.js +0 -85
  83. package/typed/layers/quadkey-tile-layer.d.ts +0 -21
  84. package/typed/layers/quadkey-tile-layer.d.ts.map +0 -1
  85. package/typed/layers/quadkey-tile-layer.js +0 -42
  86. package/typed/layers/quadkey-tileset-2d.d.ts +0 -15
  87. package/typed/layers/quadkey-tileset-2d.d.ts.map +0 -1
  88. package/typed/layers/quadkey-tileset-2d.js +0 -36
  89. package/typed/layers/schema/carto-tile.js +0 -161
  90. package/typed/layers/spatial-index-tile-layer.js +0 -40
  91. package/typed/style/color-bins-style.js +0 -12
  92. package/typed/style/color-categories-style.js +0 -16
  93. package/typed/style/color-continuous-style.js +0 -12
  94. package/typed/style/palette.js +0 -62
  95. package/typed/style/utils.js +0 -15
  96. package/typed/utils.js +0 -4
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@deck.gl/carto",
3
3
  "description": "CARTO official integration with Deck.gl. Build geospatial applications using CARTO and Deck.gl.",
4
4
  "license": "MIT",
5
- "version": "8.8.9",
5
+ "version": "8.8.11",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
@@ -61,5 +61,5 @@
61
61
  "@deck.gl/layers": "^8.0.0",
62
62
  "@loaders.gl/core": "^3.0.0"
63
63
  },
64
- "gitHead": "1cceaed3dad056c3fb51864e7f56585271ed1e5a"
64
+ "gitHead": "f70e8c354aee34949134f9e802f59cf60db4de19"
65
65
  }
@@ -13,7 +13,7 @@ import {
13
13
  import {format as d3Format} from 'd3-format';
14
14
  import moment from 'moment-timezone';
15
15
 
16
- import {Layer, _ConstructorOf as ConstructorOf} from '@deck.gl/core';
16
+ import {Accessor, Layer, _ConstructorOf as ConstructorOf} from '@deck.gl/core';
17
17
  import {CPUGridLayer, HeatmapLayer, HexagonLayer} from '@deck.gl/aggregation-layers';
18
18
  import {GeoJsonLayer} from '@deck.gl/layers';
19
19
  import {H3HexagonLayer, MVTLayer} from '@deck.gl/geo-layers';
@@ -23,6 +23,15 @@ import H3TileLayer from '../layers/h3-tile-layer';
23
23
  import QuadbinTileLayer from '../layers/quadbin-tile-layer';
24
24
  import {TILE_FORMATS} from './maps-api-common';
25
25
  import {assert} from '../utils';
26
+ import {
27
+ CustomMarkersRange,
28
+ MapDataset,
29
+ MapTextSubLayerConfig,
30
+ TextLabel,
31
+ VisConfig,
32
+ VisualChannelField,
33
+ VisualChannels
34
+ } from './types';
26
35
 
27
36
  const SCALE_FUNCS = {
28
37
  linear: scaleLinear,
@@ -36,6 +45,10 @@ const SCALE_FUNCS = {
36
45
  };
37
46
  export type SCALE_TYPE = keyof typeof SCALE_FUNCS;
38
47
 
48
+ function identity<T>(v: T): T {
49
+ return v;
50
+ }
51
+
39
52
  const UNKNOWN_COLOR = '#868d91';
40
53
 
41
54
  export const AGGREGATION = {
@@ -88,6 +101,13 @@ const sharedPropMap = {
88
101
  }
89
102
  };
90
103
 
104
+ const customMarkersPropsMap = {
105
+ color: 'getIconColor',
106
+ visConfig: {
107
+ radius: 'getIconSize'
108
+ }
109
+ };
110
+
91
111
  const aggregationVisConfig = {
92
112
  colorAggregation: x => ({colorAggregation: AGGREGATION[x] || AGGREGATION.sum}),
93
113
  colorRange: x => ({colorRange: x.colors.map(hexToRGBA)}),
@@ -110,11 +130,16 @@ function mergePropMaps(a: Record<string, any> = {}, b: Record<string, any> = {})
110
130
 
111
131
  export function getLayer(
112
132
  type: string,
113
- config,
114
- dataset
133
+ config: MapTextSubLayerConfig,
134
+ dataset: MapDataset
115
135
  ): {Layer: ConstructorOf<Layer>; propMap: any; defaultProps: any} {
136
+ let basePropMap: any = sharedPropMap;
137
+
138
+ if (config.visConfig?.customMarkers && !config.textLabel) {
139
+ basePropMap = mergePropMaps(sharedPropMap, customMarkersPropsMap);
140
+ }
116
141
  if (type === 'mvt' || type === 'tileset' || type === 'h3' || type === 'quadbin') {
117
- return getTileLayer(dataset);
142
+ return getTileLayer(dataset, basePropMap);
118
143
  }
119
144
 
120
145
  const geoColumn = dataset?.geoColumn;
@@ -160,7 +185,7 @@ export function getLayer(
160
185
  assert(layer, `Unsupported layer type: ${type}`);
161
186
  return {
162
187
  ...layer,
163
- propMap: mergePropMaps(sharedPropMap, layer.propMap),
188
+ propMap: mergePropMaps(basePropMap, layer.propMap),
164
189
  defaultProps: {...defaultProps, ...layer.defaultProps}
165
190
  };
166
191
  }
@@ -183,7 +208,7 @@ export function layerFromTileDataset(
183
208
  return CartoTileLayer;
184
209
  }
185
210
 
186
- function getTileLayer(dataset) {
211
+ function getTileLayer(dataset: MapDataset, basePropMap) {
187
212
  const {
188
213
  aggregationExp,
189
214
  aggregationResLevel,
@@ -197,7 +222,7 @@ function getTileLayer(dataset) {
197
222
 
198
223
  return {
199
224
  Layer: layerFromTileDataset(formatTiles, scheme),
200
- propMap: sharedPropMap,
225
+ propMap: basePropMap,
201
226
  defaultProps: {
202
227
  ...defaultProps,
203
228
  ...(aggregationExp && {aggregationExp}),
@@ -225,7 +250,7 @@ function domainFromAttribute(attribute, scaleType: SCALE_TYPE, scaleLength: numb
225
250
  }
226
251
 
227
252
  function domainFromValues(values, scaleType: SCALE_TYPE) {
228
- if (scaleType === 'ordinal') {
253
+ if (scaleType === 'ordinal' || scaleType === 'point') {
229
254
  return groupSort(
230
255
  values,
231
256
  g => -g.length,
@@ -268,7 +293,7 @@ function normalizeAccessor(accessor, data) {
268
293
  return accessor;
269
294
  }
270
295
 
271
- export function opacityToAlpha(opacity) {
296
+ export function opacityToAlpha(opacity?: number) {
272
297
  return opacity !== undefined ? Math.round(255 * Math.pow(opacity, 1 / 2.2)) : 255;
273
298
  }
274
299
 
@@ -339,16 +364,71 @@ export function getColorAccessor(
339
364
  return normalizeAccessor(accessor, data);
340
365
  }
341
366
 
367
+ const FALLBACK_ICON =
368
+ 'data:image/svg+xml;charset=utf-8;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMTAwIDEwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4NCiAgPGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iNTAiLz4NCjwvc3ZnPg==';
369
+
370
+ export function getIconUrlAccessor(
371
+ field: VisualChannelField | null | undefined,
372
+ fallbackUrl: string | null | undefined,
373
+ range: CustomMarkersRange | null | undefined,
374
+ maxIconSize: number,
375
+ data: any
376
+ ) {
377
+ const urlToUnpackedIcon = (url: string) => ({
378
+ id: `${url}@@${maxIconSize}`,
379
+ url,
380
+ width: maxIconSize,
381
+ height: maxIconSize,
382
+ mask: true
383
+ });
384
+ let unknownValue = fallbackUrl || FALLBACK_ICON;
385
+
386
+ if (range?.othersMarker) {
387
+ unknownValue = range.othersMarker;
388
+ }
389
+
390
+ const unknownIcon = urlToUnpackedIcon(unknownValue);
391
+ if (!range || !field) {
392
+ return () => unknownIcon;
393
+ }
394
+
395
+ const mapping: Record<string, any> = {};
396
+ for (const {value, markerUrl} of range.markerMap) {
397
+ if (markerUrl) {
398
+ mapping[value] = urlToUnpackedIcon(markerUrl);
399
+ }
400
+ }
401
+
402
+ const accessor = properties => {
403
+ const propertyValue = properties[field.name];
404
+ return mapping[propertyValue] || unknownIcon;
405
+ };
406
+ return normalizeAccessor(accessor, data);
407
+ }
408
+
409
+ export function getMaxMarkerSize(visConfig: VisConfig, visualChannels: VisualChannels): number {
410
+ const {radiusRange, radius} = visConfig;
411
+ const {radiusField, sizeField} = visualChannels;
412
+ const field = radiusField || sizeField;
413
+ return Math.ceil(radiusRange && field ? radiusRange[1] : radius);
414
+ }
415
+
416
+ export function negateAccessor<T>(accessor: Accessor<T, number>): Accessor<T, number> {
417
+ return typeof accessor === 'function' ? (d, i) => -accessor(d, i) : -accessor;
418
+ }
419
+
342
420
  export function getSizeAccessor(
343
421
  {name},
344
- scaleType: SCALE_TYPE,
422
+ scaleType: SCALE_TYPE | undefined,
345
423
  aggregation,
346
- range: Iterable<Range>,
424
+ range: Iterable<Range> | undefined,
347
425
  data: any
348
426
  ) {
349
- const scale = SCALE_FUNCS[scaleType as any]();
350
- scale.domain(calculateDomain(data, name, scaleType));
351
- scale.range(range);
427
+ const scale = scaleType ? SCALE_FUNCS[scaleType as any]() : identity;
428
+ if (scaleType) {
429
+ scale.domain(calculateDomain(data, name, scaleType));
430
+ scale.range(range);
431
+ }
352
432
 
353
433
  let accessorKeys = getAccessorKeys(name, aggregation);
354
434
  const accessor = properties => {
@@ -369,7 +449,7 @@ const FORMATS: Record<string, (value: any) => string> = {
369
449
  default: String
370
450
  };
371
451
 
372
- export function getTextAccessor({name, type}, data) {
452
+ export function getTextAccessor({name, type}: VisualChannelField, data) {
373
453
  const format = FORMATS[type] || FORMATS.default;
374
454
  const accessor = properties => {
375
455
  return format(properties[name]);
@@ -377,13 +457,19 @@ export function getTextAccessor({name, type}, data) {
377
457
  return normalizeAccessor(accessor, data);
378
458
  }
379
459
 
380
- export function getTextPixelOffsetAccessor({alignment, anchor, size}, radius) {
460
+ export function getTextPixelOffsetAccessor(
461
+ {alignment, anchor, size}: TextLabel,
462
+ radius
463
+ ): Accessor<unknown, [number, number]> {
381
464
  const padding = 20;
382
465
  const signX = anchor === 'middle' ? 0 : anchor === 'start' ? 1 : -1;
383
466
  const signY = alignment === 'center' ? 0 : alignment === 'bottom' ? 1 : -1;
384
467
  const sizeOffset = alignment === 'center' ? 0 : size;
385
468
 
386
- const calculateOffset = r => [signX * (r + padding), signY * (r + padding + sizeOffset)];
469
+ const calculateOffset = (r: number): [number, number] => [
470
+ signX * (r + padding),
471
+ signY * (r + padding + sizeOffset)
472
+ ];
387
473
 
388
474
  return typeof radius === 'function'
389
475
  ? d => {
@@ -405,8 +405,7 @@ async function _fetchMapDataset(
405
405
  accessToken: string,
406
406
  credentials: CloudNativeCredentials,
407
407
  clientId?: string,
408
- headers?: Headers,
409
- queryParameters?: QueryParameters
408
+ headers?: Headers
410
409
  ) {
411
410
  const {
412
411
  aggregationExp,
@@ -416,7 +415,8 @@ async function _fetchMapDataset(
416
415
  format,
417
416
  geoColumn,
418
417
  source,
419
- type
418
+ type,
419
+ queryParameters
420
420
  } = dataset;
421
421
  // First fetch metadata
422
422
  const {url, mapFormat} = await _fetchDataUrl({
@@ -8,10 +8,14 @@ import {
8
8
  getTextAccessor,
9
9
  getTextPixelOffsetAccessor,
10
10
  OPACITY_MAP,
11
- opacityToAlpha
11
+ opacityToAlpha,
12
+ getIconUrlAccessor,
13
+ negateAccessor,
14
+ getMaxMarkerSize
12
15
  } from './layer-map';
13
16
  import {_flatten as flatten, log} from '@deck.gl/core';
14
17
  import {assert} from '../utils';
18
+ import {MapDataset, MapTextSubLayerConfig, VisualChannels} from './types';
15
19
 
16
20
  export function parseMap(json) {
17
21
  const {keplerMapConfig, datasets} = json;
@@ -30,7 +34,7 @@ export function parseMap(json) {
30
34
  layers: extractTextLayers(layers.reverse()).map(({id, type, config, visualChannels}) => {
31
35
  try {
32
36
  const {dataId} = config;
33
- const dataset = datasets.find(d => d.id === dataId);
37
+ const dataset: MapDataset | null = datasets.find(d => d.id === dataId);
34
38
  assert(dataset, `No dataset matching dataId: ${dataId}`);
35
39
  const {data} = dataset;
36
40
  assert(data, `No data loaded for dataId: ${dataId}`);
@@ -128,7 +132,7 @@ function mapProps(source, target, mapping) {
128
132
  }
129
133
  }
130
134
 
131
- function createStyleProps(config, mapping) {
135
+ function createStyleProps(config: MapTextSubLayerConfig, mapping) {
132
136
  const result: Record<string, any> = {};
133
137
  mapProps(config, result, mapping);
134
138
 
@@ -153,9 +157,22 @@ function createStyleProps(config, mapping) {
153
157
  }
154
158
 
155
159
  /* eslint-disable complexity, max-statements */
156
- function createChannelProps(visualChannels, type, config, data) {
157
- const {colorField, colorScale, sizeField, sizeScale, strokeColorField, strokeColorScale} =
158
- visualChannels;
160
+ function createChannelProps(
161
+ visualChannels: VisualChannels,
162
+ type: string,
163
+ config: MapTextSubLayerConfig,
164
+ data
165
+ ) {
166
+ const {
167
+ colorField,
168
+ colorScale,
169
+ radiusField,
170
+ radiusScale,
171
+ sizeField,
172
+ sizeScale,
173
+ strokeColorField,
174
+ strokeColorScale
175
+ } = visualChannels;
159
176
  let {heightField, heightScale} = visualChannels;
160
177
  if (type === 'hexagonId') {
161
178
  heightField = sizeField;
@@ -179,6 +196,7 @@ function createChannelProps(visualChannels, type, config, data) {
179
196
  const {colorAggregation: aggregation, colorRange: range} = visConfig;
180
197
  result.getFillColor = getColorAccessor(
181
198
  colorField,
199
+ // @ts-ignore
182
200
  colorScale,
183
201
  {aggregation, range},
184
202
  visConfig.opacity,
@@ -186,6 +204,18 @@ function createChannelProps(visualChannels, type, config, data) {
186
204
  );
187
205
  }
188
206
 
207
+ if (radiusField || sizeField) {
208
+ result.getPointRadius = getSizeAccessor(
209
+ // @ts-ignore
210
+ radiusField || sizeField,
211
+ // @ts-ignore
212
+ radiusScale || sizeScale,
213
+ visConfig.sizeAggregation,
214
+ visConfig.radiusRange || visConfig.sizeRange,
215
+ data
216
+ );
217
+ }
218
+
189
219
  if (strokeColorField) {
190
220
  const fallbackOpacity = type === 'point' ? visConfig.opacity : 1;
191
221
  const opacity =
@@ -193,35 +223,70 @@ function createChannelProps(visualChannels, type, config, data) {
193
223
  const {strokeColorAggregation: aggregation, strokeColorRange: range} = visConfig;
194
224
  result.getLineColor = getColorAccessor(
195
225
  strokeColorField,
226
+ // @ts-ignore
196
227
  strokeColorScale,
228
+ // @ts-ignore
197
229
  {aggregation, range},
198
230
  opacity,
199
231
  data
200
232
  );
201
233
  }
234
+
202
235
  if (heightField) {
203
236
  result.getElevation = getSizeAccessor(
204
237
  heightField,
238
+ // @ts-ignore
205
239
  heightScale,
206
240
  visConfig.heightAggregation,
207
241
  visConfig.heightRange || visConfig.sizeRange,
208
242
  data
209
243
  );
210
244
  }
211
- if (sizeField) {
212
- result.getPointRadius = getSizeAccessor(
213
- sizeField,
214
- sizeScale,
215
- visConfig.sizeAggregation,
216
- visConfig.radiusRange || visConfig.sizeRange,
217
- data
218
- );
219
- }
245
+
220
246
  if (textLabelField) {
221
247
  result.getText = getTextAccessor(textLabelField, data);
222
248
  result.pointType = 'text';
223
249
  const radius = result.getPointRadius || visConfig.radius;
224
250
  result.getTextPixelOffset = getTextPixelOffsetAccessor(textLabel, radius);
251
+ } else if (visConfig.customMarkers) {
252
+ const maxIconSize = getMaxMarkerSize(visConfig, visualChannels);
253
+ const {getPointRadius, getFillColor} = result;
254
+ result.pointType = 'icon';
255
+ result.getIcon = getIconUrlAccessor(
256
+ visualChannels.customMarkersField,
257
+ visConfig.customMarkersUrl,
258
+ visConfig.customMarkersRange,
259
+ maxIconSize,
260
+ data
261
+ );
262
+ result._subLayerProps = {
263
+ 'points-icon': {
264
+ loadOptions: {
265
+ image: {
266
+ type: 'imagebitmap'
267
+ },
268
+ imagebitmap: {
269
+ resizeWidth: maxIconSize,
270
+ resizeHeight: maxIconSize,
271
+ resizeQuality: 'high'
272
+ }
273
+ }
274
+ }
275
+ };
276
+
277
+ if (getFillColor) {
278
+ result.getIconColor = getFillColor;
279
+ }
280
+
281
+ if (getPointRadius) {
282
+ result.getIconSize = getPointRadius;
283
+ }
284
+
285
+ if (visualChannels.rotationField) {
286
+ result.getIconAngle = negateAccessor(
287
+ getSizeAccessor(visualChannels.rotationField, undefined, null, undefined, data)
288
+ );
289
+ }
225
290
  }
226
291
 
227
292
  return result;
@@ -0,0 +1,108 @@
1
+ import {SCALE_TYPE} from './layer-map';
2
+
3
+ export type VisualChannelField = {
4
+ name: string;
5
+ type: string;
6
+ };
7
+
8
+ export type VisualChannels = {
9
+ colorField?: VisualChannelField;
10
+ colorScale?: SCALE_TYPE;
11
+
12
+ customMarkersField?: VisualChannelField;
13
+ customMarkersScale?: SCALE_TYPE;
14
+
15
+ radiusField?: VisualChannelField;
16
+ radiusScale?: SCALE_TYPE;
17
+
18
+ rotationScale?: SCALE_TYPE;
19
+ rotationField?: VisualChannelField;
20
+
21
+ sizeField?: VisualChannelField;
22
+ sizeScale?: SCALE_TYPE;
23
+
24
+ strokeColorField?: VisualChannelField;
25
+ strokeColorScale?: SCALE_TYPE;
26
+
27
+ heightField?: VisualChannelField;
28
+ heightScale?: SCALE_TYPE;
29
+ };
30
+
31
+ export type ColorRange = {
32
+ category: string;
33
+ colors: string[];
34
+ colorMap: string[][] | undefined;
35
+ name: string;
36
+ type: string;
37
+ };
38
+
39
+ export type CustomMarkersRange = {
40
+ markerMap: {
41
+ value: string;
42
+ markerUrl?: string;
43
+ }[];
44
+ othersMarker?: string;
45
+ };
46
+
47
+ export type VisConfig = {
48
+ filled?: boolean;
49
+ opacity?: number;
50
+ enable3d?: boolean;
51
+
52
+ colorAggregation?: any;
53
+ colorRange: ColorRange;
54
+
55
+ customMarkers?: boolean;
56
+ customMarkersRange?: CustomMarkersRange | null;
57
+ customMarkersUrl?: string | null;
58
+
59
+ radius: number;
60
+ radiusRange?: number[];
61
+
62
+ sizeAggregation?: any;
63
+ sizeRange?: any;
64
+
65
+ strokeColorAggregation?: any;
66
+ strokeOpacity?: number;
67
+ strokeColorRange?: ColorRange;
68
+
69
+ heightRange?: any;
70
+ heightAggregation?: any;
71
+ };
72
+
73
+ export type TextLabel = {
74
+ field: VisualChannelField | null | undefined;
75
+ alignment?: 'center' | 'bottom' | 'top';
76
+ anchor?: 'middle' | 'start' | 'end';
77
+ size: number;
78
+ color?: number[];
79
+ offset?: [number, number];
80
+ };
81
+
82
+ export type MapLayerConfig = {
83
+ columns?: Record<string, any>;
84
+ color?: number[];
85
+ label?: string;
86
+ dataId: string;
87
+ textLabel: TextLabel[];
88
+ visConfig: VisConfig;
89
+ };
90
+
91
+ export type MapTextSubLayerConfig = Omit<MapLayerConfig, 'textLabel'> & {
92
+ textLabel?: TextLabel;
93
+ };
94
+
95
+ export type MapConfigLayer = {
96
+ type: string;
97
+ id: string;
98
+ config: MapLayerConfig;
99
+ visualChannels: VisualChannels;
100
+ };
101
+
102
+ export type MapDataset = {
103
+ id: string;
104
+ data: any;
105
+ aggregationExp: string | null;
106
+ aggregationResLevel: number | null;
107
+ geoColumn: string;
108
+ };
@@ -1,6 +1,8 @@
1
- /* global TextDecoder */
2
- import Protobuf from 'pbf';
3
- import {log, DefaultProps} from '@deck.gl/core';
1
+ import {registerLoaders} from '@loaders.gl/core';
2
+ import CartoVectorTileLoader from './schema/carto-vector-tile-loader';
3
+ registerLoaders([CartoVectorTileLoader]);
4
+
5
+ import {DefaultProps} from '@deck.gl/core';
4
6
  import {ClipExtension} from '@deck.gl/extensions';
5
7
  import {
6
8
  MVTLayer,
@@ -11,81 +13,16 @@ import {
11
13
  _TileLoadProps as TileLoadProps
12
14
  } from '@deck.gl/geo-layers';
13
15
  import {GeoJsonLayer} from '@deck.gl/layers';
14
- import {geojsonToBinary} from '@loaders.gl/gis';
15
- import {KeyValueProperties, Tile, TileReader} from './schema/carto-tile';
16
- import {TileFormat, TILE_FORMATS} from '../api/maps-api-common';
17
- import {LoaderOptions, LoaderWithParser} from '@loaders.gl/loader-utils';
16
+ import {binaryToGeojson, geojsonToBinary} from '@loaders.gl/gis';
18
17
  import type {BinaryFeatures} from '@loaders.gl/schema';
18
+ import {TileFormat, TILE_FORMATS} from '../api/maps-api-common';
19
19
  import type {Feature} from 'geojson';
20
20
 
21
- function parseJSON(arrayBuffer: ArrayBuffer): any {
22
- return JSON.parse(new TextDecoder().decode(arrayBuffer));
23
- }
24
-
25
- function parsePbf(buffer: ArrayBuffer): Tile {
26
- const pbf = new Protobuf(buffer);
27
- const tile = TileReader.read(pbf);
28
- return tile;
29
- }
30
-
31
- function unpackProperties(properties: KeyValueProperties[]) {
32
- if (!properties || !properties.length) {
33
- return [];
34
- }
35
- return properties.map(item => {
36
- const currentRecord: Record<string, unknown> = {};
37
- item.data.forEach(({key, value}) => {
38
- currentRecord[key] = value;
39
- });
40
- return currentRecord;
41
- });
42
- }
43
-
44
- function parseCartoTile(arrayBuffer: ArrayBuffer, options?: LoaderOptions): BinaryFeatures | null {
45
- if (!arrayBuffer) return null;
46
- const formatTiles = options && options.cartoTile && options.cartoTile.formatTiles;
47
- if (formatTiles === TILE_FORMATS.GEOJSON) return geojsonToBinary(parseJSON(arrayBuffer).features);
48
-
49
- const tile = parsePbf(arrayBuffer);
50
-
51
- const {points, lines, polygons} = tile;
52
- const data = {
53
- points: {...points, properties: unpackProperties(points.properties)},
54
- lines: {...lines, properties: unpackProperties(lines.properties)},
55
- polygons: {...polygons, properties: unpackProperties(polygons.properties)}
56
- };
57
-
58
- // Note: there is slight, difference in `numericProps` type, however geojson/mvtlayer can cope with this
59
- return data as unknown as BinaryFeatures;
60
- }
61
-
62
21
  const defaultTileFormat = TILE_FORMATS.BINARY;
63
22
 
64
- const CartoTileLoader: LoaderWithParser = {
65
- name: 'CARTO Tile',
66
- version: '1',
67
- id: 'cartoTile',
68
- module: 'carto',
69
- extensions: ['pbf'],
70
- mimeTypes: [
71
- 'application/vnd.carto-vector-tile',
72
- 'application/x-protobuf' // Back-compatibility
73
- ],
74
- category: 'geometry',
75
- worker: false,
76
- parse: async (arrayBuffer, options) => parseCartoTile(arrayBuffer, options),
77
- parseSync: parseCartoTile,
78
- options: {
79
- cartoTile: {
80
- formatTiles: defaultTileFormat
81
- }
82
- }
83
- };
84
-
85
23
  const defaultProps: DefaultProps<CartoTileLayerProps> = {
86
24
  ...MVTLayer.defaultProps,
87
- formatTiles: defaultTileFormat,
88
- loaders: [CartoTileLoader]
25
+ formatTiles: defaultTileFormat
89
26
  };
90
27
 
91
28
  /** All properties supported by CartoTileLayer. */
@@ -110,6 +47,12 @@ export default class CartoTileLayer<
110
47
  static layerName = 'CartoTileLayer';
111
48
  static defaultProps = defaultProps;
112
49
 
50
+ initializeState(): void {
51
+ super.initializeState();
52
+ const binary = this.props.formatTiles === TILE_FORMATS.BINARY;
53
+ this.setState({binary});
54
+ }
55
+
113
56
  getTileData(tile: TileLoadProps) {
114
57
  const url = _getURLFromTemplate(this.state.data, tile);
115
58
  if (!url) {
@@ -120,17 +63,13 @@ export default class CartoTileLayer<
120
63
  const {fetch, formatTiles} = this.props;
121
64
  const {signal} = tile;
122
65
 
123
- loadOptions = {
124
- ...loadOptions,
125
- mimeType: 'application/x-protobuf'
126
- };
127
-
128
- if (formatTiles) {
129
- log.assert(
130
- Object.values(TILE_FORMATS).includes(formatTiles),
131
- `Invalid value for formatTiles: ${formatTiles}. Use value from TILE_FORMATS`
132
- );
133
- loadOptions.cartoTile = {formatTiles};
66
+ // The backend doesn't yet support our custom mime-type, so force it here
67
+ // TODO remove once backend sends the correct mime-type
68
+ if (formatTiles === TILE_FORMATS.BINARY) {
69
+ loadOptions = {
70
+ ...loadOptions,
71
+ mimeType: 'application/vnd.carto-vector-tile'
72
+ };
134
73
  }
135
74
 
136
75
  return fetch(url, {propName: 'data', layer: this, loadOptions, signal});
@@ -161,4 +100,15 @@ export default class CartoTileLayer<
161
100
  const subLayer = new GeoJsonLayer(subLayerProps);
162
101
  return subLayer;
163
102
  }
103
+
104
+ getPickingInfo(params) {
105
+ const info = super.getPickingInfo(params);
106
+
107
+ if (this.state.binary && info.index !== -1) {
108
+ const {data} = params.sourceLayer!.props;
109
+ info.object = binaryToGeojson(data as BinaryFeatures, {globalFeatureId: info.index}) as DataT;
110
+ }
111
+
112
+ return info;
113
+ }
164
114
  }