I am rasterizing a vector layer in a python console in QGIS, but I need to rasterize only on a selection of the input layer. My code looks like this.

    shape = r"C:\....\vectorlayer.shp"

    layer = QgsVectorLayer(shape, 'shape1')

    extents = layer.extent()

    xmin = extents.xMinimum()

    xmax = extents.xMaximum()

    ymin = extents.yMinimum()

    ymax = extents.yMaximum()

    alg = 'gdal:rasterize'

    params={"INPUT" : layer,"FIELD":None,"BURN":3,"UNITS":1,"WIDTH":20,"HEIGHT":20,"EXTENT" : "%f,%f,%f,%f"% (xmin, xmax, ymin, ymax),"NODATA":0,"DATA_TYPE":1,"INVERT":0,"OUTPUT" : r'C:\.....\Raster.tif'}

    test = processing.run(alg, params)


I tried to make the query in several ways.

    query = '"select * from {layer} where attribute" = 3'

    selection = layer.getFeatures(QgsFeatureRequest().setFilterExpression(query))
    layer.select([k.id() for k in selection])

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.