You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Parse.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @Author : Caven Chen
  3. */
  4. import Position from '../position/Position'
  5. import { Cesium } from '../../namespace'
  6. import { Transform } from '../transform'
  7. class Parse {
  8. /**
  9. * Parses all kinds of coordinates to position
  10. * @param position
  11. * @returns {Position}
  12. */
  13. static parsePosition(position) {
  14. let result = new Position()
  15. if (!position) {
  16. return result
  17. }
  18. if (typeof position === 'string') {
  19. result = Position.fromString(position)
  20. } else if (Array.isArray(position)) {
  21. result = Position.fromArray(position)
  22. } else if (
  23. !(Object(position) instanceof Position) &&
  24. Object(position).hasOwnProperty('lng') &&
  25. Object(position).hasOwnProperty('lat')
  26. ) {
  27. result = Position.fromObject(position)
  28. } else if (Object(position) instanceof Position) {
  29. result = position
  30. } else if (Object(position) instanceof Cesium.Cartesian3) {
  31. result = Transform.transformCartesianToWGS84(position)
  32. } else if (Object(position) instanceof Cesium.Cartographic) {
  33. result = Transform.transformCartographicToWGS84(position)
  34. }
  35. return result
  36. }
  37. /**
  38. * Parses all kinds of coordinates array to position array
  39. * @param positions
  40. * @returns {unknown[]}
  41. */
  42. static parsePositions(positions) {
  43. if (typeof positions === 'string') {
  44. if (positions.indexOf('#') >= 0) {
  45. throw new Error('the positions invalid')
  46. }
  47. positions = positions.split(';').filter((item) => !!item)
  48. }
  49. return positions.map((item) => {
  50. return this.parsePosition(item)
  51. })
  52. }
  53. /**
  54. * Parses point position to array
  55. * @param position
  56. * @returns {*[]}
  57. */
  58. static parsePointCoordToArray(position) {
  59. position = this.parsePosition(position)
  60. return [position.lng, position.lat]
  61. }
  62. /**
  63. * Parses polyline positions to array
  64. * @param positions
  65. * @returns {[]}
  66. */
  67. static parsePolylineCoordToArray(positions) {
  68. let result = []
  69. positions = this.parsePositions(positions)
  70. positions.forEach((item) => {
  71. result.push([item.lng, item.lat])
  72. })
  73. return result
  74. }
  75. /**
  76. * Parses polygon positions to array
  77. * @param positions
  78. * @param loop
  79. * @returns {[][]}
  80. */
  81. static parsePolygonCoordToArray(positions, loop = false) {
  82. let result = []
  83. positions = this.parsePositions(positions)
  84. positions.forEach((item) => {
  85. result.push([item.lng, item.lat])
  86. })
  87. if (loop && result.length > 0) {
  88. result.push(result[0])
  89. }
  90. return [result]
  91. }
  92. }
  93. export default Parse