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?