<?xml version="1.0" encoding="utf-8"?> <map name="Offline Map with TMS"> <options> <terrain_engine>osgEarth::Drivers::TerrainEngineNode</terrain_engine> </options> <!-- 原有的GDAL图层 --> <GDALImage name="World GeoTIFF"> <url>../data/world.tif</url> </GDALImage> <!-- 新增的离线TMS地图图层 --> <image name="Local TMS Map" driver="tms"> <url>file:///D:/OSG/OSGTEST/EPSG4326/tms.xml</url> <format>auto</format> <profile> <srs>wgs84</srs> <!-- EPSG:4326 --> </profile> <!-- 修改后的缓存配置 --> <cache policy="persist" enabled="false"> <path>D:/OSG/OSGTEST/EPSG4326/tms_cache</path> <create_policy>create_once</create_policy> <format>tms</format> </cache> <!-- 错误处理和调试信息 --> <on_error>report</on_error> <debug>true</debug> </image> <!-- 地图初始视图配置 --> <initial_viewpoint> <heading>0</heading> <pitch>-30</pitch> <range>500000</range> <longitude>0</longitude> <latitude>0</latitude> </initial_viewpoint> </map> osgEarth 的 C++ 如何加载
时间: 2025-05-31 14:52:19 浏览: 38
### 使用 C++ 在 osgEarth 中加载离线 TMS 瓦片地图
#### 配置 `.earth` 文件加载离线 TMS 地图
osgEarth 可以通过读取 `.earth` 文件来加载离线 TMS 瓦片地图。以下是一个典型的 `.earth` 文件配置示例:
```xml
<?xml version="1.0" encoding="utf-8"?>
<map name="TMS Offline Example">
<!-- 定义影像图层 -->
<image driver="tms">
<url>file:///E:/osgearth/tiles/{z}/{x}/{y}.png</url>
<tilesize>256</tilesize>
<profile srs="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"/>
</image>
<!-- 如果有高程数据,可以添加如下高程图层 -->
<elevation driver="gdal">
<uri>file:///E:/osgearth/elevation/dem.tif</uri>
</elevation>
</map>
```
在这个例子中,`{z}/{x}/{y}` 是标准的 TMS 切片路径模式[^1]。确保替换 `file:///E:/osgearth/tiles/` 为你自己的瓦片存储路径。
---
#### 使用 C++ API 加载离线 TMS 地图
除了通过 `.earth` 文件加载之外,也可以直接使用 osgEarth 的 C++ API 来创建和加载离线 TMS 地图。下面提供了一个完整的代码示例:
```cpp
#include <osgEarth/Map>
#include <osgEarth/TerrainEngineNode>
#include <osgEarthDrivers/driver_registry>
#include <iostream>
// 主函数入口
int main(int argc, char** argv)
{
// 创建一个新的 Map 对象
osg::ref_ptr<osgEarth::Map> map = new osgEarth::Map();
// 添加 TMS 影像图层
{
osgEarth::ImageLayerOptions imgOpts("offline_tms_layer");
imgOpts.driver() = "tms"; // 设置驱动为 tms
imgOpts.url() = "file:///E:/osgearth/tiles/{z}/{x}/{y}.png"; // 替换为实际路径
imgOpts.tileSize() = 256; // 设置瓦片大小为 256 像素
imgOpts.profile()->srs() = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"; // 设置投影参数
map->addImageLayer(new osgEarth::ImageLayer(imgOpts)); // 将图层添加到地图对象中
}
// 如果存在高程数据,则添加 GDAL 高程图层
{
osgEarth::ElevationLayerOptions elevOpts("dem_elevation_layer");
elevOpts.driver() = "gdal"; // 设置驱动为 gdal
elevOpts.uri() = "file:///E:/osgearth/elevation/dem.tif"; // 替换为实际 DEM 数据路径
map->addElevationLayer(new osgEarth::ElevationLayer(elevOpts)); // 将高程图层添加到地图对象中
}
// 创建 TerrainEngineNode 并绑定地图
osg::ref_ptr<osgEarth::TerrainEngineNode> terrainNode =
new osgEarth::TerrainEngineNode(map.get());
// 初始化 OSG Viewer 并运行应用程序
osgViewer::Viewer viewer;
viewer.setSceneData(terrainNode); // 设置场景节点
return viewer.run(); // 启动渲染循环
}
```
这段代码展示了如何通过 C++ API 动态创建一个包含离线 TMS 图像图层和 GDAL 高程图层的地图,并将其显示出来[^1]。
---
#### 关键点说明
1. **TMS 图像图层选项 (`ImageLayerOptions`)**
- `driver`: 指定使用的驱动程序名称,在这里是 `"tms"`。
- `url`: 指向本地磁盘上的 TMS 瓦片目录,采用 `{z}/{x}/{y}` 结构。
- `tileSize`: 设定瓦片尺寸,默认为 256×256 像素。
- `profile.srs`: 定义空间参考系统 (SRS),这里使用的是墨卡托投影。
2. **GDAL 高程图层选项 (`ElevationLayerOptions`)**
- `driver`: 指定为 `"gdal"`,表示使用 GDAL 驱动器。
- `uri`: 指向本地磁盘上的 DEM 数据文件(通常是 GeoTIFF 格式)。
3. **地形引擎节点 (`TerrainEngineNode`)**
- 这是 osgEarth 渲染的核心组件之一,负责管理和绘制三维地形模型。
---
#### 注意事项
- 确保本地瓦片目录结构严格遵循 `{z}/{x}/{y}` 的命名规则。
- 所有的依赖库(如 GDAL 和 GEOS)应正确安装并配置环境变量[^2]。
- 若未指定高程图层,则默认会生成平坦地形。
---
阅读全文
相关推荐















