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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Eric Younkin - NOAA Federal
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:


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
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: PYQGIS - Numpy array to QgsRasterLayer without writing to file first

Benjamin Jakimow

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:
 
 
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
[hidden email]
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: [hidden email]

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

_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: PYQGIS - Numpy array to QgsRasterLayer without writing to file first

Eric Younkin - NOAA Federal
Hi Benjamin,

This does appear to work, I am now able to use vsimem file handlers with gdal rasters to go from numpy arrays to qgis raster layers effectively.  Very exciting, thanks for this tip!  I had another QGIS dev question, but I will start a new thread for that.

For anyone else, this is all that I did:

dataset = gdal_driver.Create('/vsimem/imagetst.tif', cols, rows, no_bands, gdal.GDT_Float32, creation_options)
...write array to band, set geotransform, etc....

rlayer = QgsRasterLayer('/vsimem/imagetst.tif', 'imagetst', 'gdal')

Thanks again,
Eric

On Wed, Mar 3, 2021 at 12:57 AM Benjamin Jakimow <[hidden email]> wrote:

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:
 
 
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
[hidden email]
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: [hidden email]

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


--
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
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer