3

What I am trying to do is looping trough the active vector layers features and create for each polygon a rasterfile.

My approach is using gdal:rasterize in every iteration, but it wont work at the moment.

from qgis.core import *
from qgis import processing
import qgis.utils
from qgis.core import QgsVectorLayer
import os

outfolder = r'/home/user/Schreibtisch/raster'

activeLayer = qgis.utils.iface.activeLayer()
activeLayerFeatures = activeLayer.getFeatures()

for e, polygonFeature in enumerate(activeLayerFeatures):
    activeLayer.select(polygonFeature.id())
    outname = 'Polygon_{}.tif'.format(e)

    processing.run("gdal:rasterize", 
        {'INPUT':QgsProcessingFeatureSourceDefinition(activeLayer.id(), True),
        'FIELD':'','BURN':99,'UNITS':1,'WIDTH':1000,'HEIGHT':1000,
        'EXTENT':'-1.22232621099999994,-0.68586617299999997,1.18935233600000001,0.567117121',
        'NODATA':0,'OPTIONS':'','DATA_TYPE':5,'INIT':None,'INVERT':False,'EXTRA':'','OUTPUT':os.path.join(outfolder,outname)}) 
activeLayer.removeSelection()

First of all, is this the right approach? Or will this never work?

Anyway, when I run this script, I get the error

ERROR 1: Attempt to create 0x0 dataset is illegal,sizes must be larger than zero.

I tried playing around with the extend, which I copied out of the vectorlayers metadata, and the resolution, but it wont work.

1
  • That error means that, based on the inputs you have provided (units, width, height and extent) you are telling gdal to write a raster which is less than a single pixel in size. So there is something wrong with those parameters. What CRS is your polygon vector in?
    – Ben W
    Jul 1, 2021 at 3:12

0

Your Answer

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