From 6b259c684043a53a7c59a95b5bafaddb5f413947 Mon Sep 17 00:00:00 2001 From: keyeslll Date: Mon, 13 Mar 2023 16:13:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DEM_GeneRas/CProcessBase.h | 98 +++++++ DEM_GeneRas/DEM_GeneRas.cpp | 333 ++++++++++++++++++++++++ DEM_GeneRas/DEM_GeneRas.h | 75 ++++++ DEM_GeneRas/DEM_GeneRas.qrc | 6 + DEM_GeneRas/DEM_GeneRas.qss | 73 ++++++ DEM_GeneRas/DEM_GeneRas.ui | 325 +++++++++++++++++++++++ DEM_GeneRas/DEM_GeneRas.vcxproj | 92 +++++++ DEM_GeneRas/DEM_GeneRas.vcxproj.filters | 85 ++++++ DEM_GeneRas/QtGDALProcessBar.cpp | 100 +++++++ DEM_GeneRas/QtGDALProcessBar.h | 62 +++++ DEM_GeneRas/SrsMainPluginInterFace.h | 58 +++++ DEM_GeneRas/ThreadObject.cpp | 117 +++++++++ DEM_GeneRas/ThreadObject.h | 47 ++++ DEM_GeneRas/im2shp.cpp | 154 +++++++++++ DEM_GeneRas/im2shp.h | 35 +++ DEM_GeneRas/main.cpp | 16 ++ DEM_GeneRas/resources/dem_ras.svg | 1 + DEM_Predict.sln | 22 ++ 18 files changed, 1699 insertions(+) create mode 100644 DEM_GeneRas/CProcessBase.h create mode 100644 DEM_GeneRas/DEM_GeneRas.cpp create mode 100644 DEM_GeneRas/DEM_GeneRas.h create mode 100644 DEM_GeneRas/DEM_GeneRas.qrc create mode 100644 DEM_GeneRas/DEM_GeneRas.qss create mode 100644 DEM_GeneRas/DEM_GeneRas.ui create mode 100644 DEM_GeneRas/DEM_GeneRas.vcxproj create mode 100644 DEM_GeneRas/DEM_GeneRas.vcxproj.filters create mode 100644 DEM_GeneRas/QtGDALProcessBar.cpp create mode 100644 DEM_GeneRas/QtGDALProcessBar.h create mode 100644 DEM_GeneRas/SrsMainPluginInterFace.h create mode 100644 DEM_GeneRas/ThreadObject.cpp create mode 100644 DEM_GeneRas/ThreadObject.h create mode 100644 DEM_GeneRas/im2shp.cpp create mode 100644 DEM_GeneRas/im2shp.h create mode 100644 DEM_GeneRas/main.cpp create mode 100644 DEM_GeneRas/resources/dem_ras.svg create mode 100644 DEM_Predict.sln diff --git a/DEM_GeneRas/CProcessBase.h b/DEM_GeneRas/CProcessBase.h new file mode 100644 index 0000000..2106b7e --- /dev/null +++ b/DEM_GeneRas/CProcessBase.h @@ -0,0 +1,98 @@ +#pragma once +#include +using namespace std; + +/** +* @brief 进度条基类 +* +* 提供进度条基类接口,来反映当前算法的进度值 +*/ +//源码:class IMGALG_API CProcessBase +class CProcessBase +{ +public: + /** + * @brief 构造函数 + */ + CProcessBase() + { + m_dPosition = 0.0; + m_iStepCount = 100; + m_iCurStep = 0; + m_bIsContinue = true; + } + + /** + * @brief 析构函数 + */ + virtual ~CProcessBase() {} + + /** + * @brief 设置进度信息 + * @param pszMsg 进度信息 + */ + virtual void SetMessage(const char* pszMsg) = 0; + + /** + * @brief 设置进度值 + * @param dPosition 进度值 + * @return 返回是否取消的状态,true为不取消,false为取消 + */ + virtual bool SetPosition(double dPosition) = 0; + + /** + * @brief 进度条前进一步,返回true表示继续,false表示取消 + * @return 返回是否取消的状态,true为不取消,false为取消 + */ + virtual bool StepIt() = 0; + + /** + * @brief 设置进度个数 + * @param iStepCount 进度个数 + */ + virtual void SetStepCount(int iStepCount) + { + ReSetProcess(); + m_iStepCount = iStepCount; + } + + /** + * @brief 获取进度信息 + * @return 返回当前进度信息 + */ + string GetMessage() + { + return m_strMessage; + } + + /** + * @brief 获取进度值 + * @return 返回当前进度值 + */ + double GetPosition() + { + return m_dPosition; + } + + /** + * @brief 重置进度条 + */ + void ReSetProcess() + { + m_dPosition = 0.0; + m_iStepCount = 100; + m_iCurStep = 0; + m_bIsContinue = true; + } + + /*! 进度信息 */ + string m_strMessage; + /*! 进度值 */ + double m_dPosition; + /*! 进度个数 */ + int m_iStepCount; + /*! 进度当前个数 */ + int m_iCurStep; + /*! 是否取消,值为false时表示计算取消 */ + bool m_bIsContinue; +}; diff --git a/DEM_GeneRas/DEM_GeneRas.cpp b/DEM_GeneRas/DEM_GeneRas.cpp new file mode 100644 index 0000000..fe86d5d --- /dev/null +++ b/DEM_GeneRas/DEM_GeneRas.cpp @@ -0,0 +1,333 @@ +#include "DEM_GeneRas.h" + +#include +#include +#include + +#include + +VegePredict::VegePredict() +{ + //ui.setupUi(this); +} + +VegePredict::~VegePredict() +{ + +} + +QString VegePredict::PannelName() +{ + return QString::fromLocal8Bit(""); +} + +QString VegePredict::CategoryName() +{ + return QString::fromLocal8Bit("DEM模块"); +} + +QString VegePredict::EnglishName() +{ + return QString::fromLocal8Bit("DEM_Module"); +} + +QString VegePredict::ChineseName() +{ + return QString::fromLocal8Bit("植被预测"); +} + +QString VegePredict::Information() +{ + return QString::fromLocal8Bit("植被预测"); +} + +QString VegePredict::IconPath() +{ + return ":/DEM_GeneRas/resources/dem_ras.svg"; +} + +QWidget* VegePredict::CenterWidget() +{ + //QString str = QString("%1->%2,thread id:%3").arg(__FILE__).arg(__FUNCTION__).arg((int)QThread::currentThreadId()); + //qDebug() << str; + + QString gdal_path = qApp->applicationDirPath().toLocal8Bit() + "/share/gdal"; + QString pro_lib_path = qApp->applicationDirPath().toLocal8Bit() + "/share/proj"; + qputenv("GDAL_DATA", gdal_path.toLocal8Bit()); + qputenv("PROJ_LIB", pro_lib_path.toLocal8Bit()); + + bool showWin = false; + if (myWidget == nullptr) + { + myWidget = new QDialog(); + showWin = true; + qDebug() << "new QDialog()"; + } + else + { + qDebug() << "already have myWidget"; + //激活窗口并提升至顶层 + myWidget->activateWindow(); + myWidget->raise(); + + return myWidget; + } + ui.setupUi(myWidget); + + myWidget->setWindowTitle(QString::fromLocal8Bit("植被预测")); + myWidget->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint); + myWidget->setWindowIcon(QIcon(":/DEM_GeneRas/resources/dem_ras.svg")); + myWidget->setAttribute(Qt::WA_QuitOnClose, false); + + myWidget->setAttribute(Qt::WA_DeleteOnClose); + connect(myWidget, &QDialog::destroyed, this, [=]() { + qDebug() << "----DEM Predict window close----"; + + QDir pluginsDir = QDir(qApp->applicationDirPath()); + if (pluginsDir.cd("srsplugins\\DemModel")) + { + QString strConfigPath = pluginsDir.absoluteFilePath("dem_config.ini"); + QFile f(strConfigPath); + if (f.exists()) + { + WriteConfigPaths(strConfigPath); + f.close(); + } + } + + if (mWorkThread != nullptr) + { + mWorkThread->requestInterruption();//请求线程中断 + mWorkThread->quit(); + mWorkThread->wait();//调用wait后先调用finished信号对应的槽函数,执行完成后再往下走 + mWorker->on_cancel(); + mWorkThread = nullptr;//mWorkThread, &QThread::finished, mWorkThread, &QObject::deleteLater,不需要delete + mWorker = nullptr;//mWorkThread, &QThread::finished, mWorker, &QObject::deleteLater + } + myWidget->close(); + myWidget = nullptr; + //qDebug() << "--destroyed"; + }); + + connect(ui.pbt_DOM, &QPushButton::clicked, this, &VegePredict::chooseDomPath); + connect(ui.pbt_DSM, &QPushButton::clicked, this, &VegePredict::chooseDsmPath); + //connect(ui.pbt_Label, &QPushButton::pressed, this, &VegePredict::chooseLabelPath); + connect(ui.pbt_PRE, &QPushButton::clicked, this, &VegePredict::choosePreDir); + connect(ui.pbt_Moudle, &QPushButton::clicked, this, &VegePredict::chooseMouleFile); + + connect(ui.pushButton_ok, &QPushButton::clicked, this, &VegePredict::readAndStart); + connect(ui.pushButton_cancel, &QPushButton::clicked, this, &VegePredict::pbCancel); + + ui.progressBar->setTextVisible(true); + ui.progressBar->setRange(0, 100); + + QFile qssFile(":/DEM_GeneRas/DEM_GeneRas.qss"); + qssFile.open(QFile::ReadOnly); //以只读方式打开 + if (qssFile.isOpen()) + { + QString qss = QLatin1String(qssFile.readAll()); + myWidget->setStyleSheet(qss); + qssFile.close(); + } + else + qDebug() << "无法打开文件"; + + QDir pluginsDir = QDir(qApp->applicationDirPath()); + if (pluginsDir.cd("srsplugins\\DemModel")) + { + QString strConfigPath = pluginsDir.absoluteFilePath("dem_config.ini"); + QFile f(strConfigPath); + if (f.exists()) + { + ReadConfigHistoryPaths(strConfigPath); + f.close(); + } + } + + if (showWin) + myWidget->show(); + + return myWidget; +} + +void VegePredict::startWorkThread() +{ + if (mWorker != nullptr) + { + return; + } + mWorkThread = new QThread(); + mWorker = new WorkThreadObject(); + mWorker->moveToThread(mWorkThread); + connect(mWorkThread, &QThread::finished, mWorkThread, &QObject::deleteLater); + connect(mWorkThread, &QThread::finished, mWorker, &QObject::deleteLater); + connect(mWorker, &WorkThreadObject::process, myWidget, [=](int value) {ui.progressBar->setValue(value); }); + connect(mWorker, &WorkThreadObject::addDataToMap, this, &VegePredict::addMap); + connect(mWorker, &WorkThreadObject::addShpDataToMap, this, &VegePredict::openResultData); + connect(this, &VegePredict::killChildThread, mWorker, &WorkThreadObject::on_cancel); + connect(this, &VegePredict::startTiffToShp, mWorker, &WorkThreadObject::runFormatConvert); + + connect(this, &VegePredict::start, mWorker, &WorkThreadObject::runPredictWork); + mWorkThread->start(); +} + +void VegePredict::chooseDomPath() +{ + QString dirDom = QFileDialog::getExistingDirectory(ui.pbt_DOM, QString::fromLocal8Bit("选择输入DOM文件路径"), ""); + if (dirDom != "") + ui.line_DOM->setText(dirDom); +} + +void VegePredict::chooseDsmPath() +{ + QString dirDsm = QFileDialog::getExistingDirectory(ui.pbt_DSM, QString::fromLocal8Bit("选择输入Slope文件路径"), ""); + if (dirDsm != "") + ui.line_DSM->setText(dirDsm); +} + +void VegePredict::choosePreDir() +{ + QString dirPre = QFileDialog::getExistingDirectory(ui.pbt_PRE, QString::fromLocal8Bit("选择预测结果输出文件路径"), ""); + if (dirPre != "") + ui.line_PRE->setText(dirPre); +} + +void VegePredict::chooseMouleFile() +{ + QString dirPre = QFileDialog::getOpenFileName(ui.pbt_Moudle, QString::fromLocal8Bit("选择模型文件"), "", "*.pth"); + if (dirPre != "") + ui.line_Moudle->setText(dirPre); +} + +void VegePredict::addMap() +{ + QMessageBox mess(QMessageBox::NoIcon, + QString::fromLocal8Bit("植被预测结束"), + QString::fromLocal8Bit("将对如下路径中结果进行转矢量处理:\n") + ui.line_PRE->text(), + QMessageBox::Ok, NULL); + mess.setWindowFlags(Qt::Drawer); + mess.setButtonText(QMessageBox::Ok, QString::fromLocal8Bit("确认")); + int result = mess.exec(); + switch (result) + { + case QMessageBox::Ok: + emit startTiffToShp(ui.line_PRE->text(), ui.line_DSM->text()); + ui.label_process->setText(QString::fromLocal8Bit("转矢量进度:")); + ui.progressBar->setValue(0); + break; + default: + break; + } +} + +void VegePredict::ReadConfigHistoryPaths(QString strPath) +{ + QSettings configIni(strPath, QSettings::IniFormat); + + //打开标题为:[DemPredict] 的组 + configIni.beginGroup("DemPredict"); + + ui.line_DOM->setText(configIni.value("SrcDom").toString()); + ui.line_DSM->setText(configIni.value("SrcDsm").toString()); + ui.line_PRE->setText(configIni.value("PredictResult").toString()); + + configIni.endGroup();//关闭组 +} +void VegePredict::WriteConfigPaths(QString strPath) +{ + QSettings configIni(strPath, QSettings::IniFormat); + configIni.setIniCodec("utf-8"); + //打开标题为:[DemPredict] 的组 + configIni.beginGroup("DemPredict"); + + //更新输入DOM路径 + QString temp = ui.line_DOM->text(); + if (temp != "") + configIni.setValue("SrcDom", temp); + //更新输入DSM路径 + temp = ui.line_DSM->text(); + if (temp != "") + configIni.setValue("SrcDsm", temp); + //更新输出结果路径 + temp = ui.line_PRE->text(); + if (temp != "") + configIni.setValue("PredictResult", temp); + + configIni.endGroup();//关闭组 +} + +void VegePredict::readAndStart() +{ + QString dirDOM = ui.line_DOM->text(); + QString dirDSM = ui.line_DSM->text(); + QString dirPRE = ui.line_PRE->text(); + QString fileMoudle = ui.line_Moudle->text(); + + ui.progressBar->setValue(0); + + if (dirDOM == "" || dirDSM == "" || dirPRE == "" || fileMoudle == "") + { + QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("请检查输入输出文件或路径")); + mess.setWindowFlags(Qt::Drawer); + int result = mess.exec(); + return; + } + //startObjThread(); + if (mWorkThread == nullptr) + { + qDebug() << "--startThread"; + startWorkThread(); + } + //std::functionprocessCallback = [=](int val) { + // ui.progressBar->setValue(val); + // qDebug() << val << endl; + //};//类间回调函数; + + ///////////////////////////////////////////////////////////////////// + //emit startTiffToShp(ui.line_PRE->text(), ui.line_Label->text()); + ///////////////////////////////////////////////////////////////////// + ui.label_process->setText(QString::fromLocal8Bit("预测进度:")); + emit start(fileMoudle, dirDOM, dirDSM, dirPRE);//, processCallback +} + +void VegePredict::pbCancel() +{ + delete myWidget;//调起&QDialog::destroyed +} + +void VegePredict::openResultData(QStringList string_list) +{ + if (string_list.isEmpty()) + return; + QgsRasterLayer* rastLayer; + QgsVectorLayer* vecLayer; + for each (QString layerPath in string_list) + { + QFileInfo fileInfo(layerPath); + QString layerBaseName = fileInfo.baseName(); // 图层名称 + if ("tif" == fileInfo.suffix() || "tiff" == fileInfo.suffix()) + { + rastLayer = new QgsRasterLayer(fileInfo.filePath(), layerPath, "gdal"); + if (!rastLayer) + return; + QgsMapLayer* mapLayer = rastLayer; + QgsRectangle myRectangle; + rastLayer->setContrastEnhancement(QgsContrastEnhancement::StretchToMinimumMaximum + , QgsRasterMinMaxOrigin::StdDev, myRectangle); + QList mapLayers; + mapLayers << mapLayer; + QgsProject::instance()->addMapLayers(mapLayers); + //zoomToSelectedLayer(mapLayer); + } + else if ("shp" == fileInfo.suffix()) + { + vecLayer = new QgsVectorLayer(fileInfo.filePath(), layerPath); + if (!vecLayer) + return; + QgsMapLayer* mapLayer = vecLayer; + QList mapLayers; + mapLayers << mapLayer; + QgsProject::instance()->addMapLayers(mapLayers); + } + } +} diff --git a/DEM_GeneRas/DEM_GeneRas.h b/DEM_GeneRas/DEM_GeneRas.h new file mode 100644 index 0000000..ad62cb3 --- /dev/null +++ b/DEM_GeneRas/DEM_GeneRas.h @@ -0,0 +1,75 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ui_DEM_GeneRas.h" +#include "ThreadObject.h" +#include "SrsMainPluginInterFace.h" + +#include "gdal_priv.h" +//#include "gdal_alg_priv.h" + +class VegePredict : public SrsMainInterface +{ + Q_OBJECT + Q_INTERFACES(SrsMainInterface) + Q_PLUGIN_METADATA(IID MainInterface_iid) + +public: + VegePredict(); + ~VegePredict(); + + virtual QString PannelName() override; + virtual QString CategoryName() override; + + virtual QString EnglishName() override; + virtual QString ChineseName() override; + virtual QString Information() override; + virtual QString IconPath() override; + virtual QWidget* CenterWidget() override; + + //读json文件,获取历史存储的路径 + void ReadConfigHistoryPaths(QString strPath); + //保存本次打开的路径到json文件 + void WriteConfigPaths(QString strPath); + + void startWorkThread(); + + void openResultData(QStringList string_list); + +public slots: + void chooseDomPath(); + void chooseDsmPath(); + void choosePreDir(); + void chooseMouleFile(); + + void addMap(); + + void readAndStart(); + void pbCancel(); + +signals: + void start(QString Moudle, QString DOM, QString DSM, QString PRE);//, std::functionfunc + void killChildThread(); + void startTiffToShp(QString PRE, QString DSM); + +private: + Ui::DEM_GeneRasClass ui; + + QDialog* myWidget = nullptr; + + WorkThreadObject* mWorker = nullptr; + + QThread* mWorkThread = nullptr; + +}; diff --git a/DEM_GeneRas/DEM_GeneRas.qrc b/DEM_GeneRas/DEM_GeneRas.qrc new file mode 100644 index 0000000..a753381 --- /dev/null +++ b/DEM_GeneRas/DEM_GeneRas.qrc @@ -0,0 +1,6 @@ + + + resources/dem_ras.svg + DEM_GeneRas.qss + + diff --git a/DEM_GeneRas/DEM_GeneRas.qss b/DEM_GeneRas/DEM_GeneRas.qss new file mode 100644 index 0000000..3c7b210 --- /dev/null +++ b/DEM_GeneRas/DEM_GeneRas.qss @@ -0,0 +1,73 @@ +QProgressBar#progressBar { + border:0px solid white; + text-align:center; + color:black; +} +QProgressBar#progressBar:chunk { + background-color:#3FABBA; + width:20px; +} +QProgressBar#progressBar QLineEdit { + border:none; + color:black; + padding-left:5px; + font-size:16px; + background-color:transparent; +} + +QLabel{ + color:black; + font-family:'Microsoft YaHei'; + font-size:12px; +} + +/*----QPushButton样式表*/ +#pushButton_ok, #pushButton_cancel, #pbt_DOM, #pbt_DSM , +#pbt_Label, #pbt_PRE, #pbt_Moudle { + font-family:'Microsoft YaHei'; + font-size:12px; + background-color: #ffffff; + border: 1px solid #dcdfe6; + padding: 2px; + border-radius: 5px; + max-height:20px; +} +#pushButton_ok, #pushButton_cancel{ + min-width:36px; +} +#pushButton_ok:hover, #pushButton_cancel:hover, #pbt_DOM:hover, #pbt_DSM:hover +,#pbt_Label:hover, #pbt_PRE:hover, #pbt_Moudle:hover { + background-color: #ecf5ff; + color: #409eff; +} +#pushButton_ok:pressed, #pushButton_cancel:pressed, #pbt_DOM:pressed, #pbt_DSM:pressed +,#pbt_Label:pressed, #pbt_PRE:pressed, #pbt_Moudle:pressed { + border: 1px solid #3a8ee6; + color: #409eff; +} +#pushButton_ok:checked, #pushButton_cancel:checked, #pbt_DOM:checked, #pbt_DSM:checked +,#pbt_Label:checked, #pbt_PRE:checked, #pbt_Moudle:checked{ + border: 1px solid #3a8ee6; + color: #409eff; +} +#pushButton_ok:focus, #pushButton_cancel:focus, #pbt_DOM:focus, #pbt_DSM:focus +,#pbt_Label:focus, #pbt_PRE:focus, #pbt_Moudle:focus{ + border: 1px solid #3a8ee6; + color: #409eff; + outline: none; +} + +/*----QLineEdit样式*/ +#line_DOM, #line_DSM, #line_Label, #line_PRE, #line_Moudle{ + border:0px; + border-bottom: 1px solid #B3B3B3; + font-family:'Microsoft YaHei'; + font-size:12px; + background-color:transparent; +} +#line_DOM:hover, #line_DSM:hover, #line_Label:hover, #line_PRE:hover, #line_Moudle:hover{ + border-bottom: 2px solid #66A3FF; +} +#line_DOM:focus, #line_DSM:focus, #line_Label:focus, #line_PRE:focus, #line_Moudle:focus{ + border-bottom: 2px solid #7666FF; +} diff --git a/DEM_GeneRas/DEM_GeneRas.ui b/DEM_GeneRas/DEM_GeneRas.ui new file mode 100644 index 0000000..2bb9575 --- /dev/null +++ b/DEM_GeneRas/DEM_GeneRas.ui @@ -0,0 +1,325 @@ + + + DEM_GeneRasClass + + + + 0 + 0 + 500 + 300 + + + + + 0 + 0 + + + + + 500 + 300 + + + + + 500 + 300 + + + + + + + + + + + + 閫夋嫨鏂囦欢 + + + + + + + + 0 + 0 + + + + + 100 + 0 + + + + + 16777215 + 16777215 + + + + 杈撳叆妯″瀷鏂囦欢璺緞 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 0 + 0 + + + + + 100 + 0 + + + + + 16777215 + 16777215 + + + + 杈撳叆DSM鏂囦欢璺緞 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 75 + 0 + + + + + 75 + 16777215 + + + + 閫夋嫨鏂囦欢澶 + + + + + + + + + + + 0 + 0 + + + + + 100 + 0 + + + + + 16777215 + 16777215 + + + + 杈撳叆DOM鏂囦欢璺緞 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 75 + 0 + + + + + 75 + 16777215 + + + + 閫夋嫨鏂囦欢澶 + + + + + + + + 0 + 0 + + + + + 100 + 0 + + + + + 16777215 + 16777215 + + + + 棰勬祴缁撴灉鏂囦欢璺緞 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + 75 + 0 + + + + + 75 + 16777215 + + + + 閫夋嫨鏂囦欢澶 + + + + + + + + 0 + 0 + + + + + + + + + 0 + 0 + + + + + + + + + + 0 + + + QLayout::SetDefaultConstraint + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 0 + 0 + + + + 纭畾 + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 10 + 20 + + + + + + + + 鍙栨秷 + + + + + + + + + + + 杩涘害: + + + + + + + 0 + + + + + + + + + + + + + diff --git a/DEM_GeneRas/DEM_GeneRas.vcxproj b/DEM_GeneRas/DEM_GeneRas.vcxproj new file mode 100644 index 0000000..cf6b0ba --- /dev/null +++ b/DEM_GeneRas/DEM_GeneRas.vcxproj @@ -0,0 +1,92 @@ +锘 + + + + Release + x64 + + + + {AAEF388B-265D-4CF4-8832-9E6CD50B4644} + QtVS_v304 + 10.0 + $(MSBuildProjectDirectory)\QtMsBuild + DEM_Predict + + + + DynamicLibrary + v142 + + + + + + + qt5.11.2 + core;gui;widgets + release + + + + + + + + + + + + + D:\qgis\osgeo4w\include;D:\qgis\osgeo4w\apps\Qt5\include;D:\qgis\osgeo4w\apps\qgis_build_sdk\include;D:\qgis\osgeo4w\apps\Qt5\include\QtXml;$(IncludePath) + D:\qgis\osgeo4w\lib;D:\qgis\osgeo4w\apps\Qt5\lib;D:\qgis\osgeo4w\apps\qgis_build_sdk\lib;$(LibraryPath) + dem3-demPredict + + + + gdal_i.lib;qgis_gui.lib;qgis_analysis.lib;qgis_core.lib;qgis_native.lib;%(AdditionalDependencies) + + + + + true + true + EditAndContinue + Disabled + MultiThreadedDLL + + + Console + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/DEM_GeneRas/DEM_GeneRas.vcxproj.filters b/DEM_GeneRas/DEM_GeneRas.vcxproj.filters new file mode 100644 index 0000000..21007df --- /dev/null +++ b/DEM_GeneRas/DEM_GeneRas.vcxproj.filters @@ -0,0 +1,85 @@ +锘 + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + qrc;rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {99349809-55BA-4b9d-BF79-8FDBB0286EB3} + ui + + + {639EADAA-A684-42e4-A9AD-28FC9BCB8F7C} + ts + + + {8ac62ded-3917-412e-99b7-44350c87e7c8} + + + + + Resource Files + + + Header Files + + + Source Files + + + + + Source Files + + + process + + + process + + + Source Files + + + + + Form Files + + + + + Form Files + + + process + + + process + + + + + Header Files + + + process + + + Header Files + + + + + Resource Files + + + \ No newline at end of file diff --git a/DEM_GeneRas/QtGDALProcessBar.cpp b/DEM_GeneRas/QtGDALProcessBar.cpp new file mode 100644 index 0000000..f34c245 --- /dev/null +++ b/DEM_GeneRas/QtGDALProcessBar.cpp @@ -0,0 +1,100 @@ +#include "QtGDALProcessBar.h" +#include + + +QtGDALProcessBar::QtGDALProcessBar(QWidget *parent) + : QProgressBar(parent) +{ + //ui.setupUi(this); + this->setTextVisible(true); + m_dPosition = 0.0; + m_iStepCount = 100; + m_iCurStep = 0; + + +} + +QtGDALProcessBar::~QtGDALProcessBar() +{ +} + +/** +* @brief 设置进度信息 +* @param pszMsg 进度信息 +*/ +void QtGDALProcessBar::SetMessage(const char* pszMsg) +{ + if (pszMsg != NULL) + { + m_strMessage = pszMsg; + //setLabelText(QString(pszMsg)); + } +} + +/** +* @brief 设置进度值 +* @param dPosition 进度值 +*/ +bool QtGDALProcessBar::SetPosition(double dPosition) +{ + m_dPosition = dPosition; + //cout << "m_dPosition " << m_dPosition << endl; + //current_value = value(); + int temp_value = int(m_start_value +std::min(100u, (uint)(m_dPosition * 100.0 )) * m_scale); + + setValue(temp_value); + //setFormat(tr("%1%").arg(temp_value)); + //cout << "StepIt " << temp_value << "\t" << "current_value" << int(m_start_value + temp_value * m_scale) << endl; + //QCoreApplication::instance()->processEvents(); + //this->update(); + //if (this->wasCanceled()) + // return false; + + return true; +} + +bool QtGDALProcessBar::SetStartEndValue(int i_start_value, int i_end_value) +{ + if (i_end_value< i_start_value ) + { + return false; + } + else + { + m_start_value = i_start_value; + m_end_value = i_end_value; + m_scale = (m_end_value - m_start_value) / 100.0; + return true; + } +} + +/** +* @brief 进度条前进一步,返回false表示终止操作 +*/ +bool QtGDALProcessBar::StepIt() +{ + m_iCurStep++; + m_dPosition = m_iCurStep * 1.0 / m_iStepCount; + //current_value = value(); + int temp_value = std::min(100u, (uint)(m_dPosition * 100.0)); + setValue(int(m_start_value + temp_value * m_scale)); + //setFormat(QString("当前进度为:%1%").arg(temp_value)); + //cout << "StepIt " << temp_value <<"\t"<<"current_value"<< current_value << endl; + + //QCoreApplication::instance()->processEvents(); + + //if (this->wasCanceled()) + // return false; + + return true; +} + +void QtGDALProcessBar::updateProgress(int step) +{ + this->setValue(step); + //this->update(); + //cout << "updateProgress " << step << endl; + //QCoreApplication::instance()->processEvents(); +} + + diff --git a/DEM_GeneRas/QtGDALProcessBar.h b/DEM_GeneRas/QtGDALProcessBar.h new file mode 100644 index 0000000..b50aa3f --- /dev/null +++ b/DEM_GeneRas/QtGDALProcessBar.h @@ -0,0 +1,62 @@ +#pragma once + +#include +#include "CProcessBase.h" + +class QtGDALProcessBar : public QProgressBar,public CProcessBase +{ + Q_OBJECT + +public: + QtGDALProcessBar(QWidget *parent = Q_NULLPTR); + ~QtGDALProcessBar(); + + /** + * @brief 设置进度信息 + * @param pszMsg 进度信息 + */ + void SetMessage(const char* pszMsg); + + /** + * @brief 设置进度值 + * @param dPosition 进度值 + */ + bool SetPosition(double dPosition); + + /// + /// 设置进度条开始结束值 + /// + /// + /// + bool SetStartEndValue(int i_start_value,int i_end_value); + + + /** + * @brief 进度条前进一步 + */ + bool StepIt(); + + /// + /// 当前进度值 + /// + int current_value; + + +public slots: + void updateProgress(int); + + +private: + /// + /// 进度条开始值 + /// + int m_start_value; + + /// + /// 进度条结束值 + /// + int m_end_value; + + float m_scale; + +}; diff --git a/DEM_GeneRas/SrsMainPluginInterFace.h b/DEM_GeneRas/SrsMainPluginInterFace.h new file mode 100644 index 0000000..9130479 --- /dev/null +++ b/DEM_GeneRas/SrsMainPluginInterFace.h @@ -0,0 +1,58 @@ +#pragma once +//#if _MSC_VER >=1600 // MSVC2015>1899, msvc_ver=14.0 +//#pragma execution_character_set("utf-8") +//#endif +//#include "qtclasslibrary1_global.h" +#include +#include +#include +#include + +struct SPluginMetaData +{ + QVector qvsReturnFilePaths; + QVector qvsSendFilePaths; + bool isAdd2Map; + +}; +class SrsMainInterface : public QObject +{ + Q_OBJECT +public: + virtual ~SrsMainInterface() {} + + /// + /// 返回pannel的名字 + /// + virtual QString PannelName() = 0; + + /// + /// 返回Category名字 + /// + /// + virtual QString CategoryName() = 0; + + /// + /// 返回插件的英文名字 + /// + /// + virtual QString EnglishName() = 0; + /// + /// 返回插件的中文名字 + /// + /// + virtual QString ChineseName() = 0; + virtual QString Information() = 0; + virtual QString IconPath() = 0; + virtual QWidget* CenterWidget() = 0; + // + //public slots: + // virtual void test(QWidget* parent) = 0; + +signals: + void AddDataToMap(QStringList string_list); + //void addDataToCanvas(QStringList string_list); +}; + +#define MainInterface_iid "com.Srs.MainInterface" +Q_DECLARE_INTERFACE(SrsMainInterface, MainInterface_iid) diff --git a/DEM_GeneRas/ThreadObject.cpp b/DEM_GeneRas/ThreadObject.cpp new file mode 100644 index 0000000..4918c6a --- /dev/null +++ b/DEM_GeneRas/ThreadObject.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include + +#include + +#include "DEM_GeneRas.h" +#include "ThreadObject.h" + +#include "im2shp.h" + +using namespace std; + +WorkThreadObject::WorkThreadObject(QObject* parent) :QObject(parent) +{ + qRegisterMetaType >("std::function"); +} + +WorkThreadObject::~WorkThreadObject() +{ + +} + +void WorkThreadObject::runPredictWork(QString Moudle, QString DOM, QString DSM, QString PRE)//, std::functionfunc +{ + QString str = QString("%1->%2,thread id:%3").arg(__FILE__).arg(__FUNCTION__).arg((int)QThread::currentThreadId()); + QDir pluginsDir = QDir(qApp->applicationDirPath()); + qDebug() << str << "----" << pluginsDir.currentPath(); + if (!pluginsDir.cd("models\\envs")) + { + qDebug() << "no folder models\\envs"; + return; + } + QString exeDirName = pluginsDir.absoluteFilePath("predict_4c_vegetation.exe"); + qDebug() << "----" << exeDirName; + + QString model = " --model_path " + Moudle; + QString dom_path = " --dom_path " + DOM + "/"; + QString dsm_path = " --dsm_path " + DSM + "/"; + QString pre_dir = " --pre_path " + PRE + "/"; + QString ss = exeDirName + model + dom_path + dsm_path + pre_dir; + qDebug() << ss; + QProcess* pProces = new QProcess(this); + connect(pProces, SIGNAL(readyReadStandardOutput()), this, SLOT(on_read())); + + pProces->start(ss); + emit process(2); +} + +void WorkThreadObject::on_read() +{ + mProces = (QProcess*)sender(); + QString output = QString::fromLocal8Bit(mProces->readAllStandardOutput()); + if (output.toFloat() > 0) + { + qDebug() << "exe out:" << output.toFloat(); + emit process(output.toFloat()); + if (output.toFloat() == 100.0) + emit addDataToMap(); + } + else + qDebug() << "Unresolved exe out:" << output; +} + +void WorkThreadObject::runFormatConvert(QString pre_path, QString DSM) +{ + QDir dir(pre_path); + QStringList nameFilters; + nameFilters << "*.tif" << "*.tiff"; + QStringList pre_tiff_files = dir.entryList(nameFilters, QDir::Files | QDir::Readable, QDir::Name); + int list_len = pre_tiff_files.size(); + for (int i = 0; i < list_len; i++) + { + emit process(float(i) / float(list_len) * 95); + qDebug() << "TiffToShp progress:" << float(i) / float(list_len) * 100; + QString file_path = pre_path + "\\" + pre_tiff_files[i]; + QString dsm_path = DSM + "\\" + pre_tiff_files[i]; + string path_preTiff = file_path.toStdString(); + string pathPureName = path_preTiff.substr(0, path_preTiff.rfind(".")); + string shp_path = pathPureName + ".shp"; + //调起栅格转矢量 + img2shp cc; + QtGDALProcessBar* gb = new QtGDALProcessBar(); + bool returnval = cc.ImagePolygonize(path_preTiff.c_str(), shp_path.c_str(), "ESRI Shapefile", 1, gb, gb); + if (returnval == true) + { + QStringList list; + list << dsm_path << QString::fromStdString(shp_path); + emit addShpDataToMap(list); + } + else + { + //QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("请检查输入数据")); + //mess.setWindowFlags(Qt::Drawer); + //int result = mess.exec(); + //return; + } + } + emit process(100); +} + +void WorkThreadObject::on_cancel() +{ + if (mProces == nullptr) + { + qDebug() << "--mProces null"; + } + else + { + //py打包exe文件名 + QString KillStr = "taskkill /f /im predict_4c_vegetation.exe"; + mProces->startDetached(KillStr); + qDebug() << "--kill Proces"; + } +} \ No newline at end of file diff --git a/DEM_GeneRas/ThreadObject.h b/DEM_GeneRas/ThreadObject.h new file mode 100644 index 0000000..3437467 --- /dev/null +++ b/DEM_GeneRas/ThreadObject.h @@ -0,0 +1,47 @@ +#ifndef THREADOBJECT_H +#define THREADOBJECT_H +#include + +#include +#include +#include +#include +#include +#include + +#include + +//#include "qtclasslibrary1_global.h" +#include "SrsMainPluginInterFace.h" +#include "ui_DEM_GeneRas.h" +//#include "Extract.h" + +using namespace std; + +class VegePredict; + +class WorkThreadObject :public QObject +{ + Q_OBJECT +public: + WorkThreadObject(QObject* parent = NULL); + ~WorkThreadObject(); + + void on_cancel(); + +signals: + void process(int value); + void addDataToMap(); + void addShpDataToMap(QStringList list); + +public slots: + void runPredictWork(QString Moudle, QString DOM, QString DSM, QString PRE);// + void on_read(); + void runFormatConvert(QString pre_path, QString DSM); + +private: + QProcess* mProces = nullptr; + QString assess_txt_path; +}; + +#endif diff --git a/DEM_GeneRas/im2shp.cpp b/DEM_GeneRas/im2shp.cpp new file mode 100644 index 0000000..00d87ff --- /dev/null +++ b/DEM_GeneRas/im2shp.cpp @@ -0,0 +1,154 @@ +#include "im2shp.h" +#include +#include "CProcessBase.h" +/** +* \brief 调用GDAL进度条接口 +* +* 该函数用于将GDAL算法中的进度信息导出到CProcessBase基类中,供给界面显示 +* +* @param dfComplete 完成进度值,其取值为 0.0 到 1.0 之间 +* @param pszMessage 进度信息 +* @param pProgressArg CProcessBase的指针 +* +* @return 返回TRUE表示继续计算,否则为取消 +*/ +int STD_API ALGTermProgress(double dfComplete, const char* pszMessage, void* pProgressArg) +{ + if (pProgressArg != NULL) + { + QtGDALProcessBar* pProcess = (QtGDALProcessBar*)pProgressArg; + pProcess->m_bIsContinue = pProcess->SetPosition(dfComplete); + + if (pProcess->m_bIsContinue) + return TRUE; + else + return FALSE; + } + else + return TRUE; +} + + +int img2shp::ImagePolygonize(const char* pszSrcFile, const char* pszDstFile, const char* pszFormat, int BandNum, + QtGDALProcessBar* probar, QProgressBar* progressBar) +{ + if (NULL == pszSrcFile || NULL == pszDstFile) + { + printf("待处理的栅格数据及输出矢量数据数据无效!"); + return false; + } + + if (GDALGetDriverCount() == 0) + GDALAllRegister(); + OGRRegisterAll(); + CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); + CPLSetConfigOption("SHAPE_ENCODING", "gb2312"); + + //打开输入图像,并判断是否正常 + GDALDataset* poSrcDS = (GDALDataset*)GDALOpen(pszSrcFile, GA_ReadOnly); + if (NULL == poSrcDS) + { + printf("输入文件不能打开,请检查文件是否存在!"); + return false; + } + + //构造矢量文件驱动并创建矢量图层 + GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat); + if (NULL == poDriver) + { + printf("不能创建指定类型的矢量文件驱动!"); + GDALClose((GDALDatasetH)poSrcDS); + return false; + } + + + /* + *Create方法主要用于创建栅格文件的新数据集,其中nXSize,nYSize,nBands,都是对栅格文件的描述,**都置零后台会默认创建矢量数据集** + */ + //根据文件名创建输出矢量数据集 + + GDALDataset* poDstDs = poDriver->Create(pszDstFile, 0, 0, 0, GDT_Unknown, NULL); + if (NULL == poDstDs) + { + printf("不能创建矢量文件!"); + GDALClose((GDALDatasetH)poSrcDS); + + return false; + } + + OGRSpatialReference* poSpatialRef = new OGRSpatialReference(poSrcDS->GetProjectionRef()); + + OGRLayer* poLayer = poDstDs->CreateLayer("Target", poSpatialRef, wkbPolygon, NULL); + if (NULL == poLayer) + { + printf("创建矢量图层失败!"); + GDALClose((GDALDatasetH)poSrcDS); + GDALClose(poDstDs); + poSpatialRef = NULL; + + return false; + } + OGRFieldDefn ofieldDef("value", OFTInteger); + if (OGRERR_NONE != poLayer->CreateField(&ofieldDef)) + { + printf("创建矢量图层属性表失败!"); + GDALClose((GDALDatasetH)poSrcDS); + GDALClose(poDstDs); + poSpatialRef = NULL; + + return false; + } + + //获取图像的对应波段 + GDALRasterBandH hSrcBand = (GDALRasterBandH)poSrcDS->GetRasterBand(1); + + GDALRasterBand* rasterband = poSrcDS->GetRasterBand(1); + + GDALProgressFunc pfnProgress = ALGTermProgress; + + if (!probar->SetStartEndValue(0, 100)) + { + cout << "初始进度值设置失败!!!" << endl; + } + + + //if (GDALPolygonize(hSrcBand, NULL, (OGRLayerH)poLayer, 0, NULL, pfnProgress, probar) != CE_None) + //if (GDALPolygonize(hSrcBand, NULL, (OGRLayerH)poLayer, 0, NULL, GDALTermProgress, NULL) != CE_None) + //qDebug() << rasterband->GetNoDataValue() << endl; + //if (0.0 == poSrcDS->GetRasterBand(1)->GetNoDataValue()) + //{ + // if (CE_None != GDALPolygonize(hSrcBand, hSrcBand, (OGRLayerH)poLayer, 0, NULL, pfnProgress, probar)) + // { + // GDALClose((GDALDatasetH)poSrcDS); + // GDALClose(poDstDs); + // delete poSpatialRef; + // poSpatialRef = NULL; + // return 0; + // } + //} + //else + //{ + // if (CE_None != GDALPolygonize(hSrcBand, NULL, (OGRLayerH)poLayer, 0, NULL, pfnProgress, probar)) + // { + // GDALClose((GDALDatasetH)poSrcDS); + // GDALClose(poDstDs); + // delete poSpatialRef; + // poSpatialRef = NULL; + // return 0; + // } + //} + if (CE_None != GDALPolygonize(hSrcBand, hSrcBand, (OGRLayerH)poLayer, 0, NULL, pfnProgress, probar)) + { + GDALClose((GDALDatasetH)poSrcDS); + GDALClose(poDstDs); + delete poSpatialRef; + poSpatialRef = NULL; + return 0; + } + + GDALClose((GDALDatasetH)poSrcDS); + GDALClose(poDstDs); + poSpatialRef = NULL; + + return true; +} \ No newline at end of file diff --git a/DEM_GeneRas/im2shp.h b/DEM_GeneRas/im2shp.h new file mode 100644 index 0000000..275b79c --- /dev/null +++ b/DEM_GeneRas/im2shp.h @@ -0,0 +1,35 @@ +#pragma once +#include "gdal_priv.h" +#include "ogrsf_frmts.h" //for ogr +#include "gdal_alg.h" //for GDALPolygonize +#include "cpl_conv.h" //for CPLMalloc() +#include "QtGDALProcessBar.h" +#include +/** +* @brief 导出符号定义 +*/ +#ifndef STD_API +#define STD_API __stdcall +#endif +/** +* \brief 调用GDAL进度条接口 +* +* 该函数用于将GDAL算法中的进度信息导出到CProcessBase基类中,供给界面显示 +* +* @param dfComplete 完成进度值,其取值为 0.0 到 1.0 之间 +* @param pszMessage 进度信息 +* @param pProgressArg CProcessBase的指针 +* +* @return 返回TRUE表示继续计算,否则为取消 +*/ +int STD_API ALGTermProgress(double dfComplete, const char* pszMessage, void* pProgressArg); + +class img2shp +{ +public: + int ImagePolygonize(const char* pszSrcFile, const char* pszDstFile, const char* pszFormat, int BandNum, QtGDALProcessBar* probar, QProgressBar* progressBar); + bool createChineseStringProperty(OGRLayer* poLayer, const char* filterFieldName, const char* createFieldName, vector nameVal, float min, QProgressBar* progressBar); +protected: +private: +}; + diff --git a/DEM_GeneRas/main.cpp b/DEM_GeneRas/main.cpp new file mode 100644 index 0000000..f8909ad --- /dev/null +++ b/DEM_GeneRas/main.cpp @@ -0,0 +1,16 @@ +#include "DEM_GeneRas.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + QString gdal_path = qApp->applicationDirPath().toLocal8Bit() + "/share/gdal"; + QString pro_lib_path = qApp->applicationDirPath().toLocal8Bit() + "/share/proj"; + qputenv("GDAL_DATA", gdal_path.toLocal8Bit()); + qputenv("PROJ_LIB", pro_lib_path.toLocal8Bit()); + + VegePredict w; + w.CenterWidget()->show(); + return a.exec(); +} diff --git a/DEM_GeneRas/resources/dem_ras.svg b/DEM_GeneRas/resources/dem_ras.svg new file mode 100644 index 0000000..653bbbf --- /dev/null +++ b/DEM_GeneRas/resources/dem_ras.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/DEM_Predict.sln b/DEM_Predict.sln new file mode 100644 index 0000000..faaac8b --- /dev/null +++ b/DEM_Predict.sln @@ -0,0 +1,22 @@ +锘 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31911.196 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DEM_GeneRas", "DEM_GeneRas\DEM_GeneRas.vcxproj", "{AAEF388B-265D-4CF4-8832-9E6CD50B4644}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AAEF388B-265D-4CF4-8832-9E6CD50B4644}.Release|x64.ActiveCfg = Release|x64 + {AAEF388B-265D-4CF4-8832-9E6CD50B4644}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6D802BB4-76E8-4FE8-8FB7-04BCDE98FF27} + EndGlobalSection +EndGlobal