类名 common/base/geometry/LineString.js
import Geometry from './Geometry'
import { defaultValue, isNumber } from '../../util'
import Zondy from '../Zondy'
import { GeometryType, IGSGeometryType } from '../enum'
import Point from './Point'
import { calcExtent } from './Utiles'

/**
 * 线几何对象
 * <br><br>[ES5引入方式]:<br/>
 * Zondy.Geometry.LineString() <br/>
 * [ES6引入方式]:<br/>
 * import { LineString } from "@mapgis/webclient-common" <br/>
 * <br/>
 * @class LineString
 * @moduleEX GeometryModule
 * @extends Geometry
 * @param {Object} options 构造参数
 * @param {Array} [options.coordinates = []] 几何点的坐标数组,支持任意单位,参考示例:<a href='#LineString'>[线几何对象]</a>
 * @param {SpatialReference} [options.spatialReference = new Zondy.SpatialReference('EPSG:4326')] 几何点的空间参考系,默认4326,当不是4326时请指定坐标系,方便进行投影转换,参考示例:<a href='#SpatialReference'>[指定坐标系]</a>
 * @summary <h5>支持如下方法:</h5>
 * <a href='#getPoint'>[1、获取线上点]</a><br/>
 * <a href='#insertPoint'>[2、插入点到线上a><br/>
 * <a href='#removePoint'>[3、删除线上点]</a><br/>
 * <a href='#setPoint'>[4、修改线上点]</a><br/>
 * <a href='#toString'>[5、返回字符串]</a><br/>
 * <a href='#toXMl'>[6、导出为OGC服务要求的xml字符串]</a><br/>
 * <a href='#toOldIGSGeometry'>[7、返回igs1.0的几何对象]</a><br/>
 * <a href='#getIGSType'>[8、返回IGS所对应的GeometryModule型]</a><br/>
 * <a href='#toDots'>[9、返回Dots对象,仅包括多边形的外圈]</a><br/>
 * <a href='#fromJSON'>[10、通过传入的json构造并返回一个新的几何对象]</a><br/>
 * <a href='#toJSON'>[11、导出为json对象]</a><br/>
 * [12、克隆几何对象]{@link Geometry#clone}
 *
 * @example <caption><h5 id='LineString'>创建几何对象</h5></caption>
 * // ES5引入方式
 * const { LineString } = Zondy.Geometry
 * // ES6引入方式
 * import { LineString } from "@mapgis/webclient-common"
 * new LineString({
 *   // 点数量必须大于等于2
 *   coordinates: [
 *     [100.0, 0.0],
 *     [101.0, 1.0]
 *   ]
 * })
 *
 * @example <caption><h7 id='SpatialReference'>指定坐标系</h7></caption>
 * // ES5引入方式
 * const { LineString } = Zondy.Geometry
 * const { SpatialReference } = Zondy
 * // ES6引入方式
 * import { LineString, SpatialReference } from "@mapgis/webclient-common"
 * new LineString({
 *   // 点数量必须大于等于2
 *   coordinates: [
 *     [12929863.44711455, 3934286.575385226],
 *     [12060733.232006868, 3377247.5680546067]
 *   ],
 *   // 当不是4326时请指定坐标系,方便进行投影转换
 *   spatialReference: new SpatialReference('EPSG:3857')
 * })
 */
class LineString extends Geometry {
  constructor(options) {
    super(options)
    options = defaultValue(options, {})
    /**
     * 几何类型
     * @member {GeometryType} LineString.prototype.type
     */
    this.type = GeometryType.lineString
    /**
     * 几何点的坐标数组
     * @member {Array} LineString.prototype.coordinates
     */
    this.coordinates = defaultValue(options.coordinates, [])
    if (!Array.isArray(this.coordinates)) {
      throw new Error('坐标必须为数组')
    }
    if (this.coordinates.length < 2) {
      throw new Error('坐标必须是两个或多个位置的数组')
    }
    if (this.coordinates[0].length === 3) {
      this.hasZ = true
    }
  }

  /**
   * 获取线上点
   * @param {Number} pointIndex 数组下标
   * @return {Point | null} 点
   * @example <caption><h7 id='getPoint'>获取线上点</h7></caption>
   * // ES5引入方式
   * const { LineString } = Zondy.Geometry
   * // ES6引入方式
   * import { LineString } from "@mapgis/webclient-common"
   * const lineString = new LineString({
   *   // 点数量必须大于等于2
   *   coordinates: [
   *     [100.0, 0.0],
   *     [101.0, 1.0]
   *   ]
   * })
   * const point = lineString.getPoint(0)
   */
  getPoint(pointIndex) {
    pointIndex = Number(pointIndex)
    if (!this._isValidate(pointIndex)) return null
    return new Point({
      coordinates: this._cloneCoordinates()[pointIndex]
    })
  }

  /**
   * 插入点到线上
   * @param {Number} pointIndex 数组下标
   * @param {Point | Number[]} point 点
   * @return {LineString} 插入点后的线对象
   * @example <caption><h7 id='insertPoint'>插入点到线上</h7></caption>
   * // ES5引入方式
   * const { LineString } = Zondy.Geometry
   * // ES6引入方式
   * import { LineString } from "@mapgis/webclient-common"
   * const lineString = new LineString({
   *   // 点数量必须大于等于2
   *   coordinates: [
   *     [100.0, 0.0],
   *     [101.0, 1.0]
   *   ]
   * })
   * lineString.insertPoint(1, [102.0, 2.0])
   */
  insertPoint(pointIndex, point) {
    if (!this._isValidate(pointIndex)) return null
    if (Array.isArray(this.coordinates)) {
      this.coordinates.splice(pointIndex, 0, Point.toCoordinates(point))
    }
    this.coordinates = JSON.parse(JSON.stringify(this.coordinates))
    return this
  }

  /**
   * 删除线上点
   * @param {Number} pointIndex 数组下标
   * @return {Point} 删除点
   * @example <caption><h7 id='removePoint'>删除线上点</h7></caption>
   * // ES5引入方式
   * const { LineString } = Zondy.Geometry
   * // ES6引入方式
   * import { LineString } from "@mapgis/webclient-common"
   * const lineString = new LineString({
   *   // 点数量必须大于等于2
   *   coordinates: [
   *     [100.0, 0.0],
   *     [101.0, 1.0]
   *   ]
   * })
   * lineString.removePoint(0)
   */
  removePoint(pointIndex) {
    pointIndex = Number(pointIndex)
    if (!this._isValidate(pointIndex)) return this
    const coords = this._cloneCoordinates()[pointIndex]
    this.coordinates.splice(pointIndex, 1)
    this.coordinates = JSON.parse(JSON.stringify(this.coordinates))
    return new Point({
      coordinates: coords
    })
  }

  /**
   * 修改线上点
   * @param {Number} pointIndex 数组下标
   * @param {Point | Number[]} point 点
   * @return {LineString} 修改后的线对象
   * @example <caption><h7 id='setPoint'>修改线上点</h7></caption>
   * // ES5引入方式
   * const { LineString } = Zondy.Geometry
   * // ES6引入方式
   * import { LineString } from "@mapgis/webclient-common"
   * const lineString = new LineString({
   *   // 点数量必须大于等于2
   *   coordinates: [
   *     [100.0, 0.0],
   *     [101.0, 1.0]
   *   ]
   * })
   * lineString.setPoint(0, [11.0, 1.2])
   */
  setPoint(pointIndex, point) {
    if (!this._isValidate(pointIndex)) return this
    if (Array.isArray(this.coordinates)) {
      this.coordinates.splice(pointIndex, 1, Point.toCoordinates(point))
    }
    this.coordinates = JSON.parse(JSON.stringify(this.coordinates))
    return this
  }

  /**
   * 通过传入的json构造并返回一个新的几何对象
   * @param {Object} [json] JSON对象
   * @example <caption><h7 id='fromJSON'>通过传入的json构造并返回一个新的几何对象</h7></caption>
   * // ES5引入方式
   * const { LineString } = Zondy.Geometry
   * // ES6引入方式
   * import { LineString } from "@mapgis/webclient-common"
   * const json = {
   *   // 点数量必须大于等于2
   *   coordinates: [
   *     [100.0, 0.0],
   *     [101.0, 1.0]
   *   ]
   * }
   * const line = LineString.fromJSON(json)
   */
  static fromJSON(json) {
    json = defaultValue(json, {})
    return new LineString(json)
  }

