osgEarth2.10 加载arcgis瓦片数据和矢量shp数据

1、效果图如下:
osgEarth2.10 加载arcgis瓦片数据和矢量shp数据_第1张图片
osgEarth2.10 加载arcgis瓦片数据和矢量shp数据_第2张图片
2、直接贴源码如下:

// MyOSGEarth.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include 
#include 
using namespace std;

#include    
#include 
#include    
#include    
#include    
#include    
#include    
#include  //事件监听
#include  //事件响应类,对渲染状态进行控制

#include 
#include 
#include 
#include 

#include 
#include 
#include 

#include 
#include 
#include 
#include 

#pragma comment(lib, "OpenThreadsd.lib")
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgUtild.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "osgTextd.lib")
#pragma comment(lib, "osgSimd.lib")

#pragma comment(lib, "osgEarthd.lib")
#pragma comment(lib, "osgEarthFeaturesd.lib")
#pragma comment(lib, "osgEarthUtild.lib")
#pragma comment(lib, "osgEarthSymbologyd.lib")


void AddAnno(std::string filePath, osg::ref_ptr<osgEarth::Map> map)
{
	osgEarth::Symbology::Style labelStyle;

	osgEarth::Symbology::TextSymbol* text = labelStyle.getOrCreateSymbol<osgEarth::Symbology::TextSymbol>();
	string name = "[行政区划_c]";		//如果需要显示汉字,则需要转换成UTF-8编码
	text->content() = osgEarth::Symbology::StringExpression(name);
	//text->priority() = osgEarth::NumericExpression("[pop_cntry]");
	text->size() = 16.0f;
	text->alignment() = osgEarth::Symbology::TextSymbol::ALIGN_CENTER_CENTER;
	text->fill()->color() = osgEarth::Symbology::Color::White;
	text->halo()->color() = osgEarth::Symbology::Color::Red;
	text->encoding() = osgEarth::Symbology::TextSymbol::ENCODING_UTF8;
	string fontFile = "C:/Boot/Fonts/SongTi.ttf";
	text->font() = fontFile;			//如果显示汉字,需要支持中文字库的字体

	// and configure a model layer:
	osgEarth::Features::FeatureModelLayerOptions fmlOpt;
	fmlOpt.name() = filePath + "_labels";
	fmlOpt.featureSourceLayer() = filePath + "_source";
	fmlOpt.styles() = new osgEarth::Symbology::StyleSheet();
	fmlOpt.styles()->addStyle(labelStyle);

	osg::ref_ptr<osgEarth::Features::FeatureModelLayer> fml = new osgEarth::Features::FeatureModelLayer(fmlOpt);
	map->addLayer(fml);
}

void AddVector(osg::ref_ptr<osgEarth::Map> map)
{
	std::string filePath = "D:/OSGCore/data/HN/HN.shp";
	osgEarth::Drivers::OGRFeatureOptions featureData;
	featureData.url() = filePath;

	// Make a feature source layer and add it to the Map:
	osgEarth::Features::FeatureSourceLayerOptions ogrLayer;
	ogrLayer.name() = filePath + "_source";
	ogrLayer.featureSource() = featureData;
	osgEarth::Features::FeatureSourceLayer*  featureSourceLayer = new osgEarth::Features::FeatureSourceLayer(ogrLayer);
	map->addLayer(featureSourceLayer);
	osgEarth::Features::FeatureSource *features = featureSourceLayer->getFeatureSource();
	if (!features)
	{
		printf(("无法打开该矢量文件!"));
		return;
	}

	//设置样式
	osgEarth::Symbology::Style style;

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

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

	//设置矢量面样式(包括边界线)
	osgEarth::Symbology::LineSymbol* ls = style.getOrCreateSymbol<osgEarth::Symbology::LineSymbol>();
	ls->stroke()->color() = osgEarth::Symbology::Color("#FA8072");
	ls->stroke()->width() = 1.0;
	ls->tessellationSize()->set(100, osgEarth::Units::KILOMETERS);

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

	//
	osgEarth::Features::FeatureModelLayerOptions fmlOpt;
	fmlOpt.name() = filePath;
	fmlOpt.featureSourceLayer() = filePath + "_source";
	fmlOpt.enableLighting() = false;
	fmlOpt.styles() = new osgEarth::Symbology::StyleSheet();
	fmlOpt.styles()->addStyle(style);

	osg::ref_ptr<osgEarth::Features::FeatureModelLayer> fml = new osgEarth::Features::FeatureModelLayer(fmlOpt);
	map->addLayer(fml);

	//AddAnno(filePath, map);
}

int main()
{
	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
	osg::ref_ptr<osg::Group> root = new osg::Group();

	//osg::ref_ptr node = osgDB::readNodeFile("mymap.earth");

	osgEarth::ProfileOptions profileOpts;

	//地图配置:设置缓存目录
	osgEarth::Drivers::FileSystemCacheOptions cacheOpts;
	string cacheDir = "D:/OSGCore/tmp";
	cacheOpts.rootPath() = cacheDir;

	osgEarth::MapOptions mapOpts;
	mapOpts.cache() = cacheOpts;
	mapOpts.profile() = profileOpts;

	//创建地图节点
	osg::ref_ptr<osgEarth::Map> map = new osgEarth::Map(mapOpts);
	osg::ref_ptr<osgEarth::MapNode> mapNode = new osgEarth::MapNode(map);
	
	osgEarth::Drivers::XYZOptions tileOptions2;
	tileOptions2.url() = "https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer/tile/{z}/{y}/{x}";// 加载ArcGIS街道数据
	tileOptions2.profile()->namedProfile() = ("spherical-mercator");
	osgEarth::ImageLayerOptions options2 = osgEarth::ImageLayerOptions(tileOptions2);
	osg::ref_ptr<osgEarth::ImageLayer> layer2 = new osgEarth::ImageLayer(options2);
	mapNode->getMap()->addLayer(layer2);

	AddVector(map);

	root->addChild(mapNode.get());

	//优化场景数据
	osgUtil::Optimizer optimzier;
	optimzier.optimize(root.get());

	viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));
	viewer->addEventHandler(new osgViewer::StatsHandler());//实现状态信息统计
	viewer->addEventHandler(new osgViewer::WindowSizeHandler());

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

	//解决Lines or Annotations (FeatureNode, etc.) 不被渲染的问题
	osgEarth::GLUtils::setGlobalDefaults(viewer->getCamera()->getOrCreateStateSet());

	viewer->setUpViewInWindow(100, 100, 800, 600);

	osgUtil::Optimizer optimizer;
	optimizer.optimize(root.get());

	viewer->setSceneData(root.get());
	viewer->realize();
	viewer->run();
	return 0;
}

你可能感兴趣的:(OSG/OSGEarth开发,osgEarth,arcgis瓦片数据)