10

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.

  • Ok I fixed it by using the multiparts to singleparts function first. Then it works. – Toke Apr 14 '14 at 9:32
10

Sure You can get the function from the processing toolbox. Here's how to use it: As per http://docs.qgis.org/2.8/en/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"

Note above code is invalid for 3.0+ for the alglist example you can do:

print([a.id() for a in gsApplication.processingRegistry().algorithms() if "clip" in a.id()])

for the alghelp example you can do:

processing.algorithmHelp("qgis:clip")

For QGIS3 see this question:

What is the new alglist and alghelp in QGIS 3.0 Processing?

  • 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 '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 '14 at 9:07
  • 2
    You need to have the features that you want to clip selectged. I've edited the answer to reflect this. – Mr Purple Apr 15 '14 at 21:50
  • How would one do this without actually creating shapefiles? I have a layer with hundreds of disc-shaped polygons that I want to iterate through, using each as an overlay for a single layer of point data. Can I just get a list of QgsFeature objects instead somehow? – J. Taylor Dec 9 '15 at 2:07
  • You should ask that as a separate question – Mr Purple Dec 10 '15 at 1:47
5

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.runalg("qgis:clip", overlay_layer, layer_to_clip, "/tmp/output.shp")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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