0

As the title says, in QGIS 3.6 I made a model calculating "Forest fire susceptibility index - RC", model works fine and gives correct values. But when I export this model as Python script and run it the final result is false. Instead of giving values min: 32, max:110, it gives 1.79769e+308 and -1.79769e+308.

I need this Python script to work as model itself. Log doesn't report any error, it is a bit different though than log after running model. My guess is that something is wrong with line 132 with raster calculator. Here is the complete script:

from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterRasterLayer
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterRasterDestination
import processing


class ForestFiresSusceptibilityIndexRc(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterRasterLayer('a', 'A', defaultValue=None))
        self.addParameter(QgsProcessingParameterVectorLayer('dr', 'Dr', types=[QgsProcessing.TypeVectorAnyGeometry], defaultValue=None))
        self.addParameter(QgsProcessingParameterVectorLayer('ds', 'Ds', types=[QgsProcessing.TypeVectorAnyGeometry], defaultValue=None))
        self.addParameter(QgsProcessingParameterRasterLayer('s', 'S', defaultValue=None))
        self.addParameter(QgsProcessingParameterVectorLayer('vt', 'Vt', types=[QgsProcessing.TypeVectorAnyGeometry], defaultValue=None))
        self.addParameter(QgsProcessingParameterRasterDestination('RcIndexSusceptibilityClasses', 'RC index - susceptibility classes', createByDefault=True, defaultValue=None))
        self.addParameter(QgsProcessingParameterRasterDestination('RcIndex', 'RC index', createByDefault=True, 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(7, model_feedback)
        results = {}
        outputs = {}

        # Reclassify by table - A
        alg_params = {
            'DATA_TYPE': 5,
            'INPUT_RASTER': parameters['a'],
            'NODATA_FOR_MISSING': False,
            'NO_DATA': -9999,
            'RANGE_BOUNDARIES': 1,
            'RASTER_BAND': 1,
            'TABLE': [0,0.00000001,2,0.00000001,22.5,1,22.5,67.5,2,67.5,112.5,3,112.5,157.5,4,157.5,202.5,5,202.5,247.5,4,247.5,292.5,3,292.5,337.5,2,337.5,400,1],
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['ReclassifyByTableA'] = processing.run('native:reclassifybytable', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

        feedback.setCurrentStep(1)
        if feedback.isCanceled():
            return {}

        # Rasterize (vector to raster) - Dr
        alg_params = {
            'BURN': 0,
            'DATA_TYPE': 5,
            'EXTENT': parameters['dr'],
            'FIELD': 'Vrednost',
            'HEIGHT': 25,
            'INIT': None,
            'INPUT': parameters['dr'],
            'INVERT': False,
            'NODATA': -9999,
            'OPTIONS': '',
            'UNITS': 1,
            'WIDTH': 25,
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['RasterizeVectorToRasterDr'] = processing.run('gdal:rasterize', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

        feedback.setCurrentStep(2)
        if feedback.isCanceled():
            return {}

        # Rasterize (vector to raster) - Ds
        alg_params = {
            'BURN': 0,
            'DATA_TYPE': 5,
            'EXTENT': parameters['ds'],
            'FIELD': 'Vrednost',
            'HEIGHT': 25,
            'INIT': None,
            'INPUT': parameters['ds'],
            'INVERT': False,
            'NODATA': -9999,
            'OPTIONS': '',
            'UNITS': 1,
            'WIDTH': 25,
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['RasterizeVectorToRasterDs'] = processing.run('gdal:rasterize', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

        feedback.setCurrentStep(3)
        if feedback.isCanceled():
            return {}

        # Reclassify by table - S
        alg_params = {
            'DATA_TYPE': 5,
            'INPUT_RASTER': parameters['s'],
            'NODATA_FOR_MISSING': False,
            'NO_DATA': -9999,
            'RANGE_BOUNDARIES': 1,
            'RASTER_BAND': 1,
            'TABLE': [0,5,1,5,10,2,10,25,3,25,35,4,35,100,5],
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['ReclassifyByTableS'] = processing.run('native:reclassifybytable', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

        feedback.setCurrentStep(4)
        if feedback.isCanceled():
            return {}

        # Rasterize (vector to raster) - Vt
        alg_params = {
            'BURN': 0,
            'DATA_TYPE': 5,
            'EXTENT': parameters['vt'],
            'FIELD': 'Vrednost',
            'HEIGHT': 25,
            'INIT': None,
            'INPUT': parameters['vt'],
            'INVERT': False,
            'NODATA': -9999,
            'OPTIONS': '',
            'UNITS': 1,
            'WIDTH': 25,
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['RasterizeVectorToRasterVt'] = processing.run('gdal:rasterize', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

        feedback.setCurrentStep(5)
        if feedback.isCanceled():
            return {}

        # Raster calculator - RC index
        alg_params = {
            'CELLSIZE': 25,
            'CRS': 'ProjectCrs',
            'EXPRESSION': '7 * \"\'Rasterized\' from algorithm \'Rasterize (vector to raster) - Vt\'@1\" + 5 *  ( \"\'Reclassified raster\' from algorithm \'Reclassify by table - S\'@1\" + \"\'Reclassified raster\' from algorithm \'Reclassify by table - A\'@1\" )  + 3 *  ( \"\'Rasterized\' from algorithm \'Rasterize (vector to raster) - Dr\'@1\" + \"\'Rasterized\' from algorithm \'Rasterize (vector to raster) - Ds\'@1\" ) ',
            'EXTENT': outputs['RasterizeVectorToRasterVt']['OUTPUT'],
            'LAYERS': [outputs['RasterizeVectorToRasterDr']['OUTPUT'],outputs['RasterizeVectorToRasterDs']['OUTPUT'],outputs['RasterizeVectorToRasterVt']['OUTPUT'],outputs['ReclassifyByTableA']['OUTPUT'],outputs['ReclassifyByTableS']['OUTPUT']],
            'OUTPUT': parameters['RcIndex']
        }
        outputs['RasterCalculatorRcIndex'] = processing.run('qgis:rastercalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['RcIndex'] = outputs['RasterCalculatorRcIndex']['OUTPUT']

        feedback.setCurrentStep(6)
        if feedback.isCanceled():
            return {}

        # Reclassify by table - RC index
        alg_params = {
            'DATA_TYPE': 5,
            'INPUT_RASTER': outputs['RasterCalculatorRcIndex']['OUTPUT'],
            'NODATA_FOR_MISSING': False,
            'NO_DATA': -9999,
            'RANGE_BOUNDARIES': 0,
            'RASTER_BAND': 1,
            'TABLE': [0,60,1,60,75,2,75,90,3,90,1000,4],
            'OUTPUT': parameters['RcIndexSusceptibilityClasses']
        }
        outputs['ReclassifyByTableRcIndex'] = processing.run('native:reclassifybytable', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['RcIndexSusceptibilityClasses'] = outputs['ReclassifyByTableRcIndex']['OUTPUT']
        return results

    def name(self):
        return 'Forest Fires Susceptibility Index - RC'

    def displayName(self):
        return 'Forest Fires Susceptibility Index - RC'

    def group(self):
        return 'GIS programiranje'

    def groupId(self):
        return 'GIS programiranje'

    def createInstance(self):
        return ForestFiresSusceptibilityIndexRc()
2
  • How are you running the stand alone script? Commented May 27, 2020 at 16:31
  • I am running it through QGIS 3.6.
    – Bane
    Commented May 28, 2020 at 17:14

1 Answer 1

1

The problem was/is definitely with raster calculator. So I overcame this problem by changing gdal's raster calculator with raster calculator provided by SAGA. The setup is a bit different, but overall it works. There is one strange issue though, sometimes code won't shoot results, but when I close and open QGIS and run it again immediately, it works. So this obviously needs some work, but if it can help someone I wanted to post this answer.

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.