353 lines
12 KiB
C
353 lines
12 KiB
C
|
#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()
|
|||
|
|
|||
|
|
|||
|
struct ImageGEOINFO {
|
|||
|
int width;
|
|||
|
int height;
|
|||
|
int bandnum;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
enum GDALREADARRCOPYMETHOD {
|
|||
|
MEMCPYMETHOD, // 直接拷贝
|
|||
|
VARIABLEMETHOD // 变量赋值
|
|||
|
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
// 判断是否需要输出为DLL
|
|||
|
#define DLLOUT
|
|||
|
// 文件打开 // 当指令销毁时,调用GDALClose 销毁类型
|
|||
|
std::shared_ptr<GDALDataset> OpenDataset(const QString& in_path, GDALAccess rwmode= GA_ReadOnly);
|
|||
|
void CloseDataset(GDALDataset* ptr);
|
|||
|
|
|||
|
// 数据格式转换
|
|||
|
int TIFF2ENVI(QString in_tiff_path,QString out_envi_path);
|
|||
|
int ENVI2TIFF(QString in_envi_path,QString out_tiff_path);
|
|||
|
|
|||
|
// 保存影像数据 --直接保存 ENVI 文件
|
|||
|
|
|||
|
int CreateDataset(QString new_file_path, int height, int width, int band_num, double* gt, QString projection, GDALDataType gdal_dtype, bool need_gt); // 创建文件
|
|||
|
|
|||
|
int saveDataset(QString new_file_path, int start_line, int start_cols, int band_ids, int datacols, int datarows, void* databuffer);
|
|||
|
|
|||
|
// 根据限制条件估算分块大小
|
|||
|
int block_num_pre_memory(int width, int height, GDALDataType gdal_dtype,double memey_size);
|
|||
|
|
|||
|
// 将结果转换为复数 或者 实数
|
|||
|
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> ReadComplexMatrixData(int start_line,int width, int line_num, std::shared_ptr<GDALDataset> rasterDataset, GDALDataType gdal_datatype);
|
|||
|
|
|||
|
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> ReadMatrixDoubleData(int start_line, int width, int line_num, std::shared_ptr<GDALDataset> rasterDataset, GDALDataType gdal_datatype,int band_idx);
|
|||
|
|
|||
|
Eigen::MatrixXd getGeoTranslationArray(QString in_path);
|
|||
|
ImageGEOINFO getImageINFO(QString in_path);
|
|||
|
|
|||
|
GDALDataType getGDALDataType(QString fileptah);
|
|||
|
|
|||
|
|
|||
|
struct DemBox {
|
|||
|
double min_lat; //纬度
|
|||
|
double min_lon;//经度
|
|||
|
double max_lat;//纬度
|
|||
|
double max_lon;//经度
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// gdalImage图像操作类
|
|||
|
/// </summary>
|
|||
|
|
|||
|
class gdalImage
|
|||
|
{
|
|||
|
|
|||
|
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);
|
|||
|
virtual Eigen::MatrixXd getData(int start_row, int start_col, int rows_count, int cols_count, int band_ids);
|
|||
|
virtual Eigen::MatrixXd getGeoTranslation();
|
|||
|
virtual GDALDataType getDataType();
|
|||
|
virtual void saveImage(Eigen::MatrixXd, int start_row, int start_col, int band_ids);
|
|||
|
virtual void saveImage();
|
|||
|
virtual void setNoDataValue(double nodatavalue, 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);
|
|||
|
|
|||
|
virtual Eigen::MatrixXd getHist(int bandids);
|
|||
|
|
|||
|
|
|||
|
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 gdalImageComplex:public gdalImage
|
|||
|
{
|
|||
|
|
|||
|
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);
|
|||
|
Eigen::MatrixXcd getDataComplex(int start_row, int start_col, int rows_count, int cols_count, int band_ids);
|
|||
|
|
|||
|
void saveImage() override;
|
|||
|
void savePreViewImage();
|
|||
|
public:
|
|||
|
Eigen::MatrixXcd data;
|
|||
|
};
|
|||
|
|
|||
|
// 创建影像
|
|||
|
gdalImage CreategdalImage(const QString& img_path, int height, int width, int band_num, Eigen::MatrixXd gt, QString projection, bool need_gt = true, bool overwrite = false);
|
|||
|
|
|||
|
gdalImageComplex CreategdalImageComplex(const QString& img_path, int height, int width, int band_num, Eigen::MatrixXd gt, QString projection, bool need_gt = true, bool overwrite = false);
|
|||
|
|
|||
|
gdalImageComplex CreateEchoComplex(const QString& img_path, int height, int width, int band_num);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
int ResampleGDAL(const char* pszSrcFile, const char* pszOutFile, double* gt, int new_width, int new_height, GDALResampleAlg eResample);
|
|||
|
|
|||
|
int ResampleGDALs(const char* pszSrcFile, int band_ids, GDALRIOResampleAlg eResample = GRIORA_Bilinear);
|
|||
|
|
|||
|
int alignRaster(QString inputPath, QString referencePath, QString outputPath, GDALResampleAlg eResample);
|
|||
|
|
|||
|
//--------------------- 保存文博 -------------------------------
|
|||
|
|
|||
|
int saveMatrixXcd2TiFF(Eigen::MatrixXcd data, QString out_tiff_path);
|
|||
|
|
|||
|
//----------------------------------------------------
|
|||
|
|
|||
|
|
|||
|
template<typename T>
|
|||
|
std::shared_ptr<T> readDataArr(gdalImage &img,int start_row, int start_col, int rows_count, int cols_count, int band_ids, GDALREADARRCOPYMETHOD method);
|
|||
|
|
|||
|
|
|||
|
#ifndef DLLOUT
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
#else
|
|||
|
//#define DllExport __declspec( dllexport )
|
|||
|
//double __declspec(dllexport) ProcessMGCMathX_MGC(int Xbetaidx, int Xbwidx, double XTao, double satH, char* sigma_path, char* output_path,
|
|||
|
// double p1_x, double p1_y, double p2_x, double p2_y, double p3_x, double p3_y, double p4_x, double p4_y)
|
|||
|
|
|||
|
#endif
|
|||
|
|
|||
|
#endif
|
|||
|
|
|||
|
template<typename T>
|
|||
|
std::shared_ptr<T> readDataArr(gdalImage& imgds, int start_row, int start_col, int rows_count, int cols_count, int band_ids, GDALREADARRCOPYMETHOD method)
|
|||
|
{
|
|||
|
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;
|
|||
|
|
|||
|
Eigen::MatrixXd datamatrix(rows_count, cols_count);
|
|||
|
|
|||
|
if (gdal_datatype == GDT_Byte) {
|
|||
|
char* temp = new char[rows_count * cols_count];
|
|||
|
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
|
|||
|
result=std::shared_ptr<T>(new T[rows_count * cols_count], delArrPtr);
|
|||
|
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
|
|||
|
std::memcpy(result.get(), temp, rows_count * cols_count);
|
|||
|
}
|
|||
|
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
|
|||
|
long count = rows_count * cols_count;
|
|||
|
for (long i = 0; i < count; i++) {
|
|||
|
result.get()[i] = T(temp[i]);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
delete[] temp;
|
|||
|
}
|
|||
|
else if (gdal_datatype == GDT_UInt16) {
|
|||
|
unsigned short* temp = new unsigned short[rows_count * cols_count];
|
|||
|
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
|
|||
|
result = std::shared_ptr<T>(new T[rows_count * cols_count], delArrPtr);
|
|||
|
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
|
|||
|
std::memcpy(result.get(), temp, rows_count * cols_count);
|
|||
|
}
|
|||
|
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
|
|||
|
long count = rows_count * cols_count;
|
|||
|
for (long i = 0; i < count; i++) {
|
|||
|
result.get()[i] = T(temp[i]);
|
|||
|
}
|
|||
|
}
|
|||
|
delete[] temp;
|
|||
|
}
|
|||
|
else if (gdal_datatype == GDT_Int16) {
|
|||
|
short* temp = new short[rows_count * cols_count];
|
|||
|
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
|
|||
|
result = std::shared_ptr<T>(new T[rows_count * cols_count], delArrPtr);
|
|||
|
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
|
|||
|
std::memcpy(result.get(), temp, rows_count * cols_count);
|
|||
|
}
|
|||
|
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
|
|||
|
long count = rows_count * cols_count;
|
|||
|
for (long i = 0; i < count; i++) {
|
|||
|
result.get()[i] = T(temp[i]);
|
|||
|
}
|
|||
|
}
|
|||
|
delete[] temp;
|
|||
|
}
|
|||
|
else if (gdal_datatype == GDT_UInt32) {
|
|||
|
unsigned int* temp = new unsigned int[rows_count * cols_count];
|
|||
|
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
|
|||
|
result = std::shared_ptr<T>(new T[rows_count * cols_count], delArrPtr);
|
|||
|
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
|
|||
|
std::memcpy(result.get(), temp, rows_count * cols_count);
|
|||
|
}
|
|||
|
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
|
|||
|
long count = rows_count * cols_count;
|
|||
|
for (long i = 0; i < count; i++) {
|
|||
|
result.get()[i] = T(temp[i]);
|
|||
|
}
|
|||
|
}
|
|||
|
delete[] temp;
|
|||
|
}
|
|||
|
else if (gdal_datatype == GDT_Int32) {
|
|||
|
int* temp = new int[rows_count * cols_count];
|
|||
|
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
|
|||
|
result = std::shared_ptr<T>(new T[rows_count * cols_count], delArrPtr);
|
|||
|
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
|
|||
|
std::memcpy(result.get(), temp, rows_count * cols_count);
|
|||
|
}
|
|||
|
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
|
|||
|
long count = rows_count * cols_count;
|
|||
|
for (long i = 0; i < count; i++) {
|
|||
|
result.get()[i] = T(temp[i]);
|
|||
|
}
|
|||
|
}
|
|||
|
delete[] temp;
|
|||
|
}
|
|||
|
// else if (gdal_datatype == GDT_UInt64) {
|
|||
|
// unsigned long* temp = new unsigned long[rows_count * cols_count];
|
|||
|
// 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) {
|
|||
|
// long* temp = new long[rows_count * cols_count];
|
|||
|
// 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) {
|
|||
|
float* temp = new float[rows_count * cols_count];
|
|||
|
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
|
|||
|
|
|||
|
result = std::shared_ptr<T>(new T[rows_count * cols_count], delArrPtr);
|
|||
|
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
|
|||
|
std::memcpy(result.get(), temp, rows_count * cols_count);
|
|||
|
}
|
|||
|
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
|
|||
|
long count = rows_count * cols_count;
|
|||
|
for (long i = 0; i < count; i++) {
|
|||
|
result.get()[i] = T(temp[i]);
|
|||
|
}
|
|||
|
}
|
|||
|
delete[] temp;
|
|||
|
}
|
|||
|
else if (gdal_datatype == GDT_Float64) {
|
|||
|
double* temp = new double[rows_count * cols_count];
|
|||
|
demBand->RasterIO(GF_Read, start_col, start_row, cols_count, rows_count, temp, cols_count, rows_count, gdal_datatype, 0, 0);
|
|||
|
|
|||
|
result = std::shared_ptr<T>(new T[rows_count * cols_count], delArrPtr);
|
|||
|
if (method == GDALREADARRCOPYMETHOD::MEMCPYMETHOD) {
|
|||
|
std::memcpy(result.get(), temp, rows_count * cols_count);
|
|||
|
}
|
|||
|
else if (method == GDALREADARRCOPYMETHOD::VARIABLEMETHOD) {
|
|||
|
long count = rows_count * cols_count;
|
|||
|
for (long i = 0; i < count; i++) {
|
|||
|
result.get()[i] = T(temp[i]);
|
|||
|
}
|
|||
|
}
|
|||
|
delete[] temp;
|
|||
|
}
|
|||
|
else {
|
|||
|
}
|
|||
|
GDALClose((GDALDatasetH)rasterDataset);
|
|||
|
omp_unset_lock(&lock); // 锟酵放伙拷斤拷
|
|||
|
omp_destroy_lock(&lock); // 劫伙拷斤拷
|
|||
|
// GDALDestroy(); // or, DllMain at DLL_PROCESS_DETACH
|
|||
|
return result;
|
|||
|
}
|
|||
|
|