【问题标题】:Is ReadAsArray in gdal-python the same as GDALRasterIO in C-GDAL?gdal-python 中的 ReadAsArray 是否与 C-GDAL 中的 GDALRasterIO 相同?
【发布时间】: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 行的声明吗?

提前致谢

【问题讨论】:

    标签: python c arrays gdal


    【解决方案1】:

    GDALRasterIO 返回什么?

    根据documentation,它返回一个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的类型不对,不应该是矩阵。

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多