I want to do raster calculation over raster layer for which i am using python script as:
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
bohLayer = qgis.utils.iface.activeLayer()
entries = []
# Define band1
boh1 = QgsRasterCalculatorEntry()
boh1.ref = 'boh@1'
boh1.raster = bohLayer
boh1.bandNumber = 1
entries.append( boh1 )
# Process calculation with input extent and resolution
calc = QgsRasterCalculator( 'boh@1/10000',
'E:/data/abc.tif',
'GTiff',
bohLayer.extent(),
bohLayer.width(),
bohLayer.height(),
entries )
calc.processCalculation()
Sometimes this script run successfully and gives output but mostly it give error as
AttributeError: 'NoneType' object has no attribute 'extent'
Is there any other way to do raster calculation over raster layer with python script.
bohLayer
with the QGIS Layer Tree's current layer (aka. active layer or selected layer). I guess the script fails when you have not selected yourbohLayer
in the Layer Tree. Let me know if that's right to tell you how to avoid relying on a selected layer.