1

I want to calculate some result using QgisRasterCalculator and using the code mentioned below.Problem is i am not getting any output raster also if i use this formula in raster calculator it gives nan.Please help.My code is

from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
bohLayer = iface.activeLayer()
entries = []
# Define band1
boh1 = QgsRasterCalculatorEntry()
boh1.ref = 'boh@1'
boh1.raster = bohLayer
boh1.bandNumber = 1
entries.append( boh1 )

## Define band2
#boh2 = QgsRasterCalculatorEntry()
#boh2.ref = 'boh@2'
#boh2.raster = bohLayer
#boh2.bandNumber = 2
#entries.append( boh2 )

# Process calculation with input extent and resolution
calc = QgsRasterCalculator( 'Float("%LE71970282002228EDC00_B6_clip.TIF (2)%" * 0.03705882) + 3.2', 'D:\UHI Plugin work\test\output\outputfile.tif', 'GTiff', bohLayer.extent(), bohLayer.width(), bohLayer.height(), entries )
calc.processCalculation()
  • The problem was in that you were not using the reference 'boh@1' to the active layer. – xunilk Mar 17 '15 at 23:06
2

This code works well:

from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
bohLayer = 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 * 0.03705882+ 3.2', 
                            'D:\outputfile.tif', 
                            'GTiff',
                            bohLayer.extent(), 
                            bohLayer.width(), 
                            bohLayer.height(), 
                            entries )

calc.processCalculation()

The raster outputfile.tif is in D:\ directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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