DianLi/DEM_GeneRas/DEM_GeneRas.cpp
2023-03-13 16:13:20 +08:00

334 lines
9.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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Ä£¿é");
}
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::function<void(int)>processCallback = [=](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<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);
}
}
}