PolylineObject.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* 引入Cesium */
  2. // import * as Cesium from 'Cesium';
  3. import {
  4. setSessionid
  5. } from "./common/common.js";
  6. /**
  7. *流动纹理线
  8. */
  9. import PolylineDirectionMaterialProperty from "./PolylineObject/PolylineDirectionMaterialProperty.js";
  10. /**
  11. * 线对象
  12. */
  13. class PolylineObject {
  14. /**
  15. * 默认初始化
  16. */
  17. constructor(viewer) {
  18. if (!viewer) throw new Cesium.DeveloperError('no viewer object!');
  19. this._viewer = viewer;
  20. }
  21. }
  22. /**
  23. * 通用对外公开函数
  24. */
  25. Object.assign(PolylineObject.prototype, /** @lends PolylineObject.prototype */ {
  26. /**
  27. * @description 根据GeoJson绘制线
  28. * @param {String} geoJsonUrl geoJson文件路径
  29. * @param {Object} [options] 线的样式,具有以下属性:
  30. * @param {Number} [options.id] 用于移除
  31. * @param {Number} [options.clampToGround=true] 是否贴地
  32. * @param {Number} [options.isImageAlpha=true] 是否采用图片颜色
  33. * @param {Number} [options.imgUrl] 精灵线图片
  34. * @param {String} [options.color="#FF0000"] 指定线的颜色
  35. * @param {Number} [options.width=3] 指定线的宽度,以像素为单位
  36. * @param {Number} [options.minHeigh=0]
  37. * @param {Number} [options.maxHeigh=200000000]
  38. * @param {Number} [options.duration=3000] 持续时间 毫秒,越小越快
  39. * @param {Number} [options.count] 重复次数
  40. * @param {String} [options.direction='horizontal'] 方向 vertical纵,垂直方向,horizontal横,水平方向
  41. * @param {String} [options.order] 方向正负
  42. * vertical 纵:'-'(由下到上) , '+"(由上到下)
  43. * horizontal 横:'-'(顺时针) , '+'(逆时针)
  44. */
  45. drawPolylineByGeoJson: function(geoJsonUrl, options) {
  46. return new Promise((resolve, reject) => {
  47. let _self = this;
  48. let viewer = this._viewer;
  49. if (!Cesium.defined(geoJsonUrl)) {
  50. throw new Cesium.DeveloperError("geoJsonUrl is required.");
  51. }
  52. options = options || {};
  53. options.id = options.id || setSessionid();
  54. options.clampToGround = Cesium.defaultValue(options.clampToGround, true);
  55. options.width = Cesium.defaultValue(options.width, 3);
  56. options.minHeigh = Cesium.defaultValue(options.minHeigh, 0);
  57. options.maxHeigh = Cesium.defaultValue(options.maxHeigh, 200000000);
  58. let promise = Cesium.GeoJsonDataSource.load(geoJsonUrl, {
  59. clampToGround: options.clampToGround
  60. });
  61. promise.then((dataSource) => {
  62. viewer.dataSources.add(dataSource); // 加载这个geojson资源
  63. dataSource.name = options.id
  64. let entities = dataSource.entities.values;
  65. let distanceDisplayCondition = new Cesium.DistanceDisplayCondition(options.minHeigh, options.maxHeigh);
  66. let material = new PolylineDirectionMaterialProperty(options);
  67. for (var i = 0; i < entities.length; i++) {
  68. var entity = entities[i];
  69. entity.polyline.distanceDisplayCondition = distanceDisplayCondition;
  70. entity.polyline.material = material;
  71. entity.polyline.width = options.width;
  72. if (options.clampToGround) {
  73. entity.polyline.clampToGround = true;
  74. }
  75. }
  76. resolve(entities);
  77. });
  78. });
  79. },
  80. /**
  81. * 脉冲线
  82. * @param {Object} points 坐标位置[[lng,lat],[lng,lat],,,,,]经度,以度为单位,纬度,以度为单位
  83. * @param {Object} options
  84. * @param {Number} [options.id] 指定实体对象的id
  85. * @param {Number} [options.width=5] 指定线的宽度,以像素为单位
  86. */
  87. PolylineLinkPulseMaterialProperty: function(points, options) {
  88. return new Promise((resolve, reject) => {
  89. if (!Cesium.defined(points)) {
  90. throw new Cesium.DeveloperError("points is required.");
  91. }
  92. if (points.length < 2) {
  93. reject("线对象,点数至少2个");
  94. }
  95. /* 转换坐标 */
  96. let positions = points.map(point => {
  97. return Cesium.Cartesian3.fromDegrees(point[0], point[1], point[2] || 0);
  98. });
  99. options = options || {};
  100. options.id = options.id || setSessionid();
  101. options.width = options.width || 5;
  102. let material = new PolylineDirectionMaterialProperty(options);
  103. let entity = this._viewer.entities.add({
  104. id: options.id,
  105. name: "Pulse line",
  106. polyline: {
  107. positions: positions,
  108. width: options.width,
  109. material: material,
  110. clampToGround: true, //开启贴地 如果有模型则贴模型
  111. }
  112. });
  113. resolve(entity);
  114. });
  115. },
  116. /**
  117. * 箭头线
  118. * @param {Object} points 坐标位置[[lng,lat],[lng,lat],,,,,]经度,以度为单位,纬度,以度为单位
  119. * @param {Object} options
  120. * @param {Number} [options.id] 指定实体对象的id
  121. * @param {Number} [options.width=5] 指定线的宽度,以像素为单位
  122. */
  123. PolylineArrowMaterialProperty: function(points, options) {
  124. return new Promise((resolve, reject) => {
  125. if (!Cesium.defined(points)) {
  126. throw new Cesium.DeveloperError("points is required.");
  127. }
  128. if (points.length < 2) {
  129. reject("线对象,点数至少2个");
  130. }
  131. /* 转换坐标 */
  132. let positions = points.map(point => {
  133. return Cesium.Cartesian3.fromDegrees(point[0], point[1], point[2] || 0);
  134. });
  135. options = options || {};
  136. options.id = options.id || setSessionid();
  137. options.width = options.width || 5;
  138. let material = new PolylineDirectionMaterialProperty(options);
  139. let entity = this._viewer.entities.add({
  140. id: options.id,
  141. name: "Pulse line",
  142. polyline: {
  143. positions: positions,
  144. width: options.width,
  145. material: material,
  146. clampToGround: true, //开启贴地 如果有模型则贴模型
  147. }
  148. });
  149. resolve(entity);
  150. });
  151. },
  152. /**
  153. * 绘制发光的线
  154. * @param {Object} points 坐标位置[[lng,lat],[lng,lat],,,,,]经度,以度为单位,纬度,以度为单位
  155. * @param {Object} options
  156. * @param {Array} [options.color=[255,255,255,0]] 指定线条颜色,颜色数组,[0~255,0~255,0~255,0~1],[red 红色,green 绿色,blue 蓝色,alpha 透明度]
  157. * @param {Number} [options.taperPower=1] 指定变细效果的强度,以总线长的百分比表示。如果1.0或更高,则不使用锥度效应
  158. * @param {Number} [options.width=5] 指定线的宽度,以像素为单位
  159. * @param {Number} [options.glowPower=0.25] 指定辉光的强度,作为总线宽的百分比。
  160. */
  161. drawGlowingLine(points, options) {
  162. //异步函数
  163. return new Promise((resolve, reject) => {
  164. if (!Cesium.defined(points)) {
  165. throw new Cesium.DeveloperError("points is required.");
  166. }
  167. if (points.length < 2) {
  168. reject("线对象,点数至少2个");
  169. }
  170. /* 转换坐标 */
  171. let positions = points.map(point => {
  172. return Cesium.Cartesian3.fromDegrees(point[0], point[1], point[2] || 0);
  173. });
  174. options = options || {};
  175. options.id = options.id || setSessionid();
  176. //指定线条颜色
  177. if (options.color) {
  178. if (options.color instanceof Array) {
  179. options.color = new Cesium.Color(options.color[0] / 255, options.color[1] / 255, options.color[2] / 255, options.color[3]);
  180. } else if (typeof(options.color) === 'string') {
  181. options.color = new Cesium.Color.fromCssColorString(options.color);
  182. } else {
  183. options.color = new Cesium.Color.fromCssColorString("#FFFF00");
  184. }
  185. }
  186. options.width = options.width || 5; //指定线条宽度
  187. options.glowPower = options.glowPower || 0.25; //一个数值属性,指定辉光的强度,作为总线宽的百分比。
  188. options.taperPower = options.taperPower || 1; //一个数值属性,指定变细效果的强度,以总线长的百分比表示。如果1.0或更高,则不使用锥度效应。
  189. let entity = this._viewer.entities.add({
  190. id: options.id,
  191. name: 'Glowing blue line on the surface',
  192. polyline: {
  193. clampToGround: true, //开启贴地 如果有模型则贴模型
  194. positions: positions,
  195. width: options.width,
  196. followSurface: true,
  197. material: new Cesium.PolylineGlowMaterialProperty({
  198. color: options.color, //指定线条颜色
  199. glowPower: options.glowPower,
  200. taperPower: options.taperPower
  201. }),
  202. },
  203. });
  204. resolve(entity);
  205. });
  206. },
  207. /**
  208. * 绘制指定颜色的线
  209. * @param {Object} points
  210. * @param {Object} options
  211. * @param {Array} [options.color=[255,255,255,0]] 点位颜色,颜色数组,[0~255,0~255,0~255,0~1],[red 红色,green 绿色,blue 蓝色,alpha 透明度]
  212. * @param {Number} [options.width=3] 指定线的宽度,以像素为单位
  213. */
  214. drawSpecifyColorLine(points, options) {
  215. //异步函数
  216. return new Promise((resolve, reject) => {
  217. if (!Cesium.defined(points)) {
  218. reject("points is required.");
  219. }
  220. if (points.length < 2) {
  221. reject("线对象,点数至少2个");
  222. }
  223. /* 转换坐标 */
  224. let positions = points.map(point => {
  225. return Cesium.Cartesian3.fromDegrees(point[0], point[1], point[2] || 0);
  226. });
  227. options = options || {};
  228. options.id = options.id || setSessionid();
  229. //指定线条颜色
  230. if (options.color) {
  231. if (options.color instanceof Array) {
  232. options.color = new Cesium.Color(options.color[0] / 255, options.color[1] / 255, options.color[2] / 255, options.color[3]);
  233. } else if (typeof(options.color) === 'string') {
  234. options.color = new Cesium.Color.fromCssColorString(options.color);
  235. } else {
  236. options.color = new Cesium.Color.fromCssColorString("#FFFF00");
  237. }
  238. }
  239. //指定线条宽度
  240. options.width = options.width || 5;
  241. let entity = this._viewer.entities.add({
  242. id: options.id,
  243. name: 'Red line on the surface',
  244. polyline: {
  245. clampToGround: true, //开启贴地 如果有模型则贴模型
  246. positions: positions,
  247. width: options.width,
  248. material: options.color,
  249. },
  250. });
  251. resolve(entity);
  252. });
  253. },
  254. /**
  255. * 绘制指定颜色指定边框颜色的线
  256. * @param {Object} points
  257. * @param {Object} options
  258. * @param {Array} [options.color=[255,255,255,0]] 点位颜色,颜色数组,[0~255,0~255,0~255,0~1],[red 红色,green 绿色,blue 蓝色,alpha 透明度]
  259. * @param {Number} [options.width=3] 指定线的宽度,以像素为单位
  260. * @param {String} [options.outlineColor=[255,255,255,0]] 指定点轮廓的颜色,,颜色数组,[0~255,0~255,0~255,0~1],[red 红色,green 绿色,blue 蓝色,alpha 透明,
  261. * @param {Number} [options.outlineWidth=0] 指定点轮廓的宽度
  262. */
  263. drawSpecifyColorAndOutlineColorLine(points, options) {
  264. //异步函数
  265. return new Promise((resolve, reject) => {
  266. if (!Cesium.defined(points)) {
  267. throw new Cesium.DeveloperError("points is required.");
  268. }
  269. if (points.length < 2) {
  270. reject("线对象,点数至少2个");
  271. }
  272. /* 转换坐标 */
  273. let positions = points.map(point => {
  274. return Cesium.Cartesian3.fromDegrees(point[0], point[1], point[2] || 0);
  275. });
  276. options = options || {};
  277. options.id = options.id || setSessionid();
  278. //指定线条颜色
  279. if (options.color) {
  280. if (options.color instanceof Array) {
  281. options.color = new Cesium.Color(options.color[0] / 255, options.color[1] / 255, options.color[2] / 255, options.color[3]);
  282. } else if (typeof(options.color) === 'string') {
  283. options.color = new Cesium.Color.fromCssColorString(options.color);
  284. } else {
  285. options.color = new Cesium.Color.fromCssColorString("#FFFF00");
  286. }
  287. }
  288. //指定线条宽度
  289. options.width = options.width || 5;
  290. //指定线条轮廓颜色
  291. if (options.outlineColor) {
  292. if (options.outlineColor instanceof Array) {
  293. options.outlineColor = new Cesium.Color(options.outlineColor[0] / 255, options.outlineColor[1] / 255, options.outlineColor[2] / 255, options.outlineColor[3]);
  294. } else if (typeof(options.outlineColor) === 'string') {
  295. options.outlineColor = new Cesium.Color.fromCssColorString(options.outlineColor);
  296. } else {
  297. options.outlineColor = new Cesium.Color.fromCssColorString("#FFFF00");
  298. }
  299. }
  300. //指定线条轮廓宽度
  301. options.outlineWidth = Cesium.defaultValue(options.outlineWidth, 1);
  302. let entity = this._viewer.entities.add({
  303. id: options.id,
  304. name: 'Orange line with black outline at height and following the surface',
  305. polyline: {
  306. clampToGround: true, //开启贴地 如果有模型则贴模型
  307. positions: positions,
  308. width: options.width, // 线宽
  309. material: new Cesium.PolylineOutlineMaterialProperty({
  310. color: options.color, // 指定颜色
  311. outlineWidth: options.outlineWidth, // 边框的宽度
  312. outlineColor: options.outlineColor, // 指定边框颜色
  313. }),
  314. },
  315. });
  316. resolve(entity);
  317. });
  318. },
  319. /**
  320. * 绘制指定颜色的箭头静态指示线
  321. * @param {Object} points
  322. * @param {Object} options
  323. * @param {Array} [options.color=[255,255,255,0]] 点位颜色,颜色数组,[0~255,0~255,0~255,0~1],[red 红色,green 绿色,blue 蓝色,alpha 透明度]
  324. * @param {Number} [options.width=3] 指定线的宽度,以像素为单位
  325. */
  326. drawSpecifyColorArrowStaticStateLine(points, options) {
  327. //异步函数
  328. return new Promise((resolve, reject) => {
  329. if (!Cesium.defined(points)) {
  330. throw new Cesium.DeveloperError("points is required.");
  331. }
  332. if (points.length < 2) {
  333. reject("线对象,点数至少2个");
  334. }
  335. /* 转换坐标 */
  336. let positions = points.map(point => {
  337. return Cesium.Cartesian3.fromDegrees(point[0], point[1], point[2] || 0);
  338. });
  339. options = options || {};
  340. options.id = options.id || setSessionid();
  341. //指定线条颜色
  342. if (options.color) {
  343. if (options.color instanceof Array) {
  344. options.color = new Cesium.Color(options.color[0] / 255, options.color[1] / 255, options.color[2] / 255, options.color[3]);
  345. } else if (typeof(options.color) === 'string') {
  346. options.color = new Cesium.Color.fromCssColorString(options.color);
  347. } else {
  348. options.color = new Cesium.Color.fromCssColorString("#FFFF00");
  349. }
  350. }
  351. //指定线条宽度
  352. options.width = options.width || 5;
  353. let entity = this._viewer.entities.add({
  354. id: options.id,
  355. name: 'Purple straight arrow at height',
  356. polyline: {
  357. clampToGround: true, //开启贴地 如果有模型则贴模型
  358. positions: positions,
  359. width: options.width,
  360. followSurface: false,
  361. material: new Cesium.PolylineArrowMaterialProperty(options.color),
  362. },
  363. });
  364. resolve(entity);
  365. });
  366. },
  367. /**
  368. * 绘制虚线
  369. * @param {Object} points
  370. * @param {Object} options
  371. * @param {Number} [options.width=3] 指定线的宽度,以像素为单位
  372. * @param {Array} [options.color=[255,255,255,0]] 点位颜色,颜色数组,[0~255,0~255,0~255,0~1],[red 红色,green 绿色,blue 蓝色,alpha 透明度]
  373. * @param {Array} [options.gapColor=[255,255,255,0]] 指定行中空白颜色的属性,颜色数组,[0~255,0~255,0~255,0~1],[red 红色,green 绿色,blue 蓝色,alpha 透明度]
  374. * @param {Number} [options.dashLength=16] 一个数值属性,以像素为单位指定破折号图案的长度。
  375. * @param {Number} [options.dashPattern=255] 为破折号指定16位模式的数值属性
  376. */
  377. drawDashedLine(points, options) {
  378. //异步函数
  379. return new Promise((resolve, reject) => {
  380. if (!Cesium.defined(points)) {
  381. throw new Cesium.DeveloperError("points is required.");
  382. }
  383. if (points.length < 2) {
  384. reject("线对象,点数至少2个");
  385. }
  386. /* 转换坐标 */
  387. let positions = points.map(point => {
  388. return Cesium.Cartesian3.fromDegrees(point[0], point[1], point[2] || 0);
  389. });
  390. options = options || {};
  391. options.id = options.id || setSessionid();
  392. //指定线条颜色
  393. if (options.color) {
  394. if (options.color instanceof Array) {
  395. options.color = new Cesium.Color(options.color[0] / 255, options.color[1] / 255, options.color[2] / 255, options.color[3]);
  396. } else if (typeof(options.color) === 'string') {
  397. options.color = new Cesium.Color.fromCssColorString(options.color);
  398. } else {
  399. options.color = new Cesium.Color.fromCssColorString("#FFFF00");
  400. }
  401. }
  402. //指定线条宽度
  403. options.width = options.width || 5;
  404. //指定行中空白颜色的属性
  405. if (options.gapColor) {
  406. if (options.gapColor instanceof Array) {
  407. options.gapColor = new Cesium.Color(options.gapColor[0] / 255, options.gapColor[1] / 255, options.gapColor[2] / 255, options.gapColor[3]);
  408. } else if (typeof(options.gapColor) === 'string') {
  409. options.gapColor = new Cesium.Color.fromCssColorString(options.gapColor);
  410. } else {
  411. options.gapColor = new Cesium.Color.fromCssColorString("#FFFF00");
  412. }
  413. }
  414. options.dashLength = options.dashLength || 16;
  415. options.dashPattern = options.dashPattern || 255;
  416. let entity = this._viewer.entities.add({
  417. id: options.id,
  418. name: 'CYAN dashed line',
  419. polyline: {
  420. clampToGround: true, //开启贴地 如果有模型则贴模型
  421. positions: positions,
  422. width: options.width,
  423. material: new Cesium.PolylineDashMaterialProperty({
  424. color: options.color,
  425. gapColor: options.gapColor, //指定行中空白颜色的属性。
  426. dashLength: options.dashLength, //一个数值属性,以像素为单位指定破折号图案的长度。
  427. dashPattern: options.dashPattern, //为破折号指定16位模式的数值属性
  428. }),
  429. },
  430. });
  431. resolve(entity);
  432. });
  433. },
  434. });
  435. // PolylineObject.prototype.PolylineDirectionMaterialProperty = PolylineDirectionMaterialProperty;
  436. export default PolylineObject;