【发布时间】:2020-06-21 09:00:45
【问题描述】:
我遇到了这个 gdal 代码来读取特定位置的海拔点。 这段代码只允许我阅读一点。海拔文件信息:
左上 X = 103.0000 左上 Y = 4.0000
右下 X = 104.0000 右下 Y = 3.0000
没有。列数 = 3601,行数 = 3601
如何读取所有高程点并将其存储在向量数组中?
int _tmain(int argc, _TCHAR* argv[])
{
const char* pszFilename = "C:\\Elevation.DT2";
double adfGeoTransform[6];
float buffEleveation[1];
int row;
int col;
std::vector<float> elevationVecArr;
GDALRasterBand *poBand;
GDALDataset *poDataset;
GDALAllRegister();
poDataset = (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly );
printf( "Driver: %s/%s\n",
poDataset->GetDriver()->GetDescription(),
poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
printf( "Size is %dx%dx%d\n",
poDataset->GetRasterXSize(),poDataset->GetRasterYSize(),
poDataset->GetRasterCount() );
if( poDataset->GetProjectionRef() != NULL )
printf( "Projection is `%s'\n", poDataset->GetProjectionRef() );
if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None )
{
printf( "Origin = (%.6f,%.6f)\n",
adfGeoTransform[0], adfGeoTransform[3] );
printf( "Pixel Size = (%.6f,%.6f)\n",
adfGeoTransform[1], adfGeoTransform[5] );
}
poDataset->GetGeoTransform(adfGeoTransform);
poBand = poDataset->GetRasterBand(1);
float X = (103.0000);
float Y = (4.000);
row = (int)((adfGeoTransform[3] - Y) / -adfGeoTransform[5]); // ULY - xcoord / -N-S CellSize;
col = (int)((X - adfGeoTransform[0]) / adfGeoTransform[1]); // xcoord - ULX / E-W CellSize
poBand->RasterIO(GF_Read, col, row, 1, 1, buffEleveation, 1, 1, GDT_Float32, 0, 0); // read the cell value
float elevationValue = buffEleveation[0];
elevationVecArr.push_back(buffEleveation[0]);
printf("Elevation = %.f", elevationValue);
getchar();
return 0;
}
【问题讨论】: