osgEarth 加载矢量shp数据

article/2025/10/7 22:20:05

加载osgearth自带的world.shp矢量数据:
在这里插入图片描述

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>
using namespace std;#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Group>
#include <osg/Switch>
#include <osg/Billboard>
#include <osg/Texture2D>
#include <osg/Image>
#include <osg/Vec3>
#include <osg/Vec2>
#include <osg/PositionAttitudeTransform>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include <osg/PagedLOD>
#include <osgSim/Impostor>
#include <osgViewer/ViewerEventHandlers> //事件监听
#include <osgGA/StateSetManipulator> //事件响应类,对渲染状态进行控制
#include <osgUtil/Simplifier> //简化几何体#include <osgEarth/MapNode>
#include <osgEarth/ImageLayer>
#include <osgEarth/GLUtils>#include <osgEarthDrivers/gdal/GDALOptions>
#include <osgEarthDrivers/cache_filesystem/FileSystemCache>
#include <osgEarthDrivers/feature_ogr/OGRFeatureOptions>#include <osgEarthFeatures/FeatureSourceLayer>
#include <osgEarthFeatures/FeatureModelLayer>
#include <osgEarthUtil/EarthManipulator>#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 = "[CNTRY_NAME]";		//如果需要显示汉字,则需要转换成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 = PathRef::GetAppDir() + "/fonts/SourceHanSansCN-Regular.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 = "E:/OSGCore/data/world.shp";osgEarth::Drivers::OGRFeatureOptions featureData;featureData.url() = filePath;//	   如果缺少空间参考,可以手动指定	//    ifstream infile("C:/Data/vector/hs/23.prj");//    string line;//    getline(infile, line);//    featureData.profile()->srsString() = line;// 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 133polygonSymbol->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()
{osgEarth::ProfileOptions profileOpts;//地图配置:设置缓存目录osgEarth::Drivers::FileSystemCacheOptions cacheOpts;string cacheDir = "E:/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::GDALOptions gdal;gdal.url() = "E:/OSGCore/data/world.tif";osg::ref_ptr<osgEarth::ImageLayer> layer = new osgEarth::ImageLayer("BlueMarble", gdal);map->addLayer(layer);AddVector(map);osgViewer::Viewer viewer;viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));viewer.addEventHandler(new osgViewer::StatsHandler());//实现状态信息统计viewer.addEventHandler(new osgViewer::WindowSizeHandler());viewer.setSceneData(mapNode);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);return viewer.run();
}

osgearth加载矢量数据


http://chatgpt.dhexx.cn/article/ScgFTrs8.shtml

相关文章

导入shp数据到postgis库

1、安装完postgis之后&#xff0c;打开PostGIS PostGIS Bundle 3 for PostgreSQL x64 12 Shapefile and DBF Loader Exporter 2、填写数据库信息 3、填完后就显示成功了 4、点击Add File 添加shp文件 5、点击import导入 6、去数据库查看有表存在了 其他拓展&#xff1a;postgis…

推荐一个免费下载省-市-区县行政区Shp数据的方法

推荐一个免费下载省-市-区县行政区Shp数据的方法&#xff0c;简单实用。 文章目录 一、选择下载数据二、保存json格式三、json转shp格式 一、选择下载数据 本文讲解利用阿里云提供的地图选择器网站选择想要下载的行政区的方法&#xff0c;点击打开阿里云地图选择器网站&#x…

GeoServer发布shp数据

前段项目中应甲方要求&#xff0c;需要将地图服务过程简化到越简单越好&#xff0c;由于该项目中地图只作为底图&#xff0c;只是看看而已&#xff0c;并未涉及到空间数据分析之类的。所以&#xff0c;项目中裁掉了空间数据库这一部分。在没空间数据库的情况下&#xff0c;空间…

世界城市数据集(shp格式,2种数据源)

今天&#xff0c;给大家分享世界城市数据集shapefile文件&#xff0c;包括两种数据源&#xff0c;一个是Natural Earth的ne_10m_populated_places数据集&#xff0c;其属性字段包含了中文标签&#xff1b;另一个是来于ArcGIS Hub的World Cities数据集&#xff0c;其属性字段为英…

shp数据入库、出库、可视化

shp数据入库、出库、可视化 今天给npy写了次作业&#xff0c;由于各种原因&#xff0c;不能用我用了很久的SSM或者SpringBoot框架&#xff0c;只用了最原始的servletjsp&#xff0c;然后我发现…自己已经不太会用了&#xff0c;写这个博客记录一下整套流程~ 首先&#xff0c;…

Shp数据格式说明

复制于维基百科&#xff1a;https://zh.wikipedia.org/wiki/Shapefile&#xff0c;因为的确经常会把数据格式的一些细节忘记&#xff0c;所以复制过来&#xff0c;以供后续有必要的时候&#xff0c;查缺补漏。 一、简介 ESRI Shapefile&#xff08;shp&#xff09;&#xff0…

gis利器之Gdal(二)shp数据读取

本文将简单介绍shp数据的基本知识&#xff0c;以java语言为例&#xff0c;介绍如何基于java调用gdal完成对shp数据的解析&#xff0c;主要包括空间信息解析、属性信息解析、空间参考信息解析&#xff0c;bbox信息解析等等。 shp文件是一种矢量图形存储文件&#xff0c;可以用于…

