Skip to content
This repository has been archived by the owner on Nov 12, 2020. It is now read-only.

GDAL VRT Band value filter example

Joachim Meyer edited this page Jan 11, 2019 · 1 revision

Sample for VRT pixel function to filter band values.

Add/Change the VRT xml with below:

<VRTRasterBand dataType="Int16" band="1" subClass="VRTDerivedRasterBand">
    <NoDataValue>-999</NoDataValue>
    <Description>Snow Cover</Description>
    <UnitType>%</UnitType>
    <SourceTransferType>Int16</SourceTransferType>
    <PixelFunctionType>pixel_function.by_threshold</PixelFunctionType>
    <PixelFunctionLanguage>Python</PixelFunctionLanguage>
    <PixelFunctionArguments lower="15" upper="100"/>
</VRTRasterBand>

Python pixel function with file name pixel_function.py:

NO_DATA_VALUE = -999

def by_threshold(in_ar, out_ar, x_off, y_off, x_size, y_size,
                 raster_x_size, raster_y_size, buf_radius, gt, **kwargs):
    for arr in in_ar:
        arr[(arr < float(kwargs['lower'])) |
            (arr > float(kwargs['upper']))] = NO_DATA_VALUE
        out_ar[:] = arr

Sample command line call:

PYTHONPATH=/Path/To/pixel/function/file/ GDAL_VRT_ENABLE_PYTHON=YES gdal_translate mosaic_filter.vrt mosaic_filter.tif
Clone this wiki locally