DianLi/DEM_GeneRas/DEM_GeneRas.cpp

334 lines
9.3 KiB
C++
Raw Permalink Normal View History

2023-03-13 16:13:20 +08:00
#include "DEM_GeneRas.h"
#include <qgsrasterlayer.h>
#include <qgsvectorlayer.h>
#include <qgsmapcanvas.h>
#include <QSettings>
VegePredict::VegePredict()
{
//ui.setupUi(this);
}
VegePredict::~VegePredict()
{
}
QString VegePredict::PannelName()
{
return QString::fromLocal8Bit("");
}
QString VegePredict::CategoryName()
{
return QString::fromLocal8Bit("DEMģ<EFBFBD><EFBFBD>");
}
QString VegePredict::EnglishName()
{
return QString::fromLocal8Bit("DEM_Module");
}
QString VegePredict::ChineseName()
{
return QString::fromLocal8Bit("ֲ<EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD>");
}
QString VegePredict::Information()
{
return QString::fromLocal8Bit("ֲ<EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD>");
}
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";
//<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>"));
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();//<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>
mWorker->on_cancel();
mWorkThread = nullptr;//mWorkThread, &QThread::finished, mWorkThread, &QObject::deleteLater<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ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); //<2F><>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
if (qssFile.isOpen())
{
QString qss = QLatin1String(qssFile.readAll());
myWidget->setStyleSheet(qss);
qssFile.close();
}
else
qDebug() << "<EFBFBD>޷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>";
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("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DOM<EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD>"), "");
if (dirDom != "")
ui.line_DOM->setText(dirDom);
}
void VegePredict::chooseDsmPath()
{
QString dirDsm = QFileDialog::getExistingDirectory(ui.pbt_DSM, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Slope<EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD>"), "");
if (dirDsm != "")
ui.line_DSM->setText(dirDsm);
}
void VegePredict::choosePreDir()
{
QString dirPre = QFileDialog::getExistingDirectory(ui.pbt_PRE, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD>"), "");
if (dirPre != "")
ui.line_PRE->setText(dirPre);
}
void VegePredict::chooseMouleFile()
{
QString dirPre = QFileDialog::getOpenFileName(ui.pbt_Moudle, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>"), "", "*.pth");
if (dirPre != "")
ui.line_Moudle->setText(dirPre);
}
void VegePredict::addMap()
{
QMessageBox mess(QMessageBox::NoIcon,
QString::fromLocal8Bit("ֲ<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.line_PRE->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.line_PRE->text(), ui.line_DSM->text());
ui.label_process->setText(QString::fromLocal8Bit("תʸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:"));
ui.progressBar->setValue(0);
break;
default:
break;
}
}
void VegePredict::ReadConfigHistoryPaths(QString strPath)
{
QSettings configIni(strPath, QSettings::IniFormat);
//<2F>򿪱<EFBFBD><F2BFAAB1><EFBFBD>Ϊ<EFBFBD><CEAA>[DemPredict] <20><><EFBFBD><EFBFBD>
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();//<2F>ر<EFBFBD><D8B1><EFBFBD>
}
void VegePredict::WriteConfigPaths(QString strPath)
{
QSettings configIni(strPath, QSettings::IniFormat);
configIni.setIniCodec("utf-8");
//<2F>򿪱<EFBFBD><F2BFAAB1><EFBFBD>Ϊ<EFBFBD><CEAA>[DemPredict] <20><><EFBFBD><EFBFBD>
configIni.beginGroup("DemPredict");
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DOM·<4D><C2B7>
QString temp = ui.line_DOM->text();
if (temp != "")
configIni.setValue("SrcDom", temp);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DSM·<4D><C2B7>
temp = ui.line_DSM->text();
if (temp != "")
configIni.setValue("SrcDsm", temp);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
temp = ui.line_PRE->text();
if (temp != "")
configIni.setValue("PredictResult", temp);
configIni.endGroup();//<2F>ر<EFBFBD><D8B1><EFBFBD>
}
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("<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;
}
//startObjThread();
if (mWorkThread == nullptr)
{
qDebug() << "--startThread";
startWorkThread();
}
//std::function<void(int)>processCallback = [=](int val) {
// ui.progressBar->setValue(val);
// qDebug() << val << endl;
//};//<2F><><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>;
/////////////////////////////////////////////////////////////////////
//emit startTiffToShp(ui.line_PRE->text(), ui.line_Label->text());
/////////////////////////////////////////////////////////////////////
ui.label_process->setText(QString::fromLocal8Bit("Ԥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:"));
emit start(fileMoudle, dirDOM, dirDSM, dirPRE);//, processCallback
}
void VegePredict::pbCancel()
{
delete myWidget;//<2F><><EFBFBD><EFBFBD>&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(); // ͼ<><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);
}
}
}