Ticket #3925: vrt_pixelfunc_with_autotest.patch

File vrt_pixelfunc_with_autotest.patch, 8.5 KB (added by antonio, 5 years ago)

Extended patch with autotest

  • gdal/frmts/vrt/vrtdataset.cpp

     
    921921
    922922        /* ---- Check for our sourced band 'derived' subclass ---- */
    923923        if(pszSubClass != NULL && EQUAL(pszSubClass,"VRTDerivedRasterBand")) {
    924             poBand = new VRTDerivedRasterBand
    925                 (this, GetRasterCount() + 1, eType,
    926                  GetRasterXSize(), GetRasterYSize());
    927         }
     924
     925            /* We'll need a pointer to the subclass in case we need */
     926            /* to set the new band's pixel function below. */
     927            VRTDerivedRasterBand* poDerivedBand;
     928
     929            poDerivedBand = new VRTDerivedRasterBand
     930                (this, GetRasterCount() + 1, eType,
     931                 GetRasterXSize(), GetRasterYSize());
     932
     933            /* Set the pixel function options it provided. */
     934            const char* pszFuncName =
     935                CSLFetchNameValue(papszOptions, "PixelFunctionType");
     936            if (pszFuncName != NULL)
     937                poDerivedBand->SetPixelFunctionName(pszFuncName);
     938
     939            const char* pszTransferTypeName =
     940                CSLFetchNameValue(papszOptions, "SourceTransferType");
     941            if (pszTransferTypeName != NULL) {
     942                GDALDataType eTransferType =
     943                    GDALGetDataTypeByName(pszTransferTypeName);
     944                if (eTransferType == GDT_Unknown) {
     945                    CPLError( CE_Failure, CPLE_AppDefined,
     946                              "invalid SourceTransferType: \"%s\".",
     947                              pszTransferTypeName);
     948                    delete poDerivedBand;
     949                    return CE_Failure;
     950                }
     951                poDerivedBand->SetSourceTransferType(eTransferType);
     952            }
     953
     954            /* We're done with the derived band specific stuff, so */
     955            /* we can assigned the base class pointer now. */
     956            poBand = poDerivedBand;
     957        }
    928958        else {
    929959
    930960            /* ---- Standard sourced band ---- */
  • autotest/gdrivers/vrtderived.py

     
     1#!/usr/bin/env python
     2###############################################################################
     3# $Id: vrtfilt.py 19755 2010-05-22 15:49:45Z rouault $
     4#
     5# Project:  GDAL/OGR Test Suite
     6# Purpose:  Test VRT driver based filtering.
     7# Author:   Frank Warmerdam <warmerdam@pobox.com>
     8#
     9###############################################################################
     10# Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com>
     11#
     12# Permission is hereby granted, free of charge, to any person obtaining a
     13# copy of this software and associated documentation files (the "Software"),
     14# to deal in the Software without restriction, including without limitation
     15# the rights to use, copy, modify, merge, publish, distribute, sublicense,
     16# and/or sell copies of the Software, and to permit persons to whom the
     17# Software is furnished to do so, subject to the following conditions:
     18#
     19# The above copyright notice and this permission notice shall be included
     20# in all copies or substantial portions of the Software.
     21#
     22# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     23# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     24# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
     25# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     26# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     27# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     28# DEALINGS IN THE SOFTWARE.
     29###############################################################################
     30
     31import os
     32import sys
     33import gdal
     34
     35sys.path.append( '../pymod' )
     36
     37import gdaltest
     38
     39def _xmlsearch(root, nodetype, name):
     40    for node in root[2:]:
     41        if node[0] == nodetype and node[1] == name:
     42            return node
     43    else:
     44        None
     45
     46###############################################################################
     47# Verify raster band subClass
     48
     49def vrtderived_1():
     50    filename = 'tmp/derived.vrt'
     51    vrt_ds = gdal.GetDriverByName('VRT').Create(filename, 50, 50, 0)
     52
     53    options = [
     54        'subClass=VRTDerivedRasterBand',
     55    ]
     56    vrt_ds.AddBand(gdal.GDT_Byte, options)
     57
     58    simpleSourceXML = '''    <SimpleSource>
     59      <SourceFilename>data/byte.tif</SourceFilename>
     60      <SourceBand>1</SourceBand>
     61    </SimpleSource>'''
     62
     63    md = {}
     64    md['source_0'] = simpleSourceXML
     65
     66    vrt_ds.GetRasterBand(1).SetMetadata(md, 'vrt_sources')
     67    vrt_ds = None
     68
     69    xmlstring = open(filename).read()
     70    gdal.Unlink(filename)
     71
     72    node = gdal.ParseXMLString( xmlstring )
     73    node = _xmlsearch(node, gdal.CXT_Element, 'VRTRasterBand')
     74    node = _xmlsearch(node, gdal.CXT_Attribute, 'subClass')
     75    node = _xmlsearch(node, gdal.CXT_Text, 'VRTDerivedRasterBand')
     76    if node is None:
     77        gdaltest.post_reason( 'invalid subclass' )
     78        return 'fail'
     79
     80    return 'success'
     81
     82###############################################################################
     83# Verify derived raster band pixel function type
     84
     85def vrtderived_2():
     86    filename = 'tmp/derived.vrt'
     87    vrt_ds = gdal.GetDriverByName('VRT').Create(filename, 50, 50, 0)
     88
     89    options = [
     90        'subClass=VRTDerivedRasterBand',
     91        'PixelFunctionType=dummy',
     92    ]
     93    vrt_ds.AddBand(gdal.GDT_Byte, options)
     94
     95    simpleSourceXML = '''    <SimpleSource>
     96      <SourceFilename>data/byte.tif</SourceFilename>
     97      <SourceBand>1</SourceBand>
     98    </SimpleSource>'''
     99
     100    md = {}
     101    md['source_0'] = simpleSourceXML
     102
     103    vrt_ds.GetRasterBand(1).SetMetadata(md, 'vrt_sources')
     104    vrt_ds = None
     105
     106    xmlstring = open(filename).read()
     107    gdal.Unlink(filename)
     108
     109    node = gdal.ParseXMLString( xmlstring )
     110    node = _xmlsearch(node, gdal.CXT_Element, 'VRTRasterBand')
     111    node = _xmlsearch(node, gdal.CXT_Element, 'PixelFunctionType')
     112    node = _xmlsearch(node, gdal.CXT_Text, 'dummy')
     113    if node is None:
     114        gdaltest.post_reason( 'incorrect PixelFunctionType value' )
     115        return 'fail'
     116
     117    return 'success'
     118
     119###############################################################################
     120# Verify derived raster band transfer type
     121
     122def vrtderived_3():
     123    filename = 'tmp/derived.vrt'
     124    vrt_ds = gdal.GetDriverByName('VRT').Create(filename, 50, 50, 0)
     125
     126    options = [
     127        'subClass=VRTDerivedRasterBand',
     128        'PixelFunctionType=dummy',
     129        'SourceTransferType=Byte',
     130    ]
     131    vrt_ds.AddBand(gdal.GDT_Byte, options)
     132
     133    simpleSourceXML = '''    <SimpleSource>
     134      <SourceFilename>data/byte.tif</SourceFilename>
     135      <SourceBand>1</SourceBand>
     136    </SimpleSource>'''
     137
     138    md = {}
     139    md['source_0'] = simpleSourceXML
     140
     141    vrt_ds.GetRasterBand(1).SetMetadata(md, 'vrt_sources')
     142    vrt_ds = None
     143
     144    xmlstring = open(filename).read()
     145    gdal.Unlink(filename)
     146
     147    node = gdal.ParseXMLString( xmlstring )
     148    node = _xmlsearch(node, gdal.CXT_Element, 'VRTRasterBand')
     149    node = _xmlsearch(node, gdal.CXT_Element, 'SourceTransferType')
     150    node = _xmlsearch(node, gdal.CXT_Text, 'Byte')
     151    if node is None:
     152        gdaltest.post_reason( 'incorrect SourceTransferType value' )
     153        return 'fail'
     154
     155    return 'success'
     156
     157###############################################################################
     158# Check handling of invalid derived raster band transfer type
     159
     160def vrtderived_4():
     161    filename = 'tmp/derived.vrt'
     162    vrt_ds = gdal.GetDriverByName('VRT').Create(filename, 50, 50, 0)
     163
     164    options = [
     165        'subClass=VRTDerivedRasterBand',
     166        'PixelFunctionType=dummy',
     167        'SourceTransferType=Invalid',
     168    ]
     169    gdal.PushErrorHandler('CPLQuietErrorHandler')
     170    ret = vrt_ds.AddBand(gdal.GDT_Byte, options)
     171    gdal.PopErrorHandler()
     172    if ret == 0:
     173        gdaltest.post_reason( 'invalid SourceTransferType value not detected' )
     174        return 'fail'
     175
     176    return 'success'
     177
     178###############################################################################
     179# Cleanup.
     180
     181def vrtderived_cleanup():
     182    try:
     183        os.remove( 'tmp/derived.vrt' )
     184    except:
     185        pass
     186    return 'success'
     187
     188gdaltest_list = [
     189    vrtderived_1,
     190    vrtderived_2,
     191    vrtderived_3,
     192    vrtderived_4,
     193    vrtderived_cleanup,
     194]
     195
     196if __name__ == '__main__':
     197
     198    gdaltest.setup_run( 'vrtderived' )
     199
     200    gdaltest.run_tests( gdaltest_list )
     201
     202    gdaltest.summarize()