HeightLimit.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* 引入Cesium */
  2. // import * as Cesium from 'Cesium';
  3. /* 引入算法 */
  4. import * as turf from "@turf/turf";
  5. /**
  6. * 限高分析
  7. */
  8. class HeightLimit {
  9. /**
  10. * 默认初始化
  11. * @param {Object} viewer 三维场景
  12. */
  13. constructor(viewer) {
  14. if (!viewer) throw new Cesium.DeveloperError('no viewer object!');
  15. this._viewer = viewer;
  16. this.targetY = 0;
  17. }
  18. /**
  19. * 这里主要是为了添加一个高出地面的 polygon , 用到的经纬度数据和需要校验的建筑物数据一样
  20. * @ignore 忽略注释,注释不生成Doc
  21. * @param {Object} options
  22. */
  23. _setPolygon(options) {
  24. let _self = this;
  25. _self.HiePolygon = _self._viewer.entities.add({
  26. polygon: {
  27. hierarchy: new Cesium.PolygonHierarchy(_self.data),
  28. material: _self._handleColor('#FFF8DC', options.alpha),
  29. height: new Cesium.CallbackProperty(_self._createPolygonUpdateFunction(), false),
  30. perPositionHeight: false,
  31. outline: true,
  32. outlineColor: _self._handleColor('red', options.alpha),
  33. outlineWidth: 1.0,
  34. }
  35. });
  36. }
  37. /**
  38. * @ignore 忽略注释,注释不生成Doc
  39. */
  40. _createPolygonUpdateFunction() {
  41. let _self = this;
  42. return function() {
  43. return _self.targetY;
  44. };
  45. }
  46. /**
  47. * @ignore 忽略注释,注释不生成Doc
  48. * @param {Object} color
  49. * @param {Object} alpha
  50. */
  51. _handleColor(color, alpha) {
  52. return Cesium.Color.fromCssColorString(color).withAlpha(alpha || 1);
  53. }
  54. /**
  55. * @ignore 忽略注释,注释不生成Doc
  56. * 获取最小高度
  57. */
  58. _getMinHeight(points) {
  59. let height = 0;
  60. let positions = points.map(point => {
  61. let _height = point[2] || 0;
  62. height = _height;
  63. if (_height < height) {
  64. height = _height;
  65. }
  66. });
  67. return height;
  68. }
  69. }
  70. /**
  71. * 通用对外公开函数
  72. */
  73. Object.assign(HeightLimit.prototype, /** @lends HeightLimit.prototype */ {
  74. /**
  75. * 绘制限高区域
  76. */
  77. addPrimitive(points, options) {
  78. this.removePrimitive();
  79. options = options || {};
  80. options.height = Cesium.defaultValue(options.height, 50);
  81. //图上选点获取最小高度
  82. options.baseHeight = Cesium.defaultValue(options.baseHeight, 50);
  83. // //根据坐标获取最小高度
  84. // options.baseHeight = this._getMinHeight(points);
  85. options.color = Cesium.defaultValue(options.color, 'red');
  86. options.alpha = Cesium.defaultValue(options.alpha, 0.6);
  87. /* 转换坐标 */
  88. let positions = points.map(point => {
  89. return Cesium.Cartesian3.fromDegrees(point[0], point[1], point[2] || 0);
  90. });
  91. let _self = this;
  92. _self.data = positions;
  93. _self.baseHeight = options.baseHeight;
  94. _self.targetY = options.baseHeight + options.height;
  95. let polygonInstance = new Cesium.GeometryInstance({
  96. geometry: new Cesium.PolygonGeometry({
  97. polygonHierarchy: new Cesium.PolygonHierarchy(_self.data),
  98. height: _self.baseHeight, //分层底部海拔
  99. extrudedHeight: _self.baseHeight + 3000, //分层顶部海拔
  100. }),
  101. attributes: {
  102. color: Cesium.ColorGeometryInstanceAttribute.fromColor(
  103. Cesium.Color.fromCssColorString(options.color).withAlpha(options.alpha)
  104. ), //设置高亮颜色
  105. show: new Cesium.ShowGeometryInstanceAttribute(true), //设置初始化后是否显示
  106. }
  107. });
  108. _self.limitHeightPrimitive = _self._viewer.scene.primitives.add(
  109. new Cesium.ClassificationPrimitive({ //ClassificationPrimitive 用来生成可以穿透
  110. geometryInstances: polygonInstance,
  111. releaseGeometryInstances: false,
  112. classificationType: Cesium.ClassificationType.CESIUM_3D_TILE, //只绘制在3dtiles上
  113. })
  114. );
  115. // 这里主要是为了添加一个高出地面的 polygon , 用到的经纬度数据和需要校验的建筑物数据一样
  116. _self._setPolygon(options)
  117. },
  118. /**
  119. * 移除限高分析
  120. */
  121. removePrimitive() {
  122. if (this.HiePolygon) {
  123. this._viewer.entities.remove(this.HiePolygon); //移除图层
  124. }
  125. if (this.limitHeightPrimitive) {
  126. this._viewer.scene.primitives.remove(this.limitHeightPrimitive); //移除图层
  127. }
  128. },
  129. /**
  130. * 高度变化
  131. * @param {Object} height
  132. */
  133. changeHeight(height) {
  134. if (!this.limitHeightPrimitive) return;
  135. // this._height = height - this.baseHeight;
  136. this.targetY = this.baseHeight + height;
  137. let cartographic = Cesium.Cartographic.fromCartesian(this.limitHeightPrimitive._primitive._boundingSpheres[0].center);
  138. let surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, this.baseHeight);
  139. let offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, this.baseHeight + height);
  140. let translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
  141. this.limitHeightPrimitive._primitive.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
  142. },
  143. });
  144. export default HeightLimit;