Get all the number of row and column into vector array using GDAL


I am using the below code to read the elevation point, code below give me all the elevation point into vector array. The value only the elevation. How can i get also all the number of row and columns into a vector array also

for Example:





Exception thrown at 0x01BC8AD4 (gdal111.dll) in TacNav.exe: 0xC0000005: Access violation reading location 0x42C40034.

It show above error when i try to using nested loop and it also does not get into the array.

  int _tmain(int argc, _TCHAR* argv[])
        const char* pszFilename = "C:\\Elevationsample.DT2";
        double adfGeoTransform[6];
        int buffEleveationValue[1];
         int rows = 3601;
         int columns = 3601;
        std::vector<float> buffElevation(rows * columns);
        GDALRasterBand *poBand;
        GDALDataset  *poDataset;
        poDataset = (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly );
        printf( "Driver: %s/%s\n",
            poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
        printf( "Size is %dx%dx%d\n",
            poDataset->GetRasterCount() );
        if( poDataset->GetProjectionRef()  != NULL )
            printf( "Projection is `%s'\n", poDataset->GetProjectionRef() );
        if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None )
            printf( "Origin = (%.6f,%.6f)\n",
                    adfGeoTransform[0], adfGeoTransform[3] );
            printf( "Pixel Size = (%.6f,%.6f)\n",
                    adfGeoTransform[1], adfGeoTransform[5] );
        poBand = poDataset->GetRasterBand(1); 

for (int c = 0; c < columns; c++)
        for (int r = 0; r< rows; r++)

        poBand->RasterIO(GF_Read, c ,r, 1,1,buffEleveationValue, c, r, GDT_Float32, 0, 0); // read the cell value




        return 0;
asked on Stack Overflow Jun 24, 2020 by Mr Test • edited Jun 26, 2020 by Mr Test

1 Answer


If what you are trying to do is put all that data into a single vector as you have it described then you would need to set up a struct to hold that data like this:

struct elevationData
   int row;
   int col;
   float elevation;

Then make a vector to hold that type of struct.

std::vector<elevationData> yourVector;

Although this would be a rather inefficient way to store this data. Instead I would come up with some sort of indexing scheme to store your data in a single float vector. If you were consistent with your indexing you could determine the row and column of an element by looking at the index. A simple indexing rule could be:

column + (row * numColumns) = index
answered on Stack Overflow Jun 25, 2020 by jsmit42

User contributions licensed under CC BY-SA 3.0