3

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

  import time
  from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
  rasterpath='inputfile1'
  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 )


  rasterpath='inputfile2'
  bohLayer1 = QgsRasterLayer(rasterpath,"temp1")
  QgsMapLayerRegistry.instance().addMapLayer(bohLayer1)

  # 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 )
  calc.processCalculation()

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 '17 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 '17 at 7:46
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)

to

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

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.