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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * @Author: Caven
  3. * @Date: 2020-01-09 09:10:37
  4. * @Last Modified by: Caven
  5. * @Last Modified time: 2020-02-04 15:24:43
  6. */
  7. import Overlay from '../Overlay'
  8. import Cesium from '@/namespace'
  9. DC.Polygon = class extends Overlay {
  10. constructor(positions) {
  11. if (
  12. !positions ||
  13. (typeof positions !== 'string' && !Array.isArray(positions))
  14. ) {
  15. throw new Error('the positions invalid')
  16. }
  17. super()
  18. this._positions = []
  19. this._holes = []
  20. this._preparePositions(positions)
  21. this._delegate = new Cesium.Entity()
  22. this._state = DC.OverlayState.INITIALIZED
  23. this.type = DC.OverlayType.POLYGON
  24. }
  25. set positions(positions) {
  26. this._preparePositions(positions)
  27. }
  28. get positions() {
  29. return this._positions
  30. }
  31. set holes(holes) {
  32. if (holes && holes.length) {
  33. this._holes = holes.map(item => this._preparePositions(item))
  34. }
  35. }
  36. get holes() {
  37. return this._holes
  38. }
  39. get center() {
  40. let boundingSphere = Cesium.BoundingSphere.fromPoints(
  41. DC.T.transformWSG84ArrayToCartesianArray(this._positions)
  42. )
  43. return DC.T.transformCartesianToWSG84(boundingSphere.center)
  44. }
  45. get area() {
  46. let result = 0
  47. if (this._positions) {
  48. let h = 0
  49. let ellipsoid = Cesium.Ellipsoid.WGS84
  50. let positions = [...this._positions]
  51. positions.push(positions[0])
  52. for (let i = 1; i < positions.length; i++) {
  53. let oel = ellipsoid.cartographicToCartesian(
  54. DC.T.transformWSG84ToCartographic(positions[i - 1])
  55. )
  56. let el = ellipsoid.cartographicToCartesian(
  57. DC.T.transformWSG84ToCartographic(positions[i])
  58. )
  59. h += oel.x * el.y - el.x * oel.y
  60. }
  61. result = Math.abs(h).toFixed(2)
  62. }
  63. return result
  64. }
  65. /**
  66. * prepare entity
  67. */
  68. _preparePositions(positions) {
  69. if (typeof positions === 'string') {
  70. if (positions.indexOf('#') >= 0) {
  71. throw new Error('the positions invalid')
  72. }
  73. positions = positions.split(';')
  74. }
  75. this._positions = positions.map(item => {
  76. if (Array.isArray(item)) {
  77. return DC.Position.fromCoordArray(item)
  78. } else {
  79. return DC.Position.fromCoordString(item)
  80. }
  81. })
  82. }
  83. _prepareHierarchy() {
  84. let result = new Cesium.PolygonHierarchy()
  85. result.positions = DC.T.transformWSG84ArrayToCartesianArray(this._positions)
  86. result.holes = this._holes.map(
  87. item =>
  88. new Cesium.PolygonHierarchy(
  89. DC.T.transformWSG84ArrayToCartesianArray(item)
  90. )
  91. )
  92. return result
  93. }
  94. _prepareDelegate() {
  95. /**
  96. * initialize the Overlay parameter
  97. */
  98. this._delegate.polygon = {
  99. ...this._style,
  100. hierarchy: new Cesium.CallbackProperty(time => {
  101. return this._prepareHierarchy()
  102. })
  103. }
  104. this._delegate.layer = this._layer
  105. this._delegate.overlayId = this._id
  106. }
  107. _addCallback(layer) {
  108. this._layer = layer
  109. this._prepareDelegate()
  110. this._layer.delegate.entities.add(this._delegate)
  111. this._state = DC.OverlayState.ADDED
  112. }
  113. _removeCallback() {
  114. if (this._layer) {
  115. this._layer.delegate.entities.remove(this._delegate)
  116. this._state = DC.OverlayState.REMOVED
  117. }
  118. }
  119. /**
  120. *
  121. * @param {*} extrudedHeight
  122. * @param {*} duration
  123. */
  124. setExtrudedHeight(extrudedHeight, duration) {
  125. if (this._delegate.polygon) {
  126. let now = Cesium.JulianDate.now()
  127. let oriValue = this._delegate.polygon.extrudedHeight
  128. ? this._delegate.polygon.extrudedHeight.getValue(now)
  129. : 0
  130. let rate = 0
  131. let stopTime = now
  132. if (duration) {
  133. rate = (extrudedHeight - oriValue) / duration
  134. stopTime = DC.JulianDate.addSeconds(
  135. now,
  136. duration,
  137. new Cesium.JulianDate()
  138. )
  139. }
  140. this._delegate.polygon.extrudedHeight = new Cesium.CallbackProperty(
  141. time => {
  142. let result = 0
  143. if (DC.JulianDate.greaterThan(stopTime, time)) {
  144. result =
  145. oriValue +
  146. (duration - Cesium.JulianDate.secondsDifference(stopTime, time)) *
  147. rate
  148. } else {
  149. result = extrudedHeight
  150. }
  151. return result
  152. }
  153. )
  154. }
  155. return this
  156. }
  157. /**
  158. *
  159. * @param {*} style
  160. */
  161. setStyle(style) {
  162. if (!style || Object.keys(style).length === 0) {
  163. return
  164. }
  165. this._style = style
  166. this._delegate.polygon && this._delegate.polygon.merge(style)
  167. return this
  168. }
  169. static fromEntity(entity) {}
  170. }