-
Notifications
You must be signed in to change notification settings - Fork 165
/
selection-layer.js
187 lines (161 loc) · 5.04 KB
/
selection-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
// @flow
/* eslint-env browser */
import { CompositeLayer } from '@deck.gl/core';
import { PolygonLayer } from '@deck.gl/layers';
import { polygon } from '@turf/helpers';
import turfBuffer from '@turf/buffer';
import turfDifference from '@turf/difference';
import EditableGeoJsonLayer from './editable-geojson-layer';
export const SELECTION_TYPE = {
NONE: null,
RECTANGLE: 'rectangle',
POLYGON: 'polygon'
};
const defaultProps = {
selectionType: SELECTION_TYPE.RECTANGLE,
layerIds: [],
onSelect: () => {}
};
const EMPTY_DATA = {
type: 'FeatureCollection',
features: []
};
const EXPANSION_KM = 50;
const LAYER_ID_GEOJSON = 'selection-geojson';
const LAYER_ID_BLOCKER = 'selection-blocker';
const PASS_THROUGH_PROPS = [
'lineWidthScale',
'lineWidthMinPixels',
'lineWidthMaxPixels',
'lineWidthUnits',
'lineJointRounded',
'lineMiterLimit',
'pointRadiusScale',
'pointRadiusMinPixels',
'pointRadiusMaxPixels',
'lineDashJustified',
'getLineColor',
'getFillColor',
'getRadius',
'getLineWidth',
'getLineDashArray',
'getTentativeLineDashArray',
'getTentativeLineColor',
'getTentativeFillColor',
'getTentativeLineWidth'
];
export default class SelectionLayer extends CompositeLayer {
_selectRectangleObjects(coordinates: any) {
const { layerIds, onSelect } = this.props;
const [x1, y1] = this.context.viewport.project(coordinates[0][0]);
const [x2, y2] = this.context.viewport.project(coordinates[0][2]);
const pickingInfos = this.context.deck.pickObjects({
x: Math.min(x1, x2),
y: Math.min(y1, y2),
width: Math.abs(x2 - x1),
height: Math.abs(y2 - y1),
layerIds
});
onSelect({ pickingInfos });
}
_selectPolygonObjects(coordinates: any) {
const { layerIds, onSelect } = this.props;
const mousePoints = coordinates[0].map(c => this.context.viewport.project(c));
const allX = mousePoints.map(mousePoint => mousePoint[0]);
const allY = mousePoints.map(mousePoint => mousePoint[1]);
const x = Math.min(...allX);
const y = Math.min(...allY);
const maxX = Math.max(...allX);
const maxY = Math.max(...allY);
// Use a polygon to hide the outside, because pickObjects()
// does not support polygons
const landPointsPoly = polygon(coordinates);
const bigBuffer = turfBuffer(landPointsPoly, EXPANSION_KM);
let bigPolygon;
try {
// turfDifference throws an exception if the polygon
// intersects with itself (TODO: check if true in all versions)
bigPolygon = turfDifference(bigBuffer, landPointsPoly);
} catch (e) {
// invalid selection polygon
console.log('turfDifference() error', e); // eslint-disable-line
return;
}
this.setState({
pendingPolygonSelection: {
bigPolygon
}
});
const blockerId = `${this.props.id}-${LAYER_ID_BLOCKER}`;
// HACK, find a better way
setTimeout(() => {
const pickingInfos = this.context.deck.pickObjects({
x,
y,
width: maxX - x,
height: maxY - y,
layerIds: [blockerId, ...layerIds]
});
onSelect({
pickingInfos: pickingInfos.filter(item => item.layer.id !== this.props.id)
});
}, 250);
}
renderLayers() {
const { pendingPolygonSelection } = this.state;
const mode =
{
[SELECTION_TYPE.RECTANGLE]: 'drawRectangle',
[SELECTION_TYPE.POLYGON]: 'drawPolygon'
}[this.props.selectionType] || 'view';
const inheritedProps = {};
PASS_THROUGH_PROPS.forEach(p => {
if (this.props[p] !== undefined) inheritedProps[p] = this.props[p];
});
const layers = [
new EditableGeoJsonLayer(
this.getSubLayerProps({
id: LAYER_ID_GEOJSON,
pickable: true,
mode,
selectedFeatureIndexes: [],
data: EMPTY_DATA,
onEdit: ({ updatedData, editType }) => {
if (editType === 'addFeature') {
const { coordinates } = updatedData.features[0].geometry;
if (this.props.selectionType === SELECTION_TYPE.RECTANGLE) {
this._selectRectangleObjects(coordinates);
} else if (this.props.selectionType === SELECTION_TYPE.POLYGON) {
this._selectPolygonObjects(coordinates);
}
}
},
...inheritedProps
})
)
];
if (pendingPolygonSelection) {
const { bigPolygon } = pendingPolygonSelection;
layers.push(
new PolygonLayer(
this.getSubLayerProps({
id: LAYER_ID_BLOCKER,
pickable: true,
stroked: false,
opacity: 1.0,
data: [bigPolygon],
getLineColor: obj => [0, 0, 0, 1],
getFillColor: obj => [0, 0, 0, 1],
getPolygon: o => o.geometry.coordinates
})
)
);
}
return layers;
}
shouldUpdateState({ changeFlags: { stateChanged, propsOrDataChanged } }: Object) {
return stateChanged || propsOrDataChanged;
}
}
SelectionLayer.layerName = 'SelectionLayer';
SelectionLayer.defaultProps = defaultProps;