Following on from How to use `layer.getFeatures()` in a standalone script?, I would like my script to do the following (includes illustration):
- Search for the first shapefile (Paris (O).shp) in Country_France > Original files and clip it onto Grid.shp.
- Search for the first shapefile (Paris (P).shp) in Country_France > Processed files and take the first value in the
Rank
field. - Take Paris (O).shp and add a
Rank
field with the value from Paris (P).shp. Output results in Country_France > Processed files with new filename, Paris (P)x.shp.
Search for second shapefile in Country_France > Original files (Toulon (O).shp) and repeat Step 1.
- When all shapefiles are found in Country_France, move on to Country_Germany and repeat Step 1.
Problem:
ALL output shapefiles are based on the last shapefile to be clipped from Step 1. In other words, Paris (P)x.shp) has the exact same geometry with Toulon (P)x.shp. The only difference is in the Attributes Table where the Rank
value is correctly applied:
I am expecting Paris (P)x to look like the image below (ie. Paris (O).shp clipped onto *Grid.shp, not Toulon (O).shp):
I'm quite certain the problem lies with the loops, the problem is I am unsure how to create the loop(s) which resembles the Steps above. I obtained the results using the following script:
root_dir = home + "\Desktop\Test\\"
original_dir = "Original files"
processed_dir = "Processed files"
country_dir = "Country_*/"
grid_path = root_dir + "Grid.shp"
for f in glob.glob(root_dir + country_dir + original_dir + "\*.shp"):
outputs_1=general.runalg("qgis:clip", grid_path, f, None)
for g in glob.glob(root_dir + country_dir + processed_dir + "\*(P).shp"):
pieces = list(os.path.splitext(g))
pieces[0] = pieces[0][:-3] + "(P)x"
newFile = "".join(pieces)
vLayer = QgsVectorLayer(g,"any name","ogr")
idx = vLayer.fieldNameIndex('Rank')
Rank_value = vLayer.uniqueValues(idx)[0]
general.runalg("qgis:advancedpythonfieldcalculator", outputs_1['SAVENAME'], 'Rank', 1, 10, 0, False, "value=%f" % Rank_value, newFile)