增加重采样参考影像工具

pull/4/head^2
陈增辉 2025-02-21 10:53:06 +08:00
parent 368e240c4d
commit 7650ba6a67
10 changed files with 439 additions and 4 deletions

View File

@ -141,6 +141,9 @@
<ProjectReference Include="..\ImageshowTool\ImageshowTool.vcxproj">
<Project>{8c8ca066-a93a-4098-9a46-b855efeaadf2}</Project>
</ProjectReference>
<ProjectReference Include="..\LAMPDataProcessEXE\LAMPDataProcessEXE.vcxproj">
<Project>{4e6e79a3-048c-4fb4-bbb0-43c518a3e6d4}</Project>
</ProjectReference>
<ProjectReference Include="..\RasterProcessToolWidget\RasterProcessTool.vcxproj">
<Project>{7ef67daa-dbc0-4b7f-80e8-11b4d2cb7ec2}</Project>
</ProjectReference>

View File

@ -3627,3 +3627,108 @@ bool ConvertCoordinateSystem(QString inRasterPath, QString outRasterPath, long o
return true;
}
void ResampleByReferenceRasterB(QString rasterAPath, QString rasterBPath, QString rasterCPath) {
GDALAllRegister(); // 初始化GDAL驱动
// 阶段1读取参考栅格B的元数据
GDALDatasetH hRefDS = GDALOpen(rasterBPath.toUtf8().constData(), GA_ReadOnly);
if (!hRefDS) {
qDebug() << u8"参考栅格打开失败:" << rasterBPath;
return;
}
// 获取地理变换参数和尺寸
double geotransform[6];
int refWidth = GDALGetRasterXSize(hRefDS);
int refHeight = GDALGetRasterYSize(hRefDS);
if (GDALGetGeoTransform(hRefDS, geotransform) != CE_None) {
GDALClose(hRefDS);
qDebug() << u8"参考栅格缺少地理变换参数:" << rasterBPath;
return;
}
// 计算目标地理范围
const double xmin = geotransform[0];
const double ymax = geotransform[3];
const double xmax = xmin + geotransform[1] * refWidth;
const double ymin = ymax + geotransform[5] * refHeight;
// 获取目标坐标系
const char* projRef = GDALGetProjectionRef(hRefDS);
OGRSpatialReference targetSRS;
if (projRef[0] == '\0' || targetSRS.importFromWkt(projRef) != OGRERR_NONE) {
GDALClose(hRefDS);
qDebug() << u8"参考栅格坐标系无效:" << rasterBPath;
return;
}
GDALClose(hRefDS); // 尽早释放参考数据集
// 阶段2配置Warp参数
char** warpOptions = nullptr;
warpOptions = CSLAddString(warpOptions, "-t_srs"); // 目标坐标系
char* targetSRSWkt = nullptr;
targetSRS.exportToWkt(&targetSRSWkt);
warpOptions = CSLAddString(warpOptions, targetSRSWkt);
warpOptions = CSLAddString(warpOptions, "-te"); // 目标范围
warpOptions = CSLAddString(warpOptions, QString::number(xmin, 'f', 10).toUtf8().constData());
warpOptions = CSLAddString(warpOptions, QString::number(ymin, 'f', 10).toUtf8().constData());
warpOptions = CSLAddString(warpOptions, QString::number(xmax, 'f', 10).toUtf8().constData());
warpOptions = CSLAddString(warpOptions, QString::number(ymax, 'f', 10).toUtf8().constData());
warpOptions = CSLAddString(warpOptions, "-ts"); // 目标尺寸
warpOptions = CSLAddString(warpOptions, QString::number(refWidth).toUtf8().constData());
warpOptions = CSLAddString(warpOptions, QString::number(refHeight).toUtf8().constData());
warpOptions = CSLAddString(warpOptions, "-r"); // 双线性重采样
warpOptions = CSLAddString(warpOptions, "bilinear");
warpOptions = CSLAddString(warpOptions, "-of"); // 输出为GTiff
warpOptions = CSLAddString(warpOptions, "GTiff");
warpOptions = CSLAddString(warpOptions, "-wo"); // 启用多线程
warpOptions = CSLAddString(warpOptions, "NUM_THREADS=ALL_CPUS");
warpOptions = CSLAddString(warpOptions, "-co"); // 压缩优化
warpOptions = CSLAddString(warpOptions, "COMPRESS=LZW");
// 阶段3执行重采样操作
GDALWarpAppOptions* warpAppOptions = GDALWarpAppOptionsNew(warpOptions, nullptr);
GDALDatasetH hSrcDS = GDALOpen(rasterAPath.toUtf8().constData(), GA_ReadOnly);
if (!hSrcDS) {
GDALWarpAppOptionsFree(warpAppOptions);
CSLDestroy(warpOptions);
CPLFree(targetSRSWkt);
qDebug() << u8"源栅格打开失败:" << rasterAPath;
return;
}
int bUsageError = FALSE;
GDALDatasetH hDstDS = GDALWarp(
rasterCPath.toUtf8().constData(), // 输出路径
nullptr, // 自动选择驱动
1, &hSrcDS, // 输入数据集
warpAppOptions, // 转换参数
&bUsageError // 错误状态
);
// 阶段4资源清理与状态检查
GDALClose(hSrcDS);
if (hDstDS) GDALClose(hDstDS);
GDALWarpAppOptionsFree(warpAppOptions);
CSLDestroy(warpOptions);
CPLFree(targetSRSWkt);
if (bUsageError || !hDstDS) {
qDebug() << u8"重采样过程发生错误";
return;
}
qDebug() << u8"重采样成功完成:" << rasterCPath;
}

View File

@ -109,9 +109,6 @@
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ALLRelease\ALLRelease.vcxproj">
<Project>{8a71d19d-9ac6-42e9-81ec-9e82af8075b8}</Project>
</ProjectReference>
<ProjectReference Include="..\BaseCommonLibrary\BaseCommonLibrary.vcxproj">
<Project>{872ecd6f-30e3-4a1b-b17c-15e87d373ff6}</Project>
</ProjectReference>

View File

@ -10,7 +10,7 @@
#include "QDEMResampleDialog.h"
#include "DEMLLA2XYZTool.h"
#include "QConvertCoordinateSystemDialog.h"
#include "QResampleRefrenceRaster.h"
GF3ImportDataToolButton::GF3ImportDataToolButton(QWidget* parent) :QToolAbstract(parent)
{
@ -161,3 +161,20 @@ void QConvertCoordinateSystemToolButton::excute()
QConvertCoordinateSystemDialog* dialog = new QConvertCoordinateSystemDialog();
dialog->show();
}
QResampleRefrenceRasterToolButton::QResampleRefrenceRasterToolButton(QWidget* parent)
{
this->toolPath = QVector<QString>(0);
this->toolPath.push_back(u8"基础处理");
this->toolname = QString(u8"栅格重采样(参考影像)");
}
QResampleRefrenceRasterToolButton::~QResampleRefrenceRasterToolButton()
{
}
void QResampleRefrenceRasterToolButton::excute()
{
QResampleRefrenceRaster* dialog = new QResampleRefrenceRaster;
dialog->show();
}

View File

@ -96,4 +96,17 @@ public slots:
};
class BASETOOLBOX_EXPORT QResampleRefrenceRasterToolButton : public QToolAbstract {
Q_OBJECT
public:
QResampleRefrenceRasterToolButton(QWidget* parent = nullptr);
~QResampleRefrenceRasterToolButton();
public slots:
virtual void excute() override;
};
extern "C" BASETOOLBOX_EXPORT void RegisterPreToolBox(LAMPMainWidget::RasterMainWidget* mainwindows, ToolBoxWidget* toolbox);

View File

@ -118,6 +118,7 @@
<ClCompile Include="BaseToolbox\QMergeRasterProcessDialog.cpp" />
<ClCompile Include="BaseToolbox\QOrthSlrRaster.cpp" />
<ClCompile Include="BaseToolbox\QRDOrthProcessClass.cpp" />
<ClCompile Include="BaseToolbox\QResampleRefrenceRaster.cpp" />
<ClCompile Include="BaseToolbox\SatelliteGF3xmlParser.cpp" />
<ClCompile Include="BaseToolbox\SateOrbit.cpp" />
<ClCompile Include="BaseToolbox\simptsn.cpp" />
@ -133,6 +134,7 @@
<QtMoc Include="BaseToolbox\QMergeRasterProcessDialog.h" />
<QtMoc Include="BaseToolbox\QDEMResampleDialog.h" />
<QtMoc Include="BaseToolbox\QConvertCoordinateSystemDialog.h" />
<QtMoc Include="BaseToolbox\QResampleRefrenceRaster.h" />
<ClInclude Include="BaseToolbox\SatelliteGF3xmlParser.h" />
<ClInclude Include="BaseToolbox\SateOrbit.h" />
<ClInclude Include="BaseToolbox\simptsn.h" />
@ -151,6 +153,7 @@
<QtUic Include="BaseToolbox\QMergeRasterProcessDialog.ui" />
<QtUic Include="BaseToolbox\QOrthSlrRaster.ui" />
<QtUic Include="BaseToolbox\QRDOrthProcessClass.ui" />
<QtUic Include="BaseToolbox\QResampleRefrenceRaster.ui" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\BaseCommonLibrary\BaseCommonLibrary.vcxproj">

View File

