基於Osgearth對象實現圖像的添加/刪除功能
(1)添加圖像
通過Gdal添加GeoTiff圖像,代碼如下:
osgEarth::Drivers::GDALOptions layerOpt;
layerOpt.url() = osgEarth::URI("文件路徑");
osgEarth::ImageLayerOptions imgOpt("圖層名稱", layerOpt);
imgOpt.noDataValue() = 255;//可透明指定顏色,255爲白色
osg::ref_ptr<osgEarth::ImageLayer> newLayer = new osgEarth::ImageLayer(imgOpt);
if (newLayer.valid() && newLayer->open().isOK())
{
m_MapNode->getMap()->beginUpdate();
m_MapNode->getMap()->addLayer(newLayer.get());
m_MapNode->getMap()->endUpdate();
}
(2)刪除圖像
代碼如下:
osg::ref_ptr<osgEarth::ImageLayer> ImgLayer = dynamic_cast<osgEarth::ImageLayer*>(m_MapNode->getMap()->getLayerByName("名稱"));//根據名稱刪除圖像,通過ImgLayer->getName()獲得
osg::ref_ptr<osgEarth::ImageLayer> ImgLayer = dynamic_cast<osgEarth::ImageLayer*>(m_MapNode->getMap()->getLayerByUID("UID"));//根據UID刪除圖像,ImgLayer->getUID()獲得
if (ImgLayer.valid())
{
m_MapNode->getMap()->removeLayer(ImgLayer.get());
}
與Osgearth2.7版本相比,
(1)Osgearth2.10對圖像、高程、矢量等數據統一按Layer處理
(2)Osgearth2.7採用mp引擎,添加/刪除圖像後整個窗口,用戶體驗較差;Osgearth2.10採用Rex引擎,添加/刪除圖像後的刷新效果要好許多,可滿足實時添加圖像需求,但添加高程數據仍會刷新整個窗口。
(3)Osgearth2.7獲取圖像地理範圍的方式爲newLayer->getTileSource()->getDataExtentsUnion();Osgearth2.10獲取圖像地理範圍的方式爲newLayer->getDataExtentsUnion();