So I have this script which works great:
import time
import processing
from os.path import join, normpath
start_tot = time.time()
path = normpath('C:\Users\hejhej\Dropbox\Documents\')
ext = ".shp"
layers = ["a", "b", "c"]
municipalities = ["1", "2", "3"]
for layer in layers:
start = time.time()
for value in municipalities:
input_layer = join(path, layer + ext)
input_municipality = join(path, value + ext)
alg_params1 = {
'INPUT' : input_layer,
'OVERLAY' : input_municipality,
'OUTPUT' : 'memory:'
}
res1 = processing.run("native:clip", alg_params1)['OUTPUT']
output_layer = join(path, layer + "_" + value + ext)
alg_params2 = {
'INPUT' : res1,
'OUTPUT' : output_layer
}
res2 = processing.run("native:multiparttosingleparts", alg_params2)
iface.addVectorLayer(output_layer, 'counties', 'ogr')
end = time.time()
print("Elapsed time for", layer+"_"+value, ":", end-start, "seconds.")
end_tot = time.time()
print()
print("Total time elapsed:", (end_tot-start_tot)/60, "minutes.")
Which I give full credit to @taras
I would like to upgrade this fine script by making the script put the various layers into the correct group and sub group as well as give each layer its designated colour, instead of having to do this manually afterwards.
I guess the nesting (nested lists/dictionaries) is something I can sort out myself, once I have the correct lines in place for the other stuff.
How would I make all the newly created layers to be red and to end up in a subgroup called "luke" which is in a group called "lucky"?
I have been looking at solutions such as Adding layer to group using PyQGIS for grouping and Changing color of vector layer using PyQGIS for colouring, but I can't quite figure it out myself.
edit:
This is now how the script has evolved:
import time
import processing
from os.path import join, normpath
start_tot = time.time()
root = QgsProject.instance().layerTreeRoot()
path = normpath('C:\Users\tlind\Dropbox\Documents\')
ext = ".shp"
layers = ["1", "2", "3"]
municipalities = ["a", "b", "c"]
for layer in layers:
start = time.time()
for value in municipalities:
input_layer = join(path, layer + ext)
input_municipality = join(path, value + ext)
alg_params1 = {
'INPUT' : input_layer,
'OVERLAY' : input_municipality,
'OUTPUT' : 'memory:'
}
res1 = processing.run("native:clip", alg_params1)['OUTPUT']
output_layer = join(path, layer + "_" + value + ext)
alg_params2 = {
'INPUT' : res1,
'OUTPUT' : output_layer
}
res2 = processing.run("native:multiparttosingleparts", alg_params2)
vlayer = iface.addVectorLayer(output_layer, "", "ogr")
vlayer.renderer().symbol().setColor(QColor("blue"))
vlayer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(vlayer.id())
moving = QgsProject.instance().mapLayersByName(layer+"_"+value)[0]
mylayer = root.findLayer(moving.id())
myClone = mylayer.clone()
parent = mylayer.parent()
group = root.findGroup("kalmar")
subgroup = group.findGroup(value)
subgroup.insertChildNode(0, myClone)
parent.removeChildNode(mylayer)
end = time.time()
print("Elapsed time for", layer+"_"+value, ":", end-start, "seconds.")
end_tot = time.time()
print()
print("Total time elapsed:", (end_tot-start_tot)/60, "minutes.")
I've managed to reach a step where everything is working as it should be, although I want to go deeper. What I want now may be outside PyQGIS and actually just some standard Python feature, but I would like to give different colours to the layers depending on the "layers" array.
Is there anyone who has any bright ideas?
If you check the last pasted script, I did manage to sort out the grouping.
– Tobias Lind Nov 01 '22 at 09:56