3

I am trying to make a simple plugin, from the input as a vector layer then processed with a Field Calculator function (But I want the ouput from this process to be saved in temporary memory or in other words the output to enter the next step function as an input) after that I want to do a Rasterized function.

I have tried to create the code :

def setVariables(self):

    self.rooftop = self.getRooftopLayer()
    self.rain = self.getRainfallLayer()
    self.potvol = self.dlg.le_potvol.text()


def Rasterize (self):


    alg_params = {
        'FIELD_LENGTH': 10,
        'FIELD_NAME': 'area',
        'FIELD_PRECISION': 3,
        'FIELD_TYPE': 1,
        'FORMULA': ' $area ',
        'INPUT': self.rooftop,
        'NEW_FIELD': True,
        'OUTPUT': QStringLiteral( "TEMPORARY_OUTPUT" )
    }

    outputs ['FieldCalculator'] = processing.run('qgis:fieldcalculator', alg_params, feedback=feedback)

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

    alg_params = {
        'BURN': 0,
        'DATA_TYPE': 5,
        'EXTENT': outputs['FieldCalculator']['OUTPUT'],
        'FIELD': 'area',
        'HEIGHT': 30,
        'INIT': None,
        'INPUT': outputs['FieldCalculator']['OUTPUT'],
        'INVERT': False,
        'NODATA': 0,
        'OPTIONS': '',
        'UNITS': 0,
        'WIDTH': 30,
        'OUTPUT': self.potvol
    }

    outputs['RasterizeVectorToRaster'] = processing.run('gdal:rasterize', alg_params, feedback=feedback)


def addLayers(self):

    self.iface.addRasterLayer(self.potvol, str.split(os.path.basename(self.potvol), ".")[0])

What statement I should add?

1 Answer 1

1

Add next lines. In your case you may want to add to def Rasterize(self):

def Rasterize(self):

    outputs = {} # ADD THIS LINE

    ### Your processing lines
    outputs['process1_name'] = processing.run(...)
    #...
    outputs['process2_name'] = processing.run(...)
    ###

    # Add next lines to the same scope/block with 'outputs[".."] =' and customize
    folder = "c:/ADD_A_FOLDER_PATH/" # remember last slash
    for k, v in outputs.items():
        mem_layer = v['OUTPUT']
        path = folder + k + ".shp" # k: process name
        QgsVectorFileWriter.writeAsVectorFormat(mem_layer,
                                                path,
                                                None,
                                                mem_layer.crs(),
                                                "ESRI Shapefile")
    ####
7
  • Hi. I have tried but still shows an error #FieldCalculator Process processing.run ('qgis:fieldcalculator',...'OUTPUT': "TEMPORARY_OUTPUT" }) folder = "C:\Iqbal\Thesis Plugin\rwh_yield/" for k, v in outputs.items(): mem_layer = v['OUTPUT'] path = folder + k ['FieldCalculator']+ ".shp" QgsVectorFileWriter.. #Rasterized process using input from Field Calculator processing.run('gdal:rasterize',...'EXTENT': mem_layer,..'INPUT': mem_layer,...'OUTPUT': self.potvol }) It showed "NameError: name 'outputs' is not defined" Commented Dec 17, 2019 at 10:08
  • Use / (forward slash) (or \\ two back slahes) in path or add r before path. Please review this answer: stackoverflow.com/a/2953843/5569709 Commented Dec 17, 2019 at 10:13
  • The error refered to the "outputs is not defined in for k, v in outputs.items(): Commented Dec 17, 2019 at 10:13
  • Did you add outputs = {} line before using it? Commented Dec 17, 2019 at 10:17
  • ah yes! but now is coming another error: UnboundLocalError: local variable 'mem_layer' referenced before assignment Commented Dec 17, 2019 at 10:20

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.