@ -97,6 +97,9 @@
<ClCompile Include="BaseToolbox\QConvertCoordinateSystemDialog.cpp">
<Filter>BaseToolbox</Filter>
</ClCompile>
<ClCompile Include="BaseToolbox\QResampleRefrenceRaster.cpp">
<Filter>BaseToolbox</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<QtMoc Include="BaseToolbox\DEMLLA2XYZTool.h">
@ -129,6 +132,9 @@
<QtMoc Include="BaseToolbox\QConvertCoordinateSystemDialog.h">
<Filter>BaseToolbox</Filter>
</QtMoc>
<QtMoc Include="BaseToolbox\QResampleRefrenceRaster.h">
<Filter>BaseToolbox</Filter>
</QtMoc>
</ItemGroup>
<ItemGroup>
<QtUic Include="BaseToolbox\DEMLLA2XYZTool.ui">
@ -158,5 +164,8 @@
<QtUic Include="BaseToolbox\QConvertCoordinateSystemDialog.ui">
<Filter>BaseToolbox</Filter>
</QtUic>
<QtUic Include="BaseToolbox\QResampleRefrenceRaster.ui">
<Filter>BaseToolbox</Filter>
</QtUic>
</ItemGroup>
</Project>

View File

@ -0,0 +1,97 @@
#include "QResampleRefrenceRaster.h"
#include "ui_QResampleRefrenceRaster.h"
#include <QMessageBox>
#include <QFileDialog>
QResampleRefrenceRaster::QResampleRefrenceRaster(QWidget *parent)
: QDialog(parent),ui(new Ui::QResampleRefrenceRasterClass)
{
ui->setupUi(this);
connect(ui->dialogBtn, SIGNAL(accepted()), this, SLOT(ondialogBtnaccepted()));
connect(ui->dialogBtn, SIGNAL(rejected()), this, SLOT(ondialogBtnrejected()));
connect(ui->BtnInRaster, SIGNAL(clicked(bool)), this, SLOT(onBtnInRasterClicked(bool)));
connect(ui->BtnOutRaster, SIGNAL(clicked(bool)), this, SLOT(onBtnOutRasterClicked(bool)));
connect(ui->BtnRefRaster, SIGNAL(clicked(bool)), this, SLOT(onBtnRefRasterClicked(bool)));
}
QResampleRefrenceRaster::~QResampleRefrenceRaster()
{}
void QResampleRefrenceRaster::onBtnInRasterClicked(bool)
{
QString fileName = QFileDialog::getOpenFileName(
this, // 父窗口
tr(u8"选择影像"), // 标题
QString(), // 默认路径
tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)") // 文件过滤器
);
// 如果用户选择了文件
if (!fileName.isEmpty()) {
this->ui->lineEditInRaster->setText(fileName);
}
else {
QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
}
}
void QResampleRefrenceRaster::onBtnOutRasterClicked(bool)
{
QString fileName = QFileDialog::getSaveFileName(
this, // 父窗口
tr(u8"保存影像"), // 标题
QString(), // 默认路径
tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)") // 文件过滤器
);
// 如果用户选择了文件
if (!fileName.isEmpty()) {
this->ui->lineEditOutRaster->setText(fileName);
}
else {
QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
}
}
void QResampleRefrenceRaster::onBtnRefRasterClicked(bool)
{
QString fileName = QFileDialog::getOpenFileName(
this, // 父窗口
tr(u8"选择参考影像"), // 标题
QString(), // 默认路径
tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)") // 文件过滤器
);
// 如果用户选择了文件
if (!fileName.isEmpty()) {
this->ui->lineEditRefRaster->setText(fileName);
}
else {
QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
}
}
void QResampleRefrenceRaster::ondialogBtnaccepted()
{
QString inRasterPath = this->ui->lineEditInRaster->text();
QString RefRasterPath = this->ui->lineEditRefRaster->text();
QString OutRasterPath = this->ui->lineEditOutRaster->text();
}
void QResampleRefrenceRaster::ondialogBtnrejected()
{
this->close();
}

View File

@ -0,0 +1,30 @@
#pragma once
#include <QDialog>
namespace Ui {
class QResampleRefrenceRasterClass;
}
class QResampleRefrenceRaster : public QDialog
{
Q_OBJECT
public:
QResampleRefrenceRaster(QWidget *parent = nullptr);
~QResampleRefrenceRaster();
private:
Ui::QResampleRefrenceRasterClass* ui;
public slots :
void onBtnInRasterClicked(bool);
void onBtnOutRasterClicked(bool);
void onBtnRefRasterClicked(bool);
void ondialogBtnaccepted();
void ondialogBtnrejected();
};

View File

@ -0,0 +1,161 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QResampleRefrenceRasterClass</class>
<widget class="QDialog" name="QResampleRefrenceRasterClass">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>400</height>
</rect>
</property>
<property name="windowTitle">
<string>QResampleRefrenceRaster</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="2">
<widget class="QPushButton" name="BtnInRaster">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>选择</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="BtnOutRaster">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>选择</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="lineEditOutRaster">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lineEditRefRaster">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="BtnRefRaster">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>选择</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEditInRaster">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>输入影像:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>参考影像:</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="SloperLabel">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>输入影像:</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>220</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="dialogBtn">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>