TransWikia.com

Creating Contours from Raster using PyQGIS

Geographic Information Systems Asked by Zhifang Hu on June 10, 2021

I am trying to create contours from a raster layer I have loaded in my QGIS project as below using gdal

from qgis.core import QgsProject
import processing

## QgsVectorLayer('Coordinate','Layer name','memory layer')
layer = QgsVectorLayer('LineString?crs=epsg:28356', 'Contour' , 'memory')
 
for raster in QgsProject.instance().mapLayers().values():
    if 'h_Max' in raster.name():
        processing.run('gdal:contour', raster, 1, 'ELEV' , layer)

I’ve never had any chance of getting the code to work, the error message I got is

AttributeError: 'QgsVectorLayer' object has no attribute 'feedback'

I am not sure when I should define this feedback attribute, any suggestion?

One Answer

You need to pass the algorithm parameters as a dictionary.

Try something like:

raster = [l for l in QgsProject().instance().mapLayers().values() if isinstance(l, QgsRasterLayer) and 'h_Max' in l.name()][0]

processing.runAndLoadResults("gdal:contour", 
    {'INPUT':raster,
    'BAND':1,
    'INTERVAL':10,# adjust interval as required
    'FIELD_NAME':'ELEV',
    'OUTPUT':'TEMPORARY_OUTPUT'})

By executing processing.algorithmHelp("gdal:contour") you may also get additional information about the algorithm:

Contour (gdal:contour)


----------------
Input parameters
----------------

INPUT: Input layer

    Parameter type: QgsProcessingParameterRasterLayer

    Accepted data types:
        - str: layer ID
        - str: layer name
        - str: layer source
        - QgsProperty
        - QgsRasterLayer

BAND: Band number

    Parameter type: QgsProcessingParameterBand

    Accepted data types:
        - int
        - QgsProperty

INTERVAL: Interval between contour lines

    Parameter type: QgsProcessingParameterNumber

    Accepted data types:
        - int
        - float
        - QgsProperty

FIELD_NAME: Attribute name (if not set, no elevation attribute is attached)

    Parameter type: QgsProcessingParameterString

    Accepted data types:
        - str
        - QgsProperty

CREATE_3D: Produce 3D vector

    Parameter type: QgsProcessingParameterBoolean

    Accepted data types:
        - bool
        - int
        - str
        - QgsProperty

IGNORE_NODATA: Treat all raster values as valid

    Parameter type: QgsProcessingParameterBoolean

    Accepted data types:
        - bool
        - int
        - str
        - QgsProperty

NODATA: Input pixel value to treat as "nodata"

    Parameter type: QgsProcessingParameterNumber

    Accepted data types:
        - int
        - float
        - QgsProperty

OFFSET: Offset from zero relative to which to interpret intervals

    Parameter type: QgsProcessingParameterNumber

    Accepted data types:
        - int
        - float
        - QgsProperty

EXTRA: Additional command-line parameters

    Parameter type: QgsProcessingParameterString

    Accepted data types:
        - str
        - QgsProperty

OPTIONS: Additional creation options

    Parameter type: QgsProcessingParameterString

    Accepted data types:
        - str
        - QgsProperty

OUTPUT: Contours

    Parameter type: QgsProcessingParameterVectorDestination

    Accepted data types:
        - str
        - QgsProperty
        - QgsProcessingOutputLayerDefinition

----------------
Outputs
----------------

OUTPUT:  <QgsProcessingOutputVectorLayer>
    Contours

Answered by Ben W on June 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP