DrawSector.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // DrawSector
  2. /*
  3. 九、绘制扇形
  4. */
  5. class DrawSector {
  6. constructor(arg) {
  7. this.viewer = arg.viewer;
  8. this.Cesium = arg.Cesium;
  9. this.floatingPoint = null;//标识点
  10. this._sector = null; //活动扇形
  11. this._sectorLast = null; //最后一个扇形
  12. this._positions = []; //活动点
  13. this._entities_point = []; //脏数据
  14. this._entities_sector = []; //脏数据
  15. this._sectorData = null; //用于构造扇形数据
  16. this.DrawStartEvent = new Cesium.Event(); //开始绘制事件
  17. this.DrawEndEvent = new Cesium.Event(); //结束绘制事件
  18. /* 通用参数集合 */
  19. this._param = {
  20. id: "DrawStraightArrow",
  21. polygonColor: 'rgba(0,255,0,0.5)', //面填充颜色
  22. outlineColor: 'rgba(255, 255, 255, 1)', //边框颜色
  23. outlineWidth: 1, //边框宽度
  24. }
  25. /* 创建面材质 */
  26. this.polygonMaterial = Cesium.Color.fromCssColorString(this._param.polygonColor);
  27. /* 创建线材质 */
  28. // this.outlineMaterial = new Cesium.PolylineDashMaterialProperty({//曲线
  29. // dashLength: 16,
  30. // color: Cesium.Color.fromCssColorString(this._param.outlineColor)
  31. // });
  32. this.outlineMaterial = Cesium.Color.fromCssColorString(this._param.outlineColor);
  33. }
  34. //返回扇形
  35. get sector() {
  36. return this._sectorLast;
  37. }
  38. //返回扇形数据用于加载扇形
  39. getData() {
  40. return this._sectorData;
  41. }
  42. // 修改编辑调用计算
  43. computePosition(data){
  44. let $this = this;
  45. let pnts = data;
  46. let center = $this.lonLatToMercator($this.cartesianToLatlng(pnts[0]));
  47. let pnt2 = $this.lonLatToMercator($this.cartesianToLatlng(pnts[1]));
  48. let pnt3 = $this.lonLatToMercator($this.cartesianToLatlng(pnts[2]));
  49. var radius = $this.MathDistance(pnt2, center);
  50. var startAngle = $this.getAzimuth(pnt2, center);
  51. var endAngle = $this.getAzimuth(pnt3, center);
  52. var pList = $this.getArcPoints(center, radius, startAngle, endAngle);
  53. pList.push(pnts[0], pList[0]);
  54. let arrow = [];
  55. for (var i = 0; i < pList.length; i++) {
  56. var cart3 = new $this.Cesium.Cartesian3(pList[i].x, pList[i].y, pList[i].z);
  57. arrow.push(cart3);
  58. }
  59. var lnglatArr = [];
  60. for (var d = 0; d < data.length; d++){
  61. lnglatArr.push($this.cartesianToLatlng(data[d]))
  62. }
  63. $this._sectorData = lnglatArr;
  64. return new $this.Cesium.PolygonHierarchy(arrow)
  65. }
  66. //加载扇形
  67. addload(data) {
  68. var $this = this;
  69. let pnts = data;
  70. let center = $this.lonLatToMercator(pnts[0]);
  71. let pnt2 = $this.lonLatToMercator(pnts[1]);
  72. let pnt3 = $this.lonLatToMercator(pnts[2]);
  73. var radius = $this.MathDistance(pnt2, center);
  74. var startAngle = $this.getAzimuth(pnt2, center);
  75. var endAngle = $this.getAzimuth(pnt3, center);
  76. var pList = $this.getArcPoints(center, radius, startAngle, endAngle);
  77. let pntsc = $this.Cesium.Cartesian3.fromDegrees(pnts[0][0], pnts[0][1])
  78. pList.push(pntsc, pList[0]);
  79. let arrow = [];
  80. for (var i = 0; i < pList.length; i++) {
  81. var cart3 = new $this.Cesium.Cartesian3(pList[i].x, pList[i].y, pList[i].z);
  82. arrow.push(cart3);
  83. }
  84. var arrowEntity = $this.viewer.entities.add({
  85. Type: 'DrawSector',
  86. Position: data,
  87. id: data.id || $this.objId,
  88. polygon: {
  89. hierarchy: new $this.Cesium.PolygonHierarchy(arrow),
  90. show: true,
  91. fill: true,
  92. clampToGround: true,
  93. material: $this.Cesium.Color.AQUA.withAlpha(0.9)
  94. }
  95. });
  96. return arrowEntity;
  97. }
  98. //开始创建
  99. startCreate() {
  100. var $this = this;
  101. this.handler = new this.Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
  102. this.handler.setInputAction(function (evt) { //单机开始绘制
  103. //屏幕坐标转地形上坐标
  104. var cartesian = $this.getCatesian3FromPX(evt.position);
  105. // if($this._positions.length == 3) return
  106. if ($this._positions.length < 3) {
  107. $this.floatingPoint = $this.createPoint(cartesian);
  108. $this._positions.push(cartesian);
  109. }
  110. if (!$this._sector) {
  111. // $this.createPoint(cartesian);// 绘制点
  112. }
  113. }, $this.Cesium.ScreenSpaceEventType.LEFT_CLICK);
  114. this.handler.setInputAction(function (evt) { //移动时绘制面
  115. if ($this._positions.length < 2) return;
  116. var cartesian = $this.getCatesian3FromPX(evt.endPosition);
  117. if ($this._positions.length == 2) {
  118. $this._positions.push(cartesian);
  119. }
  120. if ($this._positions.length == 3) {
  121. $this._positions.pop();
  122. $this._positions.push(cartesian);
  123. if (!$this.Cesium.defined($this._sector)) {
  124. $this._sector = $this.createsector();
  125. }
  126. }
  127. }, $this.Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  128. this.handler.setInputAction(function (evt) {
  129. if (!$this._sector) return;
  130. var cartesian = $this.getCatesian3FromPX(evt.position);
  131. $this._positions.pop();
  132. $this._positions.push(cartesian);
  133. $this._sectorData = $this._positions.concat();
  134. $this.viewer.entities.remove($this._sector); //移除
  135. $this._sector = null;
  136. $this._positions = [];
  137. $this.floatingPoint.position.setValue(cartesian);
  138. var lnglatArr = [];
  139. for (var i = 0; i < $this._sectorData.length; i++) {
  140. var lnglat = $this.cartesianToLatlng($this._sectorData[i]);
  141. lnglatArr.push(lnglat)
  142. }
  143. $this._sectorData = lnglatArr;
  144. var sector = $this.addload(lnglatArr); //加载
  145. $this._entities_sector.push(sector);
  146. $this._sectorLast = sector;
  147. $this.clearPoint();
  148. $this.destroy()
  149. }, $this.Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  150. }
  151. //创建直线扇形
  152. createsector() {
  153. // console.log(this._positions)
  154. var $this = this;
  155. var arrowEntity = $this.viewer.entities.add({
  156. polygon: {
  157. hierarchy: new $this.Cesium.CallbackProperty(
  158. function () {
  159. // if(this._positions.length < 3) return
  160. let pnts = $this._positions;
  161. let center = $this.lonLatToMercator($this.cartesianToLatlng(pnts[0]));
  162. let pnt2 = $this.lonLatToMercator($this.cartesianToLatlng(pnts[1]));
  163. let pnt3 = $this.lonLatToMercator($this.cartesianToLatlng(pnts[2]));
  164. var radius = $this.MathDistance(pnt2, center);
  165. var startAngle = $this.getAzimuth(pnt2, center);
  166. var endAngle = $this.getAzimuth(pnt3, center);
  167. var pList = $this.getArcPoints(center, radius, startAngle, endAngle);
  168. pList.push(pnts[0], pList[0]);
  169. let arrow = [];
  170. for (var i = 0; i < pList.length; i++) {
  171. var cart3 = new $this.Cesium.Cartesian3(pList[i].x, pList[i].y, pList[i].z);
  172. arrow.push(cart3);
  173. }
  174. return new $this.Cesium.PolygonHierarchy(pList);
  175. }, false),
  176. show: true,
  177. fill: true,
  178. clampToGround: true,
  179. material: $this.Cesium.Color.AQUA.withAlpha(0.5)
  180. }
  181. }
  182. )
  183. $this._entities_sector.push(arrowEntity);
  184. return arrowEntity
  185. }
  186. //创建点
  187. createPoint(cartesian) {
  188. var $this = this;
  189. var point = this.viewer.entities.add({
  190. position: cartesian,
  191. point: {
  192. pixelSize: 10,
  193. color: $this.Cesium.Color.RED,
  194. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
  195. }
  196. });
  197. $this._entities_point.push(point);
  198. return point;
  199. }
  200. cartesianToLatlng(cartesian) {
  201. var latlng = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
  202. var lat = this.Cesium.Math.toDegrees(latlng.latitude);
  203. var lng = this.Cesium.Math.toDegrees(latlng.longitude);
  204. return [lng, lat];
  205. }
  206. /**
  207. * 经纬度坐标转墨卡托坐标
  208. */
  209. // 墨卡托坐标系:展开地球,赤道作为x轴,向东为x轴正方,本初子午线作为y轴,向北为y轴正方向。
  210. // 数字20037508.34是地球赤道周长的一半:地球半径6378137米,赤道周长2*PI*r = 2 * 20037508.3427892,墨卡托坐标x轴区间[-20037508.3427892,20037508.3427892]
  211. lonLatToMercator(Latlng) {
  212. var E = Latlng[0];
  213. var N = Latlng[1];
  214. var x = E * 20037508.34 / 180;
  215. var y = Math.log(Math.tan((90 + N) * Math.PI / 360)) / (Math.PI / 180);
  216. y = y * 20037508.34 / 180;
  217. return [x, y]
  218. }
  219. WebMercator2lonLat(mercator) {
  220. let x = mercator[0] / 20037508.34 * 180;
  221. let ly = mercator[1] / 20037508.34 * 180;
  222. let y = 180 / Math.PI * (2 * Math.atan(Math.exp(ly * Math.PI / 180)) - Math.PI / 2)
  223. return [x, y];
  224. }
  225. //销毁
  226. destroy() {
  227. if (this.handler) {
  228. this.handler.destroy();
  229. this.handler = null;
  230. }
  231. }
  232. clearPoint() {
  233. this.DrawEndEvent.raiseEvent(this._sectorLast, this._sectorData);
  234. for (var i = 0; i < this._entities_point.length; i++) {
  235. this.viewer.entities.remove(this._entities_point[i]);
  236. }
  237. this._entities_point = []; //脏数据
  238. }
  239. //清空实体对象
  240. clear() {
  241. for (var i = 0; i < this._entities_point.length; i++) {
  242. this.viewer.entities.remove(this._entities_point[i]);
  243. }
  244. for (var i = 0; i < this._entities_sector.length; i++) {
  245. this.viewer.entities.remove(this._entities_sector[i]);
  246. }
  247. this.floatingPoint = null;//标识点
  248. this._sector = null; //活动扇形
  249. this._sectorLast = null; //最后一个扇形
  250. this._positions = []; //活动点
  251. this._entities_point = []; //脏数据
  252. this._entities_sector = []; //脏数据
  253. this._sectorData = null; //用于构造扇形数据
  254. }
  255. getCatesian3FromPX(px) {
  256. var cartesian;
  257. var ray = this.viewer.camera.getPickRay(px);
  258. if (!ray) return null;
  259. cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
  260. return cartesian;
  261. }
  262. // 求取扇形坐标函数/
  263. //扇形配置函数
  264. MathDistance(pnt1, pnt2) {
  265. return Math.sqrt(Math.pow(pnt1[0] - pnt2[0], 2) + Math.pow(pnt1[1] - pnt2[1], 2));
  266. }
  267. getAzimuth(startPoint, endPoint) {
  268. var azimuth;
  269. var angle = Math.asin(Math.abs(endPoint[1] - startPoint[1]) / this.MathDistance(startPoint, endPoint));
  270. if (endPoint[1] >= startPoint[1] && endPoint[0] >= startPoint[0]) {
  271. azimuth = angle + Math.PI;
  272. } else if (endPoint[1] >= startPoint[1] && endPoint[0] < startPoint[0]) {
  273. azimuth = Math.PI * 2 - angle;
  274. } else if (endPoint[1] < startPoint[1] && endPoint[0] < startPoint[0]) {
  275. azimuth = angle;
  276. } else if (endPoint[1] < startPoint[1] && endPoint[0] >= startPoint[0]) {
  277. azimuth = Math.PI - angle;
  278. }
  279. return azimuth;
  280. }
  281. getArcPoints(center, radius, startAngle, endAngle) {
  282. var x = null,
  283. y = null,
  284. z = null,
  285. pnts = [],
  286. angleDiff = endAngle - startAngle;
  287. angleDiff = angleDiff < 0 ? angleDiff + Math.PI * 2 : angleDiff;
  288. for (var i = 0; i <= 100; i++) {
  289. var angle = startAngle + angleDiff * i / 100;
  290. x = center[0] + radius * Math.cos(angle);
  291. y = center[1] + radius * Math.sin(angle);
  292. let latlon = this.WebMercator2lonLat([x, y])
  293. pnts.push(this.Cesium.Cartesian3.fromDegrees(latlon[0], latlon[1]));
  294. }
  295. return pnts;
  296. }
  297. }
  298. export default DrawSector