gdal-python 中的 ReadAsArray 是否与 C-GDAL 中的 GDALRasterIO 相同?
Is ReadAsArray in gdal-python the same as GDALRasterIO in C-GDAL?
您好,我正在尝试迭代栅格数据集 (band1) 的值。我可以使用以下代码片段在 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 中模拟相同的内容,
这是我的代码片段(不起作用)
#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 returns是做什么用的,我在第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 行的声明吗?
提前致谢
What does GDALRasterIO returns?
根据 documentation it returns a CPLErr 这是一个 int
值。您可以使用此值来检查您的读取是否成功:
CPLErr readResult = GDALRasterIO(raster_band, 0, 0,GF_Read, nXSize, 1, pafScanline, nXSize, 1, GDT_Float32, 0, 0);
if (readResult != CE_None) {
// error reading the file
}
// else continue processing
您的代码中出现的错误与您尝试重新声明同一个变量 (band1
) 两次有关。这就是编译器所抱怨的。
您首先要 int band1[nXSize][nYSize]
-> 声明一次变量,然后再声明 int band1 = GDALRasterIO(...
。由于您再次使用类型 (int
),因此您重新声明了该变量。如果你不想重新声明它,你应该只在第二行做一个赋值:band1 = GDALRasterIO(...
。反正band1
的类型不对,不应该是矩阵。
您好,我正在尝试迭代栅格数据集 (band1) 的值。我可以使用以下代码片段在 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 中模拟相同的内容,
这是我的代码片段(不起作用)
#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 returns是做什么用的,我在第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 行的声明吗?
提前致谢
What does GDALRasterIO returns?
根据 documentation it returns a CPLErr 这是一个 int
值。您可以使用此值来检查您的读取是否成功:
CPLErr readResult = GDALRasterIO(raster_band, 0, 0,GF_Read, nXSize, 1, pafScanline, nXSize, 1, GDT_Float32, 0, 0);
if (readResult != CE_None) {
// error reading the file
}
// else continue processing
您的代码中出现的错误与您尝试重新声明同一个变量 (band1
) 两次有关。这就是编译器所抱怨的。
您首先要 int band1[nXSize][nYSize]
-> 声明一次变量,然后再声明 int band1 = GDALRasterIO(...
。由于您再次使用类型 (int
),因此您重新声明了该变量。如果你不想重新声明它,你应该只在第二行做一个赋值:band1 = GDALRasterIO(...
。反正band1
的类型不对,不应该是矩阵。