在Vs2017上集成osgearth3.2和qt5.9,并加载shp文件。
提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
文章目錄
- 前言
- 一、QT5.9在vs上部署
- 1.1 在Vs中下載插件
- 1.2 配置QT
- 二、OsgEarth3.2環境配置。
- 三、在QT中配置OsgEarth3.2
- 四、在QT環境中利用osgEarth3.2加載shp文件。
- 總結
前言
經過兩個星期,從osg零基礎,配置好osgearth和qt集成,并加載好shp文件, 本著記錄自己學習的狀態,做好筆記,提升自己對代碼的熟悉狀態寫下本文,希望對大家有所幫助。
提示:以下是本篇文
提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
文章目錄
- 前言
- 一、QT5.9在vs上部署
- 1.1 在Vs中下載插件
- 1.2 配置QT
- 二、OsgEarth3.2環境配置。
- 三、在QT中配置OsgEarth3.2
- 四、在QT環境中利用osgEarth3.2加載shp文件。
- 總結
提示:以下是本篇文章正文內容,下面案例可供參考
一、QT5.9在vs上部署
如何在Vs中安裝QT,并完成部署環境。
https://www.bilibili.com/video/BV1AX4y1w7Nt?spm_id_from=333.999.0.0
根據B站上的視頻,進行完成部署qt。
1.1 在Vs中下載插件
打開 Visual Studio ,在拓展->管理拓展->聯機->搜索 qt ,然后下載.下載完畢后關閉 VS ,此時彈出安裝界面,選擇安裝即可。
1.2 配置QT
當插件下載完成時,Vs狀態欄上方會出現 Qt VS Tools 工具按鈕。
點擊Qt Versions,配置QT路徑。
路徑根據你Qt上下載Vs版本進行加載即可。
配置完成后,就可以創建QT應用程序了。
二、OsgEarth3.2環境配置。
OSG中文社區里面有很多教程。
我這里將osg公開資源以網盤形式分享給大家。
鏈接:https://pan.baidu.com/s/13aenl-NqkXRrl40luNwnzg
提取碼:539f
下載文件后,打開編譯好的庫。
里面有詳細教程
三、在QT中配置OsgEarth3.2
這一步花了我大半時間,終于在一位大佬的博客中得到解決,謝謝大哥!
https://blog.csdn.net/ambition_xiaoman/article/details/118609661
大家可以自行查看。
配置好的qt大概是這種的
能夠加載出地球即可
四、在QT環境中利用osgEarth3.2加載shp文件。
其中在QT環境配置好osgearth加載shp文件,網上有很多很多例子,但都是osgearth2.10的,但是osgearth2.1在qt上集成環境的例子好像沒有,查了很多很多教程和網站都沒有,我就根據osgEarth2.10的例子根據其幫助文檔進行書寫最終完成。
代碼如下(示例):
//頭文件 #pragma once#include <QtWidgets/QMainWindow> #include "ui_osg32qt.h" #include "GraphicsWindowQt.h"#include <QTimer> #include <osg/Notify> #include <osgDB/ReadFile> #include <osgDB/WriteFile> #include <osg/Group> #include <osg/Node> #include <osg/Camera> #include <osg/PositionAttitudeTransform> #include <osgGA/TrackballManipulator> #include <osgGA/StateSetManipulator> #include <osgViewer/ViewerEventHandlers> #include <osgViewer/Viewer>#include <osgEarth/MapNode> #include <osgEarth/GDAL> #include <osgEarth/ExampleResources> #include <osgEarth/EarthManipulator> #include <osgEarth/SpatialReference>#include <osgEarth/GeoTransForm> #include <osgEarth/GeoCommon>#include <osgEarth/Ephemeris> #include <osgEarth/Sky>#include <osgUtil/Tessellator> #include <osgEarth/GLUtils> #include <osg/Geode> #include <osg/Geometry>//要素類 #include <osgEarth/Style> #include <osgEarth/OGRFeatureSource> #include <osgEarth/FeatureModelLayer> #include <osgEarth/FeatureImageLayer>#include <QList> #include <QGroupBox> #include <ctime> #include <QStatusBar> #include <QCheckBox> #include <QPushButton> #include <QSpinBox> #include <QLineEdit>class osg32qt : public QMainWindow {Q_OBJECTpublic:osg32qt(QWidget *parent = Q_NULLPTR);~osg32qt(); private:Ui::osg32qtClass ui;private:QTimer* _timer; // 計時器,每5ms觸發一次事件osgViewer::Viewer* viewer;osg::ref_ptr<osg::Group> root;osg::ref_ptr<osg::Camera> camera;osg::ref_ptr<osg::Node> earthNode;osg::ref_ptr<osgEarth::MapNode> mapNode;osg::ref_ptr<osgEarth::Map> map;osg::ref_ptr <osg::MatrixTransform> earthForm;osg::ref_ptr<osgEarth::EarthManipulator> em;tm* t_tm;osgEarth::SkyNode* m_pSkyNode;time_t now_time; private:void InitOSG();// 初始化設置osgvoid InitUI();//界面初始化void InitTimer();//屏幕刷新初始化void InitOsgearth();//初始化osgearthvoid InitSky();//天空初始化 private slots:// 定時更新幀的槽函數void updateFrame();};//cpp文件
#include "osg32qt.h" #include <osg/TexGen> osg32qt::osg32qt(QWidget *parent): QMainWindow(parent) {ui.setupUi(this);InitOSG();InitOsgearth();InitUI();InitTimer(); } osg32qt::~osg32qt() {} void osg32qt::InitOSG()// 初始化設置osg {viewer = new osgViewer::Viewer;// 設置模型root = new osg::Group;// 顯示 .earth 文件中的地球模型//獲取屏幕分辨率 長寬osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();if (!wsi)return;unsigned int width, height;wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);//設置圖形環境特性osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;traits->windowDecoration = false;//聲明是否顯示窗口的描述traits->x = 0;traits->y = 0;traits->width = width;traits->height = height;traits->doubleBuffer = true;//創建圖形窗口是否使用雙緩存//設置照相機camera = new osg::Camera;camera->setGraphicsContext(new osgQt::GraphicsWindowQt(traits.get()));camera->setClearColor(osg::Vec4(0.2, 0.2, 0.6, 1.0));camera->setViewport(new osg::Viewport(0, 0, width, height));camera->setProjectionMatrixAsPerspective(30.0f, (double(traits->width)) / (double(traits->height)), 1.0f, 10000.0f);//設置渲染器viewer->setCamera(camera);viewer->setSceneData(root);viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);//創建為單線程viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet())); } void osg32qt::InitOsgearth() {//earthNode = new osg::Node;//earthNode = osgDB::readNodeFile("./feature_labels.earth");//從底圖影像圖層開始;我們將使用 GDAL 驅動程序加載本地 GeoTIFF 文件:map = new osgEarth::Map();osgEarth::GDALImageLayer* basemap = new osgEarth::GDALImageLayer();basemap->setURL("G:\\QT\\osgt\\osgearth-3.2\\data\\world.tif");map->addLayer(basemap);//接下來,我們添加一個圖層以提供要素數據。osgEarth::OGRFeatureSource* features = new osgEarth::OGRFeatureSource();features->setURL("G:\\QT\\osgt\\osgearth-3.2\\data\\world.shp");//features->setURL()map->addLayer(features);//定義要素數據的樣式。由于我們將渲染矢量作為線,配置線符號化器: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(100, 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 133polygonsymol->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);osgEarth::LayerVector layers;map->getLayers(layers);for (osgEarth::LayerVector::const_iterator i = layers.begin(); i != layers.end(); ++i){osgEarth::Layer* layer = i->get();if (layer->getStatus().isError() &&layer->getEnabled()){OE_WARN << layer->getName() << " : " << layer->getStatus().toString() << std::endl;}}mapNode = new osgEarth::MapNode(map.get());//mapnode初始化//mapNode = osgEarth::MapNode::findMapNode(earthNode.get());//優化場景數據earthForm = new osg::MatrixTransform;//osgearth操作器 用來設置osgearhem = new osgEarth::Util::EarthManipulator;if (mapNode.valid()){em->setNode(mapNode);}em->getSettings()->setArcViewpointTransitions(true);//設置osg渲染窗口viewer->setCameraManipulator(em);//獲取地球半徑 設置視點//double earth_R = mapNode->getMap()->getSRS()->getEllipsoid()->getRadiusEquator();const char* viewPointName = QString::fromLocal8Bit("北京").toStdString().c_str();//em->setViewpoint(osgEarth::Viewpoint(viewPointName, 112.44, 33.75, 0.0, 0.0, -90.0, 5 * earth_R), 5);//初始化天空InitSky(); } void osg32qt::InitSky() {//獲取當前時間 初始化天空now_time = time(0);t_tm = localtime(&now_time);osgEarth::DateTime cur_date_time(now_time);osgEarth::Ephemeris* ephemeris = new osgEarth::Ephemeris;//設置黑夜明暗程度osgEarth::Util::SkyOptions skyOptions;skyOptions.ambient() = 0.3;m_pSkyNode = osgEarth::SkyNode::create(skyOptions);m_pSkyNode->setName("SkyNode");m_pSkyNode->setEphemeris(ephemeris);m_pSkyNode->setDateTime(cur_date_time);viewer->setLightingMode(osg::View::SKY_LIGHT);m_pSkyNode->attach(viewer, 0);m_pSkyNode->setLighting(true);//添加到場景m_pSkyNode->addChild(mapNode);root->addChild(m_pSkyNode);} void osg32qt::InitUI()//界面初始化 {//osg 與 qt鏈接osgQt::GraphicsWindowQt* gw = dynamic_cast<osgQt::GraphicsWindowQt*>(camera->getGraphicsContext());//ui布局ui.horizontalLayout->addWidget((QWidget*)(gw->getGLWidget()));//QWidget* osg_widget = (QWidget*)(gw->getGLWidget());//this->setCentralWidget(osg_widget);//窗口最大化this->setWindowState(Qt::WindowMaximized);this->setWindowTitle(QString::fromLocal8Bit("數字地球")); } void osg32qt::InitTimer()//屏幕刷新初始化 {_timer = new QTimer;QObject::connect(_timer, SIGNAL(timeout()), this, SLOT(updateFrame()));_timer->start(10);} void osg32qt::updateFrame() {viewer->frame(); }效果圖:
# 五、示例程序可自取 https://download.csdn.net/download/weixin_44413499/46987576
總結
OsgEarth3.2相關教程特別少,現在網絡上大多都是osgEarth2.10的版本,因為我配置OsgEarth2.10和qt的環境出了一些問題,不然也不會用OsgEarth3.2了,繼續加油。總結
以上是生活随笔為你收集整理的在Vs2017上集成osgearth3.2和qt5.9,并加载shp文件。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小米从北京迁往武汉南京,一线大厂逃离或为
- 下一篇: 转:solr 从数据库导入数据,全量索引