Hi, I have a question to update the code I sent in a former below to QGIS 2.0. Again I need the mean value, currently use:
vpoly = QgsVectorLayer("Polygon", "pointbuffer", "memory") feature = QgsFeature() feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(3517601,5406205)).buffer(10000,5)) provider = vpoly.dataProvider() provider.addFeatures( [feature] ) vpoly.commitChanges() stats = QgsZonalStatistics(vpoly,"/path/to/corine2006_z0_test_gk3.tif") stats.calculateStatistics(None) allAttrs = provider.attributeIndexes() for feature in vpoly.getFeatures(): for attr in feature.attributes(): return attr attr in this case is probably the sum as I can see from the results, but I need the mean again, as below. For QGIS 1.8 k==0 is number of pixels, k==1 is sum, k==2 is mean, see below. But something like return attr[2] doesn't work. It says TypeError: 'float' object has no attribute 'getitem'. Maybe someone has an idea how to solve this? Thanks a lot Otto Am Wed, 17 Jul 2013 09:21:55 +0200 schrieb Otto Dassau <das...@gbd-consult.de>: > Hi, > > I wanted to give a short feedback. Finally my solution for qgis 1.8 with > some help from Anita was to create a buffer polygon around the point as a > memory layer and calculated the mean value with QgsZonalStatistics. Here > is the code I used, maybe useful for others: > > from qgis.analysis import QgsZonalStatistics > from PyQt4.QtGui import * > > vpoly = QgsVectorLayer("Polygon", "pointbuffer", "memory") > feature = QgsFeature() > feature.setGeometry( > QgsGeometry.fromPoint(QgsPoint(3517601,5406205)).buffer(10000,5)) > provider = vpoly.dataProvider() > vpoly.startEditing() > provider.addFeatures( [feature] ) > vpoly.commitChanges() > stats=qgis.analysis.QgsZonalStatistics(vpoly,"/path/to/rasterlayer.tif") > stats.calculateStatistics(None) > allAttrs = provider.attributeIndexes() > provider.select(allAttrs) > provider.nextFeature(feature) > attrs = feature.attributeMap() > for (k,attr) in attrs.iteritems(): > if k==2: > > QMessageBox.information(qgis.utils.iface.mainWindow(),'Info',attr.toString()) > > Regards > Otto > > Am Wed, 3 Jul 2013 09:36:45 +0200 > schrieb Otto Dassau <das...@gbd-consult.de>: > > > Hi Fred, > > > > thanks for the hints, A and B sound interesting - PostGIS is not an > > option here. > > > > One more question, because you already used these workarounds. What > > solution A or B should be more usable (faster) from your opinion, if I > > need to calculate the mean while moving the mouse over the map canvas? > > > > Regards > > Otto > > > > Am Tue, 2 Jul 2013 18:05:10 +0100 > > schrieb Fred Lehodey <leho...@gmail.com>: > > > > > Hi, > > > > > > Not sure it's the best way but I did something similar with these > > > workarounds: > > > > > > A - with gdal: > > > 1 - the user click and you get your buffer. (already done) > > > 2 - use gdalwarp with "-cutline" and "*-*crop_to_cutline" options to > > > extract a new raster. (python subprocess) > > > 3 - read the created raster in a numpy array to do all the statistics > > > you need. > > > > > > B - with matplotlib: > > > 1 - with the buffer: create an array of the vertices > > > 2 - read the raster into another numpy array > > > 3 - use the "points_inside_poly" function of "matplotlib" lib to > > > create a numpy mask. > > > > > > Not tried but postgis raster should do the job. > > > > > > Hth, > > > Fred > > > > > > > > > On Tue, Jul 2, 2013 at 1:12 PM, Otto Dassau <das...@gbd-consult.de> > > > wrote: > > > > > > > Hi, > > > > > > > > for a qgis 1.8 plugin I would like to query the mean of pixel values > > > > of a raster layer within a defined buffer for the current mouse > > > > position (not for > > > > an existing point layer). > > > > > > > > For the buffer it works: > > > > > > > > ... > > > > feat.geometry().asPoint() > > > > (3.5176e+06,5.4062e+06) > > > > buff_geom = feat.geometry().buffer(1000, 5) > > > > buff_geom.asPolygon() [[(3.5186e+06,5.4062e+06), > > > > (3.51855e+06,5.4059e+06), (3.51841e+06,5.40562e+06), > > > > (3.51819e+06,5.4054e+06), (3.51791e+06,5.40525e+06), > > > > (3.5176e+06,5.4052e+06), (3.51729e+06,5.40525e+06), > > > > (3.51701e+06,5.4054e+06),...]] > > > > > > > > but now I try to find a solution to query the raster pixels within > > > > the buffer zone and calculate the mean, but I don't know how and if > > > > this is possible in QGIS 1.8 at all. I found QgsZonalStatistics, but > > > > that doesn't work for my task. > > > > > > > > Does anybody have a solution for this or a better idea, how to solve > > > > this? > > > > > > > > Thanks > > > > > > > > Otto > > > > _______________________________________________ > > > > Qgis-developer mailing list > > > > Qgis-developer@lists.osgeo.org > > > > http://lists.osgeo.org/mailman/listinfo/qgis-developer > > > > > > > > > > -- Geoinformatik Büro Dassau - http://www.gbd-consult.de FOSSGIS consulting , training , support and analysis Ackerstrasse 144c , D - 40233 Düsseldorf , Germany Tel: +49-(0)211-47468178 , Mobil: +49-(0)171-4687540 -- Community Advisor - QGIS Project Steering Committee _______________________________________________ Qgis-developer mailing list Qgis-developer@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-developer