The ability to write and run cross platform scripts for QGIS using python is OUTSTANDING! However, when I try to run the SAGA:MERGESHAPESLAYERS algorithm from a python script it works fine in windows but returns no output shape when I run it in ubuntu. I'm running QGIS 2.4 on ubuntu.
I'm confident that I DO have SAGA 2.1 on the ubuntu system because I can successfully run the convex hull algorithm.
To illustrate the issue I have written the following scripts. The first of which creates a few of shape files in the temp directory then the second attempts to apply the aforementioned SAGA algorithms. You should be able to easilty cut and paste the scripts into the QGIS python console in order to illustrate the issue.
First script which creates a few shape files in the temp directory:
#BUILD EXAMPLE SHAPE FILES TO ILLUSTRATE THE ISSUE
from qgis import core
from qgis.utils import iface
import os
import tempfile
import sys
epsg=4326
my_crs =core.QgsCoordinateReferenceSystem(epsg,core.QgsCoordinateReferenceSystem.EpsgCrsId)
iface.mapCanvas().mapRenderer().setDestinationCrs(my_crs)
vlayer01=QgsVectorLayer("Polygon?crs=epsg:"+str(epsg)+"&field=id:integer&field=name:string(20)&index=yes","polygon_01","memory")
bbl_dp = vlayer01.dataProvider()
vlayer01.startEditing()
feature_01_01 = QgsFeature()
feature_01_01.setGeometry(QgsGeometry.fromPolygon(\
[[QgsPoint(0,100),QgsPoint(0,110), QgsPoint(10,110),QgsPoint(10,100)]]))
feature_01_02 = QgsFeature()
feature_01_02.setGeometry(QgsGeometry.fromPolygon(\
[[QgsPoint(-10,100),QgsPoint(-10,110), QgsPoint(-20,110),QgsPoint(-20,100)]]))
(res, outFeats) = vlayer01.dataProvider().addFeatures([feature_01_01,feature_01_02])
vlayer01.commitChanges()
QgsMapLayerRegistry.instance().addMapLayer(vlayer01)
vlayer01_path=os.path.join(tempfile.gettempdir(),"vlayer_01.shp")
write_error = \
QgsVectorFileWriter.writeAsVectorFormat(vlayer01, \
vlayer01_path, "system", \
QgsCoordinateReferenceSystem(epsg), \
"ESRI Shapefile")
vlayer02=QgsVectorLayer("Polygon?crs=epsg:"+str(epsg)+"&field=id:integer&field=name:string(20)&index=yes","polygon_02","memory")
bbl_dp = vlayer02.dataProvider()
vlayer02.startEditing()
feature_02_01 = QgsFeature()
feature_02_01.setGeometry(QgsGeometry.fromPolygon(\
[[QgsPoint(20,110),QgsPoint(20,120), QgsPoint(30,120),QgsPoint(30,110)]]))
(res, outFeats) = vlayer02.dataProvider().addFeatures([feature_02_01])
vlayer02.commitChanges()
QgsMapLayerRegistry.instance().addMapLayer(vlayer02)
vlayer02_path=os.path.join(tempfile.gettempdir(),"vlayer_02.shp")
write_error = \
QgsVectorFileWriter.writeAsVectorFormat(vlayer02, \
vlayer02_path, "system", \
QgsCoordinateReferenceSystem(epsg), \
"ESRI Shapefile")
vlayer03=QgsVectorLayer("Polygon?crs=epsg:"+str(epsg)+"&field=id:integer&field=name:string(20)&index=yes","polygon_03","memory")
bbl_dp = vlayer03.dataProvider()
vlayer03.startEditing()
feature_03_01 = QgsFeature()
feature_03_01.setGeometry(QgsGeometry.fromPolygon(\
[[QgsPoint(3,103),QgsPoint(3,113), QgsPoint(13,113),QgsPoint(13,103)]]))
(res, outFeats) = vlayer03.dataProvider().addFeatures([feature_03_01])
vlayer03.commitChanges()
QgsMapLayerRegistry.instance().addMapLayer(vlayer03)
vlayer03_path=os.path.join(tempfile.gettempdir(),"vlayer_03.shp")
write_error = \
QgsVectorFileWriter.writeAsVectorFormat(vlayer03, \
vlayer03_path, "system", \
QgsCoordinateReferenceSystem(epsg), \
"ESRI Shapefile")
qgis.utils.iface.setActiveLayer(vlayer01)
qgis.utils.iface.zoomToActiveLayer()
Second script which addresses the issue
###SAGA PROCESSING ALGORITHM ISSUE STARTS HERE###
import processing
alg_list=[]
alg_list.append("qgis:mergevectorlayers")
alg_list.append("saga:mergeshapeslayers")
alg_list.append("saga:convexhull")
for alg in alg_list:
if alg=="saga:convexhull":
SHAPES=vlayer01_path
POLYPOINTS=0
HULLS=os.path.join(tempfile.gettempdir(),"output_saga_convexhull.shp")
BOXES=""
processing_result=processing.runalg(alg,SHAPES,POLYPOINTS,HULLS,BOXES)
print alg+" was "+str(processing_result)
resultlayer = QgsVectorLayer(HULLS, "saga:convexhull", "ogr")
QgsMapLayerRegistry.instance().addMapLayer(resultlayer)
qgis.utils.iface.setActiveLayer(resultlayer)
qgis.utils.iface.zoomToActiveLayer()
if alg=="saga:mergeshapeslayers":
MAIN=vlayer01_path
LAYERS=vlayer02_path+";"+vlayer03_path
OUT=os.path.join(tempfile.gettempdir(),"output_saga_mergeshapeslayers.shp")
processing_result=processing.runalg(alg,MAIN,LAYERS,OUT)
print alg+" was "+str(processing_result)
resultlayer = QgsVectorLayer(OUT, "saga:mergeshapeslayers", "ogr")
QgsMapLayerRegistry.instance().addMapLayer(resultlayer)
qgis.utils.iface.setActiveLayer(resultlayer)
qgis.utils.iface.zoomToActiveLayer()
if alg=="qgis:mergevectorlayers":
LAYER1=vlayer01_path
LAYER2=vlayer02_path
SAVENAME=os.path.join(tempfile.gettempdir(),"output_qgis_mergevectorlayers.shp")
processing_result=processing.runalg(alg,LAYER1,LAYER2,SAVENAME)
print alg+" was "+str(processing_result)
resultlayer = QgsVectorLayer(SAVENAME, "qgis:mergevectorlayers", "ogr")
QgsMapLayerRegistry.instance().addMapLayer(resultlayer)
qgis.utils.iface.setActiveLayer(resultlayer)
qgis.utils.iface.zoomToActiveLayer()
The merge algorithm works only in windows and the convex hull algorithm works in both windows and ubuntu. I'd really like the SAGA:MERGESHAPESLAYERS algorithm to work cross platform as intended.