Skip to main content
Notice removed Reward existing answer by Joseph
Bounty Ended with scottbb's answer chosen by Joseph
added 82 characters in body; edited title
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Composing SQLQGIS expression based on a QGIS layer tree

I am developing a plugin for a GIS (Geographic Information Systems) software which uses Python. I have been using Python for over a year (on-off) and have the following code which works. It basically reads the names of files loaded into the GIS software and adds some string texts and puts these intoto compose a listQGIS expression for every group of files. Finally it deletes the list, ready for the next group.

EDIT:

Composing SQL expression based on a QGIS layer tree

I am developing a plugin for a GIS (Geographic Information Systems) software which uses Python. I have been using Python for over a year (on-off) and have the following code which works. It basically reads the names of files loaded into the GIS software and adds some string texts and puts these into a list for every group of files. Finally it deletes the list, ready for the next group.

EDIT:

Composing QGIS expression based on a QGIS layer tree

I am developing a plugin for a GIS (Geographic Information Systems) software which uses Python. I have been using Python for over a year (on-off) and have the following code which works. It basically reads the names of files loaded into the GIS software to compose a QGIS expression for every group of files. Finally it deletes the list, ready for the next group.

Rollback to Revision 4
Source Link
Mast
  • 13.8k
  • 12
  • 57
  • 127
root = QgsProject.instance().layerTreeRoot()
policy_group = root.findGroup('Name''Names')
formula_score4 = []
formula_score3a = []
formula_score3 = []
formula_score2a = []
formula_score2 = []
formula_score1 = []
for group in policy_group.children():
    for layer in group.children():
        score4 = '"' + layer.layerName() + '_Score' + '"' + ' = 4 OR '
        formula_score4.append(score4)
    for layer in group.children():
        score3a = 'coalesce(' + '"' + layer.layerName() + '_Score' + '"' + ' = 3, 0.00)*3 OR '
        formula_score3a.append(score3a)
    for layer in group.children():
        score3 = '"' + layer.layerName() + '_Score' + '"' + ' = 3 OR '
        formula_score3.append(score3)
    for layer in group.children():
        score2a = 'coalesce(' + '"' + layer.layerName() + '_Score' + '"' + ' = 2, 0.00)*2 OR '
        formula_score2a.append(score2a)
    for layer in group.children():
        score2 = '"' + layer.layerName() + '_Score' + '"' + ' = 2 OR '
        formula_score2.append(score2)
    for layer in group.children():
        score1 = '"' + layer.layerName() + '_Score' + '"' + ' = 1 OR '
        formula_score1.append(score1)   
    formxformula1 = "CASE WHEN " + "".join(str(x) for x in formula_score4) + "".join(str(x) for x in formula_score3a) + ">=9 THEN 4 " + "WHEN " + "".join(str(x) for x in formula_score3) + "".join(str(x) for x in formula_score2a) + ">=6 THEN 3 " + "WHEN " + "".join(str(x) for x in formula_score2) + "THEN 2 " + "WHEN " + "".join(str(x) for x in formula_score1) + "THEN 1 ELSE 1 END"
    new_formformula2 = formxformula1.replace("OR >=9 THEN 4 ", ">=9 THEN 4 ")
    new_form2formula3 = new_formformula2.replace("OR >=6 THEN 3 ", ">=6 THEN 3 ")
    new_form3formula4 = new_form2formula3.replace("OR THEN 2 ", "THEN 2 ")
    formulafinal_formula = new_form3formula4.replace("OR THEN 1 ELSE 1 END", "THEN 1 ELSE 1 END")
    formula
    del formula_score4[:]
    del formula_score3a[:]
    del formula_score3[:]
    del formula_score2a[:]
    del formula_score2[:]
    del formula_score1[:]

Executing code and printing outputCode executed and result shown in QGIS Python Console

