createPolygon.js
10.6 KB
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
import movePrompt from './MovePrompt';
import addCompany from './addCompany';
export default class CreatePolygon {
constructor(viewer, style) {
this.objId = Number((new Date()).getTime() + "" + Number(Math.random() * 1000).toFixed(0));
this.viewer = viewer;
this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
this.polygon = null;
this.polyline = null;
this.positions = [];
this.style = style;
this.state = 0; //1为新增 2为编辑 0为清除
this.gonPointArr = [];
this.modifyPoint = null;
this.prompt = new movePrompt.movePrompt(viewer);
}
start(callBack) {
var that = this;
this.handler.setInputAction(function(evt) { //单机开始绘制
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer, [that.polygon]);
if (that.positions.length == 0) {
that.positions.push(cartesian.clone());
}
that.positions.push(cartesian);
var point = addCompany.createPoint.call(that, {position: cartesian});
point.wz = that.gonPointArr.length;
that.gonPointArr.push(point);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
this.handler.setInputAction(function(evt) { //移动时绘制面
if (that.positions.length < 1) {
that.prompt.updatePrompt(evt.endPosition, "单击开始绘制");
return;
}
that.prompt.updatePrompt(evt.endPosition, "单击新增,右键结束");
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer, [that.polygon]);
if (that.positions.length == 2) {
if (!Cesium.defined(that.polyline)) that.polyline = addCompany.createPolyline.call(that);
} else {
if (!Cesium.defined(that.polygon)) {
that.polygon = addCompany.createPolygon.call(that);
that.polygon.isFilter = true;
that.polygon.objId = that.objId;
if (that.polyline) that.viewer.entities.remove(that.polyline);
}
}
that.positions.pop();
that.positions.push(cartesian);
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.handler.setInputAction(function(evt) {
if (!that.polygon) return;
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer, [that.polygon]);
that.state = 1;
that.handler.destroy();
if (that.floatPoint) {
if (that.floatPoint) that.floatPoint.show = false;
that.floatPoint = null;
}
that.positions.pop();
that.positions.push(cartesian);
var point = addCompany.createPoint.call(that, {position: cartesian});
point.wz = that.gonPointArr.length;
that.gonPointArr.push(point);
if (that.prompt) {
that.prompt.destroy();
that.prompt = null;
}
callBack(that.polygon);
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}
createByPositions(lnglatArr, callBack) {
if (!lnglatArr) return;
var positions = that.getCatesian3FromPX.lnglatArrToCartesianArr(lnglatArr);
if (!positions) return;
this.polygon = addCompany.createPolygon.call(this);
this.positions = positions;
callBack(this.polygon);
for (var i = 0; i < positions.length; i++) {
var point = addCompany.createPoint.call(that, {position: positions[i]});
point.isFilter = true;
point.wz = this.gonPointArr.length;
this.gonPointArr.push(point);
}
this.state = 1;
this.polygon.objId = this.objId;
}
startModify() {
if (this.state != 1 && this.state != 2) return; //表示还没绘制完成
if (!this.modifyHandler) this.modifyHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
var that = this;
for (var i = 0; i < that.gonPointArr.length; i++) {
var point = that.gonPointArr[i];
if (point) point.show = true;
}
this.modifyHandler.setInputAction(function(evt) {
var pick = that.viewer.scene.pick(evt.position);
if (Cesium.defined(pick) && pick.id) {
if (!pick.id.objId)
that.modifyPoint = pick.id;
that.forbidDrawWorld(true);
} else {
for (var i = 0; i < that.gonPointArr.length; i++) {
var point = that.gonPointArr[i];
if (point) point.show = false;
}
if (that.modifyHandler) {
that.modifyHandler.destroy();
that.modifyHandler = null;
}
that.state = 2;
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
this.modifyHandler.setInputAction(function(evt) { //移动时绘制面
if (that.positions.length < 1 || !that.modifyPoint) return;
var cartesian = that.getCatesian3FromPX(evt.endPosition, that.viewer, [that.polygon, that.modifyPoint]);
if (cartesian) {
that.modifyPoint.position.setValue(cartesian);
that.positions[that.modifyPoint.wz] = cartesian;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.modifyHandler.setInputAction(function(evt) {
that.forbidDrawWorld(false);
if (!that.modifyPoint) return;
var cartesian = that.getCatesian3FromPX(evt.position, that.viewer, [that.polygon, that.modifyPoint]);
that.modifyPoint.position.setValue(cartesian);
that.positions[that.modifyPoint.wz] = cartesian;
that.modifyPoint = null;
that.forbidDrawWorld(false);
}, Cesium.ScreenSpaceEventType.LEFT_UP);
}
endModify(callback) {
for (var i = 0; i < this.gonPointArr.length; i++) {
var point = this.gonPointArr[i];
if (point) point.show = false;
}
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
}
this.state = 2;
if(callback) callback(this.polygon);
}
getPositions() {
return this.positions;
}
getLnglats() {
return Cesium.caratesianArrToLnglatArr(this.positions);
}
getAttr() {
if (!this.polygon) return;
var obj = {};
var polygon = this.polygon.polygon;
obj.fill = polygon.fill._value;
obj.outline = polygon.outline._value;
obj.outlineWidth = polygon.outlineWidth._value;
obj.outlineColor = polygon.outlineColor._value;
obj.clampToGround = line.clampToGround._value;
obj.color = line.material.color._value;
return obj;
}
setStyle() {}
remove() {
if (this.polygon) {
this.state = 0;
this.viewer.entities.remove(this.polygon);
this.polygon = null;
}
}
setVisible(vis) {
this.polygon.show = vis;
}
forbidDrawWorld(isForbid) {
this.viewer.scene.screenSpaceCameraController.enableRotate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTilt = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableTranslate = !isForbid;
this.viewer.scene.screenSpaceCameraController.enableInputs = !isForbid;
}
destroy() {
if (this.handler) {
this.handler.destroy();
this.handler = null;
}
if (this.modifyHandler) {
this.modifyHandler.destroy();
this.modifyHandler = null;
}
if (this.polygon) {
this.viewer.entities.remove(this.polygon);
this.polygon = null;
}
if (this.polyline) {
this.viewer.entities.remove(this.polyline);
this.polyline = null;
}
this.positions = [];
this.style = null;
if (this.modifyPoint) {
this.viewer.entities.remove(this.modifyPoint);
this.modifyPoint = null;
}
for (var i = 0; i < this.gonPointArr.length; i++) {
var point = this.gonPointArr[i];
this.viewer.entities.remove(point);
}
this.gonPointArr = [];
this.state = 0;
if (this.prompt) this.prompt.destroy();
if (this.polyline) {
this.polyline = null;
this.viewer.entities.remove(this.polyline);
}
}
getCatesian3FromPX(px, viewer, entitys) {
var picks = viewer.scene.drillPick(px);
this.viewer.scene.render();
var cartesian;
var isOn3dtiles = false;
for (var i = 0; i < picks.length; i++) {
var isContinue = false;
for (var step = 0; step < entitys.length; step++) {
if (entitys[step] && picks[i].id && entitys[step].objId == picks[i].id.objId) {
isContinue = true;
break;
}
}
if (isContinue) continue;
if ((picks[i] && picks[i].primitive) || picks[i] instanceof Cesium.Cesium3DTileFeature) { //模型上拾取
isOn3dtiles = true;
}
}
if (isOn3dtiles) {
cartesian = viewer.scene.pickPosition(px);
} else {
var ray = viewer.camera.getPickRay(px);
if (!ray) return null;
cartesian = viewer.scene.globe.pick(ray, viewer.scene);
}
return cartesian;
}
}