[gdal-dev] C++ / GDAL - reading raster, exception, access violation, help please!

Tim Bodin TimBodin at comcast.net
Fri Nov 9 19:29:26 PST 2018


I sure hope someone can give me some advice.  I'm modestly experienced with
GIS and spatial stuff, but I'm trying to do rasters in C++ for the first
time.  I've been cobbling experiments together, generally successfully, but
have not consistently been able to get a raster read working, and I don't
know C++ or GDAL deeply enough to understand what to do.

 

Code is snapshotted below.  Using Windows, Visual Studio 2017.  If I enable
the 5 lines in the inner loop near the bottom, I get an Exception/Access
Violation on the "inDS->GetRasterBand(etc)" statement.  If I disable that
section, and specifically that line, it runs to completion without problem.
Can anyone suggest what I might try?  Thanks for helping!

 

*************************

 

#include "stdafx.h"

#include <iostream>

#include <gdal.h>

#include <gdal_priv.h>

#include "cpl_conv.h"

 

using namespace std;

 

int main()

{

       GDALDataset  *inDS;

       GDALAllRegister();

       const char *input = "D:/Imagery/CDLClipped.tif";

       inDS = (GDALDataset*) GDALOpen(input, GA_ReadOnly);

       if (inDS == NULL)

       {

             cout << "unable to open input" << endl;

       }

       int nRows, nCols;

       double transform[6];

       double noData;

       nRows = inDS->GetRasterBand(1)->GetYSize();

       nCols = inDS->GetRasterBand(1)->GetXSize();

       noData = inDS->GetRasterBand(1)->GetNoDataValue();

       inDS->GetGeoTransform(transform);

       cout << "nRows, nCols, noData = " << nRows << ", " << nCols << ", "
<< noData << endl;

       float *inDSRow = (float*)CPLMalloc(sizeof(float)*nCols);

       for (int i = 0; i < 5; i++)

       {

             inDS->GetRasterBand(1)->RasterIO(GF_Read, 0, i, nCols, 1,
inDSRow, nCols, 1, GDT_CFloat32, 0, 0);

             for (int j = 0; j < 5 ; j++)

             {

                    cout << inDSRow[j] << endl;

             }

       }

       cin.ignore();

}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20181109/9159620e/attachment.html>


More information about the gdal-dev mailing list