Cesium 中对entities的查询,删除操作

本文详细介绍了在Cesium库中如何通过ID查询entities,提供viewer.entities.getById()方法以及删除entities的方法,包括removeById(myId)和entities.removeAll(),并展示了如何创建并给entities分配ID。

1. Cesium 中对entities的查询,删除操作

在Cesium 中我们如何对entities进行查询,和删除操作

查询操作

viewer.entities.getById('myId')

删除操作

viewer.entities.removeById('myId');

全部删除

viewer.entities.removeAll();

这里要传入一个ID,这个ID就是我们在创建 entities 时的ID

const redBox = viewer.entities.add({
    name: '一个红色的正方形',
    position: Cesium.Cartesian3.fromDegrees(-107.0, 40.0, 0.0),
    id: 'myId', // 就是这个
    box: {
      dimensions: new Cesium.Cartesian3(40000, 30000, 50000), // 用来定义这个 正方形的 长,宽,高
      material: Cesium.Color.RED.withAlpha(0.5), // 用来定义这个正方形的,材质表现形式
      outline: true, // 是否显示边框
      outlineColor: Cesium.Color.BLACK, // 边框线,颜色。
      // shadows: true
    }
  })
要在 Cesium 中将一个局部多边形压平,可以使用地形提取工具(TerrainProvider)和高程调整工具(ElevationSampler)。具体步骤如下: 1. 创建一个局部多边形的 Entity 对象,并将其添加到场景中。 2. 获取该 Entity 对象的位置、高度和边界框信息。 3. 创建一个地形提取工具(例如 CesiumTerrainProvider),并设置其参数,例如数据源和最大 LOD 等级。 4. 创建一个高程调整工具(例如 QuantizedMeshTerrainData),并设置其参数,例如局部多边形的位置和边界框信息。 5. 将地形提取工具和高程调整工具绑定起来,生成一个压平后的局部多边形模型(例如 QuantizedMesh)。 6. 将压平后的局部多边形模型添加到场景中,并删除原来的局部多边形 Entity 对象。 下面是一个示例代码: ``` var polygonEntity = viewer.entities.add({ polygon: { hierarchy: Cesium.Cartesian3.fromDegreesArray([ -72.0, 40.0, -70.0, 35.0, -75.0, 30.0, -70.0, 30.0, -68.0, 40.0 ]), height: 0, material: Cesium.Color.RED.withAlpha(0.5) } }); var position = polygonEntity.position.getValue(viewer.clock.currentTime); var height = polygonEntity.polygon.height.getValue(viewer.clock.currentTime); var boundingBox = Cesium.Entity.computeBoundingBox(polygonEntity, viewer.clock.currentTime); var min = boundingBox.minimum; var max = boundingBox.maximum; var terrainProvider = new Cesium.CesiumTerrainProvider({ url: 'https://assets.agi.com/stk-terrain/v1/tilesets/world/tiles' }); var elevationSampler = new Cesium.ElevationSampler(terrainProvider); elevationSampler.setSamplingOptions({ extent: Cesium.Rectangle.fromDegrees(min.x, min.y, max.x, max.y) }); var vertices = []; var indices = []; var vertexCount = 0; for (var i = 0; i < polygonEntity.polygon.hierarchy.getValue().positions.length; i++) { var position = polygonEntity.polygon.hierarchy.getValue().positions[i]; var cartographic = Cesium.Cartographic.fromCartesian(position); var elevation = elevationSampler.sampleHeight(cartographic); vertices.push(position.x, position.y, elevation); vertexCount++; } var mesh = new Cesium.QuantizedMesh({ vertices: new Uint16Array(vertices), indices: new Uint16Array(indices), boundingSphere: Cesium.BoundingSphere.fromVertices(vertices) }); var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(position); var model = viewer.scene.primitives.add(new Cesium.Primitive({ geometryInstances: new Cesium.GeometryInstance({ geometry: mesh, modelMatrix: modelMatrix }), appearance: new Cesium.MaterialAppearance({ material: Cesium.Material.fromType('Color', { color: Cesium.Color.RED.withAlpha(0.5) }) }) })); viewer.entities.remove(polygonEntity); ``` 在这个示例代码中,我们创建了一个红色透明的多边形 Entity 对象,并获取了其位置、高度和边界框信息。然后,我们创建了一个地形提取工具和一个高程调整工具,并使用它们生成了一个压平后的局部多边形模型,并将其添加到场景中。最后,我们删除了原来的多边形 Entity 对象。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值