6

I'm fighting with pyqgis with a very simple issue. I have a raster layer loaded in my TOC and I'd like to return with python its NoData value.

I looked in the API but (and sure it is my lack) I cannot find a way to do that.

So I get my raster as an object in the console:

rl = iface.activeLayer()

and I tried what I found in some websites (see the list at the bottom):

rl.getNoDataValue() #[1]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'QgsRasterLayer' object has no attribute 'getNoDataValue'


rl.noDataValue() #[2] old API
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'QgsRasterLayer' object has no attribute 'noDataValue'

#[1]: http://qgis.sourceforge.net/qgis_api/html/classQgsRasterLayer.html#a10

#[2]: http://qgis.org/api/1.8/classQgsRasterLayer.html#a558e7f4bb4abe6b95c6882fbe4d2e19f

Look into the QgsRasterLayer class dir(rl) but I cannot find a way to return the NoData values.

  • That method ('noDataValue') is in QgsRasterBlock class. – xunilk Sep 7 '16 at 14:26
7

That method ('noDataValue') is in QgsRasterBlock class (not in QgsRasterLayer). To get access to this method you can try out this code:

layer = iface.activeLayer()
extent = layer.extent()

provider = layer.dataProvider()

rows = layer.rasterUnitsPerPixelY()
cols = layer.rasterUnitsPerPixelX()

block = provider.block(1, extent,  rows, cols)

print block.noDataValue()

It works for me.

  • Thanks it works. I did not know that I can use this class to achieve the no data values. – matteo Sep 7 '16 at 16:22
  • in the new 3.x api you can get it directly from provider as documented in: qgis.org/api/… – Luigi Pirelli Aug 30 '18 at 12:15

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.