Using QGIS 2.2 and Python, I would like to use the Union algorithm on multiple shapefiles (contained in a folder TEST) which would then produce single shapefile containing all the data.
Currently the following code only outputs the union of the first and last file, how can I modify it so that the output of 2 layers is saved and then use that output with another layer etc?
UPDATE: The shapefiles are being processed in terms of the clip_function
however I can't load the temp files when running the qgis:union
algorithms. Also, an error occurs when it tries to output the Result.
##Model=name
##Select_folder=folder
##Cellsize=number 1000
##Result=output vector
import os
import glob
import tempfile
from qgis import utils
from qgis.core import QgsVectorLayer, QgsMapLayerRegistry, QgsVectorFileWriter, QgsCoordinateReferenceSystem
# Create grid with defined parameters
outputs_1=processing.runalg("qgis:creategrid", Cellsize, Cellsize, 24108, 18351.157175, 258293.802316, 665638.226408, 1, 'EPSG:7405', None)
def clip_function():
# Set directory and search for all .shp files
os.chdir("C:\Users\TEST\DATA\\")
for fname in glob.glob("*.shp"):
# Clip .shp files within directory to grid and save files as temp
outputs_2=processing.runalg("qgis:clip", outputs_1['SAVENAME'], fname, None)
outputs_3=processing.runalg("qgis:fieldcalculator", outputs_2['OUTPUT'], 'Rank_' + fname, 1, 10, 0, True, 1 , None)
def binary_alg_wrapper(list_of_files,final_file_name):
run_count=0
temp_path=tempfile.gettempdir()
temp_input_name="temp_input_layer"
temp_output_name="temp_output_layer"
temp_input_layer_filename=str(os.path.join(temp_path,temp_input_name+".shp"))
temp_output_layer_filename=str(os.path.join(temp_path,temp_output_name+".shp"))
for file_name in list_of_files:
EPSG_Code_for_file=4326
run_count+=1
if run_count==1:
#Copy the first file into a temp location
temp_output_layer = QgsVectorLayer(file_name, "temp_output_layer", "ogr")
Add_result = QgsMapLayerRegistry.instance().addMapLayer(temp_output_layer)
write_error = QgsVectorFileWriter.writeAsVectorFormat(temp_output_layer,temp_output_layer_filename,"system",QgsCoordinateReferenceSystem(EPSG_Code_for_file), "ESRI Shapefile")
QgsMapLayerRegistry.instance().removeMapLayer(temp_output_layer.id())
if run_count>1:
#Copy the result of the last algorithm into a temp location
old_output_layer = QgsVectorLayer(temp_output_layer_filename, "input_layer", "ogr")
Add_result=QgsMapLayerRegistry.instance().addMapLayer(old_output_layer)
write_error = QgsVectorFileWriter.writeAsVectorFormat(old_output_layer,temp_input_layer_filename, "system", QgsCoordinateReferenceSystem(EPSG_Code_for_file), "ESRI Shapefile")
QgsMapLayerRegistry.instance().removeMapLayer(old_output_layer.id())
#Read in a new file and the file stored in the temp location
input1_layer = QgsVectorLayer(temp_input_layer_filename, "input1_layer", "ogr")
input2_layer = QgsVectorLayer(file_name, "input2_layer", "ogr")
#Run the binary algorithm and store the result into a temp location or output location
if run_count==len(list_of_files):
processing.runalg("qgis:union", input1_layer, input2_layer, temp_output_layer_filename)
else:
processing.runalg("qgis:union", input1_layer, input2_layer, final_file_name)
QgsMapLayerRegistry.instance().removeMapLayer(input1_layer.id())
QgsMapLayerRegistry.instance().removeMapLayer(input2_layer.id())
path_res = tempfile.gettempdir()
file_list = [shp for shp in glob.glob(path_res + "*.shp")]
Result=(os.path.join(path_res, "result.shp"))
if Select_folder:
clip_function()
binary_alg_wrapper(file_list, Result)