Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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