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.

Is there a way to call the clip function in QQGIS from the python console? It is found under geoprocessing tools in the vector menu.

share|improve this question
    
Ok I fixed it by using the multiparts to singleparts function first. Then it works. –  Toke Apr 14 at 9:32

2 Answers 2

Sure You can get the function from the processing toolbox. Here's how to use it: As per http://qgis.org/de/docs/user_manual/processing/console.html

From the console you can get a list of all the algorithms available which contain the word "clip" by typing:

import processing
processing.alglist("clip")

Then you could find out how to use what appears the most appropriate function with:

processing.alghelp("qgis:clip")

Then simply use the algorithm in your script as follows:

processing.runalg("qgis:clip",inputlayer,overlaylayer,"output_file.shp")

Note: The algorithm will work only on slected features"

share|improve this answer
    
Sorry for bringing this topic to life again, but never made this work, and now I need it again. In the pyhton console I get the two layers by Layer = qgis.utils.iface.activeLayer() and InputLayer = qgis.utils.iface.activeLayer(). Then I use processing.runandload("qgis:clip",InputLayer,Layer,"output_file.shp") and the new layer is added to the Layers menu with the name Clipped. But the layer is empty. If I use these two layers with the clip function within Qgis, the output layer has the lines from the clipping. Anyone who what can be wrong? I do not get any errors. –  Toke Apr 14 at 7:03
    
It works if I use two polygons, but one of my layers are a line and not polygon. It contains several lines, and I can use this layer for clipping when doing it in Qgis. –  Toke Apr 14 at 9:07
    
You need to have the features that you want to clip selectged. I've edited the answer to reflect this. –  Mr Purple Apr 15 at 21:50

Assuming that you have a layer called "overlay" and another one called "layer_to_clip" loaded.

# get the overlay layer in the console
overlay_layer = [x for x in iface.legendInterface().layers() if x.name() == 'overlay'][0]

# get the layer to clip in the console
layer_to_clip = [x for x in iface.legendInterface().layers() if x.name() == 'layer_to_clip'][0]

# run the algorithm and output the results in /tmp/output.shp

processing.algrun("qgis:clip", overlay_layer, layer_to_clip, "/tmp/output.shp")
share|improve this answer

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.