zhaoxing
2023-12-07 6f4d8199423dfc0bb6701341bc9b8785d50d2315
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
//第1步骤:事故模拟
 
class Sgmn1Sgfx {
  //========== 构造方法 ==========
  //创建一个模型编辑对象
  constructor(map, options) {
    this.map = map;
    this.gaoDePoi = new mars3d.query.GaodePOI({
      // key: ['ae29a37307840c7ae4a785ac905927e0'],
    });
 
    this.ellipseArr = []; //保存事故范围的椭圆
    this.interval = -1;
    this.iNow = 0; //开始时间
    this.iALLTime = 60; //总时长时间
    this.iALL = 6; //模拟总次数
    this.isStartPlay = false; //用于标记是否开始模拟
    this.windArg = null;
    this.affectedPointArr = []; //受影响点的坐标信息
    this.affectedYWArr = []; //受影响的业务点 模拟===
    this.timelineCallback = null; //用于同步进度轴
    this.bootstrapTableCallback = null; // 用于同步查询结果
    this.pageNum = 1; //当前页码
    this.newPoint = null; //当前查询的中心点
    this._radiusX = null; //当前查询的范围
    this.lastEllipseData = null; //模拟范围的最后一个椭圆
    this.isLoadMore = false;
 
    this.graphicLayer = new mars3d.layer.GraphicLayer({
      hasEdit: false,
    });
    map.addLayer(this.graphicLayer);
 
    this.queryData = []; //当前查询的结果
    this.oneQueryEnd = false; //标识一次查询结束
    this._setArgAgain = false;
  }
 
  //========== 对外属性 ==========
  //模拟点
  get point() {
    return this.incidentLnglat;
  }
 
  //获取最后一个椭圆的相关信息
  get extentData() {
    return this.lastEllipseData;
  }
  //获取所有受影响的企业点
  get pois() {
    return this.affectedPointArr;
  }
  //获取受影响的业务点
  get YWpois() {
    return this.affectedYWArr;
  }
  //获取所有受影响的点
  get allPois() {
    return this.affectedPointArr.concat(this.affectedYWArr);
  }
  //获取影响的范围
  get radiusX() {
    return this._radiusX;
  }
  //获取事故影响点
  get sgDatas() {
    return this.queryData;
  }
  //事故模拟是否结束
  get state() {
    return this.iNow > this.iALL && this.oneQueryEnd;
  }
 
  // set _setArgAgain(bool){
  //     this.argAgain = bool;
  // }
  // get argAgain(){
  //     return this.argAgain;
  // }
 
  //========== 方法 ==========
  //开始激活(需求的参数通过opts传入)
  start(arg, opt) {
    if (this.windArg && arg) {
      if (this.windArg.windSpeed != arg.windSpeed || this.windArg.windDirection != arg.windDirection || this.windArg.playSpeed != arg.playSpeed) {
        this.clearRes();
        this._setArgAgain = true;
      }
    }
 
    if (this.isStartPlay && !this._setArgAgain && this.iNow == 0) {
      //不用分析
      opt.noComputeCallback();
      return;
    }
    if (!this.windArg) {
      this.windArg = arg;
    }
    if (!this.timelineCallback) {
      this.timelineCallback = opt.timelineCallback;
    }
    if (!this.bootstrapTableCallback) {
      this.bootstrapTableCallback = opt.bootstrapTableCallback;
    }
    var _fs = this.windArg.windSpeed; //风速 m/s
    var _fx = Number(this.windArg.windDirection); //风向
    var _sd = Number(this.windArg.playSpeed); //速度
    var speed = 15 / _sd;
    var that = this;
    this.interval = setInterval(function () {
      that.isStartPlay = true;
      that.iNow++;
      if (that.iNow > that.iALL || that.iNow < 0) {
        that.stopPlay();
        return;
      }
      var time = (that.iNow / that.iALL) * that.iALLTime;
      that._addMnCicles(time, _fs, _fx);
      if (that.timelineCallback) {
        that.timelineCallback(that.iNow);
      }
    }, speed * 1000);
  }
 
