I would like to calculate covariance matrices between different bands of the same raster in QGIS (2 or 3). I have found only one built-in processing tool capable of doing this, named r.covar from GRASS.
Unfortunately, in QGIS this tool is structured to only accept "entire" raster layers as input, while in GRASS it is indeed possible to calculate covariance matrices between bands of the same raster dataset.
I have tried to trick PyQGIS to accepting GDAL's bands as inputs, but it only accepts raster layers. Does anyone have a solution to this problem or perhaps a user script that does creates covariance and correlation matrices?
I would strongly prefer not to save each band as a separate file strictly for this problem.
Sample code:
import processing,gdal
rasterLayer = iface.activeLayer()
ds = gdal.Open("C:\\Multispectral_Clipped.tif")
outputRaster= "C:\\Output.html"
extent =rasterLayer.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()
#Not working option
output=processing.runalg('grass7:r.covar', [ds.GetRasterBand(1),ds.GetRasterBand(2), ds.GetRasterBand(3)],True,"%f,%f,%f,%f" %(xmin, xmax, ymin, ymax),outputRaster,None)
#Working option
output=processing.runalg('grass7:r.covar', [rasterLayer],True,"%f,%f,%f,%f" %(xmin, xmax, ymin, ymax),outputRaster,None)
EDIT I suspect my request above is not possible. So the question is then just how to run r.covar with multiple inputs in PyQGIS (QGIS 2.18)?
Even with having the line:
output=processing.runalg('grass7:r.covar', [rasterList],True,"%f,%f,%f,%f" %(xmin, xmax, ymin, ymax),outputHtml,outputRaw)
And the rasterList containing a Python list like so: ['C:\\band1.tiff', 'C:\\band2.tiff', 'C:\\band3.tiff']
I am getting a Wrong parameter value error. I've also tried loading them as QgsRasterLayer, like done here with the StringToRaster function, but got the same error.
outputHtmlandoutputRaw? Does it work if you replace these withNone? – Joseph Mar 29 '18 at 11:28outputHtml = "C:\\correlation_output.html"andoutputRaw = "C:\\correlation_output.txt". I tried replacing them withNone, it still doesn't work. Furthermore, the error saysWrong parameter value: ['C:\\band1.tiff', 'C:\\band2.tiff', 'C:\\band3.tiff'], pointing to the input error there. – 15Step Mar 29 '18 at 11:36.tifor.tiff? – Joseph Mar 29 '18 at 11:42for i in range (1,ds.RasterCount+1): srcband = ds.GetRasterBand(i) out_ds = gdal.Translate(out_path + 'band' + str(i) + '.tiff', ds, format='GTiff', bandList=[i]) out_ds=NoneI tried saving them as .tif, but got the same error. Also, r.covar works in the interface with the rasters, both as .tif or tiff. – 15Step Mar 29 '18 at 11:55