2

When multiplying two rasters together using code in PyQGIS, the resultant raster is much larger than expected.

Each input raster takes up ~ 11KB as .tif files, but when I multiply two together it becomes a much larger .tif file, nearly 20MB, which seems way too big considering the input sizes.

Here is my code below to create the 20MB output raster:

expression = "r1@1 * r2@1"
combinedExtent = self.getCombinedExtent(layerList)
calc = QgsRasterCalculator(expression, outputPath, 'GTiff', combinedExtent, mainRaster.crs(), mainRaster.width(), mainRaster.height(), layerReferences)
calcResult = calc.processCalculation()
intersectLayer = QgsRasterLayer(outputPath, "Combined Raster layer")

With outputPath being where the output raster is saved, combinedExtent being the overall extent of both rasters in the calculation, and mainRaster being the overall background raster that both of the smaller rasters to be multiplied lie upon.

Below are the rasters used for multiplication, r1 and r2, which are each roughly 14KB in size:

r1, one of the rasters used for calculation r2, the other raster used for calculations

When multiplied with a similar raster, r2, the following is created, displaying the area of intersect between the two rasters: Raster output from multiplying two rasters But this file is roughly 20MB in size, which seems way too big considering the small size of both input rasters.

Hopefully you can download r1 and r2 as .tifs as attached and see the same issue with PyQGIS.

1 Answer 1

2

QgsRasterCalculator outputs in FLOAT32 and that, unfortunately, cannot be changed.

FLOAT32 takes 4 bytes for each pixel, so you end with IMAGE_WIDTH * IMAGE_HEIGHT * 4 bytes for your picture (plus some additional for header and such, this is just a rough estimate).

https://api.qgis.org/api/3.30/qgsrastercalculator_8cpp_source.html


What you can do to avoid it, is to use GDAL Raster Calculator in which you can adjust the output format.

enter image description here

enter image description here

You can call GDAL Raster Calculator from pyqgis.

Edit:

as @bugmenot123 mentioned, GDAL Raster Calculator also offers you to set compression on the output file, which can further lower the size of the file.

enter image description here

3
  • 1
    Compression is probably what makes them small, a Byte file would still be at least 20/4 -> 5MB. But that should also be possible to set when using the Processing tool instead of QgsRasterCalculator. Commented Mar 13 at 15:25
  • 1
    Of course, I forgot to mention that part. There are many more options available when using GDAL Raster Calculator than there are in QgsRasterCalculator which has most options hardcoded.
    – ViperMiniQ
    Commented Mar 13 at 15:47
  • Thanks @ViperMiniQ, I'll have a look into that.
    – oliver
    Commented Mar 14 at 8:22

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.