[GIS] Executing intersect algorithm with pyqgis

intersectionpyqgis

I want to run the process "qgis:intersection" in a pyton-script and try it like this:

import processing
a1 = QgsMapLayerRegistry.instance().mapLayersByName('Layer1')[0]
a2 = QgsMapLayerRegistry.instance().mapLayersByName('Layer2')[0]
processing.runandload("qgis:intersection", a1, a2, "memory:myCut")

But there comes the "Problem executing algorithm" message: "GEOS geoprocessing error: One or more input features have invalid geometry."

If i do the same by the qgis-gui (Vector/Geoprocessing Tools/Intersect…) in QGIS 2.14.21 like this:
enter image description here

then everything is ok and qgis do the intersection.

Doing the intersectin with QGIS 2.18.13 with the gui, I become also an error like above.

"Layer2" is a large layer with some hundredthousands polygons an if i use the "Topology Checker" i get about 600 invalid geometries.

Why does the intersection work only with the qgis-gui in 2.14.21 and not somewhere else?

Best Answer

Thank you xlDias, "saga:intersect" solved the Problem!