cesium黄河怎么绘制
时间: 2025-05-05 11:11:40 浏览: 20
### 如何在Cesium中绘制黄河
为了实现黄河这一地理要素的可视化,在Cesium环境中可以通过加载GeoJSON文件来表示河流路径。由于黄河是一条重要的中国河流,其坐标数据可以预先准备成GeoJSON格式并导入到项目中[^1]。
下面提供一段Python脚本用于生成简单的GeoJSON格式的数据片段,该片段仅作示意用途:
```python
import json
# 假设这是部分黄河的经纬度坐标列表 (经度, 纬度)
coordinates = [
[101.789586, 36.06114],
[102.789586, 35.06114],
# 更多坐标...
]
geojson_data = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": coordinates
}
}]
}
with open('yellow_river.geojson', 'w') as f:
json.dump(geojson_data, f)
print("GeoJSON file has been created.")
```
接着是在JavaScript端使用上述生成好的`yellow_river.geojson`文件,并将其添加至Cesium场景中的方法:
```javascript
// 加载 GeoJSON 文件
const viewer = new Cesium.Viewer('cesiumContainer');
Cesium.GeoJsonDataSource.load('./data/yellow_river.geojson').then(function(dataSource){
viewer.dataSources.add(dataSource);
// 设置视角聚焦于黄河位置
const entities = dataSource.entities.values;
let rectangle = Cesium.Rectangle.fromDegrees(
...entities.reduce((acc, entity) => {
if (!entity.polygon || !entity.polygon.hierarchy) return acc;
const positions = Cesium.PolygonPipeline.computeCartesianPositions(viewer.scene.globe.ellipsoid,
entity.polygon.hierarchy.getValue(Cesium.JulianDate.now()), false);
return [...acc, ...positions.map(cart => cart.toDegrees())];
}, []).flat()
);
viewer.camera.flyTo({
destination : rectangle,
orientation : {
heading : Cesium.Math.toRadians(0),
pitch : Cesium.Math.toRadians(-90),
roll : 0.0
},
duration: 2
});
});
```
此代码段展示了如何读取本地存储的一个名为`yellow_river.geojson`的文件,并把它加入到当前的地图实例当中去显示出来;同时也调整了相机的位置以便更好地查看这条河。
阅读全文
相关推荐
















