【发布时间】:2019-11-01 13:14:38
【问题描述】:
您好,我正在尝试迭代栅格数据集 (band1) 的值。我可以使用以下代码 sn-p 在 python 中执行此操作(对不起,我无法提供原始栅格)
import numpy as np
import gdal
path = "data/Isle_wight.tif"
ds = gdal.Open(path)
myarray = np.array(ds.GetRasterBand(1).ReadAsArray())
print(myarray.shape[0])
#print(columns)
for j in range(myarray.shape[0]-1):
for i in range(myarray.shape[1]-1):
print( myarray[j][i])
我的目标是在 C-GDAL 中效仿,
这是我的代码 sn-p(不起作用)
#include "gdal/gdal.h"
#include "gdal/cpl_conv.h"
int main()
{
GDALDatasetH raster;
GDALAllRegister();
raster = GDALOpen( "/home/roger/Documents/98_GDAL_C++_snippets/data/Isle_wight.tif", GA_ReadOnly);
if(raster==NULL)
{
printf("Invalid Raster Dataset");
}
else
{
GDALRasterBandH raster_band;
int XSize, YSize;
raster_band = GDALGetRasterBand( raster, 1);
GDALGetBlockSize( raster_band, &XSize, &YSize);
float *pafScanline;
int nXSize = GDALGetRasterBandXSize (raster_band);
int nYSize = GDALGetRasterBandYSize (raster_band);
pafScanline = (float*) CPLMalloc(sizeof(float)*nXSize);
int address_of_pafScanline = *pafScanline;
int band1[nXSize][nYSize];
int band1 = GDALRasterIO(raster_band, 0, 0,GF_Read, nXSize, 1, pafScanline, nXSize, 1, GDT_Float32, 0, 0);
for(int i = 0; i <= nXSize; i++) {
for(int j = 0; j < nYSize; j++ ) {
printf("%d", band1[i][j]);
}
}
}
}
问题:GDALRasterIO返回什么?,我在第25行写的时候做了一个数组声明:
int band1[nXSize][nYSize],
然后我读取栅格数据并将其分配给以下行 (26) 中的前一个 band1 数组:
int band1 = GDALRasterIO(raster_band, 0, 0,GF_Read, nXSize, 1, pafScanline, nXSize, 1, GDT_Float32, 0, 0);
通过编译时
cc open_array.c -o open_array -lgdal
我收到以下错误:
open_array.c: In function ‘main’:
open_array.c:26:9: error: conflicting types for ‘band1’
int band1 = GDALRasterIO(raster_band, 0, 0,GF_Read, nXSize, 1, pafScanline, nXSize, 1, GDT_Float32, 0, 0);
^~~~~
open_array.c:25:9: note: previous declaration of ‘band1’ was here
int band1[nXSize][nYSize];
^~~~~
open_array.c:29:31: error: subscripted value is neither array nor pointer nor vector
printf("%d", band1[i][j]);
为什么会这样? 我应该去掉第 25 行的声明吗?
提前致谢
【问题讨论】: