{
//开始调用GDAL
int i, xSize, ySize, bandNum;
GDALDataset *poDataset;
//注册
GDALAllRegister();
//坐标转换参数
double geoInfo[6];
//打开文件获取数据集
poDataset = (GDALDataset *)GDALOpen("G:\\dem\\***.tif", GA_ReadOnly);
//存储影像值
int *pafScan;
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 图像纵坐标 ?米/每像素*/
//获取影像波段数
bandNum = poDataset->GetRasterCount();
pafScan = new int[xSize * ySize ]; //指向存储数据,一个波段
//将影像第一个波段的全部值存入pafscan
poDataset->GetRasterBand(1)->RasterIO( GF_Read, 0, 0, xSize, ySize,
pafScan, xSize, ySize, GDT_Float32, 0, 0);//将一个波段存入pafScan
}
else
{
exit(1);
}
//左上角坐标值
double x0, y0;
x0 = geoInfo[0];
y0 = geoInfo[3];
//经纬度转化为栅格点
double dx, dy;
dx = 114.355727 - x0;
dy = 30.528824 - y0;
int x = (int)(dx / geoInfo[1]);
int y = (int)(dy / geoInfo[5]);
int band=1;//波段
float buffer[1];//申请一个尽可能小的缓存区
float height= 0;
//通过经纬度查询当前高度值
if poDataset->GetRasterBand(1)->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
{
height=buffer[0];
}
//最后一定要关闭影像
GDALClose(poDataset);
}
使用GDAL查询tiff影像任意一点的高程
3星 · 超过75%的资源 需积分: 46 21 浏览量
2018-11-07
17:22:58
上传
评论 7
收藏 231KB ZIP 举报
N3802608
- 粉丝: 64
- 资源: 4
最新资源
- 基于SpringBoot+Vue3快速开发平台、自研工作流引擎源码设计.zip
- docker安装部署全流程
- 基于树莓派的人脸识别系统python源码+项目部署说明+超详细代码注释.zip
- Python和R爬取分析赶集网北京二手房数据.zip
- Python和R爬取分析赶集网北京二手房数据.zip
- Java知识体系最强总结(2021版).txt
- Python知识点Python知识点Python知识点Python知识点Python知识点PythonPython知识点.txt
- Java开发基于seetaface6的人脸识别(活体检测)的封装源码.zip
- JSP在线失物招领管理平台源码.zip
- JSP在线旅游美食展现管理系统源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