5

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?

1

1 Answer 1

1

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 as None.

Firstly, you need to change this:

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"})

to this:

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": None})

and then you may handle your result in this way:

# You may use any name instead of 'result'
result = processing.getObject(target_ds["OUTPUT"])

Now it works?

1
  • 1
    Is "OUTPUT": None still supposed to work in QGIS 3.18? It tells me that it got a bad parameter value. And processing.getObject does not exist. Commented May 21, 2021 at 16:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.