I have a folder with 1600 subfolders. I've developed an script to merge two shapefiles from each subfolder and store the result in the same subfolder. I ran the script and it worked well. However, when I check the results, it seems that for the second half of those subfolders the output is stored as an empty shapefile while for others it is ok (with the expected output). When I ran the process again only for second half of those folders, the results are ok (they are not empty). I want to run the process for the whole data once and this way it is not possible. Do you have any idea how I can improve it?

# inputs
# 1- main local administrative layer that shows all the Dutch municipalities
# 2- the main path for storing the results
# 3- the municipality bounadaries with 500 meter 
# 4- the local road network

# import the required modules and libraries
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from qgis.utils import iface
from qgis.core import *
from qgis.gui import *
from qgis.analysis import *
from qgis.PyQt.QtCore import QVariant

# removing all the layers from interface

# reading the admin layer
admin_fn= "/home/.../germany_r1_admin_1.shp"
admin_layer = iface.addVectorLayer(admin_fn, "admin", "ogr")

# defining the main path
main_path = "/home/.../clipping_results"

def merge_roads_boundary (feature, path):
    processing.run("qgis:mergevectorlayers", {'LAYERS': [path + '/' + str(feature['city_id']) + '_' + feature['Mun'] +  '/municipality_admin_buffer_boundary.shp',
                                                         path + '/' + str(feature['city_id']) + '_' + feature['Mun'] +  '/road_network.shp'],
                                              'CRS': 'EPSG:32632', 
                                              'OUTPUT': path + '/' + str(feature['city_id']) + '_' + feature['Mun'] +  "/road_network_local_merged.shp"})

k = 0
start = 0
end = 1600
for fet in admin_layer.getFeatures():
    k = k + 1
    if start < k < end:
        city_name = fet["Mun"]
        merge_roads_boundary (fet, main_path)

  • maybe you should try to make two cycles of 800 folders each, one for the first half, the other for the second? Dec 28 '21 at 20:03
  • @ФилиппУтин it this not work this way. I have to change the start values and rerun the script again! Dec 28 '21 at 20:09
  • Are you sure the both layers to be merged are not empty? Dec 28 '21 at 21:46
  • @KadirŞahbaz Yes I am sure. As I mentioned above, when I ran the process for part of the loop (say half or one third of it) it works perfectly and produces the expected outcome. The problem is that I can not finish the process (with valid output) in one go! Dec 28 '21 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.