1、为图层添加渲染器和读取器,分离图层基础信息、渲染和读写;
2、LayerTreeLayerNode持有MapLayer图层引用,为右键菜单做准备; 3、改名:imageviewer → mapcavas,mapcavas持有MapLayer图层引用,为刷新图像做准备;
This commit is contained in:
123
HPPA/RasterDataProvider.cpp
Normal file
123
HPPA/RasterDataProvider.cpp
Normal file
@ -0,0 +1,123 @@
|
||||
#include "RasterDataProvider.h"
|
||||
#include <QString>
|
||||
#include <QDebug>
|
||||
|
||||
#if HPPA_HAVE_GDAL
|
||||
#include <gdal_priv.h>
|
||||
#include <cpl_conv.h>
|
||||
#endif
|
||||
|
||||
RasterDataProvider::RasterDataProvider(const QString& uri)
|
||||
: m_uri(uri), m_dataset(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
RasterDataProvider::~RasterDataProvider()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
bool RasterDataProvider::open()
|
||||
{
|
||||
#if HPPA_HAVE_GDAL
|
||||
GDALAllRegister();
|
||||
m_dataset = (GDALDataset*)GDALOpen((const char*)m_uri.toLocal8Bit().constData(), GA_ReadOnly);
|
||||
if (!m_dataset) {
|
||||
qWarning() << "RasterDataProvider: failed to open dataset:" << m_uri;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
#else
|
||||
Q_UNUSED(m_uri);
|
||||
qWarning() << "RasterDataProvider: GDAL not available, open will fail.";
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void RasterDataProvider::close()
|
||||
{
|
||||
#if HPPA_HAVE_GDAL
|
||||
if (m_dataset) {
|
||||
GDALClose((GDALDatasetH)m_dataset);
|
||||
m_dataset = nullptr;
|
||||
}
|
||||
#else
|
||||
m_dataset = nullptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
int RasterDataProvider::bandCount() const
|
||||
{
|
||||
#if HPPA_HAVE_GDAL
|
||||
if (!m_dataset) return 0;
|
||||
return m_dataset->GetRasterCount();
|
||||
#else
|
||||
Q_UNUSED(this);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
int RasterDataProvider::width() const
|
||||
{
|
||||
#if HPPA_HAVE_GDAL
|
||||
if (!m_dataset) return 0;
|
||||
return m_dataset->GetRasterXSize();
|
||||
#else
|
||||
Q_UNUSED(this);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
int RasterDataProvider::height() const
|
||||
{
|
||||
#if HPPA_HAVE_GDAL
|
||||
if (!m_dataset) return 0;
|
||||
return m_dataset->GetRasterYSize();
|
||||
#else
|
||||
Q_UNUSED(this);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::vector<double> RasterDataProvider::bandWavelengths() const
|
||||
{
|
||||
std::vector<double> res;
|
||||
#if HPPA_HAVE_GDAL
|
||||
if (!m_dataset) return res;
|
||||
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);
|
||||
} else {
|
||||
// push a negative to indicate unknown
|
||||
res.push_back(-1.0);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return res;
|
||||
}
|
||||
|
||||
bool RasterDataProvider::readBandAsFloat(int bandIndex, std::vector<float>& outBuffer) const
|
||||
{
|
||||
#if HPPA_HAVE_GDAL
|
||||
if (!m_dataset) return false;
|
||||
int bands = m_dataset->GetRasterCount();
|
||||
if (bandIndex < 0 || bandIndex >= bands) return false;
|
||||
GDALRasterBand* band = m_dataset->GetRasterBand(bandIndex + 1);
|
||||
if (!band) return false;
|
||||
int w = m_dataset->GetRasterXSize();
|
||||
int h = m_dataset->GetRasterYSize();
|
||||
outBuffer.assign(w * h, 0.0f);
|
||||
CPLErr err = band->RasterIO(GF_Read, 0, 0, w, h, outBuffer.data(), w, h, GDT_Float32, 0, 0);
|
||||
return err == CE_None;
|
||||
#else
|
||||
Q_UNUSED(bandIndex);
|
||||
Q_UNUSED(outBuffer);
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user