Wednesday 27 May 2015

pyqgis - Including QgsRasterBandStats in plugin?



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

self.dlg.lineEdit.addItems(minum)
self.dlg.lineEdit_2.addItems(maxim)


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



Answer



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

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...