Saturday 30 January 2016

Set raster layer additional no data value with PyQgis


I need a raster layer transparency configured like this:


enter image description here


Since I load the layer with PyQgis, the parameters should be set programmatically. For global transparency and transparency band I found that it works via myrasterlayer.renderer().setOpacity() and myrasterlayer.renderer().setAlphaBand(0), but I did not find a method to set the additional no data value to 0: http://qgis.org/api/classQgsRasterRenderer.html


How to set this value with PyQgis?




Answer



The method 'setNoDataValue' is in QgsRasterDataProvider. You can use next code to try it out. It worked for me.


layer = iface.activeLayer()

provider = layer.dataProvider()

provider.setNoDataValue(1, 0) #first one is referred to band number

layer.triggerRepaint()

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...