DianLi/DEM_Fill/DEM_Fill.cpp

365 lines
9.9 KiB
C++
Raw Permalink Normal View History

2023-03-13 16:22:02 +08:00
#include "DEM_Fill.h"
#include <qgsrasterlayer.h>
#include <qgsvectorlayer.h>
#include <qgsmapcanvas.h>
void WorkThreadObject::runFillingWork(QString in_masked, QString out_filled, QString scale, QString size)
{
QDir pyDir = QDir(qApp->applicationDirPath());
if (!pyDir.cd("models\\mask_fill"))
{
qDebug() << "no folder models\\mask_fill";
return;
}
QString pyFilePath = pyDir.absoluteFilePath("demFillHoles.py");
if (pyFilePath == "")
{
qDebug() << "not find demFillHoles.py";
return;
}
QDir datDir = QDir(qApp->applicationDirPath());
if (!datDir.cd("QGIS_3.22.8\\bin"))
{
qDebug() << "no folder QGIS_3.22.8\\bin";
return;
}
QString datPath = datDir.absoluteFilePath("python-qgis-ltr.bat");
if (datPath == "")
{
qDebug() << "not find qgis env";
return;
}
const char* cmd = (datPath + " " + pyFilePath + " --in_holedem " + in_masked + "/" + " --work_space " + out_filled + "/" +
" --scale " + scale + " --win_size " + size).toStdString().c_str();
cout << cmd << "\n";
run_cmd(cmd);
//QString cmd = datPath + " " + pyFilePath
// + " --in_holedem " + in_masked + "/" + " --work_space " + out_filled + "/"
// + " --scale " + scale + " --win_size " + size;
//QProcess* pProces = new QProcess(this);
//connect(pProces, SIGNAL(readyReadStandardOutput()), this, SLOT(readProcessStandardOutput()));
//pProces->start(cmd);
//emit process(2);
}
void WorkThreadObject::readProcessStandardOutput()
{
mProcess = (QProcess*)sender();
QString output = QString::fromLocal8Bit(mProcess->readAllStandardOutput());
output.chop(2);
if (output.toFloat() != 0)
{
qDebug() << "process:" << output.toFloat();
emit process(output.toFloat());
}
else
qDebug() << "unsolved exe out:" << output;
}
int WorkThreadObject::run_cmd(const char* cmd)
{
char MsgBuff[1024];
int MsgLen = 1020;
FILE* fp;
if (cmd == NULL)
return -1;
if ((fp = _popen(cmd, "r")) == NULL)
return -2;
else
{
memset(MsgBuff, 0, MsgLen);
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ִ<EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
while (fgets(MsgBuff, MsgLen, fp) != NULL)
{
printf("MsgBuff: %s\n", MsgBuff);
QString qStr = QString(MsgBuff);
if (qStr.toDouble() != 0.0)
emit process(qStr.toDouble());
}
//<2F>ر<EFBFBD>ִ<EFBFBD>еĽ<D0B5><C4BD><EFBFBD>
if (_pclose(fp) == -1)
return -3;
}
return 0;
}
///////////////////////////////////////////////////////////
DataFilling::DataFilling()
{
//ui.setupUi(this);
}
QString DataFilling::PannelName()
{
return QString::fromLocal8Bit("");
}
QString DataFilling::CategoryName()
{
return QString::fromLocal8Bit("DEMģ<EFBFBD><EFBFBD>");
}
QString DataFilling::EnglishName()
{
return QString::fromLocal8Bit("DEM_Module");
}
QString DataFilling::ChineseName()
{
return QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
QString DataFilling::Information()
{
return "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
}
QString DataFilling::IconPath()
{
return ":/DEM_Fill/resources/fill.svg";
}
QWidget* DataFilling::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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
myWidget->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
myWidget->setWindowIcon(QIcon(":/DEM_Fill/resources/fill.svg"));
myWidget->setAttribute(Qt::WA_QuitOnClose, false);
myWidget->setAttribute(Qt::WA_DeleteOnClose);
connect(myWidget, &QDialog::destroyed, this, [=] {
qDebug() << "----DEM Fill 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();
}
}
myWidget->close();
myWidget = nullptr;
if (mWorkThread != nullptr)
{
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>
mWorkThread = nullptr;//mWorkThread, &QThread::finished, mWorkThread, &QObject::deleteLater<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫdelete
mWorker = nullptr;//mWorkThread, &QThread::finished, mWorker, &QObject::deleteLater
}
});
connect(ui.pbtMasked, &QPushButton::pressed, this, &DataFilling::chooseMaskedSlope);
connect(ui.pbtOutFill, &QPushButton::pressed, this, &DataFilling::chooseOutFill);
connect(ui.pushButton_ok, &QPushButton::pressed, this, &DataFilling::readAndStart);
connect(ui.pushButton_cancel, &QPushButton::pressed, this, &DataFilling::pbCancel);
ui.progressBar->setTextVisible(true);
ui.progressBar->setRange(0, 100);
ui.pbtMasked->setFocus();
ui.lineRectangleScale->setValidator(new QRegExpValidator(QRegExp("^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$")));
ui.lineExpandSize->setValidator(new QIntValidator(0, 999, this));
ui.lineRectangleScale->setText("0.1");
ui.lineExpandSize->setText("3");
QFile qssFile(":/DEM_Fill/DEM_Fill.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 DataFilling::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::sendMaskedTiff, this, &DataFilling::openResultData);
connect(mWorker, &WorkThreadObject::process, myWidget, [=](int value) {
ui.progressBar->setValue(value);
if (value == 100)
{
QMessageBox mess(QMessageBox::Information,
QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"),
QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>:\n") + outPath);
mess.setWindowFlags(Qt::Drawer);
mess.setStandardButtons(QMessageBox::Yes);
mess.button(QMessageBox::StandardButton::Yes)->setText(QString::fromLocal8Bit("ȷ<EFBFBD><EFBFBD>"));
//mess.setTextInteractionFlags(Qt::TextSelectableByMouse);
int result = mess.exec();
pbCancel();
}
});
connect(this, &DataFilling::startDataFill, mWorker, &WorkThreadObject::runFillingWork);
mWorkThread->start();
}
void DataFilling::chooseMaskedSlope()
{
QString dirDom = QFileDialog::getExistingDirectory(ui.pbtMasked, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DEM<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>"), "");
if (dirDom != "")
ui.lineMasked->setText(dirDom);
}
void DataFilling::chooseOutFill()
{
QString dirDom = QFileDialog::getExistingDirectory(ui.pbtOutFill, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>"), "");
if (dirDom != "")
ui.lineOutFill->setText(dirDom);
}
void DataFilling::readAndStart()
{
QString in_masked = ui.lineMasked->text();
QString out_filled = ui.lineOutFill->text();
QString scale = ui.lineRectangleScale->text();
QString size = ui.lineExpandSize->text();
if (in_masked == "" || out_filled == "" || scale == "" || size == "")
{
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>"));
mess.setWindowFlags(Qt::Drawer);
int result = mess.exec();
return;
}
outPath = out_filled;
ui.progressBar->setValue(0);
if (mWorkThread == nullptr)
{
qDebug() << "--startThread";
startWorkThread();
}
emit startDataFill(in_masked, out_filled, scale, size);
}
void DataFilling::ReadConfigHistoryPaths(QString strPath)
{
QSettings configIni(strPath, QSettings::IniFormat);
//<2F>򿪱<EFBFBD><F2BFAAB1><EFBFBD>Ϊ<EFBFBD><CEAA>[DemFill] <20><><EFBFBD><EFBFBD>
configIni.beginGroup("DemFill");
ui.lineMasked->setText(configIni.value("MaskedFiles").toString());
ui.lineOutFill->setText(configIni.value("FilledFiles").toString());
configIni.endGroup();//<2F>ر<EFBFBD><D8B1><EFBFBD>
}
void DataFilling::WriteConfigPaths(QString strPath)
{
QSettings configIni(strPath, QSettings::IniFormat);
configIni.setIniCodec("utf-8");
//<2F>򿪱<EFBFBD><F2BFAAB1><EFBFBD>Ϊ<EFBFBD><CEAA>[DemFill] <20><><EFBFBD><EFBFBD>
configIni.beginGroup("DemFill");
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>·<EFBFBD><C2B7>
QString temp = ui.lineMasked->text();
if (temp != "")
configIni.setValue("MaskedFiles", temp);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DOM·<4D><C2B7>
temp = ui.lineOutFill->text();
if (temp != "")
configIni.setValue("FilledFiles", temp);
configIni.endGroup();//<2F>ر<EFBFBD><D8B1><EFBFBD>
}
void DataFilling::pbCancel()
{
delete myWidget;//<2F><><EFBFBD><EFBFBD>&QDialog::destroyed
}
void DataFilling::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);
}
}
}