GDAL读取栅格文件小例

前提是将机器上的gdal先配置好

 

C++东拉西扯弄出一份代码:

 

#include "stdafx.h" #include "include//gdal_priv.h" #include "include//cpl_string.h" #include "include//ogr_spatialref.h" #include "include//gdalwarper.h" #include "include//gdal_pam.h" #include "include//cpl_conv.h" #include "include//gdal.h" #include #include #include #include #include #include #include #include "windows.h" #include #include //istringstream #include //read & write file #include #include #include //use system command //#include #include using namespace std; #pragma comment(lib,"lib//gdal_i.lib") void getValueByLoc(string inputFile, double targetX, double targetY) { int i, xSize, ySize; //开始调用GDAL GDALDataset *poDataset; //注册 GDALAllRegister(); double geoInfo[6]; //打开文件获取数据集 poDataset = (GDALDataset *)GDALOpen(inputFile.c_str(), GA_ReadOnly); if(poDataset != NULL) { poDataset->GetRasterBand(1)->GetNoDataValue(&i); //获取图像的长宽像素值 xSize = poDataset->GetRasterXSize(); ySize = poDataset->GetRasterYSize(); poDataset->GetGeoTransform(geoInfo); // geoInfo[0] /* top left x, 图像左上角x坐标值*/ // geoInfo[1] /* w-e pixel resolution,图像横坐标 ?米/每像素 */ // geoInfo[2] /* rotation, 0 if image is "north up" */ // geoInfo[3] /* top left y 图像左上角y坐标值*/ // geoInfo[4] /* rotation, 0 if image is "north up" */ // geoInfo[5] /* n-s pixel resolution 图像纵坐标 ?米/每像素*/ } else { exit(1); } //OGRSpatialReference oUTM,oLatLong; //OGRCoordinateTransformation *poTransform; //oUTM.SetFromUserInput(poDataset->GetProjectionRef()); //oLatLong.SetFromUserInput("EPSG:4326"); //左上角坐标值 double x0, y0; x0 = geoInfo[0]; y0 = geoInfo[3]; //cout<<"x0:"<RasterIO( GF_Read, // GDALRWFlag eRWFlag,----> eRWFlag Either GF_Read to read a region of data, or GF_Write to write a region of data. x, // int nXOff, ----> The pixel offset to the top left corner of the region of the band to be accessed. This would be zero to start from the left side. y, // int nYOff, ----> The line offset to the top left corner of the region of the band to be accessed. This would be zero to start from the top. 1, // int nXSize, ----> The width of the region of the band to be accessed in pixels. 1, // int nYSize, ----> The height of the region of the band to be accessed in lines. (void *)buffer,// void * pData, ----> The buffer into which the data should be read, or from which it should be written. This buffer must contain at least nBufXSize * nBufYSize * nBandCount words of type eBufType. It is organized in left to right,top to bottom pixel order. Spacing is controlled by the nPixelSpace, and nLineSpace parameters. 1, // int nBufXSize, ----> the width of the buffer image into which the desired region is to be read, or from which it is to be written. 1, // int nBufYSize, ----> the height of the buffer image into which the desired region is to be read, or from which it is to be written. GDT_Float32, // GDALDataType eBufType, ----> the type of the pixel values in the pData data buffer. The pixel values will automatically be translated to/from the GDALRasterBand data type as needed. 1, // int nBandCount, ----> the number of bands being read or written. &band, // int * panBandMap, ----> the list of nBandCount band numbers being read/written. Note band numbers are 1 based. This may be NULL to select the first nBandCount bands. 0, // int nPixelSpace, ----> the byte offset from the start of one pixel value in pData to the start of the next pixel value within a scanline. If defaulted (0) the size of the datatype eBufType is used. 0, // int nLineSpace, ----> The byte offset from the start of one scanline in pData to the start of the next. If defaulted (0) the size of the datatype eBufType * nBufXSize is used. 0 //int nBandSpace , ----> the byte offset from the start of one bands data to the start of the next. If defaulted (0) the value will be nLineSpace * nBufYSize implying band sequential organization of the data buffer. )==CE_Failure) cout<<"poDataset->RasterIO failure"<

 

根据命令行输入的文件名及目标点经纬度读取EPSG:4326的点灰度值,没有转投影

你可能感兴趣的:(GDAL读取栅格文件小例)