1- import { CesiumZondy } from " ../core/Base" ;
1+ import { CesiumZondy } from ' ../core/Base' ;
22
3- import BaseLayer from " ./BaseLayer" ;
3+ import BaseLayer from ' ./BaseLayer' ;
44
55/**
66 * @author 三维基础平台研发中心·邱文坤
@@ -9,7 +9,6 @@ import BaseLayer from "./BaseLayer";
99 * @classdesc 实体绘制控制器类
1010 * @description 该类实现了实体数据的绘制与删除功能
1111 * @param option.viewer = viewer 视图
12- * @see
1312 */
1413export default class EntityController extends BaseLayer {
1514 constructor ( option ) {
@@ -34,7 +33,7 @@ export default class EntityController extends BaseLayer {
3433 * let point = entityController.appendPoint(115.2, 31, 200, '点', 100, color1, color2, 2);
3534 * // 跳转到点位置
3635 * viewer.flyTo(point);
37- *
36+ *
3837 * @returns {entity } 返回点对象 移除通过removeEntity(entity)
3938 */
4039 appendPoint (
@@ -46,7 +45,7 @@ export default class EntityController extends BaseLayer {
4645 color ,
4746 outlineColor ,
4847 outlineWidth ,
49- description ,
48+ description
5049 ) {
5150 if ( undefined === this . viewer ) {
5251 return undefined ;
@@ -59,9 +58,9 @@ export default class EntityController extends BaseLayer {
5958 pixelSize : pixelSize ,
6059 color : color ,
6160 outlineColor : outlineColor ,
62- outlineWidth : outlineWidth ,
61+ outlineWidth : outlineWidth
6362 } ,
64- description : description ,
63+ description : description
6564 } ) ;
6665 return point ;
6766 }
@@ -80,14 +79,272 @@ export default class EntityController extends BaseLayer {
8079 name : name ,
8180 position : Cesium . Cartesian3 . fromDegrees ( lat , lon , height ) ,
8281 point : new Cesium . PointGraphics ( ) ,
83- description : description ,
82+ description : description
8483 } ;
8584 if ( Cesium . defined ( options ) ) {
8685 Object . extend ( param , options ) ;
8786 }
8887 var point = this . viewer . entities . add ( param ) ;
8988 return point ;
9089 }
90+
91+ /**
92+ * @private
93+ * 添加
94+ * @param {object } entityOption 包含entity中相关选项设置
95+ *{
96+ * id:
97+ * name:
98+ * availability:
99+ * show:
100+ * description:
101+ * position:
102+ * orientation:
103+ * viewFrom:
104+ * parent:
105+ *}
106+ * @returns {entity } 返回点对象 移除通过removeEntity(entity)
107+ */
108+ appendGraphics ( options ) {
109+ if ( ! Cesium . defined ( options ) ) {
110+ return null ;
111+ }
112+ let entity = new Cesium . Entity ( options ) ;
113+ this . viewer . entities . add ( entity ) ;
114+ return entity ;
115+ }
116+
117+ /**
118+ * 画多边形区
119+ * @param {String } name 名称
120+ * @param {Array } points 点数组(顺序是逆时针
121+ * @param {Color } fillColor 区填充色 默认白色半透明
122+ * @param {Color } outlineColor 外框线颜色 默认红色半透明
123+ * @returns {entity } 绘制的多边形区对象 移除通过removeEntity(entity)
124+ * @example
125+ * let entityController = new EntityController({viewer:viewer});
126+ * let arryPoint =[ -115.0,37.0,
127+ -115.0,32.0,
128+ -107.0, 33.0,
129+ -102.0,31.0,
130+ -102.0,35.0];
131+ * let fillColor = new Cesium.Color(1, 0, 0, 1);
132+ * let outlineColor = new Cesium.Color(1, 1, 0, 1);
133+ * let entity = entityController.appendPolygon('1',arryPoint,fillColor,outlineColor);
134+ * viewer.flyTo(entity);
135+
136+ */
137+ appendPolygon ( name , points , fillColor , outlineColor , options ) {
138+ if ( fillColor === undefined ) {
139+ fillColor = Cesium . Color . WHITE . withAlpha ( 0.5 ) ;
140+ }
141+ if ( outlineColor === undefined ) {
142+ outlineColor = Cesium . Color . RED . withAlpha ( 0.5 ) ;
143+ }
144+ var outlineWidth = 20 ;
145+ var height = 0 ;
146+ if ( Cesium . defined ( options ) ) {
147+ outlineWidth = Cesium . defaultValue ( options . outlineWidth , 20 ) ;
148+ height = Cesium . defaultValue ( options . height , 0 ) ;
149+ }
150+ let para = {
151+ name : name ,
152+ polygon : {
153+ hierarchy : Cesium . Cartesian3 . fromDegreesArray ( points ) ,
154+ extrudedHeight : 0 ,
155+ material : fillColor ,
156+ outline : true ,
157+ outlineColor : outlineColor ,
158+ outlineWidth : outlineWidth ,
159+ height : height
160+ }
161+ } ;
162+ if ( Cesium . defined ( options ) ) {
163+ Object . extend ( para , options ) ;
164+ }
165+ let polygon = this . viewer . entities . add ( para ) ;
166+ return polygon ;
167+ }
168+
169+ /**
170+ * 添加带洞多边形(二维)
171+ * @param {String } name 名称
172+ * @param {Array } latLons_out 外圈坐标:[x1,y1,x2,y2,x3,y3]
173+ * @param {Array } latLons_in 内圈Array<[x1,y1,x2,y2,x3,y3]>
174+ * @param {Object } options 参数对象
175+ * @param {Color } options.material 填充颜色 new Cesium.Color(0, 0, 1, 1)
176+ * @returns {entity } 绘制的多边形区对象 移除通过removeEntity(entity)
177+ * @example
178+ * let entityController = new EntityController({viewer:viewer});
179+ * let arryPointOut = [95.1550, 30.8902, 95.1668, 30.8800, 95.1836, 30.8902, 95.1696, 30.91];
180+ * let arrayPointIn =[[95.1617, 30.8902, 95.1668, 30.8882, 95.1766, 30.8939, 95.1696, 30.8996]];
181+ * let material = new Cesium.Color(1, 0, 0, 1);
182+ * let entity = entityController.appendHolePolygon('1',arryPointOut,arrayPointIn,{material:material});
183+ * viewer.flyTo(entity);
184+ */
185+ appendHolePolygon ( name , latLons_out , latLons_in , options ) {
186+ let holeHierars = [ ] ;
187+ if ( Cesium . defined ( latLons_in ) && latLons_in . length > 0 ) {
188+ for ( var i = 0 ; i < latLons_in . length ; i ++ ) {
189+ holeHierars . push (
190+ new Cesium . PolygonHierarchy (
191+ Cesium . Cartesian3 . fromDegreesArray ( latLons_in [ i ] )
192+ )
193+ ) ;
194+ }
195+ }
196+ let polyHierar = new Cesium . PolygonHierarchy (
197+ Cesium . Cartesian3 . fromDegreesArray ( latLons_out ) ,
198+ holeHierars
199+ ) ;
200+ let para = {
201+ name : name ,
202+ polygon : {
203+ hierarchy : polyHierar ,
204+ perPositionHeight : false ,
205+ outline : true ,
206+ outlineColor : Cesium . Color . RED . withAlpha ( 1.0 ) ,
207+ outlineWidth : 10 ,
208+ material : Cesium . Color . BLUE . withAlpha ( 0.5 )
209+ }
210+ } ;
211+ if ( Cesium . defined ( options ) ) {
212+ Object . extend ( para . polygon , options ) ;
213+ }
214+ let polygon = this . viewer . entities . add ( para ) ;
215+ return polygon ;
216+ }
217+
218+ /**
219+ * 添加贴地区
220+ * @param {String } name
221+ * @param {Array } latLons_out 外圈点
222+ * @param {Array } latLons_in 内圈点
223+ * @param {Number } step 插值步长
224+ * @param {Object } options 可扩展参数
225+ * @param {Function } callback 回调
226+ */
227+ appendHolePolygonOnTerrain (
228+ name ,
229+ latLons_out ,
230+ latLons_in ,
231+ step ,
232+ options ,
233+ callback
234+ ) {
235+ let cartographics = [ ] ;
236+ let ellipsoid = this . viewer . scene . globe . ellipsoid ;
237+
238+ let num = latLons_out . length / 2 ;
239+ let positions = [ ] ;
240+ for ( let i in num ) {
241+ positions . push (
242+ new Cesium . Cartesian2 (
243+ latLons_out [ i * 2 ] ,
244+ latLons_out [ 2 * i + 1 ]
245+ )
246+ ) ;
247+ }
248+ let pnts = CommFunction . linearInterpolate ( positions , step ) ;
249+
250+ if ( pnts !== null && pnts . length > 0 ) {
251+ for ( let j in pnts ) {
252+ let cartographic = ellipsoid . cartesianToCartographic (
253+ Cesium . Cartesian3 . fromDegrees (
254+ pnts [ j ] . x ,
255+ pnts [ j ] . y ,
256+ 0 ,
257+ ellipsoid
258+ )
259+ ) ;
260+ cartographics . push ( cartographic ) ;
261+ }
262+ }
263+
264+ let outNum = [ ] ;
265+ outNum . push ( cartographics . length ) ; //外圈点数
266+
267+ if ( defined ( latLons_in ) && latLons_in . length > 0 ) {
268+ for ( let k in latLons_in ) {
269+ let lonLatArr_hole = latLons_in [ k ] ;
270+ let num_hole = lonLatArr_hole . length / 2 ;
271+ let positions_hole = [ ] ;
272+ for ( var m in num_hole ) {
273+ positions_hole . push (
274+ new Cesium . Cartesian2 (
275+ lonLatArr_hole [ m * 2 ] ,
276+ lonLatArr_hole [ 2 * m + 1 ]
277+ )
278+ ) ;
279+ }
280+ let pnts_hole = CommFunction . linearInterpolate (
281+ positions_hole ,
282+ step
283+ ) ;
284+
285+ if ( pnts_hole !== null && pnts_hole . length > 0 ) {
286+ for ( let n in pnts_hole ) {
287+ let cartographic_tem = ellipsoid . cartesianToCartographic (
288+ Cesium . Cartesian3 . fromDegrees (
289+ pnts_hole [ n ] . x ,
290+ pnts_hole [ n ] . y ,
291+ 0 ,
292+ ellipsoid
293+ )
294+ ) ;
295+ cartographics . push ( cartographic_tem ) ;
296+ }
297+ outNum . push ( cartographics . length ) ;
298+ }
299+ positions_hole . length = 0 ;
300+ }
301+ }
302+
303+ let viewer = this . viewer ;
304+
305+ let promise = Cesium . sampleTerrain (
306+ viewer . terrainProvider ,
307+ 16 ,
308+ cartographics
309+ ) ;
310+ when ( promise , function ( updatedPositions ) {
311+ let cartesianPositions = ellipsoid . cartographicArrayToCartesianArray (
312+ updatedPositions
313+ ) ;
314+
315+ let outPos = cartesianPositions . slice ( 0 , outNum [ 0 ] ) ;
316+ let inPolygonHierarchyArr = [ ] ;
317+ for ( var i in outNum ) {
318+ inPolygonHierarchyArr . push (
319+ new Cesium . PolygonHierarchy (
320+ cartesianPositions . slice ( outNum [ i - 1 ] , outNum [ i ] )
321+ )
322+ ) ;
323+ }
324+ let polyHierar = new Cesium . PolygonHierarchy (
325+ outPos ,
326+ inPolygonHierarchyArr
327+ ) ;
328+ let para = {
329+ name : name ,
330+ polygon : {
331+ hierarchy : polyHierar ,
332+ perPositionHeight : true ,
333+ outline : true ,
334+ outlineColor : Cesium . Color . RED . withAlpha ( 0.5 ) ,
335+ outlineWidth : 10 ,
336+ material : Cesium . Color . BLUE . withAlpha ( 0.5 )
337+ }
338+ } ;
339+ if ( Cesium . defined ( options ) ) {
340+ Object . extend ( para , options ) ;
341+ }
342+ let retultEntity = this . viewer . entities . add ( para ) ;
343+ if ( typeof callback === 'function' ) {
344+ callback ( retultEntity ) ;
345+ }
346+ } ) ;
347+ }
91348}
92349
93350CesiumZondy . Layer . EntityController = EntityController ;
0 commit comments