1

I want to create a very simple script in python that polygonizes a raster. Based on python cookbook + forums search I wrote the following code

#import des modules
from osgeo import gdal, ogr
from qgis.core import *
from qgis.gui import *
import sys
from PyQt4.QtCore import * 

##srcRaster=Raster
##Result=output vector

# 1. Open Raster and get band 1
src_ds = gdal.Open(srcRaster)
srcband = src_ds.GetRasterBand(1)

# 2. Create output datasource
Result = QgsVectorLayer("Polygon", "temporary_points", "memory")
Result.startEditing()  
Result.dataProvider().addAttributes([QgsField("ClassNo",     QVariant.String)])
Result.updateFields()
gdal.Polygonize( srcband, None, Result, 0, [], callback=None )

which ends up with the error "in method 'Polygonize', argument 3 of type 'OGRLayerShadow *". The truth is, I don't really understand what should be the syntax of gdal_polygonize in a qgis python script (command line can be found here).

Could anybody please help ?

3
  • 2
    the third parameter needs to be an instance of the GDAL/OGR vector layer class; you're passing in an instance of the QGIS vector layer class... not the same thing! You might find it easier to call the gdal polygonalize using processing.runalg('gdalogr:polygonize', input, field, output)
    – Steven Kay
    Nov 6, 2015 at 22:30
  • oh thanks. processing.runalg('gdalogr:polygonize', input, field, output) did work. No need to take the first band out. no need to create the polygon layer prior to run polygonize either.
    – user16542
    Nov 7, 2015 at 6:44
  • @StevenKay - You should post your comment as an answer :)
    – Joseph
    Nov 10, 2015 at 11:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.