| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /**
- * @Author: Caven
- * @Date: 2020-03-22 00:10:25
- */
-
- import Position from '@dc-modules/position/Position'
-
- class Parse {
- /**
- * Parses all kinds of coordinates to position
- * @param position
- * @returns {Position}
- */
- static parsePosition(position) {
- let result = new Position()
- if (!position) {
- return result
- }
- if (typeof position === 'string') {
- result = Position.fromString(position)
- } else if (Array.isArray(position)) {
- result = Position.fromArray(position)
- } else if (
- !(Object(position) instanceof Position) &&
- Object(position).hasOwnProperty('lng') &&
- Object(position).hasOwnProperty('lat')
- ) {
- result = Position.fromObject(position)
- } else if (Object(position) instanceof Position) {
- result = position
- }
- return result
- }
-
- /**
- * Parses all kinds of coordinates array to position array
- * @param positions
- * @returns {unknown[]}
- */
- static parsePositions(positions) {
- if (typeof positions === 'string') {
- if (positions.indexOf('#') >= 0) {
- throw new Error('the positions invalid')
- }
- positions = positions.split(';').filter(item => !!item)
- }
- return positions.map(item => {
- if (typeof item === 'string') {
- return Position.fromString(item)
- } else if (Array.isArray(item)) {
- return Position.fromArray(item)
- } else if (
- !(Object(item) instanceof Position) &&
- Object(item).hasOwnProperty('lng') &&
- Object(item).hasOwnProperty('lat')
- ) {
- return Position.fromObject(item)
- } else if (Object(item) instanceof Position) {
- return item
- }
- })
- }
-
- /**
- * Parses point position to array
- * @param position
- * @returns {*[]}
- */
- static parsePointCoordToArray(position) {
- position = this.parsePosition(position)
- return [position.lng, position.lat]
- }
-
- /**
- * Parses polyline positions to array
- * @param positions
- * @returns {[]}
- */
- static parsePolylineCoordToArray(positions) {
- let result = []
- positions = this.parsePositions(positions)
- positions.forEach(item => {
- result.push([item.lng, item.lat])
- })
- return result
- }
-
- /**
- * Parses polygon positions to array
- * @param positions
- * @param loop
- * @returns {[][]}
- */
- static parsePolygonCoordToArray(positions, loop = false) {
- let result = []
- positions = this.parsePositions(positions)
- positions.forEach(item => {
- result.push([item.lng, item.lat])
- })
- if (loop && result.length > 0) {
- result.push(result[0])
- }
- return [result]
- }
- }
-
- export default Parse
|