[gdal-dev] VRT Derived Bands from Python?

Even Rouault even.rouault at mines-paris.org
Wed May 11 14:08:23 EDT 2011

Le mercredi 11 mai 2011 11:53:55, Knut-Frode Dagestad a écrit :
> Hi,
> Derived Bands of the VRT format seems like a very powerful resource.
>  From the tutorial (http://www.gdal.org/gdal_vrttut.html) it is clear
> that the Pixel Functions themselves have to be written in C, which is
> probably anyway best for performance.
> I also get the impression that the corresponding VRT files with Derived
> Bands can only be used by applications written in C/C++, since the Pixel
> Functions are only available inside the scope of such an application
> program:
> http://gis.stackexchange.com/questions/2940/gdal-pixel-functions-question
> Does this mean that Pixel Functions can not be used by command line
> utilities (e.g. gdalwarp and gdal_translate) or from Python?

The quick answer is : you can't use VRTDerivedBand from command line
utilities or Python.

The long answer is : but of course, as often in situations where you can't, 
you can... Provided you have a C compiler handy, the workaround here is to 
benefit from the GDAL plugin mechanism for other purposes than writing a GDAL 

Create a gdalmypixelfunction.c whose content is :

#include "gdal.h"

CPLErr MyFunction(void **papoSources, int nSources, void *pData,
                    int nXSize, int nYSize,
                    GDALDataType eSrcType, GDALDataType eBufType,
                    int nPixelSpace, int nLineSpace)
   /* implementation to write here */

void GDALRegisterMe() /* this is the entry point. don't change this name */
    GDALAddDerivedBandPixelFunc("MyFunction", MyFunction);

Compile it as a shared library, called gdal_XXXXXXXX.so (or .dll on
windows), define GDAL_DRIVER_PATH to point to the directory of the shared
library, et voilà ! The library will be loaded at runtime when the utilities 
or the python bindings call the GDALRegisterAll() method and the pixel 
function will be available to the VRT driver.

> Best regards from Knut-Frode
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev

More information about the gdal-dev mailing list