当前位置:   article > 正文

cesium-无人机航线问题_cesium.js航线高度

cesium.js航线高度

1.cesium 创建三维航线 

写的磕磕绊绊 期间遇到很多问题

1.无人机的姿态实时更新

 查阅cesium文档 发现了Cesium.Transforms.headingPitchRollQuaternion(position, hpr)

2.无人机视锥跟随无人机转向

这个问题困了好久 一开始尝试自己画了锥形 后面控制锥形旋转 平移 一直没成功  只能做到一直向下探测跟随  后面做了个模型  一开始也没成功 直达发现模型修改原点起点位置  将模型用blender把起点设置成锥顶 

3.无人机飞行会有后退动作

在画航线的时候 一开始设置

  1. // interpolationAlgorithm: Cesium.HermitePolynomialApproximation,
  2. // interpolationDegree: 2

发现在碰到重复点的时候会有一个向前起飞又回退的动作 考虑过速度和惯性问题  一直尝试也没成功 后来改成

  1. interpolationDegree: 1,
  2. interpolationAlgorithm: Cesium.LinearApproximation

 4.目前还存在的问题 

点击航线将无人机定位到航线点击位置  存在误差  在俯视的情况下 正确性颇高 但是其他角度就会有问题

  1. init () {
  2. const Cesium = this.Cesium
  3. // console.log(this.path3DList)
  4. this.showSpin = true
  5. let _this = this
  6. let startTime = ""
  7. let endTime = ""
  8. if (this.flightPathList.length) {
  9. startTime = this.flightPathList[0].recordTime ? moment(this.flightPathList[0].recordTime).format("YYYY-MM-DDTHH:mm:ss") + "Z" : ''
  10. endTime = this.flightPathList[this.flightPathList.length - 1].recordTime ? moment(this.flightPathList[this.flightPathList.length - 1].recordTime).format("YYYY-MM-DDTHH:mm:ss") + "Z" : ''
  11. }
  12. var viewer = new Cesium.Viewer("cesiumContainer", {
  13. infoBox: false,
  14. terrainProvider: Cesium.createWorldTerrain(),
  15. shouldAnimate: false,
  16. geocoder: false,//右上角第一个位置的查找工具
  17. homeButton: false,//右上角第二个位置的home图标
  18. sceneModePicker: false,//右上角第三个选择视角模式
  19. baseLayerPicker: false,//右上角第四个位置的图层选择器
  20. navigationHelpButton: false,//右上角第五个导航帮助按钮
  21. timeline: true,
  22. animation: false,//控制左下角的动画器件
  23. fullscreenButton: false,
  24. scene3DOnly: true,
  25. selectionIndicator: true,
  26. })
  27. //设置地形
  28. // viewer.terrainProvider = Cesium.createWorldTerrain({
  29. // requestWaterMask: true,//水渲染需求
  30. // requestVertexNormals: true//顶点法线渲染需求
  31. // })
  32. // viewer.scene.globe.depthTestAgainstTerrain = true//深度显示(用于湖泊河流 水深对周围景观的影响)
  33. // viewer.scene.globe.enableLighting = true//全局日照(受太阳,月亮的位置而影响光照信息)
  34. // console.log(this.flightPathList)
  35. let point = this.flightPathList[this.currentCount]
  36. // console.log(point.yaw, point.pitch, point.roll, Number(point.height), 'height')
  37. var initialPosition = new Cesium.Cartesian3.fromDegrees(point.longitude, point.latitude, Number(point.height))//摄像机位置 ,经度,纬度,高度
  38. // var initialOrientation = new Cesium.HeadingPitchRoll.fromDegrees(point.yaw, point.pitch, point.roll)//飞行 专用的 表示旋转角度之类的东西:
  39. var homeCameraView = {
  40. destination: initialPosition,
  41. // orientation: {
  42. // heading: Number(initialOrientation.heading) + 5, //偏航角
  43. // pitch: Number(initialOrientation.pitch) + 5, //俯仰角
  44. // roll: initialOrientation.roll//滚转角
  45. // },
  46. complete: () => {
  47. setTimeout(() => {
  48. viewer.camera.zoomOut(2000)
  49. this.showSpin = false
  50. }, 2000)
  51. }
  52. }
  53. //飞行时,相机的动画属性设置
  54. homeCameraView.dutaion = 1.0
  55. homeCameraView.maximumHeight = 2000
  56. homeCameraView.pitchAdjustHeight = 2000
  57. homeCameraView.endTransform = Cesium.Matrix4.IDENTITY
  58. //
  59. // var CesiumViewerSceneController = viewer.scene.screenSpaceCameraController
  60. // CesiumViewerSceneController.inertiaSpin = 1
  61. // CesiumViewerSceneController.inertiaTranslate = 1
  62. // CesiumViewerSceneController.inertiaZoom = 1
  63. var czml = [
  64. {
  65. id: "document",
  66. name: "CZML Path",
  67. version: "1.0",
  68. clock: {
  69. interval: `${startTime}/${endTime}`,
  70. currentTime: `${startTime}`,
  71. // currentTime: `${'2021-11-16T17:03:44Z'}`,
  72. multiplier: 1,
  73. clockStep: Cesium.ClockStep.TICK_DEPENDENT,
  74. // clockStep: 1
  75. },
  76. },
  77. {
  78. id: "path_2",
  79. name: "path2",
  80. availability: `${startTime}/${endTime}`,
  81. path: {
  82. show: [
  83. {
  84. interval: `${startTime}/${endTime}`,
  85. boolean: false
  86. }
  87. ],
  88. width: 0,
  89. },
  90. position: {
  91. epoch: `${startTime}`,
  92. cartographicDegrees: this.path3DList,
  93. },
  94. },
  95. {
  96. id: "path",
  97. name: "path1",
  98. availability: `${startTime}/${endTime}`,
  99. path: {
  100. show: [
  101. {
  102. interval: `${startTime}/${endTime}`,
  103. boolean: true
  104. }
  105. ],
  106. width: 6,
  107. material: {
  108. solidColor: {
  109. color: { rgba: [255, 161, 65, 255], }
  110. }
  111. }
  112. },
  113. billboard: {
  114. color: {
  115. rgba: [0, 255, 255, 255]
  116. },
  117. eyeOffset: { "cartesian": [0, 0, 0] },
  118. pixelOffset: { "cartesian2": [0, 0] },
  119. horizontalOrigin: "CENTER",
  120. verticalOrigin: "CENTER",
  121. image: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAjSURBVChTYyAa/EcDUGEIgIphAKg0XRSAAFQMDqDChAADAwDC13+BJ+0oDwAAAABJRU5ErkJgggAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",
  122. scale: 1,
  123. show: true
  124. },
  125. position: {
  126. epoch: `${startTime}`,
  127. cartographicDegrees: this.path3DList,
  128. },
  129. }
  130. ]
  131. var dronePromise = Cesium.CzmlDataSource.load(czml)
  132. var drone
  133. var drone_2
  134. dronePromise.then(dataSource => {
  135. viewer.dataSources.add(dataSource)
  136. drone = dataSource.entities.getById('path')
  137. drone_2 = dataSource.entities.getById('path_2')
  138. // 无人机模型
  139. drone.model = {
  140. id: 'wrj',
  141. name: 'wrj',
  142. uri: '/WRJ02.glb',
  143. minimumPixelSize: 48,
  144. scale: 0.2,
  145. maximumScale: 10
  146. }
  147. // uri: 'https://gist.githubusercontent.com/rahwang/9843cb77fc1c6d07c287566ed4e08ee3/raw/dc3a9ff6fc73b784519ac9371c0e2cbd3ab3dc47/CesiumDrone.gltf',
  148. // 添加无人机位置
  149. //VelocityOrientationProperty(速度方向属性):基于所给的位置属性的速度来得出一个四元旋转属性
  150. // drone.orientation = new Cesium.VelocityOrientationProperty(drone.position)
  151. drone.position.setInterpolationOptions({
  152. // interpolationAlgorithm: Cesium.HermitePolynomialApproximation,
  153. // interpolationDegree: 2
  154. interpolationDegree: 1,
  155. interpolationAlgorithm: Cesium.LinearApproximation
  156. })
  157. drone_2.position.setInterpolationOptions({
  158. interpolationDegree: 1,
  159. interpolationAlgorithm: Cesium.LinearApproximation
  160. })
  161. drone_2.model = {
  162. id: 'wrj_zhuiti',
  163. name: 'wrj_zhuiti',
  164. uri: '/WRJ_zhuiti.glb',
  165. minimumPixelSize: 48,
  166. scale: 1,
  167. maximumScale: 10
  168. }
  169. //跟踪无人机
  170. // viewer.trackedEntity = drone
  171. //取消跟踪无人机
  172. // viewer.trackedEntity = undefined
  173. viewer.camera.flyTo(homeCameraView)
  174. })
  175. //白模
  176. // var shpTileset = new Cesium.Cesium3DTileset({
  177. // url: 'http://10.2.13.73:9000/model/ad9d5be0767e11ebab9c9b084bddd6a4/tileset.json',
  178. // })
  179. // shpTileset.style = new Cesium.Cesium3DTileStyle({
  180. // color: {
  181. // conditions: [
  182. // ["${height} >= 300", "rgba(45, 0, 75, 0.5)"],
  183. // ["${height} >= 200", "rgb(102, 71, 151)"],
  184. // ["${height} >= 100", "rgb(170, 162, 204)"],
  185. // ["${height} >= 50", "rgb(224, 226, 238)"],
  186. // ["${height} >= 25", "rgb(252, 230, 200)"],
  187. // ["${height} >= 10", "rgb(248, 176, 87)"],
  188. // ["${height} >= 5", "rgb(198, 106, 11)"],
  189. // ["true", "rgb(127, 59, 8)"],
  190. // ],
  191. // },
  192. // })
  193. // viewer.scene.primitives.add(shpTileset)
  194. viewer.timeline.makeLabel = (datetime) => {
  195. return this.timeFmt(datetime, "HH:mm:ss")
  196. }
  197. viewer.timeline.zoomTo(viewer.clock.startTime, viewer.clock.stopTime)//设置时间轴可见趋近
  198. viewer.clock.canAnimate = false
  199. viewer.clock.onTick.addEventListener((tick) => {
  200. // this.scanEntity.orientation = null
  201. // viewer.entities.remove(this.scanBox)
  202. let time = this.timeFmt(tick.currentTime, "YYYY-MM-DD HH:mm:ss")
  203. this.preTime = this.nowTime
  204. this.nowTime = time
  205. if (this.preTime == this.nowTime) {
  206. return
  207. }
  208. this.currentTime = getTime(this.flightPathList[0].recordTime, time)
  209. let allTime = (new Date(this.flightPathList[this.flightPathList.length - 1].recordTime).getTime() - new Date(this.flightPathList[0].recordTime).getTime()) / 1000
  210. let nowTime = (new Date(time).getTime() - new Date(this.flightPathList[0].recordTime).getTime()) / 1000
  211. this.currentWidth = (nowTime * 1000) / (allTime * 1000) * 100 + '%'
  212. this.isPlay = viewer.clock.shouldAnimate
  213. this.$emit('start3D', { isPlay: viewer.clock.shouldAnimate, time, tick: viewer.clock.shouldAnimate })
  214. function getTime (start, now) {
  215. let time = (new Date(now).getTime() - new Date(start).getTime()) / 1000
  216. // console.log(time, 'time')
  217. _this.$emit('getCurrentCount', time)
  218. let s = showNum(time % 60)
  219. let m = showNum(parseInt(time / 60))
  220. return `${m}:${s}`
  221. }
  222. function showNum (num) {
  223. if (num < 10) {
  224. return '0' + num
  225. }
  226. return num
  227. }
  228. //设置偏转俯仰翻滚
  229. if (this.timePathList[time]) {
  230. let { pitch, roll, yaw, longitude, latitude, height } = this.timePathList[time]
  231. var position = Cesium.Cartesian3.fromDegrees(longitude, latitude, height)
  232. var position_2 = Cesium.Cartesian3.fromDegrees(longitude, latitude, height)
  233. //no 视锥
  234. var hpr = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(Number(yaw) + 90), Cesium.Math.toRadians(Number(pitch)), Cesium.Math.toRadians(Number(roll)))
  235. //has 视锥
  236. // var hpr = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(Number(yaw) + 180), Cesium.Math.toRadians(Number(pitch)), Cesium.Math.toRadians(Number(roll)))
  237. var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpr)
  238. drone.orientation = orientation
  239. viewer.entities.remove(this.scanBox)
  240. var headingC = Cesium.Math.toRadians(180 + Number(yaw))
  241. var pitchC = Cesium.Math.toRadians(90.0 + 0)
  242. var rollC = Cesium.Math.toRadians(0)
  243. var hprC = new Cesium.HeadingPitchRoll(headingC, pitchC, rollC)
  244. drone_2.orientation = Cesium.Transforms.headingPitchRollQuaternion(position_2, hprC)
  245. }
  246. })
  247. viewer.clock.onStop.addEventListener((tick) => {
  248. viewer.clock.shouldAnimate = false
  249. this.$emit('clearMap3D')
  250. })
  251. //航点
  252. this.drawPoint(viewer)
  253. //
  254. // currentCount
  255. var time_count = new Date(_this.flightPathList[_this.currentCount].recordTime)
  256. var utc_time = Cesium.JulianDate.fromDate(time_count)//UTC
  257. viewer.clock.currentTime = Cesium.JulianDate.addHours(utc_time, 8, new Cesium.JulianDate())
  258. //航点点击
  259. viewer.screenSpaceEventHandler.setInputAction(function leftClick (movement) {
  260. var pickedFeature = viewer.scene.pick(movement.position)
  261. console.log(pickedFeature)
  262. if (pickedFeature && pickedFeature.id && pickedFeature.id._name == "point") {
  263. let time_bj = new Date(_this.flightPathList[pickedFeature.id._id].recordTime)
  264. let utc = Cesium.JulianDate.fromDate(time_bj)//UTC
  265. viewer.clock.currentTime = Cesium.JulianDate.addHours(utc, 8, new Cesium.JulianDate())
  266. return
  267. }
  268. if (pickedFeature && pickedFeature.id && (pickedFeature.id._name == "wrj_zhuiti" || pickedFeature.id._name == 'wrj' || pickedFeature.id._name == 'path2')) {
  269. return
  270. }
  271. if (pickedFeature && pickedFeature.id && (pickedFeature.id._name != "wrj_zhuiti" || pickedFeature.id._name != 'path2')) {
  272. let pointSelectArray = []
  273. var earthPosition = viewer.camera.pickEllipsoid(movement.position, viewer.scene.globe.ellipsoid)
  274. var cartographic = Cesium.Cartographic.fromCartesian(earthPosition, viewer.scene.globe.ellipsoid, new Cesium.Cartographic())
  275. var lng = Cesium.Math.toDegrees(cartographic.longitude)
  276. var lat = Cesium.Math.toDegrees(cartographic.latitude)
  277. // var height = cartographic.height
  278. // function getDistance (lng1, lat1, lng2, lat2) {
  279. // let startPosition = new Cesium.Cartesian3.fromDegrees(lng1, lat1)
  280. // let endPosition = new Cesium.Cartesian3.fromDegrees(lng2, lat2)
  281. // let distance = Cesium.Cartesian3.distance(startPosition, endPosition)//求两点直线距离,单位为米
  282. // return distance
  283. // }
  284. // console.log("[Lng=>" + lng + ",Lat=>" + lat + ",H=>" + height + "]")
  285. // 经纬度转换成三角函数中度分表形式
  286. function rad (d) {
  287. return d * Math.PI / 180.0
  288. }
  289. // 根据经纬度计算距离
  290. function getDistance (lng1, lat1, lng2, lat2,) {
  291. var radLat1 = rad(lat1)
  292. var radLat2 = rad(lat2)
  293. var a = radLat1 - radLat2
  294. var b = rad(lng1) - rad(lng2)
  295. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
  296. Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)))
  297. s = s * 6378.137 // EARTH_RADIUS;
  298. s = Math.round(s * 10000) //输出为公里
  299. return s
  300. }
  301. _this.flightPathList.forEach(el => {
  302. pointSelectArray.push(getDistance(el.longitude, el.latitude, lng, lat))
  303. })
  304. // console.log(lat, lng, height, pointSelectArray)
  305. //获取最小值的下标
  306. function getMinIndex (arr) {
  307. var min = arr[0]
  308. //声明了个变量 保存下标值
  309. var index = 0
  310. for (var i = 0; i < arr.length; i++) {
  311. if (min > arr[i]) {
  312. min = arr[i]
  313. index = i
  314. }
  315. }
  316. return index
  317. }
  318. let index = getMinIndex(pointSelectArray)
  319. let time_bj = new Date(_this.flightPathList[index].recordTime)
  320. let utc = Cesium.JulianDate.fromDate(time_bj)//UTC
  321. viewer.clock.currentTime = Cesium.JulianDate.addHours(utc, 8, new Cesium.JulianDate())
  322. }
  323. }, Cesium.ScreenSpaceEventType.LEFT_UP)//LEFT_CLICK
  324. //鼠标移动
  325. var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
  326. handler.setInputAction((movement) => {
  327. var haveEn = viewer.scene.pick(movement.endPosition)
  328. // console.log(haveEn, 'haveEn1', haveEn.id._id)
  329. if (haveEn) {
  330. viewer._container.style.cursor = "pointer"
  331. } else {
  332. viewer._container.style.cursor = "default"
  333. }
  334. }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
  335. viewer._cesiumWidget._creditContainer.style.display = "none"
  336. this.viewer = viewer
  337. console.log(viewer)
  338. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/583150
推荐阅读
相关标签
  

闽ICP备14008679号