PolygonObject.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* 引入Cesium */
  2. // import * as Cesium from 'Cesium';
  3. import {
  4. setSessionid
  5. } from "./common/common.js";
  6. /**
  7. * 面对象
  8. */
  9. class PolygonObject {
  10. /**
  11. * 默认初始化
  12. */
  13. constructor(viewer) {
  14. if (!viewer) throw new Cesium.DeveloperError('no viewer object!');
  15. this._viewer = viewer;
  16. this._drawEntities = []; //存储entities
  17. this._polygonEntity = null;
  18. }
  19. }
  20. /**
  21. * 通用对外公开函数
  22. */
  23. Object.assign(PolygonObject.prototype, /** @lends PolygonObject.prototype */ {
  24. /**
  25. * @description 根据GeoJson绘制面
  26. * @param {Object} geoJsonUrl geoJson文件路径
  27. * @param {Object} [options] 面的样式,具有以下属性:
  28. * @param {Number} [options.id] 用于移除
  29. * @param {Number} [options.clampToGround=true] 是否贴地
  30. * @param {Number} [options.pixelSize=10] 指定点的大小,以像素为单位
  31. * @param {String} [options.color="#FF0000"] 指定点、线、面的颜色
  32. * @param {String} [options.outlineColor="#FFFF00"] 指定点轮廓的颜色
  33. * @param {Number} [options.outlineWidth=0] 指定点轮廓的宽度
  34. */
  35. drawPolygonByGeoJson(geoJsonUrl, options) {
  36. return new Promise((resolve, reject) => {
  37. let _self = this;
  38. if (!Cesium.defined(geoJsonUrl)) {
  39. throw new Cesium.DeveloperError("geoJsonUrl is required.");
  40. }
  41. options = options || {};
  42. options.id = options.id || setSessionid();
  43. options.clampToGround = Cesium.defaultValue(options.clampToGround, true);
  44. options.color = Cesium.defaultValue(options.color, "#FF0000");
  45. options.outlineColor = Cesium.defaultValue(options.outlineColor, "#FFFF00");
  46. options.outlineWidth = Cesium.defaultValue(options.outlineWidth, 1);
  47. options.extrudedHeight = Cesium.defaultValue(options.extrudedHeight, 0);
  48. let promise = Cesium.GeoJsonDataSource.load(geoJsonUrl, {
  49. clampToGround: true,
  50. stroke: Cesium.Color.WHITE, // 边框颜色
  51. strokeWidth: 3, // 边框宽度
  52. fill: Cesium.Color.RED.withAlpha(0.5), // 填充颜色
  53. });
  54. promise.then((dataSource) => {
  55. _self._viewer.dataSources.add(dataSource); // 加载整个geojson资源
  56. dataSource.name = options.id
  57. let entities = dataSource.entities.values;
  58. for (let index = 0; index < entities.length; index++) {
  59. let entity = entities[index];
  60. entity.polygon.material = new Cesium.Color.fromCssColorString(color).withAlpha(0.1);
  61. entity.polygon.extrudedHeight = options.extrudedHeight; //拉伸高度,单位是米
  62. entity.polygon.fill = false;
  63. entity.polygon.outline = false;
  64. entity.polygon.outlineWidth = options.outlineWidth;
  65. entity.polygon.outlineColor = options.outlineColor;
  66. entity.polyline = {
  67. positions: entity.polygon.hierarchy._value.positions,
  68. width: entity.polygon.outlineWidth,
  69. material: new Cesium.Color.fromCssColorString(color).withAlpha(0.1),
  70. // material: new Cesium.PolylineDashMaterialProperty({
  71. // color: new Cesium.Color.fromCssColorString(color).withAlpha(0.1),
  72. // }),//虚线
  73. }
  74. if (options.clampToGround) {
  75. entity.polyline.clampToGround = true;
  76. }
  77. }
  78. resolve(entities);
  79. });
  80. });
  81. },
  82. /**
  83. * 根据坐标点生成面
  84. * @param {Object} [points] 坐标
  85. * @param {Object} [options] 面的样式,具有以下属性:
  86. * @param {Array} [options.color=[255,255,255,0]] 面颜色,颜色数组,[0~255,0~255,0~255,0~1],[red 红色,green 绿色,blue 蓝色,alpha 透明度]
  87. */
  88. generatePolygonByPoints(points, options) {
  89. //异步函数
  90. return new Promise((resolve, reject) => {
  91. let _self = this;
  92. if (!Cesium.defined(points)) {
  93. throw new Cesium.DeveloperError("points is required.");
  94. }
  95. if (points.length < 3) {
  96. reject("面对象,点数至少3个");
  97. }
  98. /* 转换坐标 */
  99. let positions = points.map(point => {
  100. return Cesium.Cartesian3.fromDegrees(point[0], point[1], point[2] || 0);
  101. });
  102. options = options || {};
  103. options.id = options.id || setSessionid();
  104. options.fill = options.fill || true;
  105. //面颜色
  106. if (options.color) {
  107. options.color = new Cesium.Color(options.color[0]/255, options.color[1]/255, options.color[2]/255, options.color[3]);
  108. } else {
  109. options.color = new Cesium.Color.fromCssColorString("#ff0000");
  110. }
  111. options.outline = options.outline || false;
  112. //轮廓颜色
  113. if (options.outlineColor) {
  114. options.outlineColor = new Cesium.Color(options.outlineColor[0]/255, options.outlineColor[1]/255, options.outlineColor[2]/255, options.outlineColor[3]);
  115. } else {
  116. options.outlineColor = new Cesium.Color.fromCssColorString("#000");
  117. }
  118. //轮廓宽度
  119. options.outlineWidth = Cesium.defaultValue(options.outlineWidth, 1);
  120. // let entity = _self._viewer.entities.add({
  121. // id: options.id,
  122. // name: "Generate surfaces based on coordinate points",
  123. // polygon: {
  124. // hierarchy: positions,
  125. // material: options.color,
  126. // fill:false,
  127. // outline: true,
  128. // outlineWidth:5,
  129. // outlineColor:new Cesium.Color.fromCssColorString("#ddd").withAlpha(0.1)
  130. // }
  131. // });
  132. let entity = new Cesium.Entity({
  133. id: options.id,
  134. name: "Generate surfaces based on coordinate points",
  135. polygon: {
  136. hierarchy: positions,
  137. material: options.color,
  138. fill: options.fill,
  139. outline: options.outline,
  140. outlineWidth: options.outlineWidth,
  141. outlineColor: options.outlineColor
  142. }
  143. });
  144. if (options.outline) {
  145. entity.polyline = {
  146. positions: entity.polygon.hierarchy._value.positions,
  147. width: options.outlineWidth,
  148. material: options.outlineColor,
  149. // material: new Cesium.PolylineDashMaterialProperty({
  150. // color: options.outlineColor,
  151. // }),//虚线
  152. clampToGround: true
  153. }
  154. }
  155. _self._viewer.entities.add(entity);
  156. resolve(entity);
  157. })
  158. },
  159. });
  160. export default PolygonObject;