[GIS] Calling clip function in pyQGIS from python console

clippyqgis

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.

Best Answer

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?