383 lines
11 KiB
C++
383 lines
11 KiB
C++
|
#include "LandslidePredict.h"
|
|||
|
|
|||
|
#include <qgsrasterlayer.h>
|
|||
|
#include <qgsvectorlayer.h>
|
|||
|
#include <qgsmapcanvas.h>
|
|||
|
|
|||
|
LandslidePredict::LandslidePredict()
|
|||
|
{
|
|||
|
//ui.setupUi(this);
|
|||
|
}
|
|||
|
|
|||
|
QString LandslidePredict::PannelName()
|
|||
|
{
|
|||
|
return QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>");
|
|||
|
}
|
|||
|
|
|||
|
QString LandslidePredict::CategoryName()
|
|||
|
{
|
|||
|
return QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>");
|
|||
|
}
|
|||
|
|
|||
|
QString LandslidePredict::EnglishName()
|
|||
|
{
|
|||
|
return QString::fromLocal8Bit("LandslidePredict");
|
|||
|
}
|
|||
|
|
|||
|
QString LandslidePredict::ChineseName()
|
|||
|
{
|
|||
|
return QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><EFBFBD>");
|
|||
|
}
|
|||
|
|
|||
|
QString LandslidePredict::Information()
|
|||
|
{
|
|||
|
return QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><EFBFBD>");
|
|||
|
}
|
|||
|
|
|||
|
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";
|
|||
|
//<2F><><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
myWidget->activateWindow();
|
|||
|
myWidget->raise();
|
|||
|
return myWidget;
|
|||
|
}
|
|||
|
ui.setupUi(myWidget);
|
|||
|
|
|||
|
myWidget->setWindowTitle(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><EFBFBD>"));
|
|||
|
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();//<2F><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3>ж<EFBFBD>
|
|||
|
mWorkThread->quit();
|
|||
|
mWorkThread->wait();//<2F><><EFBFBD><EFBFBD>wait<69><74><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD>finished<65>źŶ<C5BA>Ӧ<EFBFBD>IJۺ<C4B2><DBBA><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
mWorkObject->on_cancel();
|
|||
|
mWorkThread = nullptr;//mWorkThread, &QThread::finished, mWorkThread, &QObject::deleteLater<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ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); //<2F><>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
|
|||
|
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);
|
|||
|
|
|||
|
//<2F><EFBFBD><F2BFAAB1><EFBFBD>Ϊ<EFBFBD><CEAA>[SldPredict] <20><><EFBFBD><EFBFBD>
|
|||
|
configIni.beginGroup("SldPredict");
|
|||
|
|
|||
|
ui.lineInDataset->setText(configIni.value("SrcDom").toString());
|
|||
|
ui.linePreResult->setText(configIni.value("PredictResult").toString());
|
|||
|
|
|||
|
configIni.endGroup();//<2F>ر<EFBFBD><D8B1><EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
void LandslidePredict::WriteConfigPaths(QString strPath)
|
|||
|
{
|
|||
|
QSettings configIni(strPath, QSettings::IniFormat);
|
|||
|
configIni.setIniCodec("utf-8");
|
|||
|
//<2F><EFBFBD><F2BFAAB1><EFBFBD>Ϊ<EFBFBD><CEAA>[SldPredict] <20><><EFBFBD><EFBFBD>
|
|||
|
configIni.beginGroup("SldPredict");
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DOM·<4D><C2B7>
|
|||
|
QString temp = ui.lineInDataset->text();
|
|||
|
if (temp != "")
|
|||
|
configIni.setValue("SrcDom", temp);
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
|
|||
|
temp = ui.linePreResult->text();
|
|||
|
if (temp != "")
|
|||
|
configIni.setValue("PredictResult", temp);
|
|||
|
|
|||
|
configIni.endGroup();//<2F>ر<EFBFBD><D8B1><EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
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(); // ͼ<><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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<QgsMapLayer*> 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<QgsMapLayer*> 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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>"));
|
|||
|
mess.setWindowFlags(Qt::Drawer);
|
|||
|
int result = mess.exec();
|
|||
|
return;
|
|||
|
}
|
|||
|
if (mWorkThread == nullptr)
|
|||
|
{
|
|||
|
qDebug() << "--startThread";
|
|||
|
startWorkThread();
|
|||
|
}
|
|||
|
|
|||
|
ui.label_progress->setText(QString::fromLocal8Bit("Ԥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:"));
|
|||
|
emit start(dirModel, dirDataset, dirPRE);
|
|||
|
}
|
|||
|
|
|||
|
void LandslidePredict::pbCancel()
|
|||
|
{
|
|||
|
qDebug() << "--pbtCancel";
|
|||
|
delete myWidget;//<2F><><EFBFBD><EFBFBD>&QDialog::destroyed
|
|||
|
}
|
|||
|
|
|||
|
void LandslidePredict::chooseInModel()
|
|||
|
{
|
|||
|
QString dirModel = QFileDialog::getOpenFileName(ui.pbtInModel, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>"), "", "*.pth");
|
|||
|
if (dirModel != "")
|
|||
|
ui.lineInModel->setText(dirModel);
|
|||
|
}
|
|||
|
|
|||
|
void LandslidePredict::chooseInDataset()
|
|||
|
{
|
|||
|
QString dirInDataset = QFileDialog::getExistingDirectory(ui.pbtInDataset, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>"), "");
|
|||
|
if (dirInDataset != "")
|
|||
|
ui.lineInDataset->setText(dirInDataset);
|
|||
|
}
|
|||
|
|
|||
|
void LandslidePredict::chooseResultPath()
|
|||
|
{
|
|||
|
QString dirPre = QFileDialog::getExistingDirectory(ui.pbtPreResult, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD>"), "");
|
|||
|
if (dirPre != "")
|
|||
|
ui.linePreResult->setText(dirPre);
|
|||
|
}
|
|||
|
|
|||
|
//Ԥ<><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>תʸ<D7AA><CAB8>
|
|||
|
void LandslidePredict::finished()
|
|||
|
{
|
|||
|
QMessageBox mess(QMessageBox::NoIcon,
|
|||
|
QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"),
|
|||
|
QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>תʸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:\n") + ui.linePreResult->text(),
|
|||
|
QMessageBox::Ok, NULL);
|
|||
|
mess.setWindowFlags(Qt::Drawer);
|
|||
|
mess.setButtonText(QMessageBox::Ok, QString::fromLocal8Bit("ȷ<EFBFBD><EFBFBD>"));
|
|||
|
int result = mess.exec();
|
|||
|
switch (result)
|
|||
|
{
|
|||
|
case QMessageBox::Ok:
|
|||
|
emit startTiffToShp(ui.linePreResult->text(), ui.lineInDataset->text());
|
|||
|
ui.label_progress->setText(QString::fromLocal8Bit("תʸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:"));
|
|||
|
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";
|
|||
|
//<2F><><EFBFBD><EFBFBD>դ<EFBFBD><D5A4>תʸ<D7AA><CAB8>
|
|||
|
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);
|
|||
|
}
|