For a project of mine, I'd like to solve a spatial analysis using QGIS 2.8.

Until now, I have never automated anything GIS-related except for some rather easy steps in ArcGIS using the ModelBuilder.

This is what's supposed to be done:

  1. Select all polygons from shapefile A that overlap a similar shapefile B (-> Select by location)
  2. Extract selected polygons from A creating shapefile C, B serves as clip feature (-> Clip)
  3. Add a columns reading area for each of the extracted features of C (-> Add geometry columns)
  4. Calculating the percentage of each feature's (C) area in regard to all extracted polygons (= sum), store this information in a new column (I'm actually not sure how to do this except manually using the individual .dbf-file, the GroupStats plugin works fine but cannot write the newly calculated information in the attribute table)
  5. Calculate a new value in a new field in C's attribute table based on the area's percentage (see 4) and an existing value (derived from A, see 2) (-> Field Calculator)
  6. Summing up the calculate values (see 5) in a new field (-> Field Calculator)
  7. Merge all selected polygons from C (-> Editor or Dissolve) (see 2)

For me, the major problem is that this workflow needs to be done some hundred times at least. Therefore, if this can be easily done using the Modeler, the Python console or something (my Python knowledge is somewhat limited but not entirely non-existent), I am willing to dedicate some time to delve into tutorials or instruction manuals in order to be better off next time.

Here's my latest model. How can I perform the fourth task?

enter image description here

Trying to add underdark's script from file resulted in this configuration error message:

Traceback (most recent call last): File "/home/tjk/.qgis2/python/plugins/processing/gui/ProcessingToolbox.py", line 221, in executeAlgorithm action.execute() File "/home/tjk/.qgis2/python/plugins/processing/script/AddScriptFromFileAction.py", line 45, in execute settings = QSettings(QSettings.NativeFormat, QSettings.UserScope, 'QuantumGIS', 'QGis') NameError: global name 'QSettings' is not defined

I tried the solution to https://stackoverflow.com/questions/18769383/qgis-preferences-in-a-python-plugin but nothing happened.

Shouldn't QSettings be defined automatically while I'm using the software, saving my personal preferences?

closed as too broad by PolyGeo Feb 4 '18 at 22:10

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


Based on your workflow description, I don't see any step which wouldn't be possible with the Modeler. Once you've built the model, you can call it in a loop from the Python console to repeat for the few hundred times you need.

For your step #4, you could do something like

enter image description here

This is the code of the set value script that I whipped up:

##My tools=group
##field=field input
##output=output vector

from processing.tools.vector import VectorWriter
from qgis.core import *
from PyQt4.QtCore import *

layer = processing.getObject(input)
provider = layer.dataProvider()
fields = provider.fields()
writer = VectorWriter(output, None,fields, provider.geometryType(), layer.crs() )

outFeat = QgsFeature()

for inFeat in processing.features(layer):
    inGeom = inFeat.geometry()
    attrs = inFeat.attributes()
    attrs[inFeat.fieldNameIndex(field)] = set_value

del writer
  • Thank you, underdark. Glad to hear that the answer is supposedly easier than I thought. I added my latest model to the original post. Could you (or somebody else) give me a hint how to integrate the statistical task described in (4)? – T. K. May 9 '15 at 9:32
  • Thank you again, underdark. Unfortunately, there seems to be a problem adding this very script due to configuration shortcomings. Please see the original post for details (and let me know if a new thread should be opened). – T. K. May 11 '15 at 8:36

You could try to do this using an other open source tool called GeoKettle. GeoKettle is a spatial ETL (Extract Transform Load) tool. It should be able to do all the above without exposing you to programming tasks.

GeoKettle has it's commercial parallel in FME from Safe Software.

  • 1
    Thank you, ragnvald. However, I would prefer to solve this using only QGIS and the tools I know at least a bit. But I saved a bookmark and will get to it the next time. – T. K. May 9 '15 at 9:35

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