2

I want to do simple raster calculations on approx. 1'000 small rasters in a standalone script in QGIS. I verified the code several times with various SE answers and youtube tutorials. The same calculation in the raster calculator GUI works properly. In the QGIS python console however, it always throws the result "1" (CreateOutputError) to me and does not create any output raster. I tried to change input file and output directory in order to check if there was an issue with my tif-file or with the drive I wanted to save the output to. I don't know what else I could test? In some forums, there was an issue with raster calculator on QGIS 3.10 reported. Any hint?

My code:

    raster_layer = QgsRasterLayer('C:\Users\baer\Desktop\GIS_temp\area_0.tif', 'ras')
    output = r"C:\Users\baer\Desktop\GIS_temp\area_0_red.tif"

    entries = []
    ras = QgsRasterCalculatorEntry()
    ras.ref = raster_layer.name() + '@1'
    ras.raster = raster_layer
    ras.bandNumber = 1
    entries.append(ras)

    calc = QgsRasterCalculator('"ras@1" + 100', output, 'GTiff', raster_layer.extent(),raster_layer.width(), raster_layer.height(), entries)

    calc.processCalculation()

Result was always:

    >>> calc.processCalculation()
    1

I also tried to add

    context = QgsCoordinateTransformContext()

which didn't help. Also varying the calculation expression didn't change anything. Enabling OpenCL acceleration, resulted in the output to be "6" (BandError) instead of "1" (CreateOutputError), but still no output raster.

3
  • 1
    Check calc.lastError()
    – user2856
    Jan 27, 2021 at 7:47
  • You can find a full list of error codes in qgis.org/api/classQgsRasterCalculator.html
    – root676
    Jan 27, 2021 at 8:39
  • thank you for the list of error codes! This makes it much easier to find the error. However, I still don't have an idea why the output file can not be written in the script, but works perfectly in the Raster Calculator tool?
    – GeoBaer
    Jan 27, 2021 at 9:20

1 Answer 1

2

Ok - I got the thing working now after some more double checking. It seems that in the end, all the trouble is due to simple backslash - forwardslash confusion from my side. Changing input to

    raster_layer = QgsRasterLayer('C:/Users/baer/Desktop/GIS_temp/area_0.tif', 'ras')

made the code working in the python console. Output path works with both writing styles:

    output = 'C:/Users/baer/Desktop/GIS_temp/area_0_red.tif'
    output = r"C:\Users\baer\Desktop\GIS_temp\area_0_red.tif"

Coming from ArcGIS and being rather new to pyqgis programming, I was not fully aware of this thing, since to my experience in arcpy scripts, usually the writing style r"[path]" works fine for dataset paths. I hope though that this answer may save some research time to others migrating from arcpy to pyqgis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.