I want to add another condition in my code.
This is my code:
tablePT = QgsVectorLayer('None', 'table_PT', 'memory')
tablePT.dataProvider().addAttributes(
[QgsField("Type", QVariant.String), QgsField("FT", QVariant.Int),
QgsField("FREE", QVariant.Int), QgsField("PRIVE", QVariant.Int),
])
tablePT.updateFields()
dicoPT = {'CHAMBRE': (0, 0, 0), 'POTEAU': (0, 0, 0)}
nbPTTotal = 0
for pt in self.couchePT.getFeatures() :
typeStruc = pt['TYPE_STRUC']
proprietaire = pt['PROPRIETAI']
if typeStruc in ['CHAMBRE', 'POTEAU'] :
if proprietaire == 'FT' :
dicoPT[typeStruc] = tuple(map(operator.add, (1, 0, 0), dicoPT[typeStruc]))
nbPTTotal += 1
elif proprietaire == 'FREE MOBILE' :
dicoPT[typeStruc] = tuple(map(operator.add, (0, 1, 0), dicoPT[typeStruc]))
nbPTTotal += 1
elif proprietaire == 'PRIVE' :
dicoPT[typeStruc] = tuple(map(operator.add, (0, 0, 1), dicoPT[typeStruc]))
nbPTTotal += 1
else:
QMessageBox.critical(None, "Problème de PT",
"Le point_technique %s possède un propriétaire qui ne fait pas partie de la liste." % str(pt['NOM']))
else:
QMessageBox.critical(None, "Problème de PT",
"Le point_technique %s possède un type de strcture qui ne fait pas partie de la liste." % str(pt['NOM']))
ligneChambre = QgsFeature()
ligneChambre.setAttributes(['chambres', dicoPT['CHAMBRE'][0], dicoPT['CHAMBRE'][1], dicoPT['CHAMBRE'][2]])
lignePoteau = QgsFeature()
lignePoteau.setAttributes(['poteaux', dicoPT['POTEAU'][0], dicoPT['POTEAU'][1], dicoPT['POTEAU'][2]])
ligneTotaux = QgsFeature()
ligneTotaux.setAttributes(['Total général', "", "", nbPTTotal])
tablePT.dataProvider().addFeatures([ligneChambre])
tablePT.dataProvider().addFeatures([lignePoteau])
tablePT.dataProvider().addFeatures([ligneTotaux])
QgsProject.instance().addMapLayer(tablePT)
This is my table for now:
but I want to get information based on two condition just like this:
I want a table with rows that result from two condition in the same time:
First row = (case when typeStruc='CHAMBRE' and ETAT='EN SERVICE') => "CHAMBRE EXISTANT"
Second row = (case when typeStruc='CHAMBRE' and ETAT='EN ETUDE') => "CHAMBRE A POSE"
Third row = (case when typeStruc='POTEAU' and ETAT='EN ETUDE') => "POTEAU EXISTANT"
Fourth row = (case when typeStruc='POTEAU' and ETAT='EN ETUDE') => "POTEAU A POSE"
asked Dec 28, 2022 at 15:37
-
Yes i add it but i don’t know how to add it in setattributesFatine MOBARIK– Fatine MOBARIK2022年12月29日 00:11:35 +00:00Commented Dec 29, 2022 at 0:11
1 Answer 1
Use this script. I guess it works for you.
layer = QgsProject.instance().mapLayersByName("PT")[0]
tablePT = QgsVectorLayer("None", "table_PT", "memory")
tablePT.dataProvider().addAttributes(
[QgsField("Type", QVariant.String),
QgsField("FT", QVariant.Int),
QgsField("FREE", QVariant.Int),
QgsField("PRIVE", QVariant.Int)])
tablePT.updateFields()
# types
t = ["Chambre Existant", "Chambre a poser", "Poteau Existant", "Poteau a poser"]
d = {t[0]: {"FT": 0, "FREE MOBILE": 0, "PRIVE": 0},
t[1]: {"FT": 0, "FREE MOBILE": 0, "PRIVE": 0},
t[2]: {"FT": 0, "FREE MOBILE": 0, "PRIVE": 0},
t[3]: {"FT": 0, "FREE MOBILE": 0, "PRIVE": 0}}
for pt in layer.getFeatures():
stru = pt["TYPE_STRUC"]
etat = pt["ETAT"]
prop = pt["PROPRIETAI"]
if prop in ["FT", "FREE MOBILE", "PRIVE"]:
if stru == "CHAMBRE":
if etat == "EN SERVICE":
d[t[0]][prop] += 1
elif etat == "EN ETUDE":
d[t[1]][prop] += 1
elif stru == "POTEAU":
if etat == "EN SERVICE":
d[t[2]][prop] += 1
elif etat == "EN ETUDE":
d[t[3]][prop] += 1
else:
print(f"Le point_technique {pt['NOM']} possède un type de strcture ...")
else:
print(f"Le point_technique {pt['NOM']} possède un propriétaire ...")
for i in [0, 1, 2, 3]:
feature = QgsFeature(tablePT.fields())
feature.setAttributes([ t[i], d[t[i]]["FT"], d[t[i]]["FREE MOBILE"], d[t[i]]["PRIVE"] ])
tablePT.dataProvider().addFeatures([feature])
QgsProject.instance().addMapLayer(tablePT)
Sample data:
TYPE_STRUC | ETAT | PROPRIETAI | NOM |
---|---|---|---|
CHAMBRE | EN ETUDE | FREE MOBILE | 9 |
EN ETUDE | FREE MOBILE | 8 | |
POTEAU | EN ETUDE | FREE MOBILE | 7 |
CHAMBRE | EN ETUDE | FREE MOBILE | 6 |
CHAMBRE | EN ETUDE | FREE MOBILE | 5 |
EN ETUDE | FREE MOBILE | 4 | |
CHAMBRE | EN ETUDE | FREE MOBILE | 3 |
POTEAU | EN ETUDE | 2 | |
CHAMBRE | EN SERVICE | PRIVE | 15 |
POTEAU | EN SERVICE | PRIVE | 14 |
CHAMBRE | EN SERVICE | FT | 13 |
POTEAU | EN SERVICE | FT | 12 |
POTEAU | EN SERVICE | FT | 11 |
CHAMBRE | EN SERVICE | FT | 10 |
CHAMBRE | EN SERVICE | 1 |
Result for the sample data:
answered Dec 29, 2022 at 17:20
Explore related questions
See similar questions with these tags.
lang-py