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.
3 Answers
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:
-
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()
andInputLayer = qgis.utils.iface.activeLayer()
. Then I useprocessing.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.– TokeApr 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.– TokeApr 14, 2014 at 9:07
-
2You need to have the features that you want to clip selectged. I've edited the answer to reflect this. 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? Dec 9, 2015 at 2:07
-
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")
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: