-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
geojson-layer.js
344 lines (303 loc) · 10.8 KB
/
geojson-layer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import uniq from 'lodash.uniq';
import {DATA_TYPES} from 'type-analyzer';
import Layer, {colorMaker} from '../base-layer';
import {GeoJsonLayer as DeckGLGeoJsonLayer} from '@deck.gl/layers';
import {getGeojsonDataMaps, getGeojsonBounds, getGeojsonFeatureTypes} from './geojson-utils';
import GeojsonLayerIcon from './geojson-layer-icon';
import {GEOJSON_FIELDS, HIGHLIGH_COLOR_3D, CHANNEL_SCALES} from 'constants/default-settings';
import {LAYER_VIS_CONFIGS} from 'layers/layer-factory';
const SUPPORTED_ANALYZER_TYPES = {
[DATA_TYPES.GEOMETRY]: true,
[DATA_TYPES.GEOMETRY_FROM_STRING]: true,
[DATA_TYPES.PAIR_GEOMETRY_FROM_STRING]: true
};
export const geojsonVisConfigs = {
opacity: 'opacity',
strokeOpacity: {
...LAYER_VIS_CONFIGS.opacity,
property: 'strokeOpacity'
},
thickness: {
...LAYER_VIS_CONFIGS.thickness,
defaultValue: 0.5
},
strokeColor: 'strokeColor',
colorRange: 'colorRange',
strokeColorRange: 'strokeColorRange',
radius: 'radius',
sizeRange: 'strokeWidthRange',
radiusRange: 'radiusRange',
heightRange: 'elevationRange',
elevationScale: 'elevationScale',
stroked: 'stroked',
filled: 'filled',
enable3d: 'enable3d',
wireframe: 'wireframe'
};
export const geoJsonRequiredColumns = ['geojson'];
export const featureAccessor = ({geojson}) => d => d[geojson.fieldIdx];
// access feature properties from geojson sub layer
export const defaultElevation = 500;
export const defaultLineWidth = 1;
export const defaultRadius = 1;
export default class GeoJsonLayer extends Layer {
constructor(props) {
super(props);
this.dataToFeature = [];
this.registerVisConfig(geojsonVisConfigs);
this.getPositionAccessor = () => featureAccessor(this.config.columns);
}
get type() {
return 'geojson';
}
get name() {
return 'Polygon';
}
get layerIcon() {
return GeojsonLayerIcon;
}
get requiredLayerColumns() {
return geoJsonRequiredColumns;
}
get visualChannels() {
const visualChannels = super.visualChannels;
return {
color: {
...visualChannels.color,
accessor: 'getFillColor',
condition: config => config.visConfig.filled,
nullValue: visualChannels.color.nullValue,
getAttributeValue: config => d => d.properties.fillColor || config.color,
// used this to get updateTriggers
defaultValue: config => config.color
},
strokeColor: {
property: 'strokeColor',
field: 'strokeColorField',
scale: 'strokeColorScale',
domain: 'strokeColorDomain',
range: 'strokeColorRange',
key: 'strokeColor',
channelScaleType: CHANNEL_SCALES.color,
accessor: 'getLineColor',
condition: config => config.visConfig.stroked,
nullValue: visualChannels.color.nullValue,
getAttributeValue: config => d =>
d.properties.lineColor || config.visConfig.strokeColor || config.color,
// used this to get updateTriggers
defaultValue: config => config.visConfig.strokeColor || config.color
},
size: {
...visualChannels.size,
property: 'stroke',
accessor: 'getLineWidth',
condition: config => config.visConfig.stroked,
nullValue: 0,
getAttributeValue: () => d => d.properties.lineWidth || defaultLineWidth
},
height: {
property: 'height',
field: 'heightField',
scale: 'heightScale',
domain: 'heightDomain',
range: 'heightRange',
key: 'height',
channelScaleType: CHANNEL_SCALES.size,
accessor: 'getElevation',
condition: config => config.visConfig.enable3d,
nullValue: 0,
getAttributeValue: () => d => d.properties.elevation || defaultElevation
},
radius: {
property: 'radius',
field: 'radiusField',
scale: 'radiusScale',
domain: 'radiusDomain',
range: 'radiusRange',
key: 'radius',
channelScaleType: CHANNEL_SCALES.radius,
accessor: 'getRadius',
nullValue: 0,
getAttributeValue: () => d => d.properties.radius || defaultRadius
}
};
}
getPositionAccessor() {
return this.getFeature(this.config.columns);
}
static findDefaultLayerProps({label, fields = []}) {
const geojsonColumns = fields
.filter(f => f.type === 'geojson' && SUPPORTED_ANALYZER_TYPES[f.analyzerType])
.map(f => f.name);
const defaultColumns = {
geojson: uniq([...GEOJSON_FIELDS.geojson, ...geojsonColumns])
};
const foundColumns = this.findDefaultColumnField(defaultColumns, fields);
if (!foundColumns || !foundColumns.length) {
return {props: []};
}
return {
props: foundColumns.map(columns => ({
label: (typeof label === 'string' && label.replace(/\.[^/.]+$/, '')) || this.type,
columns,
isVisible: true
}))
};
}
getDefaultLayerConfig(props = {}) {
return {
...super.getDefaultLayerConfig(props),
// add height visual channel
heightField: null,
heightDomain: [0, 1],
heightScale: 'linear',
// add radius visual channel
radiusField: null,
radiusDomain: [0, 1],
radiusScale: 'linear',
// add stroke color visual channel
strokeColorField: null,
strokeColorDomain: [0, 1],
strokeColorScale: 'quantile'
};
}
getHoverData(object, allData) {
// index of allData is saved to feature.properties
return allData[object.properties.index];
}
calculateDataAttribute({allData, filteredIndex}, getPosition) {
return filteredIndex.map(i => this.dataToFeature[i]).filter(d => d);
}
formatLayerData(datasets, oldLayerData) {
const {allData, gpuFilter} = datasets[this.config.dataId];
const {data} = this.updateData(datasets, oldLayerData);
const valueAccessor = f => allData[f.properties.index];
const indexAccessor = f => f.properties.index;
const accessors = this.getAttributeAccessors(valueAccessor);
return {
data,
getFilterValue: gpuFilter.filterValueAccessor(indexAccessor, valueAccessor),
...accessors
};
}
updateLayerMeta(allData) {
const getFeature = this.getPositionAccessor();
this.dataToFeature = getGeojsonDataMaps(allData, getFeature);
// get bounds from features
const bounds = getGeojsonBounds(this.dataToFeature);
// if any of the feature has properties.radius set to be true
const fixedRadius = Boolean(
this.dataToFeature.find(d => d && d.properties && d.properties.radius)
);
// keep a record of what type of geometry the collection has
const featureTypes = getGeojsonFeatureTypes(this.dataToFeature);
this.updateMeta({bounds, fixedRadius, featureTypes});
}
setInitialLayerConfig({allData}) {
this.updateLayerMeta(allData);
const {featureTypes} = this.meta;
// default settings is stroke: true, filled: false
if (featureTypes && featureTypes.polygon) {
// set both fill and stroke to true
return this.updateLayerVisConfig({
filled: true,
stroked: true,
strokeColor: colorMaker.next().value
});
} else if (featureTypes && featureTypes.point) {
// set fill to true if detect point
return this.updateLayerVisConfig({filled: true, stroked: false});
}
return this;
}
renderLayer(opts) {
const {data, gpuFilter, objectHovered, mapState, interactionConfig} = opts;
const {fixedRadius, featureTypes} = this.meta;
const radiusScale = this.getRadiusScaleByZoom(mapState, fixedRadius);
const zoomFactor = this.getZoomFactor(mapState);
const eleZoomFactor = this.getElevationZoomFactor(mapState);
const {visConfig} = this.config;
const layerProps = {
lineWidthScale: visConfig.thickness * zoomFactor * 8,
elevationScale: visConfig.elevationScale * eleZoomFactor,
pointRadiusScale: radiusScale,
lineMiterLimit: 4
};
const updateTriggers = {
...this.getVisualChannelUpdateTriggers(),
getFilterValue: gpuFilter.filterValueUpdateTriggers
};
const defaultLayerProps = this.getDefaultDeckLayerProps(opts);
const opaOverwrite = {
opacity: visConfig.strokeOpacity
};
const pickable = interactionConfig.tooltip.enabled;
const hoveredObject = this.hasHoveredObject(objectHovered);
return [
new DeckGLGeoJsonLayer({
...defaultLayerProps,
...layerProps,
...data,
pickable,
highlightColor: HIGHLIGH_COLOR_3D,
autoHighlight: visConfig.enable3d && pickable,
stroked: visConfig.stroked,
filled: visConfig.filled,
extruded: visConfig.enable3d,
wireframe: visConfig.wireframe,
wrapLongitude: false,
lineMiterLimit: 2,
rounded: true,
updateTriggers,
_subLayerProps: {
...(featureTypes.polygon ? {'polygons-stroke': opaOverwrite} : {}),
...(featureTypes.line ? {'line-strings': opaOverwrite} : {}),
...(featureTypes.point
? {
points: {
lineOpacity: visConfig.strokeOpacity
}
}
: {})
}
}),
...(hoveredObject && !visConfig.enable3d
? [
new DeckGLGeoJsonLayer({
...this.getDefaultHoverLayerProps(),
...layerProps,
wrapLongitude: false,
data: [hoveredObject],
getLineWidth: data.getLineWidth,
getRadius: data.getRadius,
getElevation: data.getElevation,
getLineColor: this.config.highlightColor,
getFillColor: this.config.highlightColor,
// always draw outline
stroked: true,
filled: false
})
]
: [])
];
}
}