2

I am writing a processing script using QGIS 3.4. I need to rasterize a polygon using gdal.RasterizeLayer. The problem is the polygon is a list of QgsGeometry objects and not a layer.

n = (2*radiusInt)+1
epsg = bldgs_lyr.sourceCrs().authid()[5:]
srs = osr.SpatialReference()
srs.ImportFromEPSG(int(epsg))
geotransform = (sourceX-radius, 1, 0, sourceY+radius, 0, -1)

output_rast = gdal.GetDriverByName('GTiff').Create(raster_uri, n, n, 1, gdal.GDT_Float32)
output_rast.SetGeoTransform(geotransform)
output_rast.SetProjection(srs.ExportToWkt())

mem_layer = QgsVectorLayer("Polygon?crs="+bldgs_lyr.sourceCrs().authid(), 'polygon', 'memory')
prov = mem_layer.dataProvider()
for geom in geometries:
    feat = QgsFeature()
    feat.setGeometry(geom)
    prov.addFeatures([feat])

gdal.RasterizeLayer(output_rast, [1], mem_layer, burn_values=1)

As you can see, I've tried creating a vector layer to use as input. But if I run this, QGIS will just crash.

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.