  /**
   * <a id='toJSON'></a>
   * 导出为json对象
   * @return {Object} json对象
   */
  toJSON() {
    const json = super.toJSON()
    json.coordinates = JSON.parse(JSON.stringify(this.coordinates))
    json.extent = this.extent.toJSON()

    return json
  }

  /**
   * 返回如下格式的字符串:"x0,y0,x1,y1,x2,y2"<a id='toString'></a>
   * @returns string
   */
  toString() {
    let _str = ''
    // 开始循环线内的点
    for (let i = 0; i < this.coordinates.length; i++) {
      // 线内的点
      const _point = this.coordinates[i]
      // 有可能是xyz,因此for循环
      for (let k = 0; k < _point.length; k++) {
        // 加,号
        _str += `${_point[k]},`
      }
    }
    // 删除最后一个,
    _str = _str.substring(0, _str.length - 1)
    return _str
  }

  /**
   * 导出为OGC服务要求的xml字符串<a id='toXMl'></a>
   * @return {String} 字符串
   */
  toXMl() {}

  /**
   * 返回igs1.0的几何对象<a id='toOldIGSGeometry'></a>
   * @returns Object igs1.0的几何对象
   */
  toOldIGSGeometry() {
    const Dots = []
    for (let i = 0; i < this.coordinates.length; i++) {
      Dots.push({
        x: this.coordinates[i][0],
        y: this.coordinates[i][1]
      })
    }
    return {
      // 线要素的空间几何对象,线由弧段构成,弧段由点构成
      LinGeom: [
        {
          Line: {
            // 弧段信息
            Arcs: [
              {
                // 弧段包含的坐标信息
                Dots,
                ArcID: 0
              }
            ]
          },
          GID: 0
        }
      ],
      GID: parseInt(String(Math.random() * 10000000))
    }
  }

  /**
   * 返回IGS所对应的GeometryModule型<a id='getIGSType'></a>
   * @returns string GeometryModule型
   */
  getIGSType() {
    return IGSGeometryType.lineString
  }

  /**
   * 返回Dots对象,仅包括多边形的外圈<a id='toDots'></a>
   * @returns Array Dots对象
   */
  toDots() {
    const Dots = []
    const coordinates = this.coordinates

    for (let i = 0; i < coordinates.length; i++) {
      Dots.push({
        x: coordinates[i][0],
        y: coordinates[i][1]
      })
    }

    return Dots
  }

  /**
   * @function LineString.prototype._isValidate
   * @private
   * @description 判断输入是否合法
   * @param {Number} pointIndex 数组下标
   * @returns {Boolean} 输入是否合法
   */
  _isValidate(pointIndex) {
    const len = this.coordinates.length
    return !(!isNumber(pointIndex) || pointIndex < 0 || pointIndex >= len)
  }

  /**
   * @function LineString.prototype._cloneCoordinates
   * @private
   * @description 拷贝几何信息
   * @returns {Array}
   */
  _cloneCoordinates() {
    return JSON.parse(JSON.stringify(this.coordinates))
  }

  /**
   * @function LineString.prototype._calcExtent
   * @private
   * @description 计算外包盒
   * @param {Array} coordinates 坐标点
   * @param {Boolean} hasZ 是否是三维
   * @returns {Extent}
   */
  _calcExtent(coordinates, hasZ) {
    return calcExtent(coordinates, hasZ, GeometryType.lineString)
  }

  /**
   * 克隆几何对象
   * @return {Geometry} 克隆后的几何对象
   */
  clone() {
    return new LineString(this.toJSON())
  }
}
Object.defineProperties(LineString.prototype, {
  /**
   * 外包盒
   * @member {Number} LineString.prototype.extent
   * */
  extent: {
    configurable: false,
    get() {
      return this._calcExtent(this.coordinates, this.hasZ)
    }
  }
})

Zondy.Geometry.LineString = LineString
export default LineString
构造函数
成员变量
方法
事件