Thursday 30 January 2020

pyqgis - python script in QGis - help using gdal.Polygonize


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 ?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...