OSGEARTH3 加载SHP矢量文件

方法一:earth文件配置

在*.earth文件中增加一个图层节点,加载shp文件:

<OGRFeatures name="china">
    <url>shp/china.shp</url>
  </OGRFeatures>
  <FeatureImage name="国界">
    <features>china</features>
    <styles>
      <style type="text/css">
        default {
        stroke: #deaaf6;
        stroke-width: 2px;
        altitude-clamping: terrain;
        altitude-technique: scene;
        render-lighting: false;
        render-depth-test: true;
        render-depth-offset-min-bias: 10000.0f;
        }
      </style>
    </styles>
  </FeatureImage>

在这里插入图片描述

方法二:矢量图层代码

使用代码的方式加载矢量文件,作为图层:

int main(int argc, char** argv)
{
	OGRRegisterAll();
	GDALAllRegister();
	CPLSetConfigOption("GDAL_DATA", "../../Data/gdal_data");
	CPLSetConfigOption("CPL_DEBUG", "YES");
	CPLSetConfigOption("CPL_LOG", "../LOG/gdal.log");

	osgEarth::initialize();

	LoadResourceNS::LoadShape();
	
	return 0;
}


void LoadResourceNS::LoadShape()
{
	// map
	osg::Node* globe = osgDB::readNodeFile("../../Data/3d-data/Data/earth/FreeEarth_flat_simple.earth");
	osgEarth::MapNode* mapNode = osgEarth::MapNode::get(globe);
	osgEarth::Map* map = mapNode->getMap();

	// shp layer
	osg::ref_ptr<osgEarth::OGRFeatureSource> features = new osgEarth::OGRFeatureSource();
	features->setURL("../../Data/3d-data/Data/shp/china.shp");

	// -- 定义要素数据的样式:配置线符号化器:
	osgEarth::Style style;

	// -- 可见性
	osgEarth::RenderSymbol* rs = style.getOrCreate<osgEarth::RenderSymbol>();
	rs->depthTest() = false;

	// -- 贴地设置
	osgEarth::AltitudeSymbol* alt = style.getOrCreate<osgEarth::AltitudeSymbol>();
	alt->clamping() = alt->CLAMP_TO_TERRAIN;
	alt->technique() = alt->TECHNIQUE_DRAPE;

	osgEarth::LineSymbol* ls = style.getOrCreateSymbol<osgEarth::LineSymbol>();
	ls->stroke()->color() = osgEarth::Color::Yellow;
	ls->stroke()->width() = 2.0f;
	ls->tessellationSize()->set(10000, osgEarth::Units::KILOMETERS);

// 	osgEarth::PolygonSymbol* polygonsymol = style.getOrCreateSymbol<osgEarth::PolygonSymbol>();
// 	polygonsymol->fill()->color() = osgEarth::Color(152.0f / 255, 251.0f / 255, 152.0f / 255, 0.8f); //238 230 133
// 	polygonsymol->outline() = true;

	// -- 将要素的路径添加到图层里
	osgEarth::FeatureImageLayer* layer = new osgEarth::FeatureImageLayer();
	layer->setFeatureSource(features);

	// -- 将style风格加载到图层中
	osgEarth::StyleSheet* sheet = new osgEarth::StyleSheet();
	sheet->addStyle(style);
	layer->setStyleSheet(sheet);
	map->addLayer(layer);

	// 已加入的Layers
	osgEarth::LayerVector layers;
	map->getLayers(layers);
	for (osgEarth::LayerVector::const_iterator i = layers.begin(); i != layers.end(); ++i)
	{
		std::cout << (*i)->getName() << " : " << (*i)->getStatus().toString() << std::endl;
	}

	// viewer
	osgViewer::Viewer viewer;
	viewer.setSceneData(mapNode);

	// manipulator
	osg::ref_ptr<osgEarth::Util::EarthManipulator> mainManipulator = new osgEarth::Util::EarthManipulator;
	viewer.setCameraManipulator(mainManipulator);

	// run
	viewer.setUpViewInWindow(100, 100, 800, 600);
	viewer.run();
}

在这里插入图片描述

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容