rasterio安装,Rasterio
请教在Windows下怎么用C++编程读取TIFF格式文件
用 gdal库。
库参考和下载在这:
下面这个文件瞎写的,你可以修改下。。需要加装库,头文件和lib
//tif.h
enum
{
TOPX?=?0,
CELLX,
XXXX,
TOPY,
XXXXX,
CELLY,
GeoTfInfoArrCount
};
?templatetypename?type,?int?nBand?=1
?class?CTifFileOp
?{
?public:
?CTifFileOp(string?FilePath=string(""));
virtual?~CTifFileOp();
double?get_nodata_value()
{
return?m_pDataset-GetRasterBand(nBand)-GetNoDataValue();
}
int?GetGeoTransform(double*?gt)const;
int?ChangeFile(string?strPath);
void?ReadTiffDataset(type?*?DataGet,?int?begRow?=?0,?int?begCol?=?0,?int?numRows?=?1,?int?numCols?=?1)const;
void?WriteTiffDataset(type?*?DataGet,?int?begRow?=?0,?int?begCol?=?0,?int?numRows?=?1,?int?numCols?=?1);
void?ReadTiffDataset(vectortype??DataGet,?int?begRow?=?0,?int?begCol?=?0,?int?numRows?=?1,?int?numCols?=?1)const
{
DataGet.clear();
DataGet.resize(numCols*numRows);
ReadTiffDataset(*DataGet.begin(),?begRow,?begCol,?numRows,?numCols);
}
size_t?GetColNum()
{
openDs();
return?m_pDataset-GetRasterXSize();
}
size_t?GetRowNum()
{
openDs();
return?m_pDataset-GetRasterYSize();
}
//获取波段数
int?GetRasterCount()
{
openDs();
return?m_pDataset-GetRasterCount();
}
//获取坐标系信息
const?char?*?GetProjectionRef()
{
openDs();
return?m_pDataset-GetProjectionRef();
}
//获取数据类型
GDALDataType?GetDataType(int?iRaster?=?nBand)
{
openDs();
return?m_pDataset-GetRasterBand(iRaster)-GetRasterDataType();
}
void?GetAllData(vectortype??DataGet)
{
//? ?double?geoInfo[GeoTfInfoArrCount]?=?{?0?};
//? ?GetGeoTransform(geoInfo);
openDs();
ReadTiffDataset(DataGet,?0,?0,?GetRowNum(),?GetColNum());
}
?private:
?string?m_strFile;
?GDALDataset?*m_pDataset?=?nullptr;
?bool?m_bWrite?=?false;
?private:
?void?openDs(GDALAccess?openMode?=?GA_ReadOnly);
?bool?CanWrite()const
?{
?return?m_bWrite;
?}
};
?
?templatetypename?type,?int?nBand
?int?CTifFileOptype,?nBand::GetGeoTransform(double*?gt)?const
?{
//?openDs();
?m_pDataset-GetGeoTransform(gt);
?return?0;
?}
?
?templatetypename?type,?int?nBand
?CTifFileOptype,?nBand::CTifFileOp(string?FilePath)
?:m_strFile(FilePath),?m_pDataset(nullptr)
?{
openDs();
?}
?templatetypename?type,?int?nBand
?CTifFileOptype,?nBand::~CTifFileOp()
?{
if?(m_pDataset)
{
GDALClose(m_pDataset);
m_pDataset?=?nullptr;
//是这样关闭?数据集?么?
}
?}
?templatetypename?type,?int?nBand
?void?CTifFileOptype,?nBand::openDs(GDALAccess?openMode)
{
?if?(m_pDataset)//当不为nullptr,认为是打开的
?{
?return;
?}
GDALAllRegister();
const?char?*pszFormat?=?"GTiff";
GDALDriver?*poDriver;
poDriver?=?GetGDALDriverManager()-GetDriverByName(pszFormat);
if?(poDriver?==?nullptr){
return;
}
if?(!m_strFile.empty())
{
m_pDataset?=?((GDALDataset?*)GDALOpen(m_strFile.c_str(),?openMode));
//? if?(m_pDataset?!=?NULL)
//? {
//? int?ibandCounts?=?m_pDataset-GetRasterCount();
//? ibandCounts?|=?(long(1)0);
//? }
if?(GA_Update?==?openMode)
{
m_bWrite?=?true;
}
else
m_bWrite?=?false;
}
// OGRCleanupAll();
}
templatetypename?type,?int?nBand
int?CTifFileOptype,?nBand::ChangeFile(string?strPath)
{
m_strFile?=?strPath;
if?(m_pDataset)
{
GDALClose(m_pDataset);
m_pDataset?=?nullptr;
//是这样关闭?数据集?么?
}
openDs();
return?0;
}
templatetypename?type,?int?nBand
void?CTifFileOptype,?nBand::ReadTiffDataset(type?*?DataGet,?int?begRow,?int?begCol,?int?numRows,?int?numCols)const
{
if?(!DataGet?||?!m_pDataset)
{
return;
}
if?(begRow??0?||?begCol??0?||?numRows?=?0?||?numCols?=?0)
{
return;
}
GDALDataType?datatype?=?m_pDataset-GetRasterBand(nBand)-GetRasterDataType();
//不知道下面读数据的这个类型参数有什么意义
m_pDataset-RasterIO(GF_Read,begCol,begRow,numCols,numRows,DataGet,numCols,numRows,datatype,1,0,0,0,0);
}
templatetypename?type,int?nBand
void?CTifFileOptype,?nBand::WriteTiffDataset(type?*?DataGet,?int?begRow,?int?begCol,?int?numRows,?int?numCols)
{
if?(!DataGet?||?!m_pDataset)
{
return;
}
if?(begRow??0?||?begCol??0?||?numRows?=?0?||?numCols?=?0)
{
return;
}
if?(!CanWrite())
{
GDALClose(m_pDataset);
m_pDataset?=?nullptr;
openDs(GA_Update);
}
GDALDataType?datatype?=?m_pDataset-GetRasterBand(nBand)-GetRasterDataType();
//不知道下面读数据的这个类型参数有什么意义
m_pDataset-RasterIO(GF_Write,?begCol,?begRow,?numCols,?numRows,?DataGet,?numCols,?numRows,?datatype,?1,?0,?0,?0,?0);
}