@deck.gl/carto 8.8.6 → 8.8.9
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/dist/dist.dev.js +36391 -28108
- package/dist/es5/api/maps-v3-client.js +31 -24
- package/dist/es5/api/maps-v3-client.js.map +1 -1
- package/dist/es5/layers/carto-layer.js +5 -0
- package/dist/es5/layers/carto-layer.js.map +1 -1
- package/dist/es5/layers/carto-tile-layer.js +1 -1
- package/dist/es5/layers/carto-tile-layer.js.map +1 -1
- package/dist/es5/layers/quadbin-utils.js +2 -0
- package/dist/es5/layers/quadbin-utils.js.map +1 -1
- package/dist/es5/layers/schema/carto-spatial-tile-loader.js +99 -0
- package/dist/es5/layers/schema/carto-spatial-tile-loader.js.map +1 -0
- package/dist/es5/layers/schema/carto-spatial-tile.js +117 -0
- package/dist/es5/layers/schema/carto-spatial-tile.js.map +1 -0
- package/dist/es5/layers/schema/carto-tile.js +5 -1
- package/dist/es5/layers/schema/carto-tile.js.map +1 -1
- package/dist/es5/layers/schema/spatialjson-utils.js +41 -0
- package/dist/es5/layers/schema/spatialjson-utils.js.map +1 -0
- package/dist/es5/layers/spatial-index-tile-layer.js +6 -0
- package/dist/es5/layers/spatial-index-tile-layer.js.map +1 -1
- package/dist/esm/api/maps-v3-client.js +19 -6
- package/dist/esm/api/maps-v3-client.js.map +1 -1
- package/dist/esm/layers/carto-layer.js +5 -0
- package/dist/esm/layers/carto-layer.js.map +1 -1
- package/dist/esm/layers/carto-tile-layer.js +1 -1
- package/dist/esm/layers/carto-tile-layer.js.map +1 -1
- package/dist/esm/layers/quadbin-utils.js +2 -5
- package/dist/esm/layers/quadbin-utils.js.map +1 -1
- package/dist/esm/layers/schema/carto-spatial-tile-loader.js +56 -0
- package/dist/esm/layers/schema/carto-spatial-tile-loader.js.map +1 -0
- package/dist/esm/layers/schema/carto-spatial-tile.js +72 -0
- package/dist/esm/layers/schema/carto-spatial-tile.js.map +1 -0
- package/dist/esm/layers/schema/carto-tile.js +2 -2
- package/dist/esm/layers/schema/carto-tile.js.map +1 -1
- package/dist/esm/layers/schema/spatialjson-utils.js +26 -0
- package/dist/esm/layers/schema/spatialjson-utils.js.map +1 -0
- package/dist/esm/layers/spatial-index-tile-layer.js +3 -0
- package/dist/esm/layers/spatial-index-tile-layer.js.map +1 -1
- package/dist.min.js +4 -4
- package/package.json +2 -2
- package/src/api/maps-v3-client.ts +33 -10
- package/src/layers/carto-layer.ts +7 -1
- package/src/layers/carto-tile-layer.ts +6 -3
- package/src/layers/quadbin-utils.ts +2 -2
- package/src/layers/schema/carto-spatial-tile-loader.ts +59 -0
- package/src/layers/schema/carto-spatial-tile.ts +83 -0
- package/src/layers/schema/carto-tile.ts +6 -6
- package/src/layers/schema/spatialjson-utils.ts +35 -0
- package/src/layers/spatial-index-tile-layer.ts +4 -0
- package/typed/api/index.js +3 -0
- package/typed/api/layer-map.js +264 -0
- package/typed/api/maps-api-common.js +50 -0
- package/typed/api/maps-client.js +125 -0
- package/typed/api/maps-v3-client.d.ts +6 -3
- package/typed/api/maps-v3-client.d.ts.map +1 -1
- package/typed/api/maps-v3-client.js +304 -0
- package/typed/api/parseMap.js +178 -0
- package/typed/basemap.js +9 -0
- package/typed/config.js +35 -0
- package/typed/index.js +10 -0
- package/typed/layers/carto-layer.d.ts +3 -0
- package/typed/layers/carto-layer.d.ts.map +1 -1
- package/typed/layers/carto-layer.js +173 -0
- package/typed/layers/carto-tile-layer.d.ts.map +1 -1
- package/typed/layers/carto-tile-layer.js +105 -0
- package/typed/layers/h3-tile-layer.js +47 -0
- package/typed/layers/h3-tileset-2d.js +85 -0
- package/typed/layers/quadbin-utils.d.ts +2 -0
- package/typed/layers/quadbin-utils.d.ts.map +1 -1
- package/typed/layers/quadkey-tile-layer.js +42 -0
- package/typed/layers/quadkey-tileset-2d.js +36 -0
- package/typed/layers/schema/carto-spatial-tile-loader.d.ts +4 -0
- package/typed/layers/schema/carto-spatial-tile-loader.d.ts.map +1 -0
- package/typed/layers/schema/carto-spatial-tile.d.ts +21 -0
- package/typed/layers/schema/carto-spatial-tile.d.ts.map +1 -0
- package/typed/layers/schema/carto-tile.d.ts +15 -3
- package/typed/layers/schema/carto-tile.d.ts.map +1 -1
- package/typed/layers/schema/carto-tile.js +161 -0
- package/typed/layers/schema/spatialjson-utils.d.ts +25 -0
- package/typed/layers/schema/spatialjson-utils.d.ts.map +1 -0
- package/typed/layers/spatial-index-tile-layer.d.ts.map +1 -1
- package/typed/layers/spatial-index-tile-layer.js +40 -0
- package/typed/style/color-bins-style.js +12 -0
- package/typed/style/color-categories-style.js +16 -0
- package/typed/style/color-continuous-style.js +12 -0
- package/typed/style/palette.js +62 -0
- package/typed/style/utils.js +15 -0
- package/typed/utils.js +4 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
class KeyValueObjectReader {
|
|
2
|
+
static read(pbf, end) {
|
|
3
|
+
return pbf.readFields(KeyValueObjectReader._readField, { key: '', value: '' }, end);
|
|
4
|
+
}
|
|
5
|
+
static _readField(tag, obj, pbf) {
|
|
6
|
+
if (tag === 1)
|
|
7
|
+
obj.key = pbf.readString();
|
|
8
|
+
else if (tag === 2)
|
|
9
|
+
obj.value = pbf.readString();
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
class PropertiesReader {
|
|
13
|
+
static read(pbf, end) {
|
|
14
|
+
return pbf.readFields(PropertiesReader._readField, { data: [] }, end);
|
|
15
|
+
}
|
|
16
|
+
static _readField(tag, obj, pbf) {
|
|
17
|
+
if (tag === 1)
|
|
18
|
+
obj.data.push(KeyValueObjectReader.read(pbf, pbf.readVarint() + pbf.pos));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
class DoublesReader {
|
|
22
|
+
static read(pbf, end) {
|
|
23
|
+
// TODO perhaps we can do better and directly map from the source
|
|
24
|
+
// ArrayBuffer using ArrayBuffer.slice()
|
|
25
|
+
const { value, size } = pbf.readFields(DoublesReader._readField, { value: [], size: 0 }, end);
|
|
26
|
+
return { value: new Float32Array(value), size };
|
|
27
|
+
}
|
|
28
|
+
static _readField(tag, obj, pbf) {
|
|
29
|
+
if (tag === 1)
|
|
30
|
+
pbf.readPackedDouble(obj.value);
|
|
31
|
+
else if (tag === 2)
|
|
32
|
+
obj.size = pbf.readVarint(true);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
class IntsReader {
|
|
36
|
+
static read(pbf, end) {
|
|
37
|
+
const { value, size } = pbf.readFields(IntsReader._readField, { value: [], size: 0 }, end);
|
|
38
|
+
return { value: new Uint32Array(value), size };
|
|
39
|
+
}
|
|
40
|
+
static _readField(tag, obj, pbf) {
|
|
41
|
+
if (tag === 1)
|
|
42
|
+
pbf.readPackedVarint(obj.value);
|
|
43
|
+
else if (tag === 2)
|
|
44
|
+
obj.size = pbf.readVarint(true);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
class NumericPropReader {
|
|
48
|
+
static read(pbf, end) {
|
|
49
|
+
return pbf.readFields(NumericPropReader._readField, { value: [] }, end);
|
|
50
|
+
}
|
|
51
|
+
static _readField(tag, obj, pbf) {
|
|
52
|
+
if (tag === 1)
|
|
53
|
+
pbf.readPackedDouble(obj.value);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
class NumericPropKeyValueReader {
|
|
57
|
+
static read(pbf, end) {
|
|
58
|
+
return pbf.readFields(NumericPropKeyValueReader._readField, { key: '', value: null }, end);
|
|
59
|
+
}
|
|
60
|
+
static _readField(tag, obj, pbf) {
|
|
61
|
+
if (tag === 1)
|
|
62
|
+
obj.key = pbf.readString();
|
|
63
|
+
else if (tag === 2)
|
|
64
|
+
obj.value = NumericPropReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
class PointsReader {
|
|
68
|
+
static read(pbf, end) {
|
|
69
|
+
return pbf.readFields(PointsReader._readField, { positions: null, globalFeatureIds: null, featureIds: null, properties: [], numericProps: {} }, end);
|
|
70
|
+
}
|
|
71
|
+
static _readField(tag, obj, pbf) {
|
|
72
|
+
if (tag === 1)
|
|
73
|
+
obj.positions = DoublesReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
74
|
+
else if (tag === 2)
|
|
75
|
+
obj.globalFeatureIds = IntsReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
76
|
+
else if (tag === 3)
|
|
77
|
+
obj.featureIds = IntsReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
78
|
+
else if (tag === 4)
|
|
79
|
+
obj.properties.push(PropertiesReader.read(pbf, pbf.readVarint() + pbf.pos));
|
|
80
|
+
else if (tag === 5) {
|
|
81
|
+
const entry = NumericPropKeyValueReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
82
|
+
obj.numericProps[entry.key] = entry.value;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Lines ========================================
|
|
87
|
+
class LinesReader {
|
|
88
|
+
static read(pbf, end) {
|
|
89
|
+
return pbf.readFields(LinesReader._readField, {
|
|
90
|
+
positions: null,
|
|
91
|
+
pathIndices: null,
|
|
92
|
+
globalFeatureIds: null,
|
|
93
|
+
featureIds: null,
|
|
94
|
+
properties: [],
|
|
95
|
+
numericProps: {}
|
|
96
|
+
}, end);
|
|
97
|
+
}
|
|
98
|
+
static _readField(tag, obj, pbf) {
|
|
99
|
+
if (tag === 1)
|
|
100
|
+
obj.positions = DoublesReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
101
|
+
else if (tag === 2)
|
|
102
|
+
obj.pathIndices = IntsReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
103
|
+
else if (tag === 3)
|
|
104
|
+
obj.globalFeatureIds = IntsReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
105
|
+
else if (tag === 4)
|
|
106
|
+
obj.featureIds = IntsReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
107
|
+
else if (tag === 5)
|
|
108
|
+
obj.properties.push(PropertiesReader.read(pbf, pbf.readVarint() + pbf.pos));
|
|
109
|
+
else if (tag === 6) {
|
|
110
|
+
const entry = NumericPropKeyValueReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
111
|
+
obj.numericProps[entry.key] = entry.value;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
class PolygonsReader {
|
|
116
|
+
static read(pbf, end) {
|
|
117
|
+
return pbf.readFields(PolygonsReader._readField, {
|
|
118
|
+
positions: null,
|
|
119
|
+
polygonIndices: null,
|
|
120
|
+
globalFeatureIds: null,
|
|
121
|
+
featureIds: null,
|
|
122
|
+
primitivePolygonIndices: null,
|
|
123
|
+
triangles: null,
|
|
124
|
+
properties: [],
|
|
125
|
+
numericProps: {}
|
|
126
|
+
}, end);
|
|
127
|
+
}
|
|
128
|
+
static _readField(tag, obj, pbf) {
|
|
129
|
+
if (tag === 1)
|
|
130
|
+
obj.positions = DoublesReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
131
|
+
else if (tag === 2)
|
|
132
|
+
obj.polygonIndices = IntsReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
133
|
+
else if (tag === 3)
|
|
134
|
+
obj.globalFeatureIds = IntsReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
135
|
+
else if (tag === 4)
|
|
136
|
+
obj.featureIds = IntsReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
137
|
+
else if (tag === 5)
|
|
138
|
+
obj.primitivePolygonIndices = IntsReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
139
|
+
else if (tag === 6)
|
|
140
|
+
obj.triangles = IntsReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
141
|
+
else if (tag === 7)
|
|
142
|
+
obj.properties.push(PropertiesReader.read(pbf, pbf.readVarint() + pbf.pos));
|
|
143
|
+
else if (tag === 8) {
|
|
144
|
+
const entry = NumericPropKeyValueReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
145
|
+
obj.numericProps[entry.key] = entry.value;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
export class TileReader {
|
|
150
|
+
static read(pbf, end) {
|
|
151
|
+
return pbf.readFields(TileReader._readField, { points: null, lines: null, polygons: null }, end);
|
|
152
|
+
}
|
|
153
|
+
static _readField(tag, obj, pbf) {
|
|
154
|
+
if (tag === 1)
|
|
155
|
+
obj.points = PointsReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
156
|
+
else if (tag === 2)
|
|
157
|
+
obj.lines = LinesReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
158
|
+
else if (tag === 3)
|
|
159
|
+
obj.polygons = PolygonsReader.read(pbf, pbf.readVarint() + pbf.pos);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare type IndexScheme = 'h3' | 'quadbin';
|
|
2
|
+
declare type TypedArray = Float32Array | Float64Array;
|
|
3
|
+
export declare type Indices = {
|
|
4
|
+
value: BigUint64Array;
|
|
5
|
+
};
|
|
6
|
+
export declare type NumericProps = Record<string, {
|
|
7
|
+
value: number[] | TypedArray;
|
|
8
|
+
}>;
|
|
9
|
+
export declare type Properties = Record<string, string | number | boolean | null>;
|
|
10
|
+
export declare type Cells = {
|
|
11
|
+
indices: Indices;
|
|
12
|
+
numericProps: NumericProps;
|
|
13
|
+
properties: Properties[];
|
|
14
|
+
};
|
|
15
|
+
export declare type SpatialBinary = {
|
|
16
|
+
scheme?: IndexScheme;
|
|
17
|
+
cells: Cells;
|
|
18
|
+
};
|
|
19
|
+
export declare type SpatialJson = {
|
|
20
|
+
id: string;
|
|
21
|
+
properties: Properties;
|
|
22
|
+
}[];
|
|
23
|
+
export declare function binaryToSpatialjson(binary: SpatialBinary): SpatialJson;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=spatialjson-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spatialjson-utils.d.ts","sourceRoot":"","sources":["../../../src/layers/schema/spatialjson-utils.ts"],"names":[],"mappings":"AAEA,oBAAY,WAAW,GAAG,IAAI,GAAG,SAAS,CAAC;AAC3C,aAAK,UAAU,GAAG,YAAY,GAAG,YAAY,CAAC;AAE9C,oBAAY,OAAO,GAAG;IAAC,KAAK,EAAE,cAAc,CAAA;CAAC,CAAC;AAC9C,oBAAY,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE;IAAC,KAAK,EAAE,MAAM,EAAE,GAAG,UAAU,CAAA;CAAC,CAAC,CAAC;AAC1E,oBAAY,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,CAAC;AAC1E,oBAAY,KAAK,GAAG;IAClB,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,YAAY,CAAC;IAC3B,UAAU,EAAE,UAAU,EAAE,CAAC;CAC1B,CAAC;AACF,oBAAY,aAAa,GAAG;IAAC,MAAM,CAAC,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,KAAK,CAAA;CAAC,CAAC;AACjE,oBAAY,WAAW,GAAG;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,UAAU,CAAC;CACxB,EAAE,CAAC;AAEJ,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,aAAa,GAAG,WAAW,CAetE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spatial-index-tile-layer.d.ts","sourceRoot":"","sources":["../../src/layers/spatial-index-tile-layer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"spatial-index-tile-layer.d.ts","sourceRoot":"","sources":["../../src/layers/spatial-index-tile-layer.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,WAAW,EAAC,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAC,SAAS,EAAE,aAAa,IAAI,YAAY,EAAC,MAAM,qBAAqB,CAAC;AAM7E,MAAM,CAAC,OAAO,OAAO,qBAAqB,CAAC,UAAU,GAAG,EAAE,CAAE,SAAQ,SAAS,CAAC,GAAG,EAAE,UAAU,CAAC;IAC5F,MAAM,CAAC,SAAS,SAA2B;IAE3C,SAAS,CAAC,oBAAoB,CAAC,IAAI,EAAE,WAAW,GAAG,IAAI;IAsBvD,sBAAsB,CAAC,IAAI,EAAE,YAAY;;;;IAOzC,yBAAyB,CAAC,IAAI,EAAE,YAAY;CAW7C"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { TileLayer } from '@deck.gl/geo-layers';
|
|
2
|
+
function isFeatureIdDefined(value) {
|
|
3
|
+
return value !== undefined && value !== null && value !== '';
|
|
4
|
+
}
|
|
5
|
+
export default class SpatialIndexTileLayer extends TileLayer {
|
|
6
|
+
_updateAutoHighlight(info) {
|
|
7
|
+
const { hoveredFeatureId } = this.state;
|
|
8
|
+
const hoveredFeature = info.object;
|
|
9
|
+
let newHoveredFeatureId;
|
|
10
|
+
if (hoveredFeature) {
|
|
11
|
+
newHoveredFeatureId = hoveredFeature.id;
|
|
12
|
+
}
|
|
13
|
+
if (hoveredFeatureId !== newHoveredFeatureId) {
|
|
14
|
+
let { highlightColor } = this.props;
|
|
15
|
+
if (typeof highlightColor === 'function') {
|
|
16
|
+
highlightColor = highlightColor(info);
|
|
17
|
+
}
|
|
18
|
+
this.setState({
|
|
19
|
+
highlightColor,
|
|
20
|
+
hoveredFeatureId: newHoveredFeatureId
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
getSubLayerPropsByTile(tile) {
|
|
25
|
+
return {
|
|
26
|
+
highlightedObjectIndex: this.getHighlightedObjectIndex(tile),
|
|
27
|
+
highlightColor: this.state.highlightColor
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
getHighlightedObjectIndex(tile) {
|
|
31
|
+
const { hoveredFeatureId } = this.state;
|
|
32
|
+
const data = tile.content;
|
|
33
|
+
const isFeatureIdPresent = isFeatureIdDefined(hoveredFeatureId);
|
|
34
|
+
if (!isFeatureIdPresent || !Array.isArray(data)) {
|
|
35
|
+
return -1;
|
|
36
|
+
}
|
|
37
|
+
return data.findIndex(feature => feature.id === hoveredFeatureId);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
SpatialIndexTileLayer.layerName = 'SpatialIndexTileLayer';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { scaleThreshold } from 'd3-scale';
|
|
2
|
+
import getPalette, { DEFAULT_PALETTE, NULL_COLOR } from './palette';
|
|
3
|
+
import { assert, getAttrValue } from './utils';
|
|
4
|
+
export default function colorBins({ attr, domain, colors = DEFAULT_PALETTE, nullColor = NULL_COLOR }) {
|
|
5
|
+
assert(Array.isArray(domain), 'Expected "domain" to be an array of numbers');
|
|
6
|
+
const palette = typeof colors === 'string' ? getPalette(colors, domain.length + 1) : colors;
|
|
7
|
+
const color = scaleThreshold().domain(domain).range(palette);
|
|
8
|
+
return d => {
|
|
9
|
+
const value = getAttrValue(attr, d);
|
|
10
|
+
return typeof value === 'number' && Number.isFinite(value) ? color(value) : nullColor;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import getPalette, { DEFAULT_PALETTE, NULL_COLOR, OTHERS_COLOR } from './palette';
|
|
2
|
+
import { assert, getAttrValue } from './utils';
|
|
3
|
+
export default function colorCategories({ attr, domain, colors = DEFAULT_PALETTE, nullColor = NULL_COLOR, othersColor = OTHERS_COLOR }) {
|
|
4
|
+
assert(Array.isArray(domain), 'Expected "domain" to be an array of numbers or strings');
|
|
5
|
+
const colorsByCategory = {};
|
|
6
|
+
const palette = typeof colors === 'string' ? getPalette(colors, domain.length) : colors;
|
|
7
|
+
for (const [i, c] of domain.entries()) {
|
|
8
|
+
colorsByCategory[c] = palette[i];
|
|
9
|
+
}
|
|
10
|
+
return d => {
|
|
11
|
+
const value = getAttrValue(attr, d);
|
|
12
|
+
return (typeof value === 'number' && Number.isFinite(value)) || typeof value === 'string'
|
|
13
|
+
? colorsByCategory[value] || othersColor
|
|
14
|
+
: nullColor;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { scaleLinear } from 'd3-scale';
|
|
2
|
+
import getPalette, { DEFAULT_PALETTE, NULL_COLOR } from './palette';
|
|
3
|
+
import { assert, getAttrValue } from './utils';
|
|
4
|
+
export default function colorContinuous({ attr, domain, colors = DEFAULT_PALETTE, nullColor = NULL_COLOR }) {
|
|
5
|
+
assert(Array.isArray(domain), 'Expected "domain" to be an array of numbers');
|
|
6
|
+
const palette = typeof colors === 'string' ? getPalette(colors, domain.length) : colors;
|
|
7
|
+
const color = scaleLinear().domain(domain).range(palette);
|
|
8
|
+
return d => {
|
|
9
|
+
const value = getAttrValue(attr, d);
|
|
10
|
+
return typeof value === 'number' && Number.isFinite(value) ? color(value) : nullColor;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import * as cartoColors from 'cartocolor';
|
|
2
|
+
import { assert } from './utils';
|
|
3
|
+
export const DEFAULT_PALETTE = 'PurpOr';
|
|
4
|
+
export const NULL_COLOR = [204, 204, 204];
|
|
5
|
+
export const OTHERS_COLOR = [119, 119, 119];
|
|
6
|
+
export default function getPalette(name, numCategories) {
|
|
7
|
+
const palette = cartoColors[name];
|
|
8
|
+
let paletteIndex = numCategories;
|
|
9
|
+
assert(palette, `Palette "${name}" not found. Expected a CARTOColors string`);
|
|
10
|
+
const palettesColorVariants = Object.keys(palette)
|
|
11
|
+
.filter(p => p !== 'tags')
|
|
12
|
+
.map(Number);
|
|
13
|
+
const longestPaletteIndex = Math.max(...palettesColorVariants);
|
|
14
|
+
const smallestPaletteIndex = Math.min(...palettesColorVariants);
|
|
15
|
+
if (!Number.isInteger(numCategories) || numCategories > longestPaletteIndex) {
|
|
16
|
+
paletteIndex = longestPaletteIndex;
|
|
17
|
+
}
|
|
18
|
+
else if (numCategories < smallestPaletteIndex) {
|
|
19
|
+
paletteIndex = smallestPaletteIndex;
|
|
20
|
+
}
|
|
21
|
+
let colors = palette[paletteIndex];
|
|
22
|
+
if (palette.tags && palette.tags.includes('qualitative')) {
|
|
23
|
+
colors = colors.slice(0, -1);
|
|
24
|
+
}
|
|
25
|
+
return colors.map(c => hexToRgb(c));
|
|
26
|
+
}
|
|
27
|
+
export function hexToRgb(hex) {
|
|
28
|
+
// Evaluate #ABC
|
|
29
|
+
let result = /^#?([a-f\d]{1})([a-f\d]{1})([a-f\d]{1})$/i.exec(hex);
|
|
30
|
+
if (result) {
|
|
31
|
+
return [
|
|
32
|
+
parseInt(result[1] + result[1], 16),
|
|
33
|
+
parseInt(result[2] + result[2], 16),
|
|
34
|
+
parseInt(result[3] + result[3], 16),
|
|
35
|
+
255
|
|
36
|
+
];
|
|
37
|
+
}
|
|
38
|
+
// Evaluate #ABCD
|
|
39
|
+
result = /^#?([a-f\d]{1})([a-f\d]{1})([a-f\d]{1})([a-f\d]{1})$/i.exec(hex);
|
|
40
|
+
if (result) {
|
|
41
|
+
return [
|
|
42
|
+
parseInt(result[1] + result[1], 16),
|
|
43
|
+
parseInt(result[2] + result[2], 16),
|
|
44
|
+
parseInt(result[3] + result[3], 16),
|
|
45
|
+
parseInt(result[4] + result[4], 16)
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
// Evaluate #ABCDEF
|
|
49
|
+
result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
50
|
+
if (result) {
|
|
51
|
+
return [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16), 255];
|
|
52
|
+
}
|
|
53
|
+
// Evaluate #ABCDEFAF
|
|
54
|
+
result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
55
|
+
assert(result, `Hexadecimal color "${hex}" was not parsed correctly`);
|
|
56
|
+
return [
|
|
57
|
+
parseInt(result[1], 16),
|
|
58
|
+
parseInt(result[2], 16),
|
|
59
|
+
parseInt(result[3], 16),
|
|
60
|
+
parseInt(result[4], 16)
|
|
61
|
+
];
|
|
62
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const ALLOWED_ATTR_TYPES = Object.freeze(['function', 'string']);
|
|
2
|
+
export function getAttrValue(attr, d) {
|
|
3
|
+
assert(typeof d === 'object', 'Expected "data" to be an object');
|
|
4
|
+
assert(ALLOWED_ATTR_TYPES.includes(typeof attr), 'Expected "attr" to be a function or string');
|
|
5
|
+
// Is function
|
|
6
|
+
if (typeof attr === 'function') {
|
|
7
|
+
return attr(d);
|
|
8
|
+
}
|
|
9
|
+
return d.properties[attr];
|
|
10
|
+
}
|
|
11
|
+
export function assert(condition, message = '') {
|
|
12
|
+
if (!condition) {
|
|
13
|
+
throw new Error(`CARTO style error: ${message}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
package/typed/utils.js
ADDED