Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Position.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. * clone a position
  82. * @returns {Position}
  83. */
  84. clone() {
  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. * clone a position
  96. * @deprecated
  97. * @returns {Position}
  98. */
  99. copy() {
  100. return this.clone()
  101. }
  102. /**
  103. *
  104. * @returns {*[]}
  105. */
  106. toArray() {
  107. return [this.lng, this.lat, this.alt, this.heading, this.pitch, this.roll]
  108. }
  109. /**
  110. *
  111. * @returns {string}
  112. */
  113. toString() {
  114. return `${this.lng},${this.lat},${this.alt},${this.heading},${this.pitch},${this.roll}`
  115. }
  116. /**
  117. *
  118. * @returns {{lng, heading, alt, roll, pitch, lat}}
  119. */
  120. toObject() {
  121. return {
  122. lng: this.lng,
  123. lat: this.lat,
  124. alt: this.alt,
  125. heading: this.heading,
  126. pitch: this.pitch,
  127. roll: this.roll
  128. }
  129. }
  130. /**
  131. *
  132. * @param arr
  133. * @returns {Position}
  134. */
  135. static fromArray(arr) {
  136. let position = new Position()
  137. if (Array.isArray(arr)) {
  138. position.lng = arr[0] || 0
  139. position.lat = arr[1] || 0
  140. position.alt = arr[2] || 0
  141. position.heading = arr[3] || 0
  142. position.pitch = arr[4] || 0
  143. position.roll = arr[5] || 0
  144. }
  145. return position
  146. }
  147. /**
  148. *
  149. * @param str
  150. * @returns {Position}
  151. */
  152. static fromString(str) {
  153. let position = new Position()
  154. if (str && typeof str === 'string') {
  155. let arr = str.split(',')
  156. position = this.fromArray(arr)
  157. }
  158. return position
  159. }
  160. /**
  161. *
  162. * @param obj
  163. * @returns {Position}
  164. */
  165. static fromObject(obj) {
  166. return new Position(
  167. obj.lng,
  168. obj.lat,
  169. obj.alt,
  170. obj.heading,
  171. obj.pitch,
  172. obj.roll
  173. )
  174. }
  175. /**
  176. * Deserialize
  177. * @param valStr
  178. * @returns {Position}
  179. */
  180. static deserialize(valStr) {
  181. let position = new Position()
  182. let obj = JSON.parse(valStr)
  183. if (obj) {
  184. position.lng = obj.lng || 0
  185. position.lat = obj.lat || 0
  186. position.alt = obj.alt || 0
  187. position.heading = obj.heading || 0
  188. position.pitch = obj.pitch || 0
  189. position.roll = obj.roll || 0
  190. }
  191. return position
  192. }
  193. }
  194. export default Position