1、基于新框架实现点击显示光谱;
2、影像拉伸显示优化;
3、右键菜单新增:移除所有栅格图层;
This commit is contained in:
tangchao0503
2026-03-06 17:33:30 +08:00
parent 4ad5c8b91e
commit 09095592af
10 changed files with 310 additions and 104 deletions

View File

@ -1,6 +1,9 @@
#include "RasterDataProvider.h"
#include <QString>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QRegularExpression>
#if HPPA_HAVE_GDAL
#include <gdal_priv.h>
@ -79,27 +82,126 @@ int RasterDataProvider::height() const
#endif
}
bool RasterDataProvider::isValidPixel(int x, int y) const
{
const int w = width();
const int h = height();
return x >= 0 && y >= 0 && x < w && y < h;
}
std::vector<double> RasterDataProvider::parseEnviHdrWavelengths() const
{
std::vector<double> res;
QFileInfo fi(m_uri);
QString hdrPath = fi.path() + "/" + fi.completeBaseName() + ".hdr";
QFile hdr(hdrPath);
if (!hdr.open(QIODevice::ReadOnly | QIODevice::Text)) {
return res;
}
QString text = QString::fromLocal8Bit(hdr.readAll());
hdr.close();
QRegularExpression rx("wavelength\\s*=\\s*\\{([^}]*)\\}", QRegularExpression::CaseInsensitiveOption | QRegularExpression::DotMatchesEverythingOption);
QRegularExpressionMatch m = rx.match(text);
if (!m.hasMatch()) {
return res;
}
const QString body = m.captured(1);
const QStringList parts = body.split(',', QString::SkipEmptyParts);
res.reserve(parts.size());
for (const QString& p : parts) {
bool ok = false;
double v = p.trimmed().toDouble(&ok);
if (ok) {
res.push_back(v);
}
}
return res;
}
std::vector<double> RasterDataProvider::bandWavelengths() const
{
std::vector<double> res;
#if HPPA_HAVE_GDAL
if (!m_dataset) return res;
// 1) Try ENVI dataset-level metadata first: wavelength = { ... }
const char* dsWave = m_dataset->GetMetadataItem("wavelength", "ENVI");
if (!dsWave) dsWave = m_dataset->GetMetadataItem("Wavelength", "ENVI");
if (dsWave) {
QString dsWaveStr = QString::fromLocal8Bit(dsWave);
dsWaveStr.remove('{').remove('}');
const QStringList parts = dsWaveStr.split(',', QString::SkipEmptyParts);
res.reserve(parts.size());
for (const QString& p : parts) {
bool ok = false;
double v = p.trimmed().toDouble(&ok);
if (ok) res.push_back(v);
}
if (!res.empty()) return res;
}
// 2) Try per-band metadata
for (int i = 1; i <= m_dataset->GetRasterCount(); ++i) {
GDALRasterBand* band = m_dataset->GetRasterBand(i);
if (!band) continue;
// Attempt to read wavelength from metadata (commonly "Wavelength" or "wavelength")
const char* val = band->GetMetadataItem("Wavelength");
if (!val) val = band->GetMetadataItem("wavelength");
if (val) {
double v = atof(val);
res.push_back(v);
bool ok = false;
double v = QString::fromLocal8Bit(val).trimmed().toDouble(&ok);
res.push_back(ok ? v : -1.0);
} else {
// push a negative to indicate unknown
res.push_back(-1.0);
}
}
if (!res.empty()) return res;
#endif
// 3) Fallback: parse ENVI .hdr directly
return parseEnviHdrWavelengths();
}
bool RasterDataProvider::readPixelSpectrum(int x, int y, std::vector<double>& outSpectrum) const
{
#if HPPA_HAVE_GDAL
outSpectrum.clear();
if (!m_dataset) return false;
if (!isValidPixel(x, y)) return false;
const int bands = m_dataset->GetRasterCount();
if (bands <= 0) return false;
outSpectrum.resize(bands);
for (int i = 0; i < bands; ++i) {
GDALRasterBand* band = m_dataset->GetRasterBand(i + 1);
if (!band) return false;
float value = 0.0f;
CPLErr err = band->RasterIO(
GF_Read,
x, y,
1, 1,
&value,
1, 1,
GDT_Float32,
0, 0);
if (err != CE_None) return false;
outSpectrum[i] = static_cast<double>(value);
}
return true;
#else
Q_UNUSED(x);
Q_UNUSED(y);
Q_UNUSED(outSpectrum);
return false;
#endif
return res;
}
bool RasterDataProvider::readBandAsFloat(int bandIndex, std::vector<float>& outBuffer) const