RasterProcessTool/BaseCommonLibrary/BaseTool/ImageOperatorBase.h

632 lines
24 KiB
C
Raw Normal View History

2024-12-24 02:50:25 +00:00
#pragma once
/**
*
* ENVI
* GDAL
* **/
#ifndef IMAGEOPERATORBASE_H
#define IMAGEOPERATORBASE_H
#include "BaseConstVariable.h"
#include "GeoOperator.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <complex>
#include <gdal.h>
#include <gdal_priv.h>
#include <gdalwarper.h>
#include <string>
#include <memory>
#include <QString>
#include <cpl_conv.h> // for CPLMalloc()
#include "LogInfoCls.h"
#include <QObject>
2025-02-20 07:30:58 +00:00
#include <QList>
2025-02-24 07:32:30 +00:00
#include <functional>
#include "ShowProessAbstract.h"
2024-12-24 02:50:25 +00:00
enum ProjectStripDelta {
Strip_6, // 6度带
Strip_3
};
enum CoordinateSystemType { // 坐标系类型
GeoCoordinateSystem,
ProjectCoordinateSystem,
UNKNOW
};
struct PointRaster { // 影像坐标点
double x;
double y;
double z;
};
struct PointXYZ {
double x, y, z;
};
struct PointGeo {
double lon, lat, ati;
};
struct PointImage {
double pixel_x, pixel_y;
};
struct ImageGEOINFO {
int width;
int height;
int bandnum;
};
enum GDALREADARRCOPYMETHOD {
MEMCPYMETHOD, // 直接拷贝
VARIABLEMETHOD // 变量赋值
};
2024-12-24 02:50:25 +00:00
/// 根据经纬度获取
/// EPSG代码根据经纬度返回对应投影坐标系统其中如果在中华人民共和国境内默认使用
/// CGCS2000坐标系统(EPSG 4502 ~ 4512 6度带,EPSG 4534 ~ 4554 3度带)其余地方使用WGS坐标系统
/// 投影方法 高斯克吕格(国内), 高斯克吕格
/// \param long 经度
/// \param lat 纬度
/// \return 对应投影坐标系统的 EPSG编码,-1 表示计算错误
2025-04-17 19:30:28 +00:00
long BASECONSTVARIABLEAPI getProjectEPSGCodeByLon_Lat(double lon, double lat, ProjectStripDelta stripState= ProjectStripDelta::Strip_6);
2024-12-24 02:50:25 +00:00
long BASECONSTVARIABLEAPI getProjectEPSGCodeByLon_Lat_inStrip3(double lon, double lat);
2024-12-24 02:50:25 +00:00
long BASECONSTVARIABLEAPI getProjectEPSGCodeByLon_Lat_inStrip6(double lon, double lat);
2024-12-24 02:50:25 +00:00
QString BASECONSTVARIABLEAPI GetProjectionNameFromEPSG(long epsgCode);
2024-12-24 02:50:25 +00:00
long BASECONSTVARIABLEAPI GetEPSGFromRasterFile(QString filepath);
2024-12-24 02:50:25 +00:00
std::shared_ptr<PointRaster> BASECONSTVARIABLEAPI GetCenterPointInRaster(QString filepath);
2024-12-24 02:50:25 +00:00
CoordinateSystemType BASECONSTVARIABLEAPI getCoordinateSystemTypeByEPSGCode(long EPSGCODE);
2024-12-24 02:50:25 +00:00
// 文件打开 // 当指令销毁时调用GDALClose 销毁类型
std::shared_ptr<GDALDataset> BASECONSTVARIABLEAPI OpenDataset(const QString& in_path, GDALAccess rwmode = GA_ReadOnly);
void BASECONSTVARIABLEAPI CloseDataset(GDALDataset* ptr);
2024-12-24 02:50:25 +00:00
// 数据格式转换
int BASECONSTVARIABLEAPI TIFF2ENVI(QString in_tiff_path, QString out_envi_path);
int BASECONSTVARIABLEAPI ENVI2TIFF(QString in_envi_path, QString out_tiff_path);
2024-12-24 02:50:25 +00:00
// 保存影像数据 --直接保存 ENVI 文件
int BASECONSTVARIABLEAPI CreateDataset(QString new_file_path, int height, int width, int band_num, double* gt, QString projection, GDALDataType gdal_dtype, bool need_gt); // 创建文件
2024-12-24 02:50:25 +00:00
int BASECONSTVARIABLEAPI saveDataset(QString new_file_path, int start_line, int start_cols, int band_ids, int datacols, int datarows, void* databuffer);
2024-12-24 02:50:25 +00:00
// 根据限制条件估算分块大小
int BASECONSTVARIABLEAPI block_num_pre_memory(int width, int height, GDALDataType gdal_dtype, double memey_size);
2024-12-24 02:50:25 +00:00
// 将结果转换为复数 或者 实数
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> BASECONSTVARIABLEAPI ReadComplexMatrixData(int start_line, int width, int line_num, std::shared_ptr<GDALDataset> rasterDataset, GDALDataType gdal_datatype);
2024-12-24 02:50:25 +00:00
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> BASECONSTVARIABLEAPI ReadMatrixDoubleData(int start_line, int width, int line_num, std::shared_ptr<GDALDataset> rasterDataset, GDALDataType gdal_datatype, int band_idx);
2024-12-24 02:50:25 +00:00
Eigen::MatrixXd BASECONSTVARIABLEAPI getGeoTranslationArray(QString in_path);
ImageGEOINFO BASECONSTVARIABLEAPI getImageINFO(QString in_path);
2024-12-24 02:50:25 +00:00
GDALDataType BASECONSTVARIABLEAPI getGDALDataType(QString fileptah);
2024-12-24 02:50:25 +00:00
struct RasterExtend {
2025-04-06 06:29:35 +00:00
double min_x;
double min_y;
double max_x;
double max_y;
2024-12-24 02:50:25 +00:00
};
2024-12-24 02:50:25 +00:00
/// <summary>
/// gdalImage图像操作类
/// </summary>
class BASECONSTVARIABLEAPI gdalImage
2024-12-24 02:50:25 +00:00
{
public: // 方法
gdalImage();
gdalImage(const QString& raster_path);
~gdalImage();
virtual void setHeight(int);
virtual void setWidth(int);
virtual void setTranslationMatrix(Eigen::MatrixXd gt);
virtual void setData(Eigen::MatrixXd, int data_band_ids = 1);
2024-12-24 02:50:25 +00:00
virtual Eigen::MatrixXd getData(int start_row, int start_col, int rows_count, int cols_count, int band_ids);
virtual Eigen::MatrixXf getDataf(int start_row, int start_col, int rows_count, int cols_count, int band_ids);
2024-12-24 02:50:25 +00:00
virtual Eigen::MatrixXi getDatai(int start_row, int start_col, int rows_count, int cols_count, int band_ids);
2025-01-20 07:49:54 +00:00
virtual ErrorCode getData(double* data, int start_row, int start_col, int rows_count, int cols_count, int band_ids);
virtual ErrorCode getData(long* data, int start_row, int start_col, int rows_count, int cols_count, int band_ids);
2024-12-24 02:50:25 +00:00
virtual Eigen::MatrixXd getGeoTranslation();
virtual GDALDataType getDataType();
virtual void saveImage(Eigen::MatrixXd, int start_row, int start_col, int band_ids);
virtual void saveImage(Eigen::MatrixXf, int start_row, int start_col, int band_ids);
2024-12-24 02:50:25 +00:00
virtual void saveImage(Eigen::MatrixXi, int start_row, int start_col, int band_ids);
virtual void saveImage(std::shared_ptr<double>, int start_row, int start_col,int rowcount,int colcount, int band_ids);
virtual void saveImage(std::shared_ptr<float>, int start_row, int start_col, int rowcount, int colcount, int band_ids);
virtual void saveImage(std::shared_ptr<int>, int start_row, int start_col, int rowcount, int colcount, int band_ids);
2024-12-24 02:50:25 +00:00
virtual void saveImage();
virtual void setNoDataValue(double nodatavalue, int band_ids);
virtual void setNoDataValuei(int nodatavalue, int band_ids);
virtual double getNoDataValue(int band_ids);
virtual int getNoDataValuei(int band_ids);
virtual int InitInv_gt();
virtual Landpoint getRow_Col(double lon, double lat);
virtual Landpoint getLandPoint(double i, double j, double ati);
virtual void getLandPoint(double i, double j, double ati, Landpoint& Lp);
virtual double mean(int bandids = 1);
double BandmaxValue(int bandids = 1);
double BandminValue(int bandids = 1);
virtual GDALRPCInfo getRPC();
virtual Eigen::MatrixXd getLandPoint(Eigen::MatrixXd points);
2024-12-24 02:50:25 +00:00
virtual Eigen::MatrixXd getHist(int bandids);
virtual RasterExtend getExtend();
2025-04-08 02:08:19 +00:00
2024-12-24 02:50:25 +00:00
public:
QString img_path; // 图像文件
int height; // 高
int width; // 宽
int band_num;// 波段数
int start_row;//
int start_col;//
int data_band_ids;
Eigen::MatrixXd gt; // 变换矩阵
Eigen::MatrixXd inv_gt; // 逆变换矩阵
Eigen::MatrixXd data;
QString projection;
};
/// <summary>
/// gdalImage图像操作类
/// </summary>
class BASECONSTVARIABLEAPI gdalImageComplex :public gdalImage
2024-12-24 02:50:25 +00:00
{
public: // 方法
gdalImageComplex(const QString& raster_path);
~gdalImageComplex();
void setData(Eigen::MatrixXcd);
void saveImage(Eigen::MatrixXcd data, int start_row, int start_col, int band_ids);
2025-02-26 01:45:43 +00:00
void saveImage(std::shared_ptr<std::complex<double>> data, long start_row, long start_col, long rowCount, long colCount, int band_ids);
2025-03-05 09:10:21 +00:00
void saveImage(std::complex<double>* data, long start_row, long start_col, long rowcount, long colcount, int banids);
2024-12-24 02:50:25 +00:00
Eigen::MatrixXcd getDataComplex(int start_row, int start_col, int rows_count, int cols_count, int band_ids);
2025-02-26 01:45:43 +00:00
std::shared_ptr<std::complex<double>> getDataComplexSharePtr(int start_row, int start_col, int rows_count, int cols_count, int band_ids);
void saveComplexImage();//override;
2024-12-24 02:50:25 +00:00
void savePreViewImage();
public:
Eigen::MatrixXcd data;
};
2025-04-08 02:08:19 +00:00
bool BASECONSTVARIABLEAPI CopyProjectTransformMatrixFromRasterAToRasterB(QString RasterAPath, QString RasterBPath);
2024-12-24 02:50:25 +00:00
// 创建影像
2025-03-05 09:10:21 +00:00
gdalImage BASECONSTVARIABLEAPI CreategdalImageDouble(QString& img_path, int height, int width, int band_num, bool overwrite = false, bool isEnvi = false);
2025-03-12 02:23:09 +00:00
gdalImage BASECONSTVARIABLEAPI CreategdalImageFloat(QString& img_path, int height, int width, int band_num, bool overwrite = false, bool isEnvi = false);
gdalImage BASECONSTVARIABLEAPI CreategdalImageDouble(const QString& img_path, int height, int width, int band_num, Eigen::MatrixXd gt, QString projection, bool need_gt = true, bool overwrite = false, bool isEnvi = false);
gdalImage BASECONSTVARIABLEAPI CreategdalImage(const QString& img_path, int height, int width, int band_num, Eigen::MatrixXd gt, QString projection,bool need_gt = true, bool overwrite = false, bool isEnvi = false, GDALDataType datetype = GDT_Float32);
gdalImage BASECONSTVARIABLEAPI CreategdalImage(const QString& img_path, int height, int width, int band_num, Eigen::MatrixXd gt, long espgcode, GDALDataType eType = GDT_Float32, bool need_gt = true, bool overwrite = false, bool isENVI = false);
2024-12-24 02:50:25 +00:00
gdalImageComplex BASECONSTVARIABLEAPI CreategdalImageComplex(const QString& img_path, int height, int width, int band_num, Eigen::MatrixXd gt, QString projection, bool need_gt = true, bool overwrite = false);
2025-02-26 01:45:43 +00:00
gdalImageComplex BASECONSTVARIABLEAPI CreategdalImageComplexNoProj(const QString& img_path, int height, int width, int band_num, bool overwrite = true);
2024-12-24 02:50:25 +00:00
gdalImageComplex BASECONSTVARIABLEAPI CreateEchoComplex(const QString& img_path, int height, int width, int band_num);
2024-12-24 02:50:25 +00:00
ErrorCode BASECONSTVARIABLEAPI DEM2XYZRasterAndSlopRaster(QString dempath, QString demxyzpath, QString demsloperPath);
2024-12-24 02:50:25 +00:00
int BASECONSTVARIABLEAPI ResampleGDAL(const char* pszSrcFile, const char* pszOutFile, double* gt, int new_width, int new_height, GDALResampleAlg eResample);
2024-12-24 02:50:25 +00:00
void BASECONSTVARIABLEAPI resampleRaster(const char* inputRaster, const char* outputRaster, double targetPixelSizeX, double targetPixelSizeY);
2024-12-24 02:50:25 +00:00
void BASECONSTVARIABLEAPI cropRasterByLatLon(const char* inputFile, const char* outputFile, double minLon, double maxLon, double minLat, double maxLat);
2024-12-24 02:50:25 +00:00
int BASECONSTVARIABLEAPI ResampleGDALs(const char* pszSrcFile, int band_ids, GDALRIOResampleAlg eResample = GRIORA_Bilinear);
2024-12-24 02:50:25 +00:00
void BASECONSTVARIABLEAPI transformRaster(const char* inputFile, const char* outputFile, int sourceEPSG, int targetEPSG);
2024-12-24 02:50:25 +00:00
ErrorCode BASECONSTVARIABLEAPI transformCoordinate(double x, double y, int sourceEPSG, int targetEPSG, Point2& p);
2024-12-24 02:50:25 +00:00
int BASECONSTVARIABLEAPI alignRaster(QString inputPath, QString referencePath, QString outputPath, GDALResampleAlg eResample);
2024-12-24 02:50:25 +00:00
2025-02-24 03:41:41 +00:00
void BASECONSTVARIABLEAPI ResampleByReferenceRasterB(QString pszSrcFile, QString RefrasterBPath, QString pszOutFile, GDALResampleAlg eResample);
2024-12-24 02:50:25 +00:00
//--------------------- 保存文博 -------------------------------
int BASECONSTVARIABLEAPI saveMatrixXcd2TiFF(Eigen::MatrixXcd data, QString out_tiff_path);
2024-12-24 02:50:25 +00:00
//----------------------------------------------------
void BASECONSTVARIABLEAPI clipRaster(QString inRasterPath, QString outRasterPath, long minRow, long maxRow, long minCol, long maxCol);
2025-02-20 08:46:54 +00:00
// 坐标系转换
2025-02-23 03:04:22 +00:00
void BASECONSTVARIABLEAPI ConvertCoordinateSystem(QString inRasterPath, QString outRasterPath, long outepsgcode);
2024-12-24 02:50:25 +00:00
//--------------------- 图像合并流程 ------------------------------
enum MERGEMODE
{
MERGE_GEOCODING,
};
2025-02-20 07:30:58 +00:00
void BASECONSTVARIABLEAPI MergeTiffs(QList<QString> inputFiles, QString outputFile);
ErrorCode BASECONSTVARIABLEAPI MergeRasterProcess(QVector<QString> filepath, QString outfileptah, QString mainString, MERGEMODE mergecode = MERGEMODE::MERGE_GEOCODING, bool isENVI = false, ShowProessAbstract* dia = nullptr);
2024-12-24 02:50:25 +00:00
ErrorCode BASECONSTVARIABLEAPI MergeRasterInGeoCoding(QVector<gdalImage> inimgs, gdalImage resultimg, gdalImage maskimg, ShowProessAbstract* dia = nullptr);
2024-12-24 02:50:25 +00:00
// 保存矩阵转换为envi文件默认数据格式为double
bool BASECONSTVARIABLEAPI saveEigenMatrixXd2Bin(Eigen::MatrixXd data, QString dataStrPath);
2024-12-24 02:50:25 +00:00
2024-12-28 08:08:44 +00:00
// 测试
void BASECONSTVARIABLEAPI testOutAntPatternTrans(QString antpatternfilename, double* antPatternArr, double starttheta, double deltetheta, double startphi, double deltaphi, long thetanum, long phinum);
void BASECONSTVARIABLEAPI testOutAmpArr(QString filename, float* amp, long rowcount, long colcount);
void BASECONSTVARIABLEAPI testOutAmpArr(QString filename, double* amp, long rowcount, long colcount);
2025-03-05 09:10:21 +00:00
void BASECONSTVARIABLEAPI testOutClsArr(QString filename, long* amp, long rowcount, long colcount);
void BASECONSTVARIABLEAPI testOutComplexDoubleArr(QString filename, std::complex<double>* data, long rowcount, long colcount);
void BASECONSTVARIABLEAPI testOutDataArr(QString filename, double* data, long rowcount, long colcount);
void BASECONSTVARIABLEAPI testOutDataArr(QString filename, float* data, long rowcount, long colcount);
void BASECONSTVARIABLEAPI testOutDataArr(QString filename, long* data, long rowcount, long colcount);
2024-12-28 08:08:44 +00:00
2025-02-24 07:32:30 +00:00
void BASECONSTVARIABLEAPI CreateSARIntensityByLookTable(QString IntensityRasterPath, QString LookTableRasterPath, QString SARIntensityPath, long min_rid, long max_rid, long min_cid, long max_cid, std::function<void(long, long)> processBarShow = {});
2024-12-24 02:50:25 +00:00
bool BASECONSTVARIABLEAPI ConvertVrtToEnvi(QString vrtPath, QString outPath);
void BASECONSTVARIABLEAPI MultiLookRaster(QString inRasterPath, QString outRasterPath, long looklineNumrow, long looklineNumCol);
ErrorCode BASECONSTVARIABLEAPI Complex2PhaseRaster(QString inComplexPath, QString outRasterPath);
ErrorCode BASECONSTVARIABLEAPI Complex2dBRaster(QString inComplexPath, QString outRasterPath);
ErrorCode BASECONSTVARIABLEAPI Complex2AmpRaster(QString inComplexPath, QString outRasterPath);
2025-04-21 01:56:53 +00:00
ErrorCode BASECONSTVARIABLEAPI amp2dBRaster(QString inPath, QString outRasterPath);
ErrorCode BASECONSTVARIABLEAPI ResampleDEM(QString indemPath, QString outdemPath, double gridx, double gridy);
2025-04-11 03:29:12 +00:00
void BASECONSTVARIABLEAPI CloseAllGDALRaster();
//--------------------- 图像文件读写 ------------------------------
2024-12-24 02:50:25 +00:00
template<typename T>
inline std::shared_ptr<T> readDataArr(gdalImage& imgds, long start_row, long start_col, long& rows_count, long& cols_count, int band_ids, GDALREADARRCOPYMETHOD method)
2024-12-24 02:50:25 +00:00
{
std::shared_ptr<T> result = nullptr;
omp_lock_t lock;
omp_init_lock(&lock);
omp_set_lock(&lock);
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
GDALDataset* rasterDataset = (GDALDataset*)(GDALOpen(imgds.img_path.toUtf8().constData(), GA_ReadOnly)); // 锟斤拷只斤拷式锟斤拷取斤拷影锟斤拷
GDALDataType gdal_datatype = rasterDataset->GetRasterBand(1)->GetRasterDataType();
GDALRasterBand* demBand = rasterDataset->GetRasterBand(band_ids);
rows_count = start_row + rows_count <= imgds.height ? rows_count : imgds.height - start_row;
cols_count = start_col + cols_count <= imgds.width ? cols_count : imgds.width - start_col;
2025-05-13 18:56:15 +00:00
int64_t pixel_count64 = static_cast<int64_t>(rows_count)* static_cast<int64_t>(cols_count);
2024-12-24 02:50:25 +00:00
//Eigen::MatrixXd datamatrix(rows_count, cols_count);
if (gdal_datatype == GDT_Byte) {
2025-05-13 18:56:15 +00:00
char* temp = new char[pixel_count64];
2024-12-24 02:50:25 +00:00
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
2025-05-13 18:56:15 +00:00
result = std::shared_ptr<T>(new T[pixel_count64], delArrPtr);
2024-12-24 02:50:25 +00:00
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
2025-05-13 18:56:15 +00:00
std::memcpy(result.get(), temp, pixel_count64);
2024-12-24 02:50:25 +00:00
}
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
2025-05-13 18:56:15 +00:00
long count = pixel_count64;
2024-12-24 02:50:25 +00:00
for (long i = 0; i < count; i++) {
result.get()[i] = T(temp[i]);
}
}
delete[] temp;
}
else if (gdal_datatype == GDT_UInt16) {
2025-05-13 18:56:15 +00:00
unsigned short* temp = new unsigned short[pixel_count64];
2024-12-24 02:50:25 +00:00
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
2025-05-13 18:56:15 +00:00
result = std::shared_ptr<T>(new T[pixel_count64], delArrPtr);
2024-12-24 02:50:25 +00:00
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
2025-05-13 18:56:15 +00:00
std::memcpy(result.get(), temp, pixel_count64);
2024-12-24 02:50:25 +00:00
}
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
2025-05-13 18:56:15 +00:00
long count = pixel_count64;
2024-12-24 02:50:25 +00:00
for (long i = 0; i < count; i++) {
result.get()[i] = T(temp[i]);
}
}
delete[] temp;
}
else if (gdal_datatype == GDT_Int16) {
2025-05-13 18:56:15 +00:00
short* temp = new short[pixel_count64];
2024-12-24 02:50:25 +00:00
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
2025-05-13 18:56:15 +00:00
result = std::shared_ptr<T>(new T[pixel_count64], delArrPtr);
2024-12-24 02:50:25 +00:00
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
2025-05-13 18:56:15 +00:00
std::memcpy(result.get(), temp, pixel_count64);
2024-12-24 02:50:25 +00:00
}
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
2025-05-13 18:56:15 +00:00
long count = pixel_count64;
2024-12-24 02:50:25 +00:00
for (long i = 0; i < count; i++) {
result.get()[i] = T(temp[i]);
}
}
delete[] temp;
}
else if (gdal_datatype == GDT_UInt32) {
2025-05-13 18:56:15 +00:00
unsigned int* temp = new unsigned int[pixel_count64];
2024-12-24 02:50:25 +00:00
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
2025-05-13 18:56:15 +00:00
result = std::shared_ptr<T>(new T[pixel_count64], delArrPtr);
2024-12-24 02:50:25 +00:00
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
2025-05-13 18:56:15 +00:00
std::memcpy(result.get(), temp, pixel_count64);
2024-12-24 02:50:25 +00:00
}
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
2025-05-13 18:56:15 +00:00
long count = pixel_count64;
2024-12-24 02:50:25 +00:00
for (long i = 0; i < count; i++) {
result.get()[i] = T(temp[i]);
}
}
delete[] temp;
}
else if (gdal_datatype == GDT_Int32) {
2025-05-13 18:56:15 +00:00
int* temp = new int[pixel_count64];
2024-12-24 02:50:25 +00:00
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
2025-05-13 18:56:15 +00:00
result = std::shared_ptr<T>(new T[pixel_count64], delArrPtr);
2024-12-24 02:50:25 +00:00
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
2025-05-13 18:56:15 +00:00
std::memcpy(result.get(), temp, pixel_count64);
2024-12-24 02:50:25 +00:00
}
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
2025-05-13 18:56:15 +00:00
long count = pixel_count64;
2024-12-24 02:50:25 +00:00
for (long i = 0; i < count; i++) {
result.get()[i] = T(temp[i]);
}
}
delete[] temp;
}
// else if (gdal_datatype == GDT_UInt64) {
2025-05-13 18:56:15 +00:00
// unsigned long* temp = new unsigned long[pixel_count64];
2024-12-24 02:50:25 +00:00
// demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count,
//rows_count, gdal_datatype, 0, 0); for (int i = 0; i < rows_count; i++) { for (int j = 0; j <
//cols_count; j++) { datamatrix(i, j) = temp[i * cols_count + j];
// }
// }
// delete[] temp;
// }
// else if (gdal_datatype == GDT_Int64) {
2025-05-13 18:56:15 +00:00
// long* temp = new long[pixel_count64];
2024-12-24 02:50:25 +00:00
// demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count,
//rows_count, gdal_datatype, 0, 0); for (int i = 0; i < rows_count; i++) { for (int j = 0; j <
//cols_count; j++) { datamatrix(i, j) = temp[i * cols_count + j];
// }
// }
// delete[] temp;
// }
else if (gdal_datatype == GDT_Float32) {
2025-05-13 18:56:15 +00:00
float* temp = new float[pixel_count64];
2024-12-24 02:50:25 +00:00
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
2025-05-13 18:56:15 +00:00
result = std::shared_ptr<T>(new T[pixel_count64], delArrPtr);
2024-12-24 02:50:25 +00:00
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
2025-05-13 18:56:15 +00:00
std::memcpy(result.get(), temp, pixel_count64);
2024-12-24 02:50:25 +00:00
}
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
2025-05-13 18:56:15 +00:00
long count = pixel_count64;
2024-12-24 02:50:25 +00:00
for (long i = 0; i < count; i++) {
result.get()[i] = T(temp[i]);
}
}
delete[] temp;
}
else if (gdal_datatype == GDT_Float64) {
2025-05-13 18:56:15 +00:00
double* temp = new double[pixel_count64];
2024-12-24 02:50:25 +00:00
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
2025-05-13 18:56:15 +00:00
result = std::shared_ptr<T>(new T[pixel_count64], delArrPtr);
2024-12-24 02:50:25 +00:00
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
2025-05-13 18:56:15 +00:00
std::memcpy(result.get(), temp, pixel_count64);
2024-12-24 02:50:25 +00:00
}
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
2025-05-13 18:56:15 +00:00
long count = pixel_count64;
2024-12-24 02:50:25 +00:00
for (long i = 0; i < count; i++) {
result.get()[i] = T(temp[i]);
}
}
delete[] temp;
}
//else if (gdal_datatype == GDT_CFloat32) {
// if (std::is_same<T, std::complex<double>>::value || std::is_same<T, std::complex<double>>::value) {
2025-05-13 18:56:15 +00:00
// float* temp = new float[pixel_count64 * 2];
2024-12-24 02:50:25 +00:00
// demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
2025-05-13 18:56:15 +00:00
// result = std::shared_ptr<T>(new T[pixel_count64], delArrPtr);
2024-12-24 02:50:25 +00:00
// if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
2025-05-13 18:56:15 +00:00
// std::memcpy(result.get(), temp, pixel_count64);
2024-12-24 02:50:25 +00:00
// }
// else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
2025-05-13 18:56:15 +00:00
// long count = pixel_count64;
2024-12-24 02:50:25 +00:00
// for (long i = 0; i < count; i++) {
// result.get()[i] = T(temp[i * 2],
// temp[i * 2 + 1]);
// }
// }
// delete[] temp;
// }
// else {
// result = nullptr;
// }
//}
//else if (gdal_datatype == GDT_CFloat64 ) {
// if (std::is_same<T, std::complex<double>>::value || std::is_same<T, std::complex<double>>::value) {
2025-05-13 18:56:15 +00:00
// double* temp = new double[pixel_count64 * 2];
2024-12-24 02:50:25 +00:00
// demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
2025-05-13 18:56:15 +00:00
// result = std::shared_ptr<T>(new T[pixel_count64], delArrPtr);
2024-12-24 02:50:25 +00:00
// if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
2025-05-13 18:56:15 +00:00
// std::memcpy(result.get(), temp, pixel_count64);
2024-12-24 02:50:25 +00:00
// }
// else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
2025-05-13 18:56:15 +00:00
// long count = pixel_count64;
2024-12-24 02:50:25 +00:00
// for (long i = 0; i < count; i++) {
// result.get()[i] = T(temp[i * 2],
// temp[i * 2 + 1]);
// }
// }
// delete[] temp;
// }
// else {
// result = nullptr;
// }
//}
else {
}
GDALClose((GDALDatasetH)rasterDataset);
omp_unset_lock(&lock); // 锟酵放伙拷斤拷
omp_destroy_lock(&lock); // 劫伙拷斤拷
2025-03-05 09:10:21 +00:00
GDALDestroy(); // or, DllMain at DLL_PROCESS_DETACH
2024-12-24 02:50:25 +00:00
return result;
2025-03-25 06:31:04 +00:00
};
2024-12-24 02:50:25 +00:00
template<typename T>
inline std::shared_ptr<T> readDataArrComplex(gdalImageComplex& imgds, long start_row, long start_col, long& rows_count, long& cols_count, int band_ids, GDALREADARRCOPYMETHOD method)
2024-12-24 02:50:25 +00:00
{
std::shared_ptr<T> result = nullptr;
omp_lock_t lock;
omp_init_lock(&lock);
omp_set_lock(&lock);
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
GDALDataset* rasterDataset = (GDALDataset*)(GDALOpen(imgds.img_path.toUtf8().constData(), GA_ReadOnly)); // 锟斤拷只斤拷式锟斤拷取斤拷影锟斤拷
GDALDataType gdal_datatype = rasterDataset->GetRasterBand(1)->GetRasterDataType();
GDALRasterBand* demBand = rasterDataset->GetRasterBand(band_ids);
rows_count = start_row + rows_count <= imgds.height ? rows_count : imgds.height - start_row;
cols_count = start_col + cols_count <= imgds.width ? cols_count : imgds.width - start_col;
2025-05-13 18:56:15 +00:00
int64_t pixel_count64 = static_cast<int64_t>(rows_count) * static_cast<int64_t>(cols_count);
2024-12-24 02:50:25 +00:00
//Eigen::MatrixXd datamatrix(rows_count, cols_count);
if (gdal_datatype == GDT_CFloat32) {
if (std::is_same<T, std::complex<double>>::value || std::is_same<T, std::complex<double>>::value) {
2025-05-13 18:56:15 +00:00
float* temp = new float[pixel_count64 * 2];
2024-12-24 02:50:25 +00:00
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
2025-05-13 18:56:15 +00:00
result = std::shared_ptr<T>(new T[pixel_count64], delArrPtr);
2024-12-24 02:50:25 +00:00
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
2025-05-13 18:56:15 +00:00
std::memcpy(result.get(), temp, pixel_count64);
2024-12-24 02:50:25 +00:00
}
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
2025-05-13 18:56:15 +00:00
long count = pixel_count64;
2024-12-24 02:50:25 +00:00
for (long i = 0; i < count; i++) {
result.get()[i] = T(temp[i * 2],
temp[i * 2 + 1]);
}
}
delete[] temp;
}
else {
result = nullptr;
}
}
else if (gdal_datatype == GDT_CFloat64) {
if (std::is_same<T, std::complex<double>>::value || std::is_same<T, std::complex<double>>::value) {
2025-05-13 18:56:15 +00:00
double* temp = new double[pixel_count64 * 2];
2024-12-24 02:50:25 +00:00
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
2025-05-13 18:56:15 +00:00
result = std::shared_ptr<T>(new T[pixel_count64], delArrPtr);
2024-12-24 02:50:25 +00:00
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
2025-05-13 18:56:15 +00:00
std::memcpy(result.get(), temp, pixel_count64);
2024-12-24 02:50:25 +00:00
}
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
2025-05-13 18:56:15 +00:00
long count = pixel_count64;
2024-12-24 02:50:25 +00:00
for (long i = 0; i < count; i++) {
result.get()[i] = T(temp[i * 2],
temp[i * 2 + 1]);
}
}
delete[] temp;
}
else {
result = nullptr;
}
}
else {
}
GDALClose((GDALDatasetH)rasterDataset);
omp_unset_lock(&lock); // 锟酵放伙拷斤拷
omp_destroy_lock(&lock); // 劫伙拷斤拷
2025-03-05 09:10:21 +00:00
GDALDestroy(); // or, DllMain at DLL_PROCESS_DETACH
2024-12-24 02:50:25 +00:00
return result;
2025-03-25 06:31:04 +00:00
};
2024-12-24 02:50:25 +00:00
2024-12-24 02:50:25 +00:00
//--------------------- 图像分块 ------------------------------
#endif