1

I'm trying to create a script which takes an unkown number of equal size rasters and returns a single raster identifying the source (raster) of the maximum value for each pixel. GDAL is telling me it cannnot find a file when I call it from a script in the QGIS processing script editor.

My script that is generating the error is below, specifically, when it calls UpdateTocRaster:

class FindTimeOfConcentration(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterMultipleLayers('waterlevelgrids', 'Water level grids', layerType=QgsProcessing.TypeRaster, defaultValue=None))

    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(5, model_feedback)
        results = {}
        outputs = {}
        layers = self.parameterAsLayerList(parameters, 'waterlevelgrids', context)
        max_raster = layers[0]
        toc_raster = self.initiateRasterID(max_raster, context, feedback)['OUTPUT']
        for count, layer in enumerate(layers[1:]):
            feedback.pushInfo(f'Processing raster {count}')
            feedback.pushInfo(f'Updating time of concentration raster')
            toc_raster = self.updateTocRaster(toc_raster, max_raster, layer, count+2, context, feedback)['OUTPUT']
            feedback.pushInfo(f'Updating max raster')
            max_raster = self.updateMaxRaster(max_raster, layer, context, feedback)['OUTPUT']
        return results

the function updateTocRaster looks like:

def updateTocRaster(self, toc_raster, max_raster, layer, count, context, feedback):
    alg_params = {
        'BAND_A': 1,
        'BAND_B': 1,
        'BAND_C': None,
        'BAND_D': None,
        'BAND_E': None,
        'BAND_F': None,
        'EXTRA': '',
        'FORMULA': f'(A>=B)*C+(A<B)*{count}',
        'INPUT_A': max_raster,
        'INPUT_B': layer,
        'INPUT_C': toc_raster,
        'INPUT_D': None,
        'INPUT_E': None,
        'INPUT_F': None,
        'NO_DATA': None,
        'OPTIONS': '',
        'RTYPE': 5,  # Float32
        'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
    }
    return processing.run('gdal:rastercalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

The log shows this error but I can load the files using the filepaths below for A, B and C. I cannot load the outfile but I assume that is because it is not created:

Processing raster 0
Updating time of concentration raster
GDAL command:
gdal_calc.bat --overwrite --calc "(A>=B)*C+(A<B)*2" --format GTiff --type Float32 -A "J:/2_Invercargill Jobs/S01500-S01599/S01555_Riverton/300_Riverton_Model/006_Design/Tuflow/2_Model/results/2d/Grids/RIV_001_100yr+1HR+T20SLR300_8m_d_g001_Max.asc" --A_band 1 -B "J:/2_Invercargill Jobs/S01500-S01599/S01555_Riverton/300_Riverton_Model/006_Design/Tuflow/2_Model/results/2d/Grids/RIV_001_100yr+2HR+T20SLR300_8m_d_g001_Max.asc" --B_band 1 -C C:/Users/chcgis/AppData/Local/Temp/processing_SQFrqz/133e2c3a47ca490d91bd8edca7c05376/OUTPUT.tif --outfile C:/Users/chcgis/AppData/Local/Temp/processing_SQFrqz/037f8b08b5a248f08d8fc0203e0137a4/OUTPUT.tif
GDAL command output:
The filename, directory name, or volume label syntax is incorrect.
Process returned error code 1
2
  • 2
    Test if it works from a path without spaces ...2_Invercargill Jobs...
    – user30184
    Apr 11, 2022 at 11:01
  • No luck, still get the same error: GDAL command output: The filename, directory name, or volume label syntax is incorrect. Process returned error code 1 Apr 11, 2022 at 21:50

1 Answer 1

1

Not really an answer but I ended up switching to using the grass calculator (r.mapcalc.simple) which worked just fine

def updateTocRaster(self, toc_raster, max_raster, layer, count, context, feedback):
    # r.mapcalc.simple
    alg_params = {
        'GRASS_RASTER_FORMAT_META': '',
        'GRASS_RASTER_FORMAT_OPT': '',
        'GRASS_REGION_CELLSIZE_PARAMETER': 0,
        'GRASS_REGION_PARAMETER': None,
        'a': max_raster,
        'b': layer,
        'c': toc_raster,
        'd': None,
        'e': None,
        'expression': f'(A>=B)*C+(B<A)*{count}',
        'f': None,
        'output': QgsProcessing.TEMPORARY_OUTPUT
    }
    return processing.run('grass7:r.mapcalc.simple', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.