| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- /**
- * @Author: Caven
- * @Date: 2020-01-09 09:10:37
- */
-
- import { Cesium } from '@dc-modules/namespace'
- import State from '@dc-modules/state/State'
- import Parse from '@dc-modules/parse/Parse'
- import { Util } from '@dc-modules/utils'
- import { Transform } from '@dc-modules/transform'
- import { center, area } from '@dc-modules/math'
- import Overlay from '../Overlay'
-
- class Polygon extends Overlay {
- constructor(positions) {
- super()
- this._delegate = new Cesium.Entity({ polygon: {} })
- this._positions = Parse.parsePositions(positions)
- this._holes = []
- this._state = State.INITIALIZED
- }
-
- get type() {
- return Overlay.getOverlayType('polygon')
- }
-
- set positions(positions) {
- this._positions = Parse.parsePositions(positions)
- this._delegate.polygon.hierarchy = this._computeHierarchy()
- return this
- }
-
- get positions() {
- return this._positions
- }
-
- set holes(holes) {
- if (holes && holes.length) {
- this._holes = holes.map(item => Parse.parsePositions(item))
- this._delegate.polygon.hierarchy = this._computeHierarchy()
- }
- return this
- }
-
- get holes() {
- return this._holes
- }
-
- get center() {
- return center([...this._positions, this._positions[0]])
- }
-
- get area() {
- return area(this._positions)
- }
-
- /**
- *
- * @private
- */
- _computeHierarchy() {
- let result = new Cesium.PolygonHierarchy()
- result.positions = Transform.transformWGS84ArrayToCartesianArray(
- this._positions
- )
- result.holes = this._holes.map(
- item =>
- new Cesium.PolygonHierarchy(
- Transform.transformWGS84ArrayToCartesianArray(item)
- )
- )
- return result
- }
-
- _mountedHook() {
- /**
- * initialize the Overlay parameter
- */
- this.positions = this._positions
- }
-
- /**
- * Sets text
- * @param text
- * @param textStyle
- * @returns {Polygon}
- */
- setLabel(text, textStyle) {
- this._delegate.position = Transform.transformWGS84ToCartesian(this.center)
- this._delegate.label = {
- text: text,
- ...textStyle
- }
- return this
- }
-
- /**
- * Sets style
- * @param style
- * @returns {Polygon}
- */
- setStyle(style) {
- if (!style || Object.keys(style).length === 0) {
- return this
- }
- delete style['positions']
- this._style = style
- Util.merge(this._delegate.polygon, this._style)
- return this
- }
-
- /**
- * Parse from entity
- * @param entity
- * @returns {any}
- */
- static fromEntity(entity) {
- let polygon = undefined
- let now = Cesium.JulianDate.now()
- if (entity.polygon) {
- let positions = Transform.transformCartesianArrayToWGS84Array(
- entity.polygon.hierarchy.getValue(now).positions
- )
- polygon = new Polygon(positions)
- polygon.attr = {
- ...entity?.properties?.getValue(now)
- }
- }
- return polygon
- }
- }
-
- Overlay.registerType('polygon')
-
- export default Polygon
|