3

I am using PyQGIS.
In QGIS1, I can use:

rlayer.setDrawingStyle ( QgsRasterLayer.PalettedColor )  

but In QGIS2, I got some errors.

I follow pyqgis developer cookbook, put some demo scripts in qgis python console without success.

rlayer.drawingStyle() #got AttributeError
rlayer.setDrawingStyle(QgsRasterLayer.SingleBandPseudoColor) 
#got AttributeError from QgsRasterLayer, change it to QgsRaster
rlayer.setDrawingStyle(QgsRaster.SingleBandPseudoColor) #got TypeError

1 Answer 1

1

Take a look at this code that works for me in qgis 2.2

  if bandCnt > 1:
      if Red_band > bandCnt or Green_band > bandCnt or Blue_band > bandCnt:
         raise GeoAlgorithmExecutionException( 'Only ' + bandCnt + 'layers in file')
      if Type == 0:
         rlayer.setDrawingStyle('MultiBandSingleBandGray')
         rlayer.renderer().setGrayBand(Red_band)
      elif Type == 1:
         rlayer.setDrawingStyle('MultiBandSingleBandPseudoColor')
         rlayer.renderer().setGrayBand(Red_band)
      else:
         rlayer.setDrawingStyle('MultiBandColor')
         rlayer.renderer().setRedBand(Red_band)
         rlayer.renderer().setGreenBand(Green_band)
         rlayer.renderer().setBlueBand(Blue_band)
   else:
       if Type == 0:
          if rlayer.rasterType() == 1:
             rlayer.setDrawingStyle('PalettedSingleBandGray')
          else:
             rlayer.setDrawingStyle('SingleBandGray')
       elif Type == 1:
          if rlayer.rasterType() == 1:
             rlayer.setDrawingStyle('PalettedSingleBandPseudoColor')
          else:
             rlayer.setDrawingStyle('SingleBandPseudoColor')
       else:
          if rlayer.rasterType() == 1:
             rlayer.setDrawingStyle('PalettedColor')
          else:
             rlayer.setDrawingStyle('SingleBandGray')
       rlayer.renderer().setGrayBand(Red_band)

Your Answer

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

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