您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Position.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * @Author: Caven
  3. * @Date: 2019-12-27 14:35:02
  4. */
  5. import { Transform } from '@dc-modules/transform'
  6. class Position {
  7. constructor(lng, lat, alt, heading, pitch, roll) {
  8. this._lng = +lng || 0
  9. this._lat = +lat || 0
  10. this._alt = +alt || 0
  11. this._heading = +heading || 0
  12. this._pitch = +pitch || 0
  13. this._roll = +roll || 0
  14. }
  15. set lng(lng) {
  16. this._lng = +lng
  17. }
  18. get lng() {
  19. return this._lng
  20. }
  21. set lat(lat) {
  22. this._lat = +lat
  23. }
  24. get lat() {
  25. return this._lat
  26. }
  27. set alt(alt) {
  28. this._alt = +alt
  29. }
  30. get alt() {
  31. return this._alt
  32. }
  33. set heading(heading) {
  34. this._heading = +heading
  35. }
  36. get heading() {
  37. return this._heading
  38. }
  39. set pitch(pitch) {
  40. this._pitch = +pitch
  41. }
  42. get pitch() {
  43. return this._pitch
  44. }
  45. set roll(roll) {
  46. this._roll = +roll
  47. }
  48. get roll() {
  49. return this._roll
  50. }
  51. /**
  52. *
  53. * @returns {string}
  54. */
  55. serialize() {
  56. let position = new Position(
  57. this._lng,
  58. this._lat,
  59. this._alt,
  60. this._heading,
  61. this._pitch,
  62. this._roll
  63. )
  64. return JSON.stringify(position)
  65. }
  66. /**
  67. * Calculate the distance between two positions
  68. * @param target
  69. * @returns {number}
  70. */
  71. distance(target) {
  72. if (!target || !(target instanceof Position)) {
  73. return 0
  74. }
  75. return Cesium.Cartesian3.distance(
  76. Transform.transformWGS84ToCartesian(this),
  77. Transform.transformWGS84ToCartesian(target)
  78. )
  79. }
  80. /**
  81. *
  82. * @returns {Position}
  83. */
  84. copy() {
  85. let position = new Position()
  86. position.lng = this.lng || 0
  87. position.lat = this.lat || 0
  88. position.alt = this.alt || 0
  89. position.heading = this.heading || 0
  90. position.pitch = this.pitch || 0
  91. position.roll = this.roll || 0
  92. return position
  93. }
  94. /**
  95. *
  96. * @returns {*[]}
  97. */
  98. toArray() {
  99. return [this.lng, this.lat, this.alt, this.heading, this.pitch, this.roll]
  100. }
  101. /**
  102. *
  103. * @returns {string}
  104. */
  105. toString() {
  106. return `${this.lng},${this.lat},${this.alt},${this.heading},${this.pitch},${this.roll}`
  107. }
  108. /**
  109. *
  110. * @returns {{lng, heading, alt, roll, pitch, lat}}
  111. */
  112. toObject() {
  113. return {
  114. lng: this.lng,
  115. lat: this.lat,
  116. alt: this.alt,
  117. heading: this.heading,
  118. pitch: this.pitch,
  119. roll: this.roll
  120. }
  121. }
  122. /**
  123. *
  124. * @param arr
  125. * @returns {Position}
  126. */
  127. static fromArray(arr) {
  128. let position = new Position()
  129. if (Array.isArray(arr)) {
  130. position.lng = arr[0] || 0
  131. position.lat = arr[1] || 0
  132. position.alt = arr[2] || 0
  133. position.heading = arr[3] || 0
  134. position.pitch = arr[4] || 0
  135. position.roll = arr[5] || 0
  136. }
  137. return position
  138. }
  139. /**
  140. *
  141. * @param str
  142. * @returns {Position}
  143. */
  144. static fromString(str) {
  145. let position = new Position()
  146. if (str && typeof str === 'string') {
  147. let arr = str.split(',')
  148. position = this.fromArray(arr)
  149. }
  150. return position
  151. }
  152. /**
  153. *
  154. * @param obj
  155. * @returns {Position}
  156. */
  157. static fromObject(obj) {
  158. return new Position(
  159. obj.lng,
  160. obj.lat,
  161. obj.alt,
  162. obj.heading,
  163. obj.pitch,
  164. obj.roll
  165. )
  166. }
  167. /**
  168. * Deserialize
  169. * @param valStr
  170. * @returns {Position}
  171. */
  172. static deserialize(valStr) {
  173. let position = new Position()
  174. let obj = JSON.parse(valStr)
  175. if (obj) {
  176. position.lng = obj.lng || 0
  177. position.lat = obj.lat || 0
  178. position.alt = obj.alt || 0
  179. position.heading = obj.heading || 0
  180. position.pitch = obj.pitch || 0
  181. position.roll = obj.roll || 0
  182. }
  183. return position
  184. }
  185. /**
  186. * Returns position from coord String
  187. * @deprecated
  188. * @param str
  189. * @returns {Position}
  190. */
  191. static fromCoordString(str) {
  192. let position = new Position()
  193. if (str && typeof str === 'string') {
  194. position = this.fromArray(str.split(','))
  195. }
  196. return position
  197. }
  198. /**
  199. * Returns position from coord array
  200. * @deprecated
  201. * @param arr
  202. * @returns {Position}
  203. */
  204. static fromCoordArray(arr) {
  205. let position = new Position()
  206. if (Array.isArray(arr)) {
  207. position = this.fromArray(arr)
  208. }
  209. return position
  210. }
  211. }
  212. export default Position