from qgis.analysis import  QgsRasterCalculator, QgsRasterCalculatorEntry

raster = '/home/francis/QGIS/epelwrf/temp/temp0000-postwrf_d01_20180720_0600_f00000.gr2'
bohLayer = QgsRasterLayer(raster)
entries = [ raster ]

boh1 = QgsRasterCalculatorEntry()
boh1.ref = 'boh@1'
boh1.raster = bohLayer
boh1.bandNumber = 1
entries.append ( boh1 )

expression = '( ' + entries[0].ref + ' ) - 275.15 '
print expression

calc = QgsRasterCalculator( expression, "/home/francis/QGIS/tiffout/1.tif", 'GTiff', bohLayer.extent(), bohLayer.width(), bohLayer.height(), entries )


I'm stuck in my project. trying to convert this gr2 file (that contains tempsfc which is in kelvin) into Celsius.

  • Welcome to the GIS SE. Could you explain exactly what is happening? Is the tool not working / giving incorrect results / errors? – Keagan Allan Aug 8 '18 at 6:45
  • Hi! Thank you so much for responding.I'm new in using GIS software in general. The source code that I found here is just not working. And I can't find anything related to what I am doing. All I need is to find out how conversion of units is done in PyQgis using 1 band raster. (the gr2 file that I am using has only 1 band and it contains the parameter 'surface temperature' which is in kelvin) I just need to convert it into celsius – Francis Kane Aug 8 '18 at 6:51
  • Are you wanting for an answer explicitly for use with PyQGIS, or QGIS in general? – Keagan Allan Aug 8 '18 at 6:58
  • just pyqgis will be preferable – Francis Kane Aug 8 '18 at 7:00

From what I can see in your question try this:

Open QGIS Click the Raster Option in the Top menu buttons, it lies between Vector and Database

Choose the first option: Raster Calculator In the Raster Bands select the Temperature Raster Under Operators select the "-" and in the Expression type 275.15

Choose the output layer location and name, keep the Format as default values.

That should work.

  • Thank you. I've already done that. but here at work, they are requiring me to find a way thru the qgis console, to do what the raster calculator is intended for but using pyqgis. – Francis Kane Aug 8 '18 at 7:00
  • Sure no problem, from the original question I didn't see you wanted to explicitly use PyQGIS. I will leave this answer here in case someone else needs it. – Keagan Allan Aug 9 '18 at 0:37

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.