'CASE WHEN "Layer_1a_Score" = 4 OR 
"Layer_1b_Score" = 4 OR 
"Layer_1c_Score" = 4 OR 
"Layer_1d_Score" = 4 OR 
"Layer_1e_Score" = 4 OR 
"Layer_1f_Score" = 4 OR 
coalesce("Layer_1a_Score"Layer_1a = 3, 0.00)*3 OR 
coalesce("Layer_1b_Score"Layer_1b = 3, 0.00)*3 OR 
coalesce("Layer_1c_Score"Layer_1c = 3, 0.00)*3 OR 
coalesce("Layer_1d_Score"Layer_1d = 3, 0.00)*3 OR 
coalesce("Layer_1e_Score"Layer_1e = 3, 0.00)*3 OR 
coalesce("Layer_1f_Score"Layer_1f = 3, 0.00)*3 >=9 
THEN 4 WHEN 
"Layer_1a_Score" = 3 OR 
"Layer_1b_Score" = 3 OR 
"Layer_1c_Score" = 3 OR 
"Layer_1d_Score" = 3 OR 
"Layer_1e_Score" = 3 OR 
"Layer_1f_Score" = 3 OR 
coalesce("Layer_1a_Score"Layer_1a = 2, 0.00)*2 OR 
coalesce("Layer_1b_Score"Layer_1b = 2, 0.00)*2 OR 
coalesce("Layer_1c_Score"Layer_1c = 2, 0.00)*2 OR 
coalesce("Layer_1d_Score"Layer_1d = 2, 0.00)*2 OR 
coalesce("Layer_1e_Score"Layer_1e = 2, 0.00)*2 OR 
coalesce("Layer_1f_Score"Layer_1f = 2, 0.00)*2 >=6 
THEN 3 WHEN 
"Layer_1a_Score" = 2 OR 
"Layer_1b_Score" = 2 OR 
"Layer_1c_Score" = 2 OR 
"Layer_1d_Score" = 2 OR 
"Layer_1e_Score" = 2 OR 
"Layer_1f_Score" = 2 
THEN 2 WHEN 
"Layer_1a_Score" = 1 OR 
"Layer_1b_Score" = 1 OR 
"Layer_1c_Score" = 1 OR 
"Layer_1d_Score" = 1 OR 
"Layer_1e_Score" = 1 OR 
"Layer_1f_Score" = 1
THEN 1 ELSE 1 END'
...
root = QgsProject.instance().layerTreeRoot()
policy_group = root.findGroup('Name')
formula_score4 = []
formula_score3a = []
formula_score3 = []
formula_score2a = []
formula_score2 = []
formula_score1 = []
for group in policy_group.children():
    for layer in group.children():
        score4 = '"' + layer.layerName() + '_Score' + '"' + ' = 4 OR '
        formula_score4.append(score4)
    for layer in group.children():
        score3a = 'coalesce(' + '"' + layer.layerName() + '_Score' + '"' + ' = 3, 0.00)*3 OR '
        formula_score3a.append(score3a)
    for layer in group.children():
        score3 = '"' + layer.layerName() + '_Score' + '"' + ' = 3 OR '
        formula_score3.append(score3)
    for layer in group.children():
        score2a = 'coalesce(' + '"' + layer.layerName() + '_Score' + '"' + ' = 2, 0.00)*2 OR '
        formula_score2a.append(score2a)
    for layer in group.children():
        score2 = '"' + layer.layerName() + '_Score' + '"' + ' = 2 OR '
        formula_score2.append(score2)
    for layer in group.children():
        score1 = '"' + layer.layerName() + '_Score' + '"' + ' = 1 OR '
        formula_score1.append(score1)   
    formx = "CASE WHEN " + "".join(str(x) for x in formula_score4) + "".join(str(x) for x in formula_score3a) + ">=9 THEN 4 " + "WHEN " + "".join(str(x) for x in formula_score3) + "".join(str(x) for x in formula_score2a) + ">=6 THEN 3 " + "WHEN " + "".join(str(x) for x in formula_score2) + "THEN 2 " + "WHEN " + "".join(str(x) for x in formula_score1) + "THEN 1 ELSE 1 END"
    new_form = formx.replace("OR >=9 THEN 4 ", ">=9 THEN 4 ")
    new_form2 = new_form.replace("OR >=6 THEN 3 ", ">=6 THEN 3 ")
    new_form3 = new_form2.replace("OR THEN 2 ", "THEN 2 ")
    formula = new_form3.replace("OR THEN 1 ELSE 1 END", "THEN 1 ELSE 1 END")
    formula
    del formula_score4[:]
    del formula_score3a[:]
    del formula_score3[:]
    del formula_score2a[:]
    del formula_score2[:]
    del formula_score1[:]