全国矢量shp数据:行政区划,县界,道路,河流....都可下载

前段时间我们分享过一次全国矢量数据&#xff0c;包含道路&#xff0c;水系&#xff0c;铁路等等 不过第二天我们就把文章删除了 以至于后台不少留言问为啥删除 主要原因是数据精度稍微有点差&#xff08;部分数据偏差过大&#xff09; 不过 今天我们来重新分享 这次本着授…

为什么不太建议你通过 Python 去找工作?

有小伙伴问&#xff1a;我是一名大专生&#xff0c;学校把 Python 做为主语言教给我们&#xff0c;但是我也去了解过&#xff0c;其实 Python 门槛挺高的&#xff0c;所以我在自学 Java&#xff0c;但是我现在并不清楚到底要不要全心的去学 Java&#xff0c;学校里的课程也越来…

学Python真的好找工作吗?资深程序员为你解答

找工作难不难&#xff0c;主要还是看你自身的经验以及个人能力&#xff0c;如果决定学的话就要仔细去学习&#xff0c;而不是分散学习&#xff0c;这样既浪费了你的学习时间&#xff0c;又没有学到东西&#xff0c;尤其是大三即将毕业的同学,你只有其实不到一年的时间去学习,正…

【转行python】:简要说说python最好找工作的三个方向

前言 目前来说python最好找工作的三个方向&#xff1a;爬虫、数据分析和web开发。 之前我跟一些转行成功的朋友交流过&#xff0c;有一个朋友印象深刻&#xff0c;跟大家聊一下&#xff0c;朋友A某211研究生毕业&#xff0c;毕业后到了一家航空相关的企业上班&#xff0c;上了…

学Python好找工作吗?需要满足Python岗位哪些要求?

学Python好找工作吗&#xff1f;需要满足哪些要求&#xff1f;随着Python的火爆&#xff0c;国内Python的岗位也随之增多&#xff0c;只要技术能力强项目经验充足&#xff0c;找到工作不是问题。即使是一些二线城市&#xff0c;Python的薪资待遇也比较好。 一、学Python好找工作…

学习Python编程好找工作吗?

说起编程语言&#xff0c;不少人都会推荐学习Python&#xff0c;但很多人对Python不太了解&#xff0c;所以比较好奇“学习Python编程是否好找工作”&#xff0c;关于这个问题&#xff0c;小编通过下文为大家详细解答一下。 从市场情况来讲&#xff0c;Python职位比较多&…

学Python真的好找工作吗?工作多年的程序员为你解答

找工作难不难&#xff0c;主要还是看你自身的经验以及个人能力&#xff0c;如果决定学的话就要仔细去学习&#xff0c;而不是分散学习&#xff0c;这样既浪费了你的学习时间&#xff0c;又没有学到东西&#xff0c;尤其是大三即将毕业的同学,你只有其实不到一年的时间去学习,正…

Python虽然很火,为啥找工作这么难

知乎有人提了这么个问题&#xff0c;Python这么火&#xff0c;为啥找工作这么难呢? 这两年因为第三波人工智能热潮让 Python火了一把&#xff0c;让中小学生、非程序员(产品经理、运营)都加入到了学习Python的行列&#xff0c;那之前Python火吗? 虽然和Java、C这些大佬有差距…

学Python找工作你起码要会这些

要想通过自学Python并找到第一份工作应该做好以下几方面的事情&#xff1a; 第一&#xff1a;掌握Python的Web开发。 虽然Python目前在大数据、机器学习领域有大量的应用&#xff0c;但是掌握Python的Web开发依然是很有必要的&#xff0c;毕竟在生产环境下&#xff0c;Python…

对于你学了python是否可以找工作。

‘’’ Date: 2022-04-22 13:50:12 LastEditors: YuanPangZi LastEditTime: 2022-04-22 13:50:14 FilePath: \python_base\0_node_base\python_des.py ‘’’ 学python能找工作吗&#xff1f;需要计算机基础吗&#xff1f; 答案是**肯定的** 学python找工作这个事情 现在pyth…

Python虽然很火,为啥找工作这么难?

前几天看到某论坛有人提了这么个问题&#xff0c;Python这么火&#xff0c;为啥找工作这么难呢? 这两年因为第三波人工智能热潮让 Python火了一把&#xff0c;让中小学生、非程序员(产品经理、运营)都加入到了学习Python的行列&#xff0c;那之前Python火吗? 虽然和Java、C这…

扎心“我学了六个月 Python,怎么还是会找不到工作”

在编程界&#xff0c;Python是一种神奇的存在。有人认为&#xff0c;只有用Python才能优雅写代码&#xff0c;提高代码效率&#xff1b;但另一部分人恨不能把Python喷成筛子。那么&#xff0c;Python到底有没有用&#xff0c;为什么用Python找不到工作&#xff1f; Python到底能…

python这么火找不到工作_python工作太难找,python找不到工作原因。

python工作难找&#xff0c;是目前python发展的必然历程&#xff0c;因为python市场虽然有点火&#xff0c;但是市场却没有那么多的python职位提供。python专家急需&#xff0c;python新手却胜任不了。python基础岗位又太少了。我们一起来看一下。 1、python的工作难找原因之一…