  //开始 图上选择事故点
  selectSgd(callback) {
    var that = this;
    this.graphicLayer.startDraw({
      type: "billboard",
      style: {
        image: "img/marker/mark1.png",
        horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
      },
      success: function (graphic) {
        var point = graphic.point;
        if (!point) {
          return;
        }
 
        var data = {
          x: point.lng,
          y: point.lat,
          z: point.alt,
        };
        that.incidentLnglat = [point.lng, point.lat, point.alt];
        if (that.sgdGraphic == null) {
          that.sgdGraphic = graphic;
        } else {
          that.clearRes(); //重新选点后 清除之前的结果
          graphic.remove();
 
          that.sgdGraphic.position = point;
          if (that.isStartPlay) {
            that.argAgain = true;
          }
        }
 
        callback(data);
      },
    });
  }
  //移除事故点
  removeSgd() {
    if (this.sgdGraphic) {
      this.sgdGraphic.remove();
      this.sgdGraphic = null;
    }
  }
 
  //事故模拟结束
  stopPlay() {
    if (!this.isStartPlay) {
      return;
    }
    //$("#btn_sgmn_start").html('开始模拟');
    clearInterval(this.interval);
    // this.isStartPlay = false;
  }
  //暂停模拟
  pausePlay() {
    //事故模拟暂停
    if (!this.isStartPlay) {
      return;
    }
    clearInterval(this.interval);
    //this.isStartPlay = false;
  }
  //添加事故模拟范围
  _addMnCicles(_time, _fs, _fx) {
    //_time时间(分钟),_fs 风速(m/s),_fx风向
    //此处加入公式,计算出半径
    var distance = (_time * 60 * _fs) / 1000; //公里
    var bearing = _fx;
    if (!this.incidentLnglat || this.incidentLnglat.length == 0) {
      return;
    }
    var point = turf.point([this.incidentLnglat[0], this.incidentLnglat[1]]);
    var newpoint = turf.destination(point, distance, bearing, {
      units: "kilometers",
    });
    this.newPoint = {
      x: newpoint.geometry.coordinates[0],
      y: newpoint.geometry.coordinates[1],
      z: this.incidentLnglat[2],
    };
 
    var radiusX = (this._radiusX = distance);
    var radiusY = distance * 0.6;
    var angle = 90 - _fx;
 
    //椭圆
    var entity = this.graphicLayer.dataSource.entities.add({
      position: Cesium.Cartesian3.fromDegrees(this.newPoint.x, this.newPoint.y, this.newPoint.z),
      ellipse: {
        show: true,
        semiMajorAxis: radiusX * 1000,
        semiMinorAxis: radiusY * 1000,
        rotation: Cesium.Math.toRadians(angle),
        classificationType: Cesium.ClassificationType.BOTH,
        fill: true,
        material: Cesium.Color.fromCssColorString("#ff0000").withAlpha(0.4),
        outline: true,
        outlineColor: Cesium.Color.fromCssColorString("#ffffff").withAlpha(0.7),
        outlineWidth: 2,
      },
    });
    this.ellipseArr.push(entity);
 
    this.map.flyTo(entity, {
      duration: 2,
    });
 
    this.pausePlay(); //暂停 此时暂停 用于计算范围内的点
    //计算椭圆焦点
    var temp = (radiusX - radiusY) / 2;
    var pt1 = turf.destination(point, temp, bearing, {
      units: "kilometers",
    });
    var pt2 = turf.destination(point, radiusX + radiusY + temp, bearing, {
      units: "kilometers",
    });
    var item = {
      center: this.newPoint, //中心点
      radiusX: radiusX * 1000, //长半轴(米)
      radiusY: radiusY * 1000, //短半轴(米)
      pt1: {
        x: pt1.geometry.coordinates[0],
        y: pt1.geometry.coordinates[1],
      }, //焦点1
      pt2: {
        x: pt2.geometry.coordinates[0],
        y: pt2.geometry.coordinates[1],
      }, //焦点2
    };
    this.pageNum = 1;
    this.radius = radiusX * 1000;
    this.oneQueryEnd = false;
 
    this.queryPOI({
      location: [this.newPoint.x, this.newPoint.y],
      radius: this.radius,
      page: this.pageNum,
    });
    //记录最后一个椭圆 用于查询结束后筛选
    this.lastEllipseData = item;
    this.queryYwData(item);
  }
  //查询
  queryPOI(opts) {
    var that = this;
    var arg = {};
    arg.location = opts.location;
    arg.radius = opts.radius;
    arg.count = 10; //count 每页数量
    arg.text = "企业";
    arg.page = opts.pageNum;
    arg.success = function (res) {
      that.oneQueryEnd = true;
      var data = res.list;
      that.queryData = [];
      that.queryData.concat(data);
      that.addEntity(data);
      if (that.bootstrapTableCallback) {
        that.bootstrapTableCallback(data);
      }
      that.start();
    };
    arg.error = function (msg) {
      window.toastr.error(msg);
    };
    this.gaoDePoi.queryCircle(arg);
  }
  //加载更多
  loadMore(callback) {
    this.isLoadMore = true;
    this.pageNum++;
    if (!this.newPoint || !this.pageNum) {
      return;
    }
    var that = this;
    var arg = {};
    arg.location = [this.newPoint.x, this.newPoint.y];
    arg.radius = this.radius;
    arg.count = 10;
    arg.text = "企业";
    arg.page = this.pageNum;
    arg.success = function (res) {
      var data = res.list;
      that.queryData.concat(data);
      that.addEntity(data);
      if (callback) {
        callback(data);
      }
    };
    arg.error = function (msg) {
      window.toastr.error(msg);
    };
    this.gaoDePoi.queryCircle(arg);
  }
  //添加事故范围内受影响的点
  addEntity(data) {
    if (!this.isLoadMore) {
      this.clearPointArr(); //椭圆变化时 清除上一次的结果 , 点击加载更多则不清除
      this.affectedPointArr = []; //清空之前的数据
    }
    for (var i = 0; i < data.length; i++) {
      var item = data[i];
      //==================构建图上目标单击后显示div=================
      var inHtml = '<div class="mars-popup-titile">' + item.name + '</div><div class="mars-popup-content" >';
      var type = $.trim(item.type);
      if (type != "") {
        inHtml += "<div><label>类别</label>" + type + "</div>";
      }
      var xzqh = $.trim(item.xzqh);
      if (xzqh != "") {
        inHtml += "<div><label>区域</label>" + xzqh + "</div>";
      }
      var tel = $.trim(item.tel);
      if (tel != "") {
        inHtml += "<div><label>电话</label>" + tel + "</div>";
      }
      var address = $.trim(item.address);
      if (address != "") {
        inHtml += "<div><label>地址</label>" + address + "</div>";
      }
      inHtml += "</div>";
 
      var entity = this.graphicLayer.dataSource.entities.add({
        position: Cesium.Cartesian3.fromDegrees(item.x, item.y),
        point: {
          color: Cesium.Color.fromCssColorString("#3388ff"),
          pixelSize: 10,
          outlineColor: Cesium.Color.fromCssColorString("#ffffff"),
          outlineWidth: 2,
          heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, //贴地
          scaleByDistance: new Cesium.NearFarScalar(1000, 1, 1000000, 0.1),
        },
        label: {
          text: item.name,
          font: "20px 楷体",
          style: Cesium.LabelStyle.FILL_AND_OUTLINE,
          fillColor: Cesium.Color.AZURE,
          outlineColor: Cesium.Color.BLACK,
          outlineWidth: 2,
          horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          pixelOffset: new Cesium.Cartesian2(0, -10), //偏移量
          heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND, //是地形上方的高度
          distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000),
        },
        popup: inHtml,
      });
      this.affectedPointArr.push({
        data: item,
        lnglat: [item.x, item.y],
        entity: entity,
      });
      item._entity = entity;
    }
  }
  clearPointArr() {
    if (!this.affectedPointArr || this.affectedPointArr.length == 0) {
      return;
    }
    for (var i = 0; i < this.affectedPointArr.length; i++) {
      var item = this.affectedPointArr[i];
      if (item.entity) {
        this.graphicLayer.dataSource.entities.remove(item.entity);
      }
    }
    this.affectedPointArr = [];
  }
 
  //添加模拟企业点
  queryYwData(params) {
    var arrdata = [];
    for (var j = 0; j < 100; j++) {
      var jd = params.center.x + 0.1 * Math.random() * (Math.random() > 0.5 ? 1 : -1);
      var wd = params.center.y + 0.1 * Math.random() * (Math.random() > 0.5 ? 1 : -1);
 
      var attr = {
        id: j,
        name: "企业" + j,
        x: jd,
        y: wd,
      };
      arrdata.push(attr);
    }
    this.showYwResult(params, arrdata);
  }
  showYwResult(params, arrdata) {
    this.clearPointYWArr();
    var that = this;
    var pt1 = Cesium.Cartesian3.fromDegrees(params.pt1.x, params.pt1.y);
    var pt2 = Cesium.Cartesian3.fromDegrees(params.pt2.x, params.pt2.y);
    var len = params.radiusX * 2;
 
    $.each(arrdata, function (index, item) {
      var position = Cesium.Cartesian3.fromDegrees(item.x, item.y);
      var len1 = Cesium.Cartesian3.distance(position, pt1);
      var len2 = Cesium.Cartesian3.distance(position, pt2);
      if (len1 + len2 > len) {
        //不在椭圆内
        return;
      }
      var entity = that.graphicLayer.dataSource.entities.add({
        position: position,
        point: {
          color: Cesium.Color.YELLOW,
          pixelSize: 6,
          outlineColor: Cesium.Color.fromCssColorString("#ffffff"),
          outlineWidth: 2,
          heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        },
        label: {
          text: item.name,
          font: "12px Helvetica",
          style: Cesium.LabelStyle.FILL_AND_OUTLINE,
          fillColor: Cesium.Color.AZURE,
          outlineColor: Cesium.Color.BLACK,
          outlineWidth: 2,
          horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          pixelOffset: new Cesium.Cartesian2(0, -10), //偏移量
          heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
        },
        popup: item.name,
        data: item,
      });
      entity.data = item;
      that.affectedYWArr.push({
        data: item,
        lnglat: [item.x, item.y],
        entity: entity,
      });
    });
  }
  clearPointYWArr() {
    if (!this.affectedYWArr || this.affectedYWArr.length == 0) {
      return;
    }
    for (var i = 0; i < this.affectedYWArr.length; i++) {
      var item = this.affectedYWArr[i];
      if (item.entity) {
        this.graphicLayer.dataSource.entities.remove(item.entity);
      }
    }
    this.affectedYWArr = [];
  }
  //返回上一步时 清除对应的地图上结果
  clearRes() {
    this.clearPointArr();
    this.clearPointYWArr();
    for (var i = 0; i < this.ellipseArr.length; i++) {
      this.graphicLayer.dataSource.entities.remove(this.ellipseArr[i]);
    }
    this.ellipseArr = [];
    clearInterval(this.interval);
    this.interval = -1;
    this.iNow = 0;
    this.iALLTime = 60;
    this.iALL = 6;
    this.isStartPlay = false;
    this.windArg = null;
    this.affectedPointArr = [];
    this.timelineCallback = null;
    this.bootstrapTableCallback = null;
    this.pageNum = 1;
    this.newPoint = null;
    this._radiusX = null;
    this.lastEllipseData = null;
    this.isLoadMore = false;
    this.queryData = [];
    this.stopDraw();
    this.queryData = []; //当前查询的结果
    this.oneQueryEnd = false; //标识一次查询结束
    this._setArgAgain = false;
  }
  //移除绑定
  stopDraw() {
    if (this.graphicLayer) {
      this.graphicLayer.stopDraw();
    }
  }
  //清除所有效果
  clearAll() {
    this.argAgain = false;
    this.clearRes();
    this.removeSgd();
    this.incidentLnglat = [];
    this.graphicLayer.clear();
  }
}