Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am using the Zonal Stats plugin in QGIS to extract raster statistics from overlaying polygons (I have shapefiles of species distribution and I want to extract environmental data from within each species' range). I have 300 or so files I need to get data from and so would like to write a script to run in the python console, however I am a complete novice with python and have no idea how to do this. Any help would be much appreciated.

share|improve this question
    
possible duplicate of Running a Python Script to Extract Raster Data in QGIS –  Chad Cooper Jan 5 '13 at 14:36
    
Chad - yes it's a similar question but its more specific. No one has answered the other question, instead they suggested other ways of solving the problem. I don't know how to delete the other post –  Thomas Jan 5 '13 at 16:46
    
In the other question i pointed you to a r-script capable of what you want and Sylvester Sneekly named you the exact method in python. If you aren't used to code in either python or r than all further hints won't help you. You need a self-coded script or a lot of mouseclicks. Learn some coding, try the examples and report back, if something doesn't work. –  Curlew Jan 5 '13 at 16:47
    
@Curlew - Sylvester's method, while it sounds very good, would represent a massive learning curve for me and I don't have time to do this at the moment. I appreciate Sylvester's help but it wasn't the straightforward answer I was after. I though I had most of the code I was after in the post I mentioned in my other question (gis.stackexchange.com/questions/23203/…). If Sylvester's method is the only way to do this in python maybe I underestimated how difficult it would be. –  Thomas Jan 5 '13 at 17:23
    
@Curlew - Your R script does indeed work, thank you. The only thing I've yet to work out is how to append the results returned in R to the .dbf file for my shapefiles (any help with this would be much appreciated). –  Thomas Jan 5 '13 at 17:23

2 Answers 2

up vote 5 down vote accepted

The below code worked for me QGis 1.8.0

You might modify this to accomodate multiple files with some loop..

from qgis.analysis import QgsZonalStatistics

#specify polygon shapefile vector
polygonLayer = QgsVectorLayer('F:/temp/zonalstat/zonePoly.shp', 'zonepolygons', "ogr") 

# specify raster filename
rasterFilePath = 'F:/temp/zonalstat/raster1.tif'

# usage - QgsZonalStatistics (QgsVectorLayer *polygonLayer, const QString &rasterFile, const QString &attributePrefix="", int rasterBand=1)
zoneStat = QgsZonalStatistics (polygonLayer, rasterFilePath, 'pre-', 1)
zoneStat.calculateStatistics(None)
share|improve this answer
    
Great. Thank you very much vinayan, that's exactly what I was after. –  Thomas Feb 6 '13 at 15:36
    
Also see here for alternative solution using R –  Thomas Feb 6 '13 at 16:16
    
glad it helped you! –  vinayan Feb 6 '13 at 17:33
    
@vinayan the QProgressDialog is useful for visual environments where you want to see how far the calculations have progressed. It has no use from the command line. You can use None as the parameter and it works fine. Then you don't need the from PyQt4.. line or the progressDialog = line. See similar post at gis.stackexchange.com/questions/23203/… –  rudivonstaden Mar 26 '13 at 20:19
    
@rudivonstaden - that makes sense now..i updated the answer –  vinayan Mar 27 '13 at 6:45

Here is way to get what you want in SAGA GIS. This probably isn't the solution you want, but it works. I'll look into the reasons why my plugins fails and update it as soon as possible.

Install SAGA GIS (should also be available via apt-get or aptitudbe in your linux distribution).

  • Start SAGA, load in your Raster and vector shape (Menu Modules -> File -> GDAL/OGR import). You can see the process below.
  • Execute Module "Grid statistics for polygons" (Menu Modules -> Shape -> Grid -> Grid-values). Values are added directly to the table. The Dialog should look like thisenter image description here
  • Go to the tab "Data" in the workspace, rightclick on your vector layer and choose to "save as" to export the shape with the added attributes. You could also display the attribute table via rightclick and then click on table show

This works for the dataset you sent me. It is also possible to call SAGA modules in QGIS via SEXTANTE as a BATCH process. To do this simply activate the SAGA modules in the SEXTANTE options.

share|improve this answer
    
thanks for the suggestion but I've already tried saga - the results it produced were inconsistent i.e. doing the same thing twice gave different results. I know the ZonalStats plugin in QGIS works, hence I'm after a way to automate ZonalStats. –  Thomas Jan 7 '13 at 13:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.