Executing code and printing output

'CASE WHEN "Layer_1a_Score" = 4 OR 
"Layer_1b_Score" = 4 OR 
"Layer_1c_Score" = 4 OR 
"Layer_1d_Score" = 4 OR 
"Layer_1e_Score" = 4 OR 
"Layer_1f_Score" = 4 OR 
coalesce("Layer_1a_Score" = 3, 0.00)*3 OR 
coalesce("Layer_1b_Score" = 3, 0.00)*3 OR 
coalesce("Layer_1c_Score" = 3, 0.00)*3 OR 
coalesce("Layer_1d_Score" = 3, 0.00)*3 OR 
coalesce("Layer_1e_Score" = 3, 0.00)*3 OR 
coalesce("Layer_1f_Score" = 3, 0.00)*3 >=9 
THEN 4 WHEN 
"Layer_1a_Score" = 3 OR 
"Layer_1b_Score" = 3 OR 
"Layer_1c_Score" = 3 OR 
"Layer_1d_Score" = 3 OR 
"Layer_1e_Score" = 3 OR 
"Layer_1f_Score" = 3 OR 
coalesce("Layer_1a_Score" = 2, 0.00)*2 OR 
coalesce("Layer_1b_Score" = 2, 0.00)*2 OR 
coalesce("Layer_1c_Score" = 2, 0.00)*2 OR 
coalesce("Layer_1d_Score" = 2, 0.00)*2 OR 
coalesce("Layer_1e_Score" = 2, 0.00)*2 OR 
coalesce("Layer_1f_Score" = 2, 0.00)*2 >=6 
THEN 3 WHEN 
"Layer_1a_Score" = 2 OR 
"Layer_1b_Score" = 2 OR 
"Layer_1c_Score" = 2 OR 
"Layer_1d_Score" = 2 OR 
"Layer_1e_Score" = 2 OR 
"Layer_1f_Score" = 2 
THEN 2 WHEN 
"Layer_1a_Score" = 1 OR 
"Layer_1b_Score" = 1 OR 
"Layer_1c_Score" = 1 OR 
"Layer_1d_Score" = 1 OR 
"Layer_1e_Score" = 1 OR 
"Layer_1f_Score" = 1
THEN 1 ELSE 1 END'
...
root = QgsProject.instance().layerTreeRoot()
policy_group = root.findGroup('Names')
formula_score4 = []
formula_score3a = []
formula_score3 = []
formula_score2a = []
formula_score2 = []
formula_score1 = []
for group in policy_group.children():
    for layer in group.children():
        score4 = '"' + layer.layerName() + '_Score' + '"' + ' = 4 OR '
        formula_score4.append(score4)
    for layer in group.children():
        score3a = 'coalesce(' + layer.layerName() + ' = 3, 0.00)*3 OR '
        formula_score3a.append(score3a)
    for layer in group.children():
        score3 = '"' + layer.layerName() + '_Score' + '"' + ' = 3 OR '
        formula_score3.append(score3)
    for layer in group.children():
        score2a = 'coalesce(' + layer.layerName() + ' = 2, 0.00)*2 OR '
        formula_score2a.append(score2a)
    for layer in group.children():
        score2 = '"' + layer.layerName() + '_Score' + '"' + ' = 2 OR '
        formula_score2.append(score2)
    for layer in group.children():
        score1 = '"' + layer.layerName() + '_Score' + '"' + ' = 1 OR '
        formula_score1.append(score1)   
    formula1 = "CASE WHEN " + "".join(str(x) for x in formula_score4) + "".join(str(x) for x in formula_score3a) + ">=9 THEN 4 " + "WHEN " + "".join(str(x) for x in formula_score3) + "".join(str(x) for x in formula_score2a) + ">=6 THEN 3 " + "WHEN " + "".join(str(x) for x in formula_score2) + "THEN 2 " + "WHEN " + "".join(str(x) for x in formula_score1) + "THEN 1 ELSE 1 END"
    formula2 = formula1.replace("OR >=9 THEN 4 ", ">=9 THEN 4 ")
    formula3 = formula2.replace("OR >=6 THEN 3 ", ">=6 THEN 3 ")
    formula4 = formula3.replace("OR THEN 2 ", "THEN 2 ")
    final_formula = formula4.replace("OR THEN 1 ELSE 1 END", "THEN 1 ELSE 1 END")
    del formula_score4[:]
    del formula_score3a[:]
    del formula_score3[:]
    del formula_score2a[:]
    del formula_score2[:]
    del formula_score1[:]

Code executed and result shown in QGIS Python Console

'CASE WHEN "Layer_1a_Score" = 4 OR 
"Layer_1b_Score" = 4 OR 
"Layer_1c_Score" = 4 OR 
"Layer_1d_Score" = 4 OR 
"Layer_1e_Score" = 4 OR 
"Layer_1f_Score" = 4 OR 
coalesce(Layer_1a = 3, 0.00)*3 OR 
coalesce(Layer_1b = 3, 0.00)*3 OR 
coalesce(Layer_1c = 3, 0.00)*3 OR 
coalesce(Layer_1d = 3, 0.00)*3 OR 
coalesce(Layer_1e = 3, 0.00)*3 OR 
coalesce(Layer_1f = 3, 0.00)*3 >=9 
THEN 4 WHEN 
"Layer_1a_Score" = 3 OR 
"Layer_1b_Score" = 3 OR 
"Layer_1c_Score" = 3 OR 
"Layer_1d_Score" = 3 OR 
"Layer_1e_Score" = 3 OR 
"Layer_1f_Score" = 3 OR 
coalesce(Layer_1a = 2, 0.00)*2 OR 
coalesce(Layer_1b = 2, 0.00)*2 OR 
coalesce(Layer_1c = 2, 0.00)*2 OR 
coalesce(Layer_1d = 2, 0.00)*2 OR 
coalesce(Layer_1e = 2, 0.00)*2 OR 
coalesce(Layer_1f = 2, 0.00)*2 >=6 
THEN 3 WHEN 
"Layer_1a_Score" = 2 OR 
"Layer_1b_Score" = 2 OR 
"Layer_1c_Score" = 2 OR 
"Layer_1d_Score" = 2 OR 
"Layer_1e_Score" = 2 OR 
"Layer_1f_Score" = 2 
THEN 2 WHEN 
"Layer_1a_Score" = 1 OR 
"Layer_1b_Score" = 1 OR 
"Layer_1c_Score" = 1 OR 
"Layer_1d_Score" = 1 OR 
"Layer_1e_Score" = 1 OR 
"Layer_1f_Score" = 1
THEN 1 ELSE 1 END'
...
Corrected mistake
Source Link
Joseph
  • 373
  • 2
  • 12
root = QgsProject.instance().layerTreeRoot()
policy_group = root.findGroup('Names''Name')
formula_score4 = []
formula_score3a = []
formula_score3 = []
formula_score2a = []
formula_score2 = []
formula_score1 = []
for group in policy_group.children():
    for layer in group.children():
        score4 = '"' + layer.layerName() + '_Score' + '"' + ' = 4 OR '
        formula_score4.append(score4)
    for layer in group.children():
        score3a = 'coalesce(' + '"' + layer.layerName() + '_Score' + '"' + ' = 3, 0.00)*3 OR '
        formula_score3a.append(score3a)
    for layer in group.children():
        score3 = '"' + layer.layerName() + '_Score' + '"' + ' = 3 OR '
        formula_score3.append(score3)
    for layer in group.children():
        score2a = 'coalesce(' + '"' + layer.layerName() + '_Score' + '"' + ' = 2, 0.00)*2 OR '
        formula_score2a.append(score2a)
    for layer in group.children():
        score2 = '"' + layer.layerName() + '_Score' + '"' + ' = 2 OR '
        formula_score2.append(score2)
    for layer in group.children():
        score1 = '"' + layer.layerName() + '_Score' + '"' + ' = 1 OR '
        formula_score1.append(score1)   
    formula1formx = "CASE WHEN " + "".join(str(x) for x in formula_score4) + "".join(str(x) for x in formula_score3a) + ">=9 THEN 4 " + "WHEN " + "".join(str(x) for x in formula_score3) + "".join(str(x) for x in formula_score2a) + ">=6 THEN 3 " + "WHEN " + "".join(str(x) for x in formula_score2) + "THEN 2 " + "WHEN " + "".join(str(x) for x in formula_score1) + "THEN 1 ELSE 1 END"
    formula2new_form = formula1formx.replace("OR >=9 THEN 4 ", ">=9 THEN 4 ")
    formula3new_form2 = formula2new_form.replace("OR >=6 THEN 3 ", ">=6 THEN 3 ")
    formula4new_form3 = formula3new_form2.replace("OR THEN 2 ", "THEN 2 ")
    final_formulaformula = formula4new_form3.replace("OR THEN 1 ELSE 1 END", "THEN 1 ELSE 1 END")
    formula
    del formula_score4[:]
    del formula_score3a[:]
    del formula_score3[:]
    del formula_score2a[:]
    del formula_score2[:]
    del formula_score1[:]

Code executed and result shown in QGIS Python ConsoleExecuting code and printing output

'CASE WHEN "Layer_1a_Score" = 4 OR 
"Layer_1b_Score" = 4 OR 
"Layer_1c_Score" = 4 OR 
"Layer_1d_Score" = 4 OR 
"Layer_1e_Score" = 4 OR 
"Layer_1f_Score" = 4 OR 
coalesce(Layer_1a"Layer_1a_Score" = 3, 0.00)*3 OR 
coalesce(Layer_1b"Layer_1b_Score" = 3, 0.00)*3 OR 
coalesce(Layer_1c"Layer_1c_Score" = 3, 0.00)*3 OR 
coalesce(Layer_1d"Layer_1d_Score" = 3, 0.00)*3 OR 
coalesce(Layer_1e"Layer_1e_Score" = 3, 0.00)*3 OR 
coalesce(Layer_1f"Layer_1f_Score" = 3, 0.00)*3 >=9 
THEN 4 WHEN 
"Layer_1a_Score" = 3 OR 
"Layer_1b_Score" = 3 OR 
"Layer_1c_Score" = 3 OR 
"Layer_1d_Score" = 3 OR 
"Layer_1e_Score" = 3 OR 
"Layer_1f_Score" = 3 OR 
coalesce(Layer_1a"Layer_1a_Score" = 2, 0.00)*2 OR 
coalesce(Layer_1b"Layer_1b_Score" = 2, 0.00)*2 OR 
coalesce(Layer_1c"Layer_1c_Score" = 2, 0.00)*2 OR 
coalesce(Layer_1d"Layer_1d_Score" = 2, 0.00)*2 OR 
coalesce(Layer_1e"Layer_1e_Score" = 2, 0.00)*2 OR 
coalesce(Layer_1f"Layer_1f_Score" = 2, 0.00)*2 >=6 
THEN 3 WHEN 
"Layer_1a_Score" = 2 OR 
"Layer_1b_Score" = 2 OR 
"Layer_1c_Score" = 2 OR 
"Layer_1d_Score" = 2 OR 
"Layer_1e_Score" = 2 OR 
"Layer_1f_Score" = 2 
THEN 2 WHEN 
"Layer_1a_Score" = 1 OR 
"Layer_1b_Score" = 1 OR 
"Layer_1c_Score" = 1 OR 
"Layer_1d_Score" = 1 OR 
"Layer_1e_Score" = 1 OR 
"Layer_1f_Score" = 1
THEN 1 ELSE 1 END'
...
root = QgsProject.instance().layerTreeRoot()
policy_group = root.findGroup('Names')
formula_score4 = []
formula_score3a = []
formula_score3 = []
formula_score2a = []
formula_score2 = []
formula_score1 = []
for group in policy_group.children():
    for layer in group.children():
        score4 = '"' + layer.layerName() + '_Score' + '"' + ' = 4 OR '
        formula_score4.append(score4)
    for layer in group.children():
        score3a = 'coalesce(' + layer.layerName() + ' = 3, 0.00)*3 OR '
        formula_score3a.append(score3a)
    for layer in group.children():
        score3 = '"' + layer.layerName() + '_Score' + '"' + ' = 3 OR '
        formula_score3.append(score3)
    for layer in group.children():
        score2a = 'coalesce(' + layer.layerName() + ' = 2, 0.00)*2 OR '
        formula_score2a.append(score2a)
    for layer in group.children():
        score2 = '"' + layer.layerName() + '_Score' + '"' + ' = 2 OR '
        formula_score2.append(score2)
    for layer in group.children():
        score1 = '"' + layer.layerName() + '_Score' + '"' + ' = 1 OR '
        formula_score1.append(score1)   
    formula1 = "CASE WHEN " + "".join(str(x) for x in formula_score4) + "".join(str(x) for x in formula_score3a) + ">=9 THEN 4 " + "WHEN " + "".join(str(x) for x in formula_score3) + "".join(str(x) for x in formula_score2a) + ">=6 THEN 3 " + "WHEN " + "".join(str(x) for x in formula_score2) + "THEN 2 " + "WHEN " + "".join(str(x) for x in formula_score1) + "THEN 1 ELSE 1 END"
    formula2 = formula1.replace("OR >=9 THEN 4 ", ">=9 THEN 4 ")
    formula3 = formula2.replace("OR >=6 THEN 3 ", ">=6 THEN 3 ")
    formula4 = formula3.replace("OR THEN 2 ", "THEN 2 ")
    final_formula = formula4.replace("OR THEN 1 ELSE 1 END", "THEN 1 ELSE 1 END")
    del formula_score4[:]
    del formula_score3a[:]
    del formula_score3[:]
    del formula_score2a[:]
    del formula_score2[:]
    del formula_score1[:]

Code executed and result shown in QGIS Python Console

'CASE WHEN "Layer_1a_Score" = 4 OR 
"Layer_1b_Score" = 4 OR 
"Layer_1c_Score" = 4 OR 
"Layer_1d_Score" = 4 OR 
"Layer_1e_Score" = 4 OR 
"Layer_1f_Score" = 4 OR 
coalesce(Layer_1a = 3, 0.00)*3 OR 
coalesce(Layer_1b = 3, 0.00)*3 OR 
coalesce(Layer_1c = 3, 0.00)*3 OR 
coalesce(Layer_1d = 3, 0.00)*3 OR 
coalesce(Layer_1e = 3, 0.00)*3 OR 
coalesce(Layer_1f = 3, 0.00)*3 >=9 
THEN 4 WHEN 
"Layer_1a_Score" = 3 OR 
"Layer_1b_Score" = 3 OR 
"Layer_1c_Score" = 3 OR 
"Layer_1d_Score" = 3 OR 
"Layer_1e_Score" = 3 OR 
"Layer_1f_Score" = 3 OR 
coalesce(Layer_1a = 2, 0.00)*2 OR 
coalesce(Layer_1b = 2, 0.00)*2 OR 
coalesce(Layer_1c = 2, 0.00)*2 OR 
coalesce(Layer_1d = 2, 0.00)*2 OR 
coalesce(Layer_1e = 2, 0.00)*2 OR 
coalesce(Layer_1f = 2, 0.00)*2 >=6 
THEN 3 WHEN 
"Layer_1a_Score" = 2 OR 
"Layer_1b_Score" = 2 OR 
"Layer_1c_Score" = 2 OR 
"Layer_1d_Score" = 2 OR 
"Layer_1e_Score" = 2 OR 
"Layer_1f_Score" = 2 
THEN 2 WHEN 
"Layer_1a_Score" = 1 OR 
"Layer_1b_Score" = 1 OR 
"Layer_1c_Score" = 1 OR 
"Layer_1d_Score" = 1 OR 
"Layer_1e_Score" = 1 OR 
"Layer_1f_Score" = 1
THEN 1 ELSE 1 END'
...
root = QgsProject.instance().layerTreeRoot()
policy_group = root.findGroup('Name')
formula_score4 = []
formula_score3a = []
formula_score3 = []
formula_score2a = []
formula_score2 = []
formula_score1 = []
for group in policy_group.children():
    for layer in group.children():
        score4 = '"' + layer.layerName() + '_Score' + '"' + ' = 4 OR '
        formula_score4.append(score4)
    for layer in group.children():
        score3a = 'coalesce(' + '"' + layer.layerName() + '_Score' + '"' + ' = 3, 0.00)*3 OR '
        formula_score3a.append(score3a)
    for layer in group.children():
        score3 = '"' + layer.layerName() + '_Score' + '"' + ' = 3 OR '
        formula_score3.append(score3)
    for layer in group.children():
        score2a = 'coalesce(' + '"' + layer.layerName() + '_Score' + '"' + ' = 2, 0.00)*2 OR '
        formula_score2a.append(score2a)
    for layer in group.children():
        score2 = '"' + layer.layerName() + '_Score' + '"' + ' = 2 OR '
        formula_score2.append(score2)
    for layer in group.children():
        score1 = '"' + layer.layerName() + '_Score' + '"' + ' = 1 OR '
        formula_score1.append(score1)   
    formx = "CASE WHEN " + "".join(str(x) for x in formula_score4) + "".join(str(x) for x in formula_score3a) + ">=9 THEN 4 " + "WHEN " + "".join(str(x) for x in formula_score3) + "".join(str(x) for x in formula_score2a) + ">=6 THEN 3 " + "WHEN " + "".join(str(x) for x in formula_score2) + "THEN 2 " + "WHEN " + "".join(str(x) for x in formula_score1) + "THEN 1 ELSE 1 END"
    new_form = formx.replace("OR >=9 THEN 4 ", ">=9 THEN 4 ")
    new_form2 = new_form.replace("OR >=6 THEN 3 ", ">=6 THEN 3 ")
    new_form3 = new_form2.replace("OR THEN 2 ", "THEN 2 ")
    formula = new_form3.replace("OR THEN 1 ELSE 1 END", "THEN 1 ELSE 1 END")
    formula
    del formula_score4[:]
    del formula_score3a[:]
    del formula_score3[:]
    del formula_score2a[:]
    del formula_score2[:]
    del formula_score1[:]

Executing code and printing output

'CASE WHEN "Layer_1a_Score" = 4 OR 
"Layer_1b_Score" = 4 OR 
"Layer_1c_Score" = 4 OR 
"Layer_1d_Score" = 4 OR 
"Layer_1e_Score" = 4 OR 
"Layer_1f_Score" = 4 OR 
coalesce("Layer_1a_Score" = 3, 0.00)*3 OR 
coalesce("Layer_1b_Score" = 3, 0.00)*3 OR 
coalesce("Layer_1c_Score" = 3, 0.00)*3 OR 
coalesce("Layer_1d_Score" = 3, 0.00)*3 OR 
coalesce("Layer_1e_Score" = 3, 0.00)*3 OR 
coalesce("Layer_1f_Score" = 3, 0.00)*3 >=9 
THEN 4 WHEN 
"Layer_1a_Score" = 3 OR 
"Layer_1b_Score" = 3 OR 
"Layer_1c_Score" = 3 OR 
"Layer_1d_Score" = 3 OR 
"Layer_1e_Score" = 3 OR 
"Layer_1f_Score" = 3 OR 
coalesce("Layer_1a_Score" = 2, 0.00)*2 OR 
coalesce("Layer_1b_Score" = 2, 0.00)*2 OR 
coalesce("Layer_1c_Score" = 2, 0.00)*2 OR 
coalesce("Layer_1d_Score" = 2, 0.00)*2 OR 
coalesce("Layer_1e_Score" = 2, 0.00)*2 OR 
coalesce("Layer_1f_Score" = 2, 0.00)*2 >=6 
THEN 3 WHEN 
"Layer_1a_Score" = 2 OR 
"Layer_1b_Score" = 2 OR 
"Layer_1c_Score" = 2 OR 
"Layer_1d_Score" = 2 OR 
"Layer_1e_Score" = 2 OR 
"Layer_1f_Score" = 2 
THEN 2 WHEN 
"Layer_1a_Score" = 1 OR 
"Layer_1b_Score" = 1 OR 
"Layer_1c_Score" = 1 OR 
"Layer_1d_Score" = 1 OR 
"Layer_1e_Score" = 1 OR 
"Layer_1f_Score" = 1
THEN 1 ELSE 1 END'
...
Tweeted twitter.com/StackCodeReview/status/730805210935906304
Notice added Reward existing answer by Joseph
Bounty Started worth 50 reputation by Joseph
Added sample output in response to comment
Source Link
Joseph
  • 373
  • 2
  • 12
Loading
edited tags; edited title
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
Loading
Edited title to make it more specific
Link
Joseph
  • 373
  • 2
  • 12
Loading
Source Link
Joseph
  • 373
  • 2
  • 12
Loading