-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy patheditable-geojson-layer.ts
602 lines (529 loc) · 19.3 KB
/
editable-geojson-layer.ts
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/* eslint-env browser */
import { RGBAColor } from '@deck.gl/core';
import { GeoJsonLayer, ScatterplotLayer, IconLayer, TextLayer } from '@deck.gl/layers';
import {
ViewMode,
ModifyMode,
TranslateMode,
ScaleMode,
RotateMode,
DuplicateMode,
SplitPolygonMode,
ExtrudeMode,
ElevationMode,
DrawPointMode,
DrawLineStringMode,
DrawPolygonMode,
DrawRectangleMode,
DrawSquareMode,
DrawRectangleFromCenterMode,
DrawSquareFromCenterMode,
DrawCircleFromCenterMode,
DrawCircleByDiameterMode,
DrawEllipseByBoundingBoxMode,
DrawRectangleUsingThreePointsMode,
DrawEllipseUsingThreePointsMode,
Draw90DegreePolygonMode,
DrawPolygonByDraggingMode,
SnappableMode,
TransformMode,
EditAction,
ClickEvent,
StartDraggingEvent,
StopDraggingEvent,
DraggingEvent,
PointerMoveEvent,
GeoJsonEditModeType,
FeatureCollection,
} from '@nebula.gl/edit-modes';
import { PROJECTED_PIXEL_SIZE_MULTIPLIER } from '../constants';
import EditableLayer, { EditableLayerProps } from './editable-layer';
import EditablePathLayer from './editable-path-layer';
const DEFAULT_LINE_COLOR: RGBAColor = [0x0, 0x0, 0x0, 0x99];
const DEFAULT_FILL_COLOR: RGBAColor = [0x0, 0x0, 0x0, 0x90];
const DEFAULT_SELECTED_LINE_COLOR: RGBAColor = [0x0, 0x0, 0x0, 0xff];
const DEFAULT_SELECTED_FILL_COLOR: RGBAColor = [0x0, 0x0, 0x90, 0x90];
const DEFAULT_TENTATIVE_LINE_COLOR: RGBAColor = [0x90, 0x90, 0x90, 0xff];
const DEFAULT_TENTATIVE_FILL_COLOR: RGBAColor = [0x90, 0x90, 0x90, 0x90];
const DEFAULT_EDITING_EXISTING_POINT_COLOR: RGBAColor = [0xc0, 0x0, 0x0, 0xff];
const DEFAULT_EDITING_INTERMEDIATE_POINT_COLOR: RGBAColor = [0x0, 0x0, 0x0, 0x80];
const DEFAULT_EDITING_SNAP_POINT_COLOR: RGBAColor = [0x7c, 0x00, 0xc0, 0xff];
const DEFAULT_EDITING_POINT_OUTLINE_COLOR: RGBAColor = [0xff, 0xff, 0xff, 0xff];
const DEFAULT_EDITING_EXISTING_POINT_RADIUS = 5;
const DEFAULT_EDITING_INTERMEDIATE_POINT_RADIUS = 3;
const DEFAULT_EDITING_SNAP_POINT_RADIUS = 7;
const DEFAULT_TOOLTIP_FONT_SIZE = 32 * PROJECTED_PIXEL_SIZE_MULTIPLIER;
const DEFAULT_EDIT_MODE = DrawPolygonMode;
function guideAccessor(accessor) {
if (!accessor || typeof accessor !== 'function') {
return accessor;
}
return (guideMaybeWrapped) => accessor(unwrapGuide(guideMaybeWrapped));
}
// The object handed to us from deck.gl is different depending on the version of deck.gl used, unwrap as necessary
function unwrapGuide(guideMaybeWrapped) {
if (guideMaybeWrapped.__source) {
return guideMaybeWrapped.__source.object;
} else if (guideMaybeWrapped.sourceFeature) {
return guideMaybeWrapped.sourceFeature.feature;
}
// It is not wrapped, return as is
return guideMaybeWrapped;
}
function getEditHandleColor(handle) {
switch (handle.properties.editHandleType) {
case 'existing':
return DEFAULT_EDITING_EXISTING_POINT_COLOR;
case 'snap-source':
return DEFAULT_EDITING_SNAP_POINT_COLOR;
case 'intermediate':
default:
return DEFAULT_EDITING_INTERMEDIATE_POINT_COLOR;
}
}
function getEditHandleOutlineColor(handle) {
return DEFAULT_EDITING_POINT_OUTLINE_COLOR;
}
function getEditHandleRadius(handle) {
switch (handle.properties.editHandleType) {
case 'existing':
return DEFAULT_EDITING_EXISTING_POINT_RADIUS;
case 'snap':
return DEFAULT_EDITING_SNAP_POINT_RADIUS;
case 'intermediate':
default:
return DEFAULT_EDITING_INTERMEDIATE_POINT_RADIUS;
}
}
export interface EditableGeojsonLayerProps<D> extends EditableLayerProps<D> {
mode?: any;
modeConfig?: any;
selectedFeatureIndexes?: number[];
onEdit?: (updatedData?, editType?: string, featureIndexes?: number[], editContext?) => void;
pickable?: boolean;
pickingRadius?: number;
pickingDepth?: number;
fp64?: boolean;
filled?: boolean;
stroked?: boolean;
lineWidthScale?: number;
lineWidthMinPixels?: number;
lineWidthMaxPixels?: number;
pickingLineWidthExtraPixels?: number;
lineWidthUnits?: string;
lineJointRounded?: boolean;
lineCapRounded?: boolean;
lineMiterLimit?: number;
pointRadiusScale?: number;
pointRadiusMinPixels?: number;
pointRadiusMaxPixels?: number;
getLineColor?: RGBAColor | ((feature, isSelected, mode) => RGBAColor);
getFillColor?: RGBAColor | ((feature, isSelected, mode) => RGBAColor);
getRadius?: number | ((feature, isSelected, mode) => number);
getLineWidth?: number | ((feature, isSelected, mode) => number);
getTentativeLineColor?: RGBAColor | ((feature, isSelected, mode) => RGBAColor);
getTentativeFillColor?: RGBAColor | ((feature, isSelected, mode) => RGBAColor);
getTentativeLineWidth?: number | ((feature, isSelected, mode) => number);
editHandleType?: string;
editHandlePointRadiusScale?: number;
editHandlePointOutline?: boolean;
editHandlePointStrokeWidth?: number;
editHandlePointRadiusUnits?: string;
editHandlePointRadiusMinPixels?: number;
editHandlePointRadiusMaxPixels?: number;
getEditHandlePointColor?: RGBAColor | ((handle) => RGBAColor);
getEditHandlePointOutlineColor?: RGBAColor | ((handle) => RGBAColor);
getEditHandlePointRadius?: number | ((handle) => number);
// icon handles
editHandleIconAtlas?: any;
editHandleIconMapping?: any;
editHandleIconSizeScale?: number;
editHandleIconSizeUnits?: string;
getEditHandleIcon?: (handle) => string;
getEditHandleIconSize?: number;
getEditHandleIconColor?: RGBAColor | ((handle) => RGBAColor);
getEditHandleIconAngle?: number | ((handle) => number);
// misc
billboard?: boolean;
}
const defaultProps: EditableGeojsonLayerProps<any> = {
mode: DEFAULT_EDIT_MODE,
// Edit and interaction events
onEdit: () => {},
pickable: true,
pickingRadius: 10,
pickingDepth: 5,
fp64: false,
filled: true,
stroked: true,
lineWidthScale: PROJECTED_PIXEL_SIZE_MULTIPLIER,
lineWidthMinPixels: 1,
lineWidthMaxPixels: Number.MAX_SAFE_INTEGER,
pickingLineWidthExtraPixels: 0,
lineWidthUnits: 'pixels',
lineJointRounded: false,
lineCapRounded: false,
lineMiterLimit: 4,
pointRadiusScale: 1,
pointRadiusMinPixels: 2,
pointRadiusMaxPixels: Number.MAX_SAFE_INTEGER,
getLineColor: (feature, isSelected, mode) =>
isSelected ? DEFAULT_SELECTED_LINE_COLOR : DEFAULT_LINE_COLOR,
getFillColor: (feature, isSelected, mode) =>
isSelected ? DEFAULT_SELECTED_FILL_COLOR : DEFAULT_FILL_COLOR,
getRadius: (f) =>
(f && f.properties && f.properties.radius) || (f && f.properties && f.properties.size) || 1,
getLineWidth: (f) => (f && f.properties && f.properties.lineWidth) || 3,
// Tentative feature rendering
getTentativeLineColor: (f) => DEFAULT_TENTATIVE_LINE_COLOR,
getTentativeFillColor: (f) => DEFAULT_TENTATIVE_FILL_COLOR,
getTentativeLineWidth: (f) => (f && f.properties && f.properties.lineWidth) || 3,
editHandleType: 'point',
// point handles
editHandlePointRadiusScale: 1,
editHandlePointOutline: true,
editHandlePointStrokeWidth: 2,
editHandlePointRadiusUnits: 'pixels',
editHandlePointRadiusMinPixels: 4,
editHandlePointRadiusMaxPixels: 8,
getEditHandlePointColor: getEditHandleColor,
getEditHandlePointOutlineColor: getEditHandleOutlineColor,
getEditHandlePointRadius: getEditHandleRadius,
// icon handles
editHandleIconAtlas: null,
editHandleIconMapping: null,
editHandleIconSizeScale: 1,
editHandleIconSizeUnits: 'pixels',
getEditHandleIcon: (handle) => handle.properties.editHandleType,
getEditHandleIconSize: 10,
getEditHandleIconColor: getEditHandleColor,
getEditHandleIconAngle: 0,
// misc
billboard: true,
};
// Mapping of mode name to mode class (for legacy purposes)
const modeNameMapping = {
view: ViewMode,
// Alter modes
modify: ModifyMode,
translate: new SnappableMode(new TranslateMode()),
transform: new SnappableMode(new TransformMode()),
scale: ScaleMode,
rotate: RotateMode,
duplicate: DuplicateMode,
split: SplitPolygonMode,
extrude: ExtrudeMode,
elevation: ElevationMode,
// Draw modes
drawPoint: DrawPointMode,
drawLineString: DrawLineStringMode,
drawPolygon: DrawPolygonMode,
drawRectangle: DrawRectangleMode,
drawSquare: DrawSquareMode,
drawRectangleFromCenter: DrawRectangleFromCenterMode,
drawSquareFromCenter: DrawSquareFromCenterMode,
drawCircleFromCenter: DrawCircleFromCenterMode,
drawCircleByBoundingBox: DrawCircleByDiameterMode,
drawEllipseByBoundingBox: DrawEllipseByBoundingBoxMode,
drawRectangleUsing3Points: DrawRectangleUsingThreePointsMode,
drawEllipseUsing3Points: DrawEllipseUsingThreePointsMode,
draw90DegreePolygon: Draw90DegreePolygonMode,
drawPolygonByDragging: DrawPolygonByDraggingMode,
};
// type State = {
// mode: GeoJsonEditMode,
// tentativeFeature: ?Feature,
// editHandles: any[],
// selectedFeatures: Feature[]
// };
export default class EditableGeoJsonLayer extends EditableLayer<
any,
EditableGeojsonLayerProps<any>
> {
static layerName = 'EditableGeoJsonLayer';
static defaultProps = defaultProps;
// setState: ($Shape<State>) => void;
renderLayers() {
const subLayerProps = this.getSubLayerProps({
id: 'geojson',
// Proxy most GeoJsonLayer props as-is
data: this.props.data,
fp64: this.props.fp64,
filled: this.props.filled,
stroked: this.props.stroked,
lineWidthScale: this.props.lineWidthScale,
lineWidthMinPixels: this.props.lineWidthMinPixels,
lineWidthMaxPixels: this.props.lineWidthMaxPixels,
lineWidthUnits: this.props.lineWidthUnits,
lineJointRounded: this.props.lineJointRounded,
lineCapRounded: this.props.lineCapRounded,
lineMiterLimit: this.props.lineMiterLimit,
pointRadiusScale: this.props.pointRadiusScale,
pointRadiusMinPixels: this.props.pointRadiusMinPixels,
pointRadiusMaxPixels: this.props.pointRadiusMaxPixels,
getLineColor: this.selectionAwareAccessor(this.props.getLineColor),
getFillColor: this.selectionAwareAccessor(this.props.getFillColor),
getPointRadius: this.selectionAwareAccessor(this.props.getRadius),
getLineWidth: this.selectionAwareAccessor(this.props.getLineWidth),
_subLayerProps: {
linestrings: {
billboard: this.props.billboard,
updateTriggers: {
// required to update dashed array attribute
all: [this.props.selectedFeatureIndexes, this.props.mode],
},
},
'polygons-stroke': {
billboard: this.props.billboard,
pickingLineWidthExtraPixels: this.props.pickingLineWidthExtraPixels,
type: EditablePathLayer,
updateTriggers: {
// required to update dashed array attribute
all: [this.props.selectedFeatureIndexes, this.props.mode],
},
},
},
updateTriggers: {
getLineColor: [this.props.selectedFeatureIndexes, this.props.mode],
getFillColor: [this.props.selectedFeatureIndexes, this.props.mode],
getPointRadius: [this.props.selectedFeatureIndexes, this.props.mode],
getLineWidth: [this.props.selectedFeatureIndexes, this.props.mode],
},
});
let layers: any = [new GeoJsonLayer(subLayerProps)];
layers = layers.concat(this.createGuidesLayers(), this.createTooltipsLayers());
return layers;
}
initializeState() {
super.initializeState();
this.setState({
selectedFeatures: [],
editHandles: [],
});
}
// TODO: is this the best way to properly update state from an outside event handler?
shouldUpdateState(opts: any) {
// console.log(
// 'shouldUpdateState',
// opts.changeFlags.propsOrDataChanged,
// opts.changeFlags.stateChanged
// );
return super.shouldUpdateState(opts) || opts.changeFlags.stateChanged;
}
updateState({
props,
oldProps,
changeFlags,
context,
}: {
props: EditableGeojsonLayerProps<any>;
oldProps: EditableGeojsonLayerProps<any>;
changeFlags: any;
context: any;
s;
}) {
super.updateState({ oldProps, props, changeFlags, context });
if (changeFlags.propsOrDataChanged) {
const modePropChanged = Object.keys(oldProps).length === 0 || props.mode !== oldProps.mode;
if (modePropChanged) {
let mode;
if (typeof props.mode === 'function') {
// They passed a constructor/class, so new it up
const ModeConstructor = props.mode;
mode = new ModeConstructor();
} else if (typeof props.mode === 'string') {
// Lookup the mode based on its name (for backwards compatibility)
mode = modeNameMapping[props.mode];
// eslint-disable-next-line no-console
console.warn(
"Deprecated use of passing `mode` as a string. Pass the mode's class constructor instead."
);
} else {
// Should be an instance of EditMode in this case
mode = props.mode;
}
if (!mode) {
console.warn(`No mode configured for ${String(props.mode)}`); // eslint-disable-line no-console,no-undef
// Use default mode
mode = new DEFAULT_EDIT_MODE();
}
if (mode !== this.state.mode) {
this.setState({ mode, cursor: null });
}
}
}
let selectedFeatures = [];
if (Array.isArray(props.selectedFeatureIndexes)) {
// TODO: needs improved testing, i.e. checking for duplicates, NaNs, out of range numbers, ...
selectedFeatures = props.selectedFeatureIndexes.map((elem) => props.data.features[elem]);
}
this.setState({ selectedFeatures });
}
getModeProps(props: EditableGeojsonLayerProps<any>) {
return {
modeConfig: props.modeConfig,
data: props.data,
selectedIndexes: props.selectedFeatureIndexes,
lastPointerMoveEvent: this.state.lastPointerMoveEvent,
cursor: this.state.cursor,
onEdit: (editAction: EditAction<FeatureCollection>) => {
// Force a re-render
// This supports double-click where we need to ensure that there's a re-render between the two clicks
// even though the data wasn't changed, just the internal tentative feature.
this.setNeedsUpdate();
props.onEdit(editAction);
},
onUpdateCursor: (cursor: string | null | undefined) => {
this.setState({ cursor });
},
};
}
selectionAwareAccessor(accessor: any) {
if (typeof accessor !== 'function') {
return accessor;
}
return (feature: Record<string, any>) =>
accessor(feature, this.isFeatureSelected(feature), this.props.mode);
}
isFeatureSelected(feature: Record<string, any>) {
if (!this.props.data || !this.props.selectedFeatureIndexes) {
return false;
}
if (!this.props.selectedFeatureIndexes.length) {
return false;
}
const featureIndex = this.props.data.features.indexOf(feature);
return this.props.selectedFeatureIndexes.includes(featureIndex);
}
getPickingInfo({ info, sourceLayer }: Record<string, any>) {
if (sourceLayer.id.endsWith('guides')) {
// If user is picking an editing handle, add additional data to the info
info.isGuide = true;
}
return info;
}
_updateAutoHighlight(info) {
// Extra handling for guides
if (info?.sourceLayer) {
if (info.isGuide) {
for (const layer of info.sourceLayer.getSubLayers()) {
layer.updateAutoHighlight(info);
}
} else {
info.sourceLayer.updateAutoHighlight(info);
}
}
}
createGuidesLayers() {
const mode = this.getActiveMode();
const guides: FeatureCollection = mode.getGuides(this.getModeProps(this.props));
if (!guides || !guides.features.length) {
return [];
}
const subLayerProps = {
linestrings: {
billboard: this.props.billboard,
autoHighlight: false,
},
'polygons-fill': {
autoHighlight: false,
},
'polygons-stroke': {
billboard: this.props.billboard,
},
};
if (this.props.editHandleType === 'icon') {
subLayerProps['points-icon'] = {
type: IconLayer,
iconAtlas: this.props.editHandleIconAtlas,
iconMapping: this.props.editHandleIconMapping,
sizeUnits: this.props.editHandleIconSizeUnits,
sizeScale: this.props.editHandleIconSizeScale,
getIcon: guideAccessor(this.props.getEditHandleIcon),
getSize: guideAccessor(this.props.getEditHandleIconSize),
getColor: guideAccessor(this.props.getEditHandleIconColor),
getAngle: guideAccessor(this.props.getEditHandleIconAngle),
};
} else {
subLayerProps['points-circle'] = {
type: ScatterplotLayer,
radiusScale: this.props.editHandlePointRadiusScale,
stroked: this.props.editHandlePointOutline,
getLineWidth: this.props.editHandlePointStrokeWidth,
radiusUnits: this.props.editHandlePointRadiusUnits,
radiusMinPixels: this.props.editHandlePointRadiusMinPixels,
radiusMaxPixels: this.props.editHandlePointRadiusMaxPixels,
getRadius: guideAccessor(this.props.getEditHandlePointRadius),
getFillColor: guideAccessor(this.props.getEditHandlePointColor),
getLineColor: guideAccessor(this.props.getEditHandlePointOutlineColor),
};
}
const layer = new GeoJsonLayer(
this.getSubLayerProps({
id: `guides`,
data: guides,
fp64: this.props.fp64,
_subLayerProps: subLayerProps,
lineWidthScale: this.props.lineWidthScale,
lineWidthMinPixels: this.props.lineWidthMinPixels,
lineWidthMaxPixels: this.props.lineWidthMaxPixels,
lineWidthUnits: this.props.lineWidthUnits,
lineJointRounded: this.props.lineJointRounded,
lineCapRounded: this.props.lineCapRounded,
lineMiterLimit: this.props.lineMiterLimit,
getLineColor: guideAccessor(this.props.getTentativeLineColor),
getLineWidth: guideAccessor(this.props.getTentativeLineWidth),
getFillColor: guideAccessor(this.props.getTentativeFillColor),
pointType: this.props.editHandleType === 'icon' ? 'icon' : 'circle',
iconAtlas: this.props.editHandleIconAtlas,
})
);
return [layer];
}
createTooltipsLayers() {
const mode = this.getActiveMode();
const tooltips = mode.getTooltips(this.getModeProps(this.props));
const layer = new TextLayer({
getSize: DEFAULT_TOOLTIP_FONT_SIZE,
...this.getSubLayerProps({
id: `tooltips`,
data: tooltips,
}),
});
return [layer];
}
onLayerClick(event: ClickEvent) {
this.getActiveMode().handleClick(event, this.getModeProps(this.props));
}
onLayerKeyUp(event: KeyboardEvent) {
this.getActiveMode().handleKeyUp(event, this.getModeProps(this.props));
}
onStartDragging(event: StartDraggingEvent) {
this.getActiveMode().handleStartDragging(event, this.getModeProps(this.props));
}
onDragging(event: DraggingEvent) {
this.getActiveMode().handleDragging(event, this.getModeProps(this.props));
}
onStopDragging(event: StopDraggingEvent) {
this.getActiveMode().handleStopDragging(event, this.getModeProps(this.props));
}
onPointerMove(event: PointerMoveEvent) {
this.setState({ lastPointerMoveEvent: event });
this.getActiveMode().handlePointerMove(event, this.getModeProps(this.props));
}
getCursor({ isDragging }: { isDragging: boolean }) {
if (this.state === null) {
// Layer in 'Awaiting state'
return null;
}
let { cursor } = this.state;
if (!cursor) {
// default cursor
cursor = isDragging ? 'grabbing' : 'grab';
}
return cursor;
}
getActiveMode(): GeoJsonEditModeType {
return this.state.mode;
}
}