[QGIS-Developer] PYQGIS - Numpy array to QgsRasterLayer without writing to file first

Benjamin Jakimow benjamin.jakimow at geo.hu-berlin.de
Tue Mar 2 21:57:39 PST 2021


Hi Eric,

you might try to use a /vsimem/ path. For example, save your image to 
"/vsimem/myimage.tif" and
open it as QgsRasterLayer. I haven't tested it with gdal.Datasets, but 
it works with with ogr.DataSources.
Probably you shouldn't overwrite or delete the "/vsimem" object while 
QGIS has still a reference on it.

More details here: 
https://gdal.org/user/virtual_file_systems.html#vsimem-in-memory-files

Greetings,

Benjamin

On 2021-03-03 00:00, Eric Younkin - NOAA Federal wrote:

> Hello,
> 
> I've been trying to figure out how to draw grids and lines from numpy 
> arrays without driving to a raster/vector file first.  I tried using 
> the gdal 'MEM' driver to make a gdal Dataset from a numpy array and 
> pass that in as a QgsRasterLayer, I get the following:
> 
>>>> memobj
> <osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow 
> *' at 0x0000019B3131AC30> >
>>>> QgsRasterLayer(memobj, 'testlayer', 'gdal') Traceback (most recent 
>>>> call last):
> File "<input>", line 1, in <module>
> TypeError: QgsRasterLayer(): arguments did not match any overloaded 
> call:
> overload 1: too many arguments
> overload 2: argument 1 has unexpected type 'Dataset'
> 
> The only other approach that I could find was:
> 
> https://gis.stackexchange.com/questions/201804/how-to-create-raster-layer-directly-from-numpy-array-in-qgis-without-saving-to
> 
> but I suspect that the return from the processing.run command is 
> probably a temp file, so it is still going to file.  Is there not a way 
> to generate a raster layer from a numpy array without writing to file 
> first?
> 
> Thanks,
> Eric
> --
> 
> Eric Younkin
> Physical Scientist
> 
> NOAA OCS, Hydrographic Systems and Technology Branch
> 1315 East-West Highway
> N/CS11, Room 6604
> Silver Spring, MD 20910
> Office: 240-847-8208
> Cell: 828-331-8197
> 
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

-- 
--
Benjamin Jakimow, Doctoral Researcher
Earth Observation Lab | Geography Department | Humboldt-Universität zu 
Berlin

e-mail: benjamin.jakimow at geo.hu-berlin.de

phone:  +49 (0) 30 2093 6894
mobile: +49 (0) 157 5656 8477
fax:    +49 (0) 30 2093 6848
mail:   Unter den Linden 6 | 10099 Berlin | Germany
room: 2'222
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20210303/feae6ccc/attachment-0001.html>


More information about the QGIS-Developer mailing list