1

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

Does that happen because of the declaration in line 25?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.