Changes between Initial Version and Version 1 of RasterVrtDerivedBands


Ignore:
Timestamp:
Feb 16, 2012, 11:08:12 AM (12 years ago)
Author:
armin
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • RasterVrtDerivedBands

    v1 v1  
     1= Using GDAL VRT images with derived bands in MapServer =
     2
     3The GDAL virtual image format (VRT) allows the creation of derived bands that derive their pixel information from their source bands. A general overview is given at the [http://www.gdal.org/gdal_vrttut.html GDAL web site]. This how-to describes the steps to make this type of raster format usable in !MapServer, based on the sample code of the GDAL site. The emphasis lies on the creation and registration of the required pixel function used for the derived band.
     4
     5== Definition of the VRT image ==
     6The VRT band definition starts with
     7{{{
     8<VRTDataset rasterXSize="1000" rasterYSize="1000">
     9    <VRTRasterBand dataType="Float32" band="1" subClass="VRTDerivedRasterBand">
     10        <Description>Magnitude</Description>
     11        <PixelFunctionType>TestFunction</PixelFunctionType>
     12        <SourceTransferType>CFloat64</SourceTransferType>
     13    ...
     14}}}
     15
     16The important part is the refernce of the pixel function, here name {{{TestFunction}}}
     17
     18== Creation of the pixel function ==
     19Create a file {{{TestFunction.c}}} with the following contents
     20
     21{{{
     22#include "gdal.h"
     23
     24CPLErr TestFunction(void **papoSources, int nSources, void *pData,
     25                    int nXSize, int nYSize,
     26                    GDALDataType eSrcType, GDALDataType eBufType,
     27                    int nPixelSpace, int nLineSpace)
     28{
     29    int ii, iLine, iCol;
     30    double pix_val;
     31    double x0, x3, x4, x8;
     32
     33    // ---- Init ----
     34    if (nSources != 4) return CE_Failure;
     35
     36    // ---- Set pixels ----
     37    for( iLine = 0; iLine < nYSize; iLine++ )
     38    {
     39        for( iCol = 0; iCol < nXSize; iCol++ )
     40        {
     41            ii = iLine * nXSize + iCol;
     42            /* Source raster pixels may be obtained with SRCVAL macro */
     43            x0 = SRCVAL(papoSources[0], eSrcType, ii);
     44            x3 = SRCVAL(papoSources[1], eSrcType, ii);
     45            x4 = SRCVAL(papoSources[2], eSrcType, ii);
     46            x8 = SRCVAL(papoSources[3], eSrcType, ii);
     47
     48            pix_val = sqrt((x3*x3+x4*x4)/(x0*x8));
     49           
     50            GDALCopyWords(&pix_val, GDT_Float64, 0,
     51                          ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace,
     52                          eBufType, nPixelSpace, 1);
     53        }
     54    }
     55
     56    // ---- Return success ----
     57    return CE_None;
     58}
     59
     60void GDALRegister_TestFunction() {
     61     GDALAddDerivedBandPixelFunc("TestFunction", TestFunction);
     62}
     63}}}