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,136
  • 29
  • 109
  • 338
Toke
  • 379
  • 1
  • 5
  • 13
  • Ok I fixed it by using the multiparts to singleparts function first. Then it works. – Toke Apr 14 '14 at 09:32

3 Answers3

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?

Mr Purple
  • 1,451
  • 12
  • 25
  • 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 07: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 09: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 09 '15 at 02:07
  • You should ask that as a separate question – Mr Purple Dec 10 '15 at 01:47
  • Thank you MrPurple -- I didn't know if it was appropriate to create a whole new question, but I'll do so now. – J. Taylor Dec 10 '15 at 03:06
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")
Domokos Endre
  • 163
  • 1
  • 4
Francisco Puga
  • 4,618
  • 21
  • 40
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:

Taras
  • 32,823
  • 4
  • 66
  • 137