7

I have some computation on a numpy array and want to display the result in QGIS. So far ok, but loading the result into QGIS fails. Here my code fragment:

zData2 = np.subtract(myarray,zData2) #do some computes
output_raster = gdal.GetDriverByName('MEM').Create('',ncols, nrows, 1 ,gdal.GDT_Float32)  
output_raster.GetRasterBand(1).WriteArray(zData2)   # Writes my array to the raster

newLayer = QgsRasterLayer(output_raster,'my new raster') #this failes...
QgsMapLayerRegistry.instance().addMapLayer(newLayer)
5
  • How exactly does it fail? Commented Dec 1, 2016 at 10:42
  • arguments did not match any overloaded call: QgsRasterLayer(): too many arguments QgsRasterLayer(QString, QString baseName=QString.null, bool loadDefaultStyleFlag=True): argument 1 has unexpected type 'Dataset' QgsRasterLayer(QString, QString, QString, bool loadDefaultStyleFlag=True): argument 1 has unexpected type 'Dataset' See log for more details
    – eurojam
    Commented Dec 1, 2016 at 11:13
  • 1
    QgsRasterLayer is expecting a string corresponding to the path, for example it works with a physical TIFF file. So I guess that is the question, how to do it with a 'MEM' dataset.
    – Benjamin
    Commented Dec 19, 2016 at 20:47
  • 1
    check this gis.stackexchange.com/questions/34082/…
    – Fran Raga
    Commented Apr 25, 2019 at 17:50
  • The solution I normally use is to create raster file (persists my data) with my calculations and then load it as a QgsRasterLayer.
    – lcoandrade
    Commented Jun 9, 2019 at 23:00

0

Browse other questions tagged or ask your own question.