Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Position.js 3.8KB

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