5

Say I have an in-memory raster (i.e. a raster made by the MEM gdal driver), how can I load this raster into a QGIS layer? QgsRasterLayer's constructor wants a path to the raster file, but the in-memory raster doesn't have one.

Creating in-memory raster layer using PyQGIS? doesn't quite cover it as the solution seems to be to avoid creating your own MEM raster altogether.

If there is a way to load a numpy array as a QGIS raster layer, that would work too. I'm just trying to avoid having to write to the disk in case of a rapidly-changing raster.

1

This has not been tested, but I would take the following approach: Instead of trying to load the data during the construction of the raster layer, load the data first into a Data Provider. This DataProvider can then be loaded into a raster layer.

The first step would be to create an empty QgsRasterDataProvider (i.e. don't provide any parameters). The documentation (here) shows a few potential methods to load the data afterwards:

If this worked, you are now ready to create an empty QgsRasterLayer and call its setDataProvider(provider) function (docs).

I hope this proves useful. Good luck!

Your Answer

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

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