Bug report #21586

Windows and Processing Raster calculator pyqgis troubles

Added by matteo ghetta 9 months ago. Updated 7 months ago.

Status:Open
Priority:High
Assignee:Victor Olaya
Category:Python bindings / sipify
Affected QGIS version:3.4.5 Regression?:No
Operating System:Windows Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:29402

Description

The bug seems to be related ONLY on Windows system. A small dataset in attach.

Briefly: it seems that when using the Processing Raster calculator in Windows via pyqgis, it is not possible to use raster(s) entire path(s). Only raster names loaded in the TOC are available.

To reproduce the error:

  • load the attached `calculated` raster in the TOC
  • run this script (that uses gdal_proximity on the loaded raster and then the output of gdal_proximity is used in the raster calculator)
param_proximity = {
    'INPUT': QgsProject.instance().mapLayersByName('calculated')[0],
    'BAND': 1,
    'UNITS' : 0,
    'OUTPUT': os.path.join(output, 'distance.tif')
}

proximity = processing.run("gdal:proximity", param_proximity)

param_calculated = {
    'EXPRESSION':'{}@1 * {}'.format(proximity['OUTPUT'], formula),
    'LAYERS': proximity['OUTPUT'],
    'OUTPUT': os.path.join(output, 'calcolato.tif')
}

calculated = processing.run("qgis:rastercalculator", param_calculated)

iface.addRasterLayer(calculated['OUTPUT'], 'final')

This is working on Linux and on QGIS 3.4, 3.6 and master while is ALWAYS failing in Windows. To make it working on Windows some lines have to be added:

param_proximity = {
    'INPUT': QgsProject.instance().mapLayersByName('calculated')[0],
    'BAND': 1,
    'UNITS' : 0,
    'OUTPUT': os.path.join(output, 'distance.tif')
}

proximity = processing.run("gdal:proximity", param_proximity)
QgsProject.instance().addMapLayer(QgsRasterLayer(proximity['OUTPUT'], 'dummy'))
rl = QgsProject.instance().mapLayersByName('dummy')[0]

param_calculated = {
    'EXPRESSION':'{}@1 * {}'.format(rl.name(), formula),
    'LAYERS': rl.name(),
    'OUTPUT': os.path.join(output, 'calcolato.tif')
}

calculated = processing.run("qgis:rastercalculator", param_calculated)

iface.addRasterLayer(calculated['OUTPUT'], 'final')

Fianlly, but still extremely important, with the same data the result is completely different. On Windows there are some super strange values (1.79769e+308 adn -1.79769e+308): some serious trouble with NODATA values?

calculated.tif (141 KB) matteo ghetta, 2019-03-14 04:00 PM

History

#1 Updated by Giovanni Manghi 9 months ago

  • Status changed from Open to Feedback

Fianlly, but still extremely important, with the same data the result is completely different. On Windows there are some super strange values (1.79769e+308 adn -1.79769e+308): some serious trouble with NODATA values?

Hi Matteo, if this is a separate problem then better file it as a separate ticket.

#2 Updated by matteo ghetta 9 months ago

Hi Giovanni.. Well, yes it is, actually difficult to replicate (I cannot understand why this is happening and when). IMHO everything is caused by the wrong misinterpretation of the raster path source. If you don't mind I'll leave it in this ticket or I can remove that part if it is confusing.

#3 Updated by Giovanni Manghi 9 months ago

matteo ghetta wrote:

IMHO everything is caused by the wrong misinterpretation of the raster path source.

if this is the case then of course is ok to leave as is.

#4 Updated by Giovanni Manghi 9 months ago

  • Status changed from Feedback to Open

#5 Updated by Victor Olaya 7 months ago

  • Assignee set to Victor Olaya

#6 Updated by Victor Olaya 7 months ago

Looks like the error is not in the Processing side. Everything seems to be correctly created, but when passed to the core class QgsRasterCalculator, that's where the wrong handling seems to be happening. In other words, calling QgsRasterCalculator with filenames instead of layer name (in Windows, at least), doesn't work. Not sure if the class is supposed to support files, that info is not in its documentation.

#7 Updated by Giovanni Manghi 7 months ago

  • Category changed from Processing/QGIS to Python bindings / sipify

Also available in: Atom PDF