| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- /*
- * @Author: Caven
- * @Date: 2020-01-09 09:10:37
- * @Last Modified by: Caven
- * @Last Modified time: 2020-02-04 15:24:43
- */
- import Overlay from '../Overlay'
- import Cesium from '@/namespace'
-
- DC.Polygon = class extends Overlay {
- constructor(positions) {
- if (
- !positions ||
- (typeof positions !== 'string' && !Array.isArray(positions))
- ) {
- throw new Error('the positions invalid')
- }
- super()
- this._positions = []
- this._holes = []
- this._preparePositions(positions)
- this._delegate = new Cesium.Entity()
- this._state = DC.OverlayState.INITIALIZED
- this.type = DC.OverlayType.POLYGON
- }
-
- set positions(positions) {
- this._preparePositions(positions)
- }
-
- get positions() {
- return this._positions
- }
-
- set holes(holes) {
- if (holes && holes.length) {
- this._holes = holes.map(item => this._preparePositions(item))
- }
- }
-
- get holes() {
- return this._holes
- }
-
- get center() {
- let boundingSphere = Cesium.BoundingSphere.fromPoints(
- DC.T.transformWSG84ArrayToCartesianArray(this._positions)
- )
- return DC.T.transformCartesianToWSG84(boundingSphere.center)
- }
-
- get area() {
- let result = 0
- if (this._positions) {
- let h = 0
- let ellipsoid = Cesium.Ellipsoid.WGS84
- let positions = [...this._positions]
- positions.push(positions[0])
- for (let i = 1; i < positions.length; i++) {
- let oel = ellipsoid.cartographicToCartesian(
- DC.T.transformWSG84ToCartographic(positions[i - 1])
- )
- let el = ellipsoid.cartographicToCartesian(
- DC.T.transformWSG84ToCartographic(positions[i])
- )
- h += oel.x * el.y - el.x * oel.y
- }
- result = Math.abs(h).toFixed(2)
- }
- return result
- }
-
- /**
- * prepare entity
- */
- _preparePositions(positions) {
- if (typeof positions === 'string') {
- if (positions.indexOf('#') >= 0) {
- throw new Error('the positions invalid')
- }
- positions = positions.split(';')
- }
- this._positions = positions.map(item => {
- if (Array.isArray(item)) {
- return DC.Position.fromCoordArray(item)
- } else {
- return DC.Position.fromCoordString(item)
- }
- })
- }
-
- _prepareHierarchy() {
- let result = new Cesium.PolygonHierarchy()
- result.positions = DC.T.transformWSG84ArrayToCartesianArray(this._positions)
- result.holes = this._holes.map(
- item =>
- new Cesium.PolygonHierarchy(
- DC.T.transformWSG84ArrayToCartesianArray(item)
- )
- )
- return result
- }
-
- _prepareDelegate() {
- /**
- * initialize the Overlay parameter
- */
- this._delegate.polygon = {
- ...this._style,
- hierarchy: new Cesium.CallbackProperty(time => {
- return this._prepareHierarchy()
- })
- }
- this._delegate.layer = this._layer
- this._delegate.overlayId = this._id
- }
-
- _addCallback(layer) {
- this._layer = layer
- this._prepareDelegate()
- this._layer.delegate.entities.add(this._delegate)
- this._state = DC.OverlayState.ADDED
- }
-
- _removeCallback() {
- if (this._layer) {
- this._layer.delegate.entities.remove(this._delegate)
- this._state = DC.OverlayState.REMOVED
- }
- }
-
- /**
- *
- * @param {*} extrudedHeight
- * @param {*} duration
- */
- setExtrudedHeight(extrudedHeight, duration) {
- if (this._delegate.polygon) {
- let now = Cesium.JulianDate.now()
- let oriValue = this._delegate.polygon.extrudedHeight
- ? this._delegate.polygon.extrudedHeight.getValue(now)
- : 0
- let rate = 0
- let stopTime = now
- if (duration) {
- rate = (extrudedHeight - oriValue) / duration
- stopTime = DC.JulianDate.addSeconds(
- now,
- duration,
- new Cesium.JulianDate()
- )
- }
- this._delegate.polygon.extrudedHeight = new Cesium.CallbackProperty(
- time => {
- let result = 0
- if (DC.JulianDate.greaterThan(stopTime, time)) {
- result =
- oriValue +
- (duration - Cesium.JulianDate.secondsDifference(stopTime, time)) *
- rate
- } else {
- result = extrudedHeight
- }
- return result
- }
- )
- }
- return this
- }
-
- /**
- *
- * @param {*} style
- */
- setStyle(style) {
- if (!style || Object.keys(style).length === 0) {
- return
- }
- this._style = style
- this._delegate.polygon && this._delegate.polygon.merge(style)
- return this
- }
-
- static fromEntity(entity) {}
- }
|