I'm making a plugin were i want to get the min and max value of a raster. When i tried the following code in the qgis python console it worked fine but in my plugin code i get the following error

 ver = provider.hasStatistics(1, QgsRasterBandStats.All)
 NameError: global name 'QgsRasterBandStats' is not defined

The code i m using is:

    layer = self.iface.activeLayer()
    renderer = layer.renderer()
    provider = layer.dataProvider()
    extent = layer.extent()

    ver = provider.hasStatistics(1, QgsRasterBandStats.All)

    stats = provider.bandStatistics(1, QgsRasterBandStats.All,extent, 0)

    minum= stats.minimumValue
    maxim = stats.maximumValue


It look like QgsRasterBandStats isn't included in my python plugin. How can i include this?


You need to import it in this way in the header of the corresponding Python file:

from qgis.core import QgsRasterBandStats

If you have other classes being imported from qgis.core, you can list them:

from qgis.core import class1, class2, ..., QgsRasterBandStats

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.