I have been researching quite a bit for creating in-memory raster layer using PyQGIS. i have done it through gdal "memory" layer. But i need QgsRasterLayer instance for displaying. I don't have the option of saving the results to disk and reading it again.
I tried doing this:
target_ds = gdal.GetDriverByName('MEM').Create('ras', x_count, y_count, 1, gdal.GDT_Int32)
target_ds= processing.runalg("gdalogr:rasterize",
{"INPUT": ellipse_shp,
"FIELD": "id1",
"DIMENSIONS": 0,
"WIDTH": x_count,
"HEIGHT": y_count,
"RAST_EXT": "%f,%f,%f,%f" % (x_min, x_max, y_min, y_max),
"TFW": 1,
"RTYPE": 4,
"NO_DATA": 1,
"COMPRESS": 0,
"JPEGCOMPRESSION": 1,
"ZLEVEL": 1,
"PREDICTOR": 1,
"TILED": False,
"BIGTIFF": 2,
"EXTRA": '',
"OUTPUT": "aa"})
But when i wanted to use it as:
target_ds["OUTPUT"]
it gave me an error as
TypeError: str cannot be converted to qgis._core.QgsRasterLayer in this context
Although QGIS documentation says it supports all gdal sources, I couldn't make it work.
Can anyone help me?
Best Answer
Effectively, I see that you have set the string
"aa"
as output. If you want to use your result for further steps, you have to set the last algorithm parameter asNone
.Firstly, you need to change this:
to this:
and then you may handle your result in this way:
Now it works?