-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrawTool.js
368 lines (322 loc) · 9.78 KB
/
DrawTool.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// 画面点位数据
let pointArr = [];
// 当前点位的数据
let nowPoint = [];
// 当前多边形中的点和线实体集合
let pointAndLineEntity = {
pointEntityArr: [],
lineEntityArr: [],
demoLineEntityArr: [],
selectedPolygon: [],
};
// 当前选择的编辑点
let nowEditPoint = [];
let selectLabelEntity; //
// 是否开始画多边形
let isDrawLine = false;
// 是否编辑
let isEditable = false;
// 事件处理类
class DrawTools {
// 删除实体
delDemoEntity(name) {
const entityTypes = {
point_name: pointAndLineEntity.pointEntityArr,
line_name: pointAndLineEntity.lineEntityArr,
line_demo_name: pointAndLineEntity.demoLineEntityArr,
};
const entities = entityTypes[name];
if (entities) {
entities.forEach(item => this.viewer.entities.remove(item));
}
}
// 经纬度转换
getLonOrLat(cartesian2) {
const cartesian = this.viewer.scene.globe.pick(
this.viewer.camera.getPickRay(cartesian2),
this.viewer.scene
);
if (!cartesian) return null;
const cartographic = Cesium.Cartographic.fromCartesian(cartesian);
return {
longitude: Cesium.Math.toDegrees(cartographic.longitude),
latitude: Cesium.Math.toDegrees(cartographic.latitude),
};
}
// 根据经纬度坐标计算出中心点
calculateCenter(coordinates) {
let totalX = 0;
let totalY = 0;
let totalZ = 0;
let count = coordinates.length;
for (let i = 0; i < count; i++) {
totalX += coordinates[i].x;
totalY += coordinates[i].y;
totalZ += coordinates[i].z;
}
let centerX = totalX / count;
let centerY = totalY / count;
let centerZ = totalZ / count;
return [centerX, centerY, centerZ];
}
// 画点
drawPoint(lonAndLat) {
return this.viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(...lonAndLat),
name: 'point_name',
point: {
color: Cesium.Color.WHITE,
outlineColor: Cesium.Color.BLACK,
outlineWidth: 1,
pixelSize: 8,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, // 贴地
},
});
}
// 画线
drawLine(name, lineArr) {
return this.viewer.entities.add({
name: name,
polyline: {
clampToGround: true, // 开启贴地
positions: new Cesium.CallbackProperty(() => {
return Cesium.Cartesian3.fromDegreesArrayHeights(lineArr);
}, false),
width: 5,
material: Cesium.Color.BLUE,
},
});
}
// 画多边形
drawPolyGon(lonAndLat) {
const lonLats = data => {
return data
.filter(d => d[0] >= -180 && d[0] <= 180 && d[1] >= -90 && d[1] <= 90)
.flatMap(d => [d[0], d[1]]);
};
this.viewer.entities.add({
name: 'polyGon_name',
polygon: {
clampToGround: true, // 开启贴地
hierarchy: new Cesium.PolygonHierarchy(
Cesium.Cartesian3.fromDegreesArray(lonLats(lonAndLat))
),
material: Cesium.Color.BLUE.withAlpha(0.5),
},
});
}
// 画标签label
drawLabel = (id, pos) => {
return this.viewer.entities.add({
name: id,
position: new Cesium.Cartesian3(...pos),
label: {
text: 'X',
font: '10px sans-serif',
fillColor: Cesium.Color.RED,
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
},
});
};
removeByType = type => {
const entities = this.viewer.entities.values;
const removeList = entities.filter(i => i[type]);
removeList.forEach(i => {
this.viewer.entities.remove(i);
});
};
// 清除选中状态
clearEntitiesByName = flagText => {
const entities = this.viewer.entities.values;
const targetEntities = entities.filter(i => i.name === flagText || i.id === flagText);
targetEntities.forEach(i => {
this.viewer.entities.remove(i);
});
};
// 处理左击事件
handleLeftClick(event) {
const pick = this.viewer.scene.pick(event.position);
if (pick && pick.id.label) {
// 删除多边形
this.clearEntitiesByName(pick.id.name);
this.clearEntities();
this.viewer.scene.requestRender();
return;
}
if (pick && pick.id && pick.id.polygon && !isDrawLine) {
this.createEditPoints(pick);
} else if (pick && pick.id && pick.id.point && !isDrawLine) {
this.startEditing(pick);
} else if (!isEditable) {
isDrawLine = true;
this.drawPolygoon(event);
}
}
// 创建编辑点
createEditPoints(pick) {
// 清空状态
this.clearEntities();
this.removeByType('label');
let positions;
if (pick.id.polygon.hierarchy._callback) {
// 获取callbackproperty的数据
positions = pick.id.polygon.hierarchy._callback().positions;
} else {
positions = pick.id.polygon.hierarchy._value.positions;
}
for (let cartesian of positions) {
const point = this.viewer.entities.add({
name: 'editablePoint',
position: cartesian,
point: {
color: Cesium.Color.WHITE,
pixelSize: 8,
outlineColor: Cesium.Color.BLACK,
outlineWidth: 1,
disableDepthTestDistance: Number.POSITIVE_INFINITY, //
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, // 贴地设置
},
});
pointAndLineEntity.pointEntityArr.push(point);
}
pointAndLineEntity.selectedPolygon.push(pick);
// 计算中心点
const centerPoint = this.calculateCenter(positions);
selectLabelEntity = this.drawLabel(pick.id.id, centerPoint);
// 重新渲染
this.viewer.scene.requestRender();
}
// 开始编辑
startEditing(pick) {
nowEditPoint = [pick];
isEditable = true;
}
// 绘制
drawPolygoon(event) {
const nowPosition = this.getLonOrLat(event.position);
if (!nowPosition) return;
pointArr.push([nowPosition.longitude, nowPosition.latitude, 0]);
nowPoint = [nowPosition.longitude, nowPosition.latitude, 0];
const point = this.drawPoint(nowPoint);
pointAndLineEntity.pointEntityArr.push(point);
this.viewer.scene.requestRender();
if (pointArr.length > 1) {
this.delDemoEntity('line_demo_name');
const line = this.drawLine('line_name', [...pointArr[pointArr.length - 2], ...nowPoint]);
pointAndLineEntity.lineEntityArr.push(line);
}
}
// 处理鼠标移动事件
handleMouseMove(event) {
this.showPointer(event);
if (isDrawLine && !isEditable) {
this.handleMouseMoveDrawing(event);
} else if (isEditable && !isDrawLine) {
this.handleMouseMoveEditing(event);
}
}
// 处理绘制时的鼠标移动
handleMouseMoveDrawing(event) {
const movePosition = this.getLonOrLat(event.startPosition);
if (!movePosition) return;
this.delDemoEntity('line_demo_name');
const demoLine = this.drawLine('line_demo_name', [
...nowPoint,
...[movePosition.longitude, movePosition.latitude, 0],
]);
pointAndLineEntity.demoLineEntityArr.push(demoLine);
this.viewer.scene.requestRender();
}
// 处理编辑时的鼠标移动
handleMouseMoveEditing(event) {
const movePosition = this.getLonOrLat(event.startPosition);
if (!movePosition) return;
nowEditPoint[0].id.position = Cesium.Cartesian3.fromDegrees(
movePosition.longitude,
movePosition.latitude,
0
);
// 更新多边形的回调属性
const positions = pointAndLineEntity.pointEntityArr.map(item =>
item.position.getValue(Cesium.JulianDate.now())
);
// 实时更新多边形的坐标
pointAndLineEntity.selectedPolygon[0].id.polygon.hierarchy = new Cesium.CallbackProperty(() => {
return new Cesium.PolygonHierarchy(positions);
}, false);
// 实时更新中心点信息
const centerPoint = this.calculateCenter(positions);
selectLabelEntity.position = new Cesium.CallbackProperty(() => {
return new Cesium.Cartesian3(...centerPoint);
}, false);
this.viewer.scene.requestRender();
}
// 显示小手
showPointer = movement => {
// 获取鼠标下的实体
const pickedObject = this.viewer.scene.pick(movement.endPosition);
// 如果鼠标悬停在 Label 上,则显示小手
if (Cesium.defined(pickedObject) && pickedObject.id.label) {
this.viewer.canvas.style.cursor = 'pointer';
} else {
this.viewer.canvas.style.cursor = 'default';
}
};
// 处理右击事件
handleRightClick() {
if (!isEditable && isDrawLine) {
this.finishDrawingPolygon();
} else if (isEditable && !isDrawLine) {
isEditable = false;
}
}
// 完成多边形绘制
finishDrawingPolygon() {
isDrawLine = false;
this.drawPolyGon(pointArr);
this.clearEntities();
}
// 清空实体
clearEntities() {
this.removeByType('label');
this.clearEntitiesByName('editablePoint');
this.delDemoEntity('line_demo_name');
this.delDemoEntity('line_name');
this.delDemoEntity('point_name');
pointArr = [];
nowPoint = [];
pointAndLineEntity = {
pointEntityArr: [],
lineEntityArr: [],
demoLineEntityArr: [],
selectedPolygon: [],
};
}
// 初始化事件
init(viewer) {
if (this.viewer) return;
this.viewer = viewer;
this.handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
this.handler.setInputAction(
this.handleLeftClick.bind(this),
Cesium.ScreenSpaceEventType.LEFT_CLICK
);
this.handler.setInputAction(
this.handleMouseMove.bind(this),
Cesium.ScreenSpaceEventType.MOUSE_MOVE
);
this.handler.setInputAction(
this.handleRightClick.bind(this),
Cesium.ScreenSpaceEventType.RIGHT_CLICK
);
}
// 销毁事件
destory() {
this.clearEntities();
this.viewer.entities.removeAll();
this.viewer.scene.requestRender();
this.handler.destroy();
this.viewer = null;
}
}