前提是将机器上的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 <stdlib.h> #include <stdio.h> #include <time.h> #include <math.h> #include <memory> #include <iomanip> #include <time.h> #include "windows.h" #include <iostream> #include <sstream> //istringstream #include <fstream> //read & write file #include <stdio.h> #include <string.h> #include <stdlib.h> //use system command //#include <unistd.h> #include <math.h> 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:"<<x0<<"y0:"<<y0<<endl; //经纬度转化为栅格点 double dx, dy; dx = targetX - x0; dy = targetY - y0; //cout<<"dx:"<<dx<<"dy:"<<dy<<endl; float buffer[1]; int x = (int)(dx/geoInfo[1]); int y = (int)(dy/geoInfo[5]); //cout<<"x:"<<x<<"y:"<<y<<endl; int band; float value = 0; //cout<<sizeof(buffer[0]);//int 是32位的 //buffer=88; band=1; if (poDataset->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"<<endl; else { //for(int i=0; i<4; ++i) //{ // cout<<"buffer[ "<< i <<" ]:"; // cout<<buffer[i]<<endl; //} //value = (buffer[0] + buffer[1] + buffer[2] + buffer[3])/4; cout<<buffer[0]<<endl; } } int main(int argc, char* argv[]) { //输入文件名称 string inputFile = "null"; string xStr = "null"; string yStr = "null"; for(int i = 1; i<argc ; i++){ if(0 == strcmp(argv[i], "-f")) { inputFile = argv[i + 1]; } else if(0 == strcmp(argv[i], "-x")) { xStr = argv[i + 1]; } else if(0 == strcmp(argv[i], "-y")) { yStr = argv[i + 1]; } } //取得经纬度坐标,并转化 double targetX = 0; double targetY = 0; if(strcmp(xStr.c_str(), "null")) { targetX = atof(xStr.c_str()); } if(strcmp(yStr.c_str(), "null")) { targetY = atof(yStr.c_str()); } getValueByLoc(inputFile, targetX, targetY); }
根据命令行输入的文件名及目标点经纬度读取EPSG:4326的点灰度值,没有转投影