11

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 31, 2013 at 12:33
1
  • Ok I fixed it by using the multiparts to singleparts function first. Then it works. Commented Apr 14, 2014 at 9:32

3 Answers 3

11

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?

answered Mar 18, 2014 at 2:42
6
  • 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. Commented Apr 14, 2014 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. Commented Apr 14, 2014 at 9:07
  • 2
    You need to have the features that you want to clip selectged. I've edited the answer to reflect this. Commented Apr 15, 2014 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? Commented Dec 9, 2015 at 2:07
  • You should ask that as a separate question Commented Dec 10, 2015 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")
answered Apr 14, 2014 at 9:17
1

In the latest PyQGIS version it should work as follows

from qgis.core import *
def clipping(layer_1, layer_2):
 layer_clip = processing.run('qgis:clip',
 {'INPUT': layer_1,
 'OVERLAY': layer_2,
 'OUTPUT': "memory:"}
 )["OUTPUT"]
 return QgsProject.instance().addMapLayer(layer_clip)
your_layer_1 = QgsProject.instance().mapLayersByName('layer_name_1')[0] # main layer
your_layer_2 = QgsProject.instance().mapLayersByName('layer_name_2')[0] # secondary layer
clipping(your_layer_1, your_layer_2)

Note: The output of algorithm will have the same geometry as the main layer


References:

answered Apr 28, 2020 at 12:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.