GDAL通过RasterIO读写图像文件,数据集的创建,图像相关信息的获取

首先创建数据集

GDALDataset *poDataset; //创建整个图像数据集
GDALRasterBand *poRasterBand;  //创建单个波段数据集
GDALAllRegister(); //注册驱动
poDataset=(GDALDataset *)GDALOpen(  //打开文件
"e:\\124036zuhe.tif"         //文件路径
,GA_ReadOnly);              //打开方式  包括GA_ReadOnly 和  GA_Update
if(poDataset!=NULL)
{
double adfGeoTransform[6];
cout<<"RasterXSize:"<<poDataset->GetRasterXSize()<<endl;
cout<<"RasterYSize:"<<poDataset->GetRasterYSize()<<endl;
cout<<"RasterBand:"<<poDataset->GetRasterBand(1)<<endl;
cout<<"RasterCount:"<<poDataset->GetRasterCount()<<endl;
cout<<"Projetion information:"<<poDataset->GetProjectionRef()<<endl; 
cout<<"仿射:"<<poDataset->GetGeoTransform(adfGeoTransform)<<endl;
cout<<"GcpCount:"<<poDataset->GetGCPCount()<<endl;
cout<<"GcpProjection:"<<poDataset->GetGCPProjection()<<endl;
cout<<"Gcp:"<<poDataset->GetGCPs()<<endl;
poRasterBand=poDataset->GetRasterBand(1);
cout<<"BandXSize:"<<poRasterBand->GetXSize()<<endl;
cout<<"BandYSize:"<<poRasterBand->GetYSize()<<endl;
cout<<"BandDataType:"<<GDALGetDataTypeName(poRasterBand->GetRasterDataType())<<endl;
cout<<"ColorType:"<<GDALGetColorInterpretationName(poRasterBand->GetColorInterpretation())<<endl;
//获取相关信息
float  *buff=new float[1000*1000*3]; //创建读写缓冲区
int num[3]={1,2,3};  //读取的波段顺序
poDataset->RasterIO(GF_Read   //设置读写方式GF_Read和GF_Write
,3000,3000,                   //读取图像的起始位置X,Y
1000,1000,                    //要读取区域的大小,分别为X,Y方向
buff,                         //读入的缓冲区指针
1000,1000,                    //指定缓冲区大小
GDT_Float32,                  //读取图像的数据类型
3,                           //读取的波段数
num                          //读取的波段顺序指针
,0,0,0);                     //先不管
//读取图像到缓冲区
const char *pszFormat = "GTiff";
char *SavePath="D:\\3.tif";
        poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);  //获取指定驱动类型
        poDatasetCreate=poDriver->Create(                              //创建待写入文件的数据集
        SavePath,                                                      //文件路径
        1000,1000,                                                     //图像文件的大小
        3,                                                             //图像的波段数
        GDT_Float32                                                    //图像数据类型
        ,NULL);                                                        //图像储存方式
        
        double  GeoTransform[6]={ 444720, 30, 0, 3751320, 0, -30 };
poDatasetCreate->RasterIO(GF_Write,0,0,1000,1000,buff,
1000,1000,GDT_Float32,3,num,0,0,0);
//设置图像坐标

poDatasetCreate->RasterIO(                                     //缓冲区内容写入创建的数据集
GF_Write,                                                  //数据集打开方式
0,0,                                                       //写入的起始位置X,Y
1000,1000,                                                 //写入的数据大小X,Y
buff,                                                      //缓冲区指针
1000,1000,                                                 //缓冲区大小
GDT_Float32,                                               //图像数据类型
3,                                                         //波段数目
num,                                                       //波段顺序
0,0,0);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值