From ef3f64410d74e554ddbb3c6826d7a5caf87c6044 Mon Sep 17 00:00:00 2001 From: keyeslll Date: Mon, 13 Mar 2023 16:17:55 +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 --- LandslidePredict.sln | 22 + LandslidePredict/CProcessBase.h | 98 +++++ LandslidePredict/LandslidePredict.cpp | 383 ++++++++++++++++++ LandslidePredict/LandslidePredict.h | 94 +++++ LandslidePredict/LandslidePredict.qrc | 6 + LandslidePredict/LandslidePredict.qss | 68 ++++ LandslidePredict/LandslidePredict.ui | 216 ++++++++++ LandslidePredict/LandslidePredict.vcxproj | 89 ++++ .../LandslidePredict.vcxproj.filters | 74 ++++ LandslidePredict/QtGDALProcessBar.cpp | 100 +++++ LandslidePredict/QtGDALProcessBar.h | 62 +++ LandslidePredict/SrsMainPluginInterFace.h | 58 +++ LandslidePredict/im2shp.cpp | 154 +++++++ LandslidePredict/im2shp.h | 35 ++ LandslidePredict/main.cpp | 10 + LandslidePredict/resources/landslide.svg | 1 + 16 files changed, 1470 insertions(+) create mode 100644 LandslidePredict.sln create mode 100644 LandslidePredict/CProcessBase.h create mode 100644 LandslidePredict/LandslidePredict.cpp create mode 100644 LandslidePredict/LandslidePredict.h create mode 100644 LandslidePredict/LandslidePredict.qrc create mode 100644 LandslidePredict/LandslidePredict.qss create mode 100644 LandslidePredict/LandslidePredict.ui create mode 100644 LandslidePredict/LandslidePredict.vcxproj create mode 100644 LandslidePredict/LandslidePredict.vcxproj.filters create mode 100644 LandslidePredict/QtGDALProcessBar.cpp create mode 100644 LandslidePredict/QtGDALProcessBar.h create mode 100644 LandslidePredict/SrsMainPluginInterFace.h create mode 100644 LandslidePredict/im2shp.cpp create mode 100644 LandslidePredict/im2shp.h create mode 100644 LandslidePredict/main.cpp create mode 100644 LandslidePredict/resources/landslide.svg diff --git a/LandslidePredict.sln b/LandslidePredict.sln new file mode 100644 index 0000000..145f357 --- /dev/null +++ b/LandslidePredict.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.32630.194 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LandslidePredict", "LandslidePredict\LandslidePredict.vcxproj", "{ABE55CAB-C9A0-4D11-AF67-DEFEF07F3DA5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {ABE55CAB-C9A0-4D11-AF67-DEFEF07F3DA5}.Release|x64.ActiveCfg = Release|x64 + {ABE55CAB-C9A0-4D11-AF67-DEFEF07F3DA5}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A9D5438D-7ABE-4CBF-BEAE-6F3FE0CBEB3F} + EndGlobalSection +EndGlobal diff --git a/LandslidePredict/CProcessBase.h b/LandslidePredict/CProcessBase.h new file mode 100644 index 0000000..2106b7e --- /dev/null +++ b/LandslidePredict/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/LandslidePredict/LandslidePredict.cpp b/LandslidePredict/LandslidePredict.cpp new file mode 100644 index 0000000..ef2deda --- /dev/null +++ b/LandslidePredict/LandslidePredict.cpp @@ -0,0 +1,383 @@ +#include "LandslidePredict.h" + +#include +#include +#include + +LandslidePredict::LandslidePredict() +{ + //ui.setupUi(this); +} + +QString LandslidePredict::PannelName() +{ + return QString::fromLocal8Bit("ģ"); +} + +QString LandslidePredict::CategoryName() +{ + return QString::fromLocal8Bit("ģ"); +} + +QString LandslidePredict::EnglishName() +{ + return QString::fromLocal8Bit("LandslidePredict"); +} + +QString LandslidePredict::ChineseName() +{ + return QString::fromLocal8Bit("ʶ"); +} + +QString LandslidePredict::Information() +{ + return QString::fromLocal8Bit("ʶ"); +} + +QString LandslidePredict::IconPath() +{ + return QString(":/LandslidePredict/resources/landslide.svg"); +} + +QWidget* LandslidePredict::CenterWidget() +{ + 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(":/LandslidePredict/resources/landslide.svg")); + myWidget->setAttribute(Qt::WA_QuitOnClose, false); + + myWidget->setAttribute(Qt::WA_DeleteOnClose); + + connect(myWidget, &QDialog::destroyed, this, [=]() { + qDebug() << "----Landslide Predict window close----"; + + QDir pluginsDir = QDir(qApp->applicationDirPath()); + if (pluginsDir.cd("srsplugins\\SldModel")) + { + QString strConfigPath = pluginsDir.absoluteFilePath("sld_config.ini"); + QFile f(strConfigPath); + if (f.exists()) + { + WriteConfigPaths(strConfigPath); + f.close(); + } + } + + if (mWorkThread != nullptr) + { + mWorkThread->requestInterruption();//߳ж + mWorkThread->quit(); + mWorkThread->wait();//waitȵfinishedźŶӦIJۺִɺ + mWorkObject->on_cancel(); + mWorkThread = nullptr;//mWorkThread, &QThread::finished, mWorkThread, &QObject::deleteLaterҪdelete + mWorkObject = nullptr;//mWorkThread, &QThread::finished, mWorker, &QObject::deleteLater + } + myWidget->close(); + myWidget = nullptr; + qDebug() << "--destroyed"; + }); + connect(ui.pbtInModel, &QPushButton::clicked, this, &LandslidePredict::chooseInModel); + connect(ui.pbtInDataset, &QPushButton::clicked, this, &LandslidePredict::chooseInDataset); + connect(ui.pbtPreResult, &QPushButton::clicked, this, &LandslidePredict::chooseResultPath); + + connect(ui.pushButton_ok, &QPushButton::clicked, this, &LandslidePredict::readAndStart); + connect(ui.pushButton_cancel, &QPushButton::clicked, this, &LandslidePredict::pbCancel); + + ui.pbtInModel->setFocus(); + + ui.progressBar->setTextVisible(true); + ui.progressBar->setRange(0, 100); + + QFile qssFile(":/LandslidePredict/LandslidePredict.qss"); + qssFile.open(QFile::ReadOnly); //ֻʽ + if (qssFile.isOpen()) + { + QString qss = QLatin1String(qssFile.readAll()); + myWidget->setStyleSheet(qss); + qssFile.close(); + } + else + qDebug() << "no qssFile"; + + QDir pluginsDir = QDir(qApp->applicationDirPath()); + if (pluginsDir.cd("srsplugins\\SldModel")) + { + QString strConfigPath = pluginsDir.absoluteFilePath("sld_config.ini"); + QFile f(strConfigPath); + if (f.exists()) + { + ReadConfigHistoryPaths(strConfigPath); + f.close(); + } + } + + if (showWin) + myWidget->show(); + + return myWidget; +} + +void LandslidePredict::startWorkThread() +{ + if (mWorkObject != nullptr) + return; + mWorkThread = new QThread(); + mWorkObject = new WorkObject(); + mWorkObject->moveToThread(mWorkThread); + connect(mWorkThread, &QThread::finished, mWorkThread, &QObject::deleteLater); + connect(mWorkThread, &QThread::finished, mWorkObject, &QObject::deleteLater); + connect(mWorkObject, &WorkObject::progress, myWidget, [=](int value) {ui.progressBar->setValue(value); }); + connect(mWorkObject, &WorkObject::preFinished, this, &LandslidePredict::finished); + connect(mWorkObject, &WorkObject::addShpDataToMap, this, &LandslidePredict::openResultData); + connect(this, &LandslidePredict::startTiffToShp, mWorkObject, &WorkObject::runFormatConvert); + + connect(this, &LandslidePredict::start, mWorkObject, &WorkObject::runPredictWork); + mWorkThread->start(); +} + +void LandslidePredict::ReadConfigHistoryPaths(QString strPath) +{ + QSettings configIni(strPath, QSettings::IniFormat); + + //򿪱Ϊ[SldPredict] + configIni.beginGroup("SldPredict"); + + ui.lineInDataset->setText(configIni.value("SrcDom").toString()); + ui.linePreResult->setText(configIni.value("PredictResult").toString()); + + configIni.endGroup();//ر +} + +void LandslidePredict::WriteConfigPaths(QString strPath) +{ + QSettings configIni(strPath, QSettings::IniFormat); + configIni.setIniCodec("utf-8"); + //򿪱Ϊ[SldPredict] + configIni.beginGroup("SldPredict"); + + //DOM· + QString temp = ui.lineInDataset->text(); + if (temp != "") + configIni.setValue("SrcDom", temp); + //· + temp = ui.linePreResult->text(); + if (temp != "") + configIni.setValue("PredictResult", temp); + + configIni.endGroup();//ر +} + +void LandslidePredict::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); + } + } +} + +void LandslidePredict::readAndStart() +{ + QString dirModel = ui.lineInModel->text(); + QString dirDataset = ui.lineInDataset->text(); + QString dirPRE = ui.linePreResult->text(); + + ui.progressBar->setValue(0); + if (dirModel == "" || dirDataset == "" || dirPRE == "") + { + QMessageBox mess(QMessageBox::NoIcon, QString::fromLocal8Bit(""), QString::fromLocal8Bit("ļ·")); + mess.setWindowFlags(Qt::Drawer); + int result = mess.exec(); + return; + } + if (mWorkThread == nullptr) + { + qDebug() << "--startThread"; + startWorkThread(); + } + + ui.label_progress->setText(QString::fromLocal8Bit("Ԥ:")); + emit start(dirModel, dirDataset, dirPRE); +} + +void LandslidePredict::pbCancel() +{ + qDebug() << "--pbtCancel"; + delete myWidget;//&QDialog::destroyed +} + +void LandslidePredict::chooseInModel() +{ + QString dirModel = QFileDialog::getOpenFileName(ui.pbtInModel, QString::fromLocal8Bit("ѡģļ"), "", "*.pth"); + if (dirModel != "") + ui.lineInModel->setText(dirModel); +} + +void LandslidePredict::chooseInDataset() +{ + QString dirInDataset = QFileDialog::getExistingDirectory(ui.pbtInDataset, QString::fromLocal8Bit("ѡԤ·"), ""); + if (dirInDataset != "") + ui.lineInDataset->setText(dirInDataset); +} + +void LandslidePredict::chooseResultPath() +{ + QString dirPre = QFileDialog::getExistingDirectory(ui.pbtPreResult, QString::fromLocal8Bit("ѡԤļ·"), ""); + if (dirPre != "") + ui.linePreResult->setText(dirPre); +} + +//Ԥתʸ +void LandslidePredict::finished() +{ + QMessageBox mess(QMessageBox::NoIcon, + QString::fromLocal8Bit("ʶ"), + QString::fromLocal8Bit("·нתʸ:\n") + ui.linePreResult->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.linePreResult->text(), ui.lineInDataset->text()); + ui.label_progress->setText(QString::fromLocal8Bit("תʸ:")); + ui.progressBar->setValue(0); + break; + default: + break; + } +} + +void WorkObject::runPredictWork(QString inModel, QString inDataset, QString outPre) +{ + QDir pluginsDir = QDir(qApp->applicationDirPath()); + if (!pluginsDir.cd("models\\envs")) + { + qDebug() << "no folder models\\envs"; + return; + } + QString exeDirName = pluginsDir.absoluteFilePath("predict_3c_landslide.exe"); + + QString strModel = " --model_path " + inModel; + QString strData = " --dom_path " + inDataset + "/"; + QString strPre = " --pre_path " + outPre + "/"; + + QString ss = exeDirName + strModel + strData + strPre; + qDebug() << ss; + QProcess* pProces = new QProcess(this); + connect(pProces, SIGNAL(readyReadStandardOutput()), this, SLOT(on_read())); + + pProces->start(ss); +} + +void WorkObject::on_read() +{ + mProcess = (QProcess*)sender(); + QString output = QString::fromLocal8Bit(mProcess->readAllStandardOutput()); + if (output.toFloat() > 0) + { + qDebug() << "exe out:" << output.toFloat(); + emit progress(output.toFloat()); + if (output.toFloat() == 100) + emit preFinished(); + } + else + qDebug() << "Unresolved exe out:" << output; +} + +void WorkObject::on_cancel() +{ + if (mProcess == nullptr) + { + qDebug() << "--mProces null"; + } + else + { + QString KillStr = "taskkill /f /im predict_3c_landslide.exe"; + mProcess->startDetached(KillStr); + qDebug() << "--kill Proces"; + } +} + +void WorkObject::runFormatConvert(QString pre, QString dataset) +{ + QDir dir(pre); + 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 progress(float(i) / float(list_len) * 95); + qDebug() << "TiffToShp progress:" << float(i) / float(list_len) * 100; + QString file_path = pre + "\\" + pre_tiff_files[i]; + QString dsm_path = dataset + "\\" + 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 + qDebug() << "ImagePolygonize fail"; + } + emit progress(100); +} \ No newline at end of file diff --git a/LandslidePredict/LandslidePredict.h b/LandslidePredict/LandslidePredict.h new file mode 100644 index 0000000..ab600e1 --- /dev/null +++ b/LandslidePredict/LandslidePredict.h @@ -0,0 +1,94 @@ +#pragma once + +#include +#include "ui_LandslidePredict.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "SrsMainPluginInterFace.h" + +#include +#include + +#include "im2shp.h" + +using namespace std; + +class WorkObject :public QObject +{ + Q_OBJECT + +public slots: + void runPredictWork(QString inModel, QString inDataset, QString outPre); + void on_read(); + void on_cancel(); + void runFormatConvert(QString pre, QString dataset); + +signals: + void progress(int value); + void preFinished(); + void addShpDataToMap(QStringList list); + +private: + QProcess* mProcess = nullptr; + +}; + +class LandslidePredict : public SrsMainInterface +{ + Q_OBJECT + Q_INTERFACES(SrsMainInterface) + Q_PLUGIN_METADATA(IID MainInterface_iid) + +public: + LandslidePredict(); + + 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; + + void startWorkThread(); + + //jsonļȡʷ洢· + void ReadConfigHistoryPaths(QString strPath); + //汾δ򿪵·jsonļ + void WriteConfigPaths(QString strPath); + + void openResultData(QStringList string_list); + +public slots: + void readAndStart(); + void pbCancel(); + + void chooseInModel(); + void chooseInDataset(); + void chooseResultPath(); + + void finished(); + +signals: + void start(QString inModel, QString inDataset, QString outPre); + void startTiffToShp(QString pre, QString dataset); + +private: + Ui::LandslidePredictClass ui; + + QDialog* myWidget = nullptr; + + QThread* mWorkThread = nullptr; + WorkObject* mWorkObject = nullptr; + +}; diff --git a/LandslidePredict/LandslidePredict.qrc b/LandslidePredict/LandslidePredict.qrc new file mode 100644 index 0000000..5853514 --- /dev/null +++ b/LandslidePredict/LandslidePredict.qrc @@ -0,0 +1,6 @@ + + + resources/landslide.svg + LandslidePredict.qss + + diff --git a/LandslidePredict/LandslidePredict.qss b/LandslidePredict/LandslidePredict.qss new file mode 100644 index 0000000..536b94f --- /dev/null +++ b/LandslidePredict/LandslidePredict.qss @@ -0,0 +1,68 @@ +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, #pbtInModel, #pbtInDataset , #pbtPreResult{ + 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, #pbtInModel:hover, #pbtInDataset:hover,#pbtPreResult:hover{ + background-color: #ecf5ff; + color: #409eff; +} +#pushButton_ok:pressed, #pushButton_cancel:pressed, #pbtInModel:pressed, #pbtInDataset:pressed,#pbtPreResult:pressed{ + border: 1px solid #3a8ee6; + color: #409eff; +} +#pushButton_ok:checked, #pushButton_cancel:checked, #pbtInModel:checked, #pbtInDataset:checked,#pbtPreResult:checked{ + border: 1px solid #3a8ee6; + color: #409eff; +} +#pushButton_ok:focus, #pushButton_cancel:focus, #pbtInModel:focus, #pbtInDataset:focus,#pbtPreResult:focus{ + border: 1px solid #3a8ee6; + color: #409eff; + outline: none; +} + +/*----QLineEditʽ*/ +#lineInModel, #lineInDataset, #linePreResult{ + border:0px; + border-bottom: 1px solid #B3B3B3; + font-family:'Microsoft YaHei'; + font-size:12px; + background-color:transparent; +} +#lineInModel:hover, #lineInDataset:hover, #linePreResult:hover{ + border-bottom: 2px solid #66A3FF; +} +#lineInModel:focus, #lineInDataset:focus, #linePreResult:focus{ + border-bottom: 2px solid #7666FF; +} diff --git a/LandslidePredict/LandslidePredict.ui b/LandslidePredict/LandslidePredict.ui new file mode 100644 index 0000000..f62b4dd --- /dev/null +++ b/LandslidePredict/LandslidePredict.ui @@ -0,0 +1,216 @@ + + + LandslidePredictClass + + + + 0 + 0 + 460 + 270 + + + + + 460 + 270 + + + + + 460 + 270 + + + + LandslidePredict + + + + + + 0 + + + + + + + 进度: + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 确认 + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 10 + 20 + + + + + + + + 取消 + + + + + + + + + + + + 0 + 0 + + + + 输入模型文件路径 + + + + + + + + + + + 75 + 0 + + + + + 75 + 16777215 + + + + 打开文件 + + + + + + + + 0 + 0 + + + + 输入预测数据路径 + + + + + + + + + + + 75 + 0 + + + + + 75 + 16777215 + + + + 打开文件夹 + + + + + + + + 0 + 0 + + + + 预测结果文件路径 + + + + + + + + + + + 75 + 0 + + + + + 75 + 16777215 + + + + 打开文件夹 + + + + + + + + + + lineInModel + pbtInModel + lineInDataset + pbtInDataset + linePreResult + pbtPreResult + pushButton_ok + pushButton_cancel + + + + + + diff --git a/LandslidePredict/LandslidePredict.vcxproj b/LandslidePredict/LandslidePredict.vcxproj new file mode 100644 index 0000000..1824301 --- /dev/null +++ b/LandslidePredict/LandslidePredict.vcxproj @@ -0,0 +1,89 @@ + + + + + Release + x64 + + + + {ABE55CAB-C9A0-4D11-AF67-DEFEF07F3DA5} + QtVS_v304 + 10.0 + $(MSBuildProjectDirectory)\QtMsBuild + + + + DynamicLibrary + v142 + + + + + + + QtQgis + core;gui;widgets + release + + + + + + + + + + + + + D:\qgis\osgeo4w\include;D:\qgis\osgeo4w\apps\qgis_build_sdk\include;D:\qgis\osgeo4w\apps\Qt5\include;D:\qgis\osgeo4w\apps\Qt5\include\QtXml;$(IncludePath) + D:\qgis\osgeo4w\lib;D:\qgis\osgeo4w\apps\qgis_build_sdk\lib;D:\qgis\osgeo4w\apps\Qt5\lib;$(LibraryPath) + landslide3_pre + + + + 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/LandslidePredict/LandslidePredict.vcxproj.filters b/LandslidePredict/LandslidePredict.vcxproj.filters new file mode 100644 index 0000000..221803d --- /dev/null +++ b/LandslidePredict/LandslidePredict.vcxproj.filters @@ -0,0 +1,74 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + qml;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 + + + + + Resource Files + + + Form Files + + + Header Files + + + Source Files + + + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + + + Form Files + + + Header Files + + + Header Files + + + + + Resource Files + + + \ No newline at end of file diff --git a/LandslidePredict/QtGDALProcessBar.cpp b/LandslidePredict/QtGDALProcessBar.cpp new file mode 100644 index 0000000..f34c245 --- /dev/null +++ b/LandslidePredict/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/LandslidePredict/QtGDALProcessBar.h b/LandslidePredict/QtGDALProcessBar.h new file mode 100644 index 0000000..b50aa3f --- /dev/null +++ b/LandslidePredict/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/LandslidePredict/SrsMainPluginInterFace.h b/LandslidePredict/SrsMainPluginInterFace.h new file mode 100644 index 0000000..9130479 --- /dev/null +++ b/LandslidePredict/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/LandslidePredict/im2shp.cpp b/LandslidePredict/im2shp.cpp new file mode 100644 index 0000000..00d87ff --- /dev/null +++ b/LandslidePredict/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ҪڴդļݼnXSizenYSizenBandsǶդļ**̨Ĭϴʸݼ** + */ + //ļʸݼ + + 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/LandslidePredict/im2shp.h b/LandslidePredict/im2shp.h new file mode 100644 index 0000000..275b79c --- /dev/null +++ b/LandslidePredict/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/LandslidePredict/main.cpp b/LandslidePredict/main.cpp new file mode 100644 index 0000000..f5b3353 --- /dev/null +++ b/LandslidePredict/main.cpp @@ -0,0 +1,10 @@ +#include "LandslidePredict.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + LandslidePredict w; + w.CenterWidget()->show(); + return a.exec(); +} diff --git a/LandslidePredict/resources/landslide.svg b/LandslidePredict/resources/landslide.svg new file mode 100644 index 0000000..283956c --- /dev/null +++ b/LandslidePredict/resources/landslide.svg @@ -0,0 +1 @@ + \ No newline at end of file