Search code examples
pythoncarraysgdal

Is ReadAsArray in gdal-python the same as GDALRasterIO in C-GDAL?


Hi I am trying to iterate over the values of a raster dataset (band1). I am able to do it in python with the following code snippet (sorry I can't provide the original raster)

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])

My goal is to emulate the same in C-GDAL,

here is my code snippet (which does not work)

#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]);
        }
    }
}

}

Question: What does GDALRasterIO returns?, I did an array declaration when writing in line 25:

int band1[nXSize][nYSize],

And then I read the raster Data and assign it to the previous band1 array in the following line (26):

int band1 = GDALRasterIO(raster_band, 0, 0,GF_Read, nXSize, 1, pafScanline, nXSize, 1, GDT_Float32, 0, 0);

When compiling through

cc open_array.c -o open_array -lgdal

I got the following error:

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]);

Why does that happen? Should I get rid of the declaration in line 25?

Thanks in advance


Solution

  • What does GDALRasterIO returns?

    As per documentation it returns a CPLErr which is an int value. You may use this value in order to check if your read was successful or not:

    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
    

    The error that you have in your code is related to the fact that you're trying to redeclare the same variable (band1) twice. That's what the complier complains about. You have first int band1[nXSize][nYSize] -> declaring the variable once and then after you have int band1 = GDALRasterIO(.... Since you're using the type (int) again you redeclare the variable. If you don't want to redeclare it you should do only an assignment on the second line: band1 = GDALRasterIO(.... Anyways the type of band1 is not right, it shouldn't be a matrix.