6.OsgEarth加载倾斜摄影

愿你出走半生,归来仍是少年!

        三维场景中常用的地表模型包含倾斜摄影,通过CC处理出来的倾斜摄影是一个通过文件夹进行分块的,然后通过元数据记录了其空间位置信息(原点)。

        本文通过解析metadata.xml文件后进行多块情况的批量加载。

1.倾斜数据解读

        一般来说包含一个data文件夹和一个metadata.xml文件。

CC倾斜

        在metadata.xml中包含了坐标系定义以及原点信息。

<?xml version="1.0" encoding="utf-8"?>
<ModelMetadata version="1">
	<!--Spatial Reference System-->
	<SRS>EPSG:4523</SRS>
	<!--Origin in Spatial Reference System-->
	<SRSOrigin>35413650,3392689,0</SRSOrigin>
	<Texture>
		<ColorSource>Visible</ColorSource>
	</Texture>
</ModelMetadata>

        其中SRS节点代表坐标系,SRSOrigin代表原点。通过tinyxml2库可进行解析获取坐标。

        在data文件夹中包含若干子文件夹,都是一块一块的倾斜数据。每个文件夹中有一个和文件夹同名的osgb文件,它是最顶级的节点。然后其他的是不同层级下的节点。

文件夹
每个文件夹内的数据

 

 2.代码

 2.1.Metadata.xml解析

        通过tinyxml2库进行xml解析,并将对应的数据转换为我们需要的osg对象。

        通过xml中的Srs我们构建出了osgEarth::SpatialReference对象作为原始数据的坐标参照。

        通过xml中的SRSOriginin我们构建出了osgEarth::GeoPoint作为模型位置。

string parentDicPath = FileUtility::getParentDic(dataFolderPath);

string  metaPath = parentDicPath + "/metadata.xml";

if (FileUtility::exist(metaPath))
{
tinyxml2::XMLDocument doc;

tinyxml2::XMLError error = doc.LoadFile(metaPath.c_str());

if (error != tinyxml2::XML_SUCCESS) {

 

	throw  "读取 xml 失败";

	return NULL;
}

auto xmlRoot = doc.RootElement();

auto srsElement = xmlRoot->FirstChildElement("SRS");

string srsValue = srsElement->GetText();

boost::to_upper(srsValue);

boost::replace_all(srsValue, "EPSG:", "");

int srsId = std::stoi(srsValue);

osgEarth::SpatialReference* srs = Cv::SrsSource::getSrs(srsId);

auto srsOriginElement = xmlRoot->FirstChildElement("SRSOrigin");

string srsOriginValue = srsOriginElement->GetText();

std::vector<std::string> xyz;

boost::split(xyz, srsOriginValue, boost::is_any_of(","), boost::token_compress_on);

double x = std::stod(xyz[0]);

double y = std::stod(xyz[1]);

double z = std::stod(xyz[2]);

osgEarth::GeoPoint position(srs, x, y, 30);

auto pos4326 = position.transform(SpatialReference::get("wgs84"));
}

 2.2.ModelLayer创建

        通过遍历data文件夹下的子文件夹,将osgb文件一个一个的加载进来。

        需要注意的是,我们建立的ModelLayer不是用过SetURL方法加载的模型数据,而是通过了SetNode方法区加载的一个GeoTransform节点。通过这个节点区包含了所以的子节点。同时通过了osgEarth::Registry::shaderGenerator().run(node)增加了着色渲染器。

        同时给modelLayer添加了位置方便定位使用。

auto childOsgbFolders = FileUtility::getChildFolders(std::filesystem::path(dataFolderPath));

 
osg::ref_ptr<osgEarth::GeoTransform> xform = new osgEarth::GeoTransform();

for (size_t i = 0; i < childOsgbFolders.size(); i++)
{
	auto folder = childOsgbFolders[i];


	auto osgbPath = folder.string() + "/" + folder.filename().string() + ".osgb";

	bool exist = FileUtility::exist(osgbPath);


	auto node = osgDB::readNodeFile(osgbPath);


	osgEarth::Registry::shaderGenerator().run(node);

	xform->addChild(node);
	 

}

xform->setPosition(pos4326);


ref_ptr<osgEarth::ModelLayer> modelLayer = new osgEarth::ModelLayer();


modelLayer->setLocation(pos4326);

modelLayer->setNode(xform);

modelLayer->setName("倾斜摄影");

 2.3.加载

        通过map的addlayer将图层加载进入地球进行了。

3.效果

加载成功
成功效果

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

就是那个帕吉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值