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.6KB

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