1

I am using GDAL 2.1.0, in both Windows and Linux environments.

I want to programmatically rasterize a shapefile in my C++ app.

gdal_rasterize

I can successfully execute the stand-alone gdal_rasterize app from the command line. For example, the following works to rasterize a shape file that contains areal features about farms.

gdal_rasterize  -a 'crop_id'  -l 'farms'  -init 0  -ts 1024 1024  -ot Byte  farms.shp  farms.tif

GDALRasterize()

The following is my attempt to perform the same rasterization within my C++ app.

GDALAllRegister();

...

//------------------------------
// Paths of the source and destination files, relative to current working directory.
string srcPath = "farms.shp";
string dstPath = "farms.tif";

//------------------------------
// Open the vector dataset.
unsigned int openFlags = GDAL_OF_VECTOR | GDAL_OF_READONLY; 
GDALDataset * pSrcDataset = static_cast<GDALDataset *>( GDALOpenEx(
  srcPath.c_str(), 
  openFlags,
  NULL, NULL, NULL ) );

// The one layer.
OGRLayer * pSrcLayer = pSrcDataset->GetLayer( 0 );

//------------------------------
// Rasterize options.
// TODO: Are these intended to be just like the command line arguments 
// I would use if executing gdal_rasterize?
// https://gdal.org/programs/gdal_rasterize.html
char ** argv = NULL;

// -a <attribute_name>
// An attribute field on the features to be used for a burn-in value.
argv = CSLSetNameValue( argv,  "-a", "crop_id" );

// -l <layer_name>
// The layer from the datasource that will be used for input features.
argv = CSLSetNameValue( argv,  "-l", pSrcLayer->GetName() );

// -init <value>
// Pre-initialize the output image bands with these values.
argv = CSLSetNameValue( argv,  "-init", "0" );

// -ts <width> <height>
// The output file size in pixels and lines. 
argv = CSLSetNameValue( argv,  "-ts", "1024 1024" );

// -ot <type>
// Force the output bands to be of the indicated data type.
argv = CSLSetNameValue( argv,  "-ot", "Byte" );

GDALRasterizeOptions * pOptions = GDALRasterizeOptionsNew( argv, NULL );

//------------------------------
// Perform rasterization.
int usageError;
GDALDataset * pDstDataset = static_cast<GDALDataset *>( GDALRasterize(
  dstPath.c_str(), NULL,
  pSrcDataset,
  pOptions,
  &usageError ) );

//------------------------------
// Cleanup.
GDALRasterizeOptionsFree( pOptions );
CSLDestroy( argv );
GDALClose( pSrcDataset );
GDALClose( pDstDataset );

Problem: Invalid arguments

The first problem is that I am providing the rasterization arguments incorrectly. The following error printed to standard output lets me know that I am doing it wrong.

GDAL code 6: Unknown option name '-a=crop_id'

Providing the arguments incorrectly is probably the cause of my other problems.

Problem: Projection and SRS unknown

The following errors printed to standard output.

GDAL code 10: Pointer 'hDS' is NULL in 'GDALGetProjectionRef'.

GDAL code 1: The input vector layer has a SRS, but the output raster dataset SRS is unknown.
Ensure output raster dataset has the same SRS, otherwise results might be incorrect.

Problem: Crash from uninitialized pointer

I eventually crash because of a bad pointer.

First-chance exception at 0x10050ee0 in make_wcf.exe: 0xC0000005: Access violation reading location 0x00000040.
Unhandled exception at 0x10050ee0 in make_wcf.exe: 0xC0000005: Access violation reading location 0x00000040.

1 Answer 1

2

The problem was that I was using the wrong CSL* function to build the rasterization argument array. The following works, and GDALRasterize() performs correctly without errors.

//------------------------------
// Rasterize options.
char ** argv = NULL;

argv = CSLAddString( argv, "-a" );
argv = CSLAddString( argv, "crop_id" );

argv = CSLAddString( argv, "-l" );
argv = CSLAddString( argv, pSrcLayer->GetName() );

argv = CSLAddString( argv, "-init" );
argv = CSLAddString( argv, "0" );

argv = CSLAddString( argv, "-ts" );
argv = CSLAddString( argv, "1024" );
argv = CSLAddString( argv, "1024" );

argv = CSLAddString( argv, "-ot" );
argv = CSLAddString( argv, "byte" );

GDALRasterizeOptions * pOptions = GDALRasterizeOptionsNew( argv, NULL );

... call GDALRasterize() as before ...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.