core/analysis/Profile.js

/*
 * 剖面分析
 * @Author: jianlei wang
 * @Date: 2024-02-21 11:21:10
 * @Last Modified by: jianlei wang
 * @Last Modified time: 2024-03-28 15:41:14
 */

import { interPoints } from '../../utils/Coordinate'
import { resetGlobeHandler } from '../../utils/Handler'
import { getDistanceTwo, getPositionDistance } from '../../utils/Measure'
import { profileOption } from '../../utils/Profile'
import { removeEntity } from '../../utils/layers/Entity'
import CreatePolyline from '../creator/polyline/CreatePolyline'

/**
 * 剖面分析主类
 * @class
 */
class Profile {
  /**
   * 构造函数
   * @param {Object} viewer - 地图场景对象
   */
  constructor(viewer) {
    this._viewer = viewer
    this._profilePoints = []
  }

  /**
   * 获取echarts参数,可以直接用于绘制折线图
   * @readonly
   * @type {Object}
   */
  get chartOption() {
    if (!this._ProfileLine || this._profilePoints.length === 0) return false
    const points = this._profilePoints
    const distance = getPositionDistance(points)
    let datas = [],
      coords = []
    let xAixMax = Math.ceil(distance)
    let dis = 0
    for (let index = 0; index < points.length; index++) {
      const element = points[index]
      if (points[index - 1]) {
        const _dis = getDistanceTwo(points[index - 1], element)
        dis += _dis
      }
      const curData = [dis.toFixed(2), element.z.toFixed(2)]
      datas.push(curData)
      const curCoords = [element.x, element.y]
      coords.push(curCoords)
    }
    const options = profileOption(this._viewer, coords, datas, xAixMax)
    const { tipEntity, option } = options
    this._TipEntity = tipEntity
    return option
  }

  /**
   * 创建新的剖面分析对象
   * @param {ProfileResult} [callback=null] - 回调函数(e,p),e:线对象,p:点集
   */
  createNewProfile(callback) {
    this.removeAll()
    let handler = resetGlobeHandler(this._viewer)
    CreatePolyline(this._viewer, handler, {}, (e) => {
      this._ProfileLine = e
      this._profilePoints = interPoints(
        this._viewer,
        'onground',
        e.polyline?.positions?.getValue(this._viewer.clock.currentTime),
        [e]
      )
      callback &&
        typeof callback == 'function' &&
        callback(e, this._profilePoints)
    })
  }

  /**
   * 移除所有
   */
  removeAll() {
    this._ProfileLine && removeEntity(this._viewer, this._ProfileLine)
    this._ProfileLine = undefined
    this._TipEntity && removeEntity(this._viewer, this._TipEntity)
    this._TipEntity = undefined
    this._profilePoints = []
  }
}
export default Profile