I try to use clip vector by polygon in pyqgis.
My code (minimal working example)
import sys
from qgis.core import *
import qgis.utils
QgsApplication.setPrefixPath("/usr",True)
qgs = QgsApplication([],True)
qgs.initQgis()
# load base layer
blayer = QgsVectorLayer("lokale_orte.shp", "lokale_orte", "ogr")
if not blayer.isValid():
print "Layer failed to load!"
else:
print "success: load layer"
# load clip layer
clayer = QgsVectorLayer("kleine_kreise.shp", "kleine_kreise", "ogr")
if not clayer.isValid():
print "Layer failed to load!"
else:
print "success: load layer"
# Prepare processing framework
sys.path.append('/usr/share/qgis/python/plugins') # Dir where Processing is located
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *
# run clip
general.runalg('gdalogr:clipvectorsbypolygon', 'lokale_orte.shp', 'kleine_kreise.shp', None, None)
The complete output and error message is:
success: load layer
success: load layer
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 196, in qgis_excepthook
showException(type, value, tb, None, messagebar=True)
File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 107, in showException
open_stack_dialog(type, value, tb, msg)
File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 142, in open_stack_dialog
iface.messageBar().popWidget()
AttributeError: 'NoneType' object has no attribute 'messageBar'
Original exception was:
Traceback (most recent call last):
File "a.py", line 34, in <module>
general.runalg('gdalogr:clipvectorsbypolygon', 'lokale_orte.shp',
'kleine_kreise.shp', None, None)
File "/usr/share/qgis/python/plugins/processing/tools/general.py", line 75, in runalg
alg = Processing.runAlgorithm(algOrName, None, *args, **kwargs)
File "/usr/share/qgis/python/plugins/processing
/core/Processing.py", line 300, in runAlgorithm
ret = runalg(alg, progress)
File "/usr/share/qgis/python/plugins/processing
/gui/AlgorithmExecutor.py", line 52, in runalg
progress.error(e.msg)
AttributeError: 'NoneType' object has no attribute 'error'
I guess my problem is the runalg call, according to http://docs.qgis.org/2.6/de/docs/user_manual/processing_algs/gdalogr/ogr_geoprocessing/clipvectorsbypolygon.html
I think I can write None for output layer to force QGIS to write the output in an memory layer. But what about options? What to write? Where is a proper documentation?
Best Answer
You may continue using the memory layer by loading it as an object. Before doing this, you need to know how the output from the algorithm is called, so typing these lines in the Python Console:
you will see that the output's name is
OUTPUT_LAYER
:This means that you will be able to use it if you add a new variable and call it in this way:
By now,
my_output
will be the object to use for calling the layer returned by the clip operation and you are ready to do something with it.EDIT For getting your script working, you may try using the following code (slightly different from yours):