I want to Apply some calculations over rasters ,For which using Following Code.

  import time
  from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
  bohLayer = QgsRasterLayer(rasterpath,"temp")
  QgsMapLayerRegistry.instance().addMapLayer(bohLayer )
  entries = []
  # Define band1
  boh1 = QgsRasterCalculatorEntry()
  boh1.ref = 'boh@1'
  boh1.raster = bohLayer
  boh1.bandNumber = 1
  entries.append( boh1 )

  bohLayer1 = QgsRasterLayer(rasterpath,"temp1")

  # Define band2
  boh2 = QgsRasterCalculatorEntry()
  boh2.ref = 'boh1@1'
  boh2.raster = bohLayer1
  boh2.bandNumber = 1
  entries.append( boh2 )

  # Process calculation with input extent and resolution
  calc =QgsRasterCalculator('Float(boh1@1-boh@1)/Float(boh1@1+boh@1)','outputfile','GTiff', bohLayer.extent(), bohLayer.width(), bohLayer.height(), entries )

But it Gives out 4 as parse Error instead of 0(as success) what is the error in code ??

  • So you expect one output raster but you get four?
    – BERA
    Jul 6, 2017 at 7:16
  • Nope. No output raster i am getting . when i am doing raster calculations over one raster then it gives 0 after code process but when i am performing over two different rasters it gives 4 and no output raster is created.
    – User18
    Jul 6, 2017 at 7:46

1 Answer 1


You need to include a space when using mathematical operators with references to rasters in your formula. So try changing



Float(boh1@1 - boh@1)/Float(boh1@1 + boh@1)
  • @User18 - Most welcome, glad it worked :)
    – Joseph
    Jul 6, 2017 at 9:49

Your Answer

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

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