You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-01-09 09:10:37
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import State from '@dc-modules/state/State'
  7. import Parse from '@dc-modules/parse/Parse'
  8. import { Util } from '@dc-modules/utils'
  9. import { Transform } from '@dc-modules/transform'
  10. import { center, area } from '@dc-modules/math'
  11. import Overlay from '../Overlay'
  12. class Polygon extends Overlay {
  13. constructor(positions) {
  14. super()
  15. this._delegate = new Cesium.Entity({ polygon: {} })
  16. this._positions = Parse.parsePositions(positions)
  17. this._holes = []
  18. this._state = State.INITIALIZED
  19. }
  20. get type() {
  21. return Overlay.getOverlayType('polygon')
  22. }
  23. set positions(positions) {
  24. this._positions = Parse.parsePositions(positions)
  25. this._delegate.polygon.hierarchy = this._computeHierarchy()
  26. return this
  27. }
  28. get positions() {
  29. return this._positions
  30. }
  31. set holes(holes) {
  32. if (holes && holes.length) {
  33. this._holes = holes.map(item => Parse.parsePositions(item))
  34. this._delegate.polygon.hierarchy = this._computeHierarchy()
  35. }
  36. return this
  37. }
  38. get holes() {
  39. return this._holes
  40. }
  41. get center() {
  42. return center([...this._positions, this._positions[0]])
  43. }
  44. get area() {
  45. return area(this._positions)
  46. }
  47. /**
  48. *
  49. * @private
  50. */
  51. _computeHierarchy() {
  52. let result = new Cesium.PolygonHierarchy()
  53. result.positions = Transform.transformWGS84ArrayToCartesianArray(
  54. this._positions
  55. )
  56. result.holes = this._holes.map(
  57. item =>
  58. new Cesium.PolygonHierarchy(
  59. Transform.transformWGS84ArrayToCartesianArray(item)
  60. )
  61. )
  62. return result
  63. }
  64. _mountedHook() {
  65. /**
  66. * initialize the Overlay parameter
  67. */
  68. this.positions = this._positions
  69. }
  70. /**
  71. * Sets text
  72. * @param text
  73. * @param textStyle
  74. * @returns {Polygon}
  75. */
  76. setLabel(text, textStyle) {
  77. this._delegate.position = Transform.transformWGS84ToCartesian(this.center)
  78. this._delegate.label = {
  79. text: text,
  80. ...textStyle
  81. }
  82. return this
  83. }
  84. /**
  85. * Sets style
  86. * @param style
  87. * @returns {Polygon}
  88. */
  89. setStyle(style) {
  90. if (!style || Object.keys(style).length === 0) {
  91. return this
  92. }
  93. delete style['positions']
  94. Util.merge(this._style, style)
  95. Util.merge(this._delegate.polygon, style)
  96. return this
  97. }
  98. /**
  99. * Parse from entity
  100. * @param entity
  101. * @returns {any}
  102. */
  103. static fromEntity(entity) {
  104. let polygon = undefined
  105. let now = Cesium.JulianDate.now()
  106. if (entity.polygon) {
  107. let positions = Transform.transformCartesianArrayToWGS84Array(
  108. entity.polygon.hierarchy.getValue(now).positions
  109. )
  110. polygon = new Polygon(positions)
  111. polygon.attr = {
  112. ...entity?.properties?.getValue(now)
  113. }
  114. }
  115. return polygon
  116. }
  117. }
  118. Overlay.registerType('polygon')
  119. export default Polygon