Search code examples
c++gdal

how to pass in a GDALResampleAlg to GDAL's RasterIO?


The the RasterIO method has a GDALResampleAlg option using the GDALRasterIOExtraArg struct:

Link

The GDAL tutorial has an example like this for reading data from a raster band:

  poBand->RasterIO( GF_Read, Xoffset, Yoffset, nXSize, nYSize,
                    pafScanline, outXSize, outYSize, GDT_Float32,
                    0, 0);

and the unused 12th argument from that can take the resampling option within the GDALRasterIOExtraArg.

Link

How does one write the C++ to actually pass in the option? I can instantiate the option from the constants:

 // how to pass this option in to RasterIO?
  GDALResampleAlg eResampleAlg = GRA_NearestNeighbour;

But I don't know C++ well enough to see how to actually use it, and I can't find any examples that I can follow enough to make it work.


Solution

  • Here is a way, there is a macro INIT_RASTERIO_EXTRA_ARG that sets up the extra-arg struct, that is listed here:

    Link

    Then set the eResampleAlg property to the appropriate constant. Some of the other extra-arg parameters are needed for certain cases, but that is outside of scope of this question.

    float *pafScanline;
    
    GDALRasterIOExtraArg psExtraArg;
    INIT_RASTERIO_EXTRA_ARG(psExtraArg);
    // TODO expose the resampling options to user
    psExtraArg.eResampleAlg = GRIORA_NearestNeighbour;
    pafScanline = (float *) CPLMalloc(sizeof(float)*outXSize*outYSize);
    CPLErr err = poBand->RasterIO( GF_Read, Xoffset, Yoffset, nXSize, nYSize,
                    pafScanline, outXSize, outYSize, GDT_Float32,
                    0, 0, &psExtraArg);
    

    See here for more Link