I am creating a plugin that generates virtual layers from other existing layers. I tried to make the process easier for me to make attribute tables by sql query but it doesn't work because layer values change from one layer to another, and this is my code:
PT_Table = QgsVectorLayer(
"?query=SELECT PROPRIETAI as Type,sum ( CASE WHEN (TYPE_STRUC='CHAMBRE' AND ETAT='EN SERVICE') THEN 1 ELSE 0 END)as chambre_existant,sum(CASE WHEN (TYPE_STRUC='CHAMBRE' AND ETAT='EN ETUDE') THEN 1 ELSE 0 END )as Chambre_a_poser, sum(CASE WHEN (TYPE_STRUC='POTEAU' AND ETAT='EN SERVICE') THEN 1 ELSE 0 END )as poteau_existant, sum(CASE WHEN (TYPE_STRUC='POTEAU' AND ETAT='EN ETUDE') THEN 1 ELSE 0 END )as poteau_a_poser FROM PT group by PROPRIETAI ",
"PT_Table",
"virtual",
)
Support_Table = QgsVectorLayer(
"?query= SELECT SUM(ROUND((s.LGR_REEL))) as 'Longueur (m)'FROM SUPPORT AS s GROUP BY s.PROPRIETAI, CASE WHEN s.PROPRIETAI = 'PRIVE' THEN NULL ELSE s.TYPE_STRUC END ORDER BY CASE s.PROPRIETAI WHEN 'FT'THEN 1 WHEN 'FREE MOBILE' THEN 2 WHEN 'PRIVE' THEN 3 ELSE 4 END",
"Support_Table",
"virtual",
)
Cable_Table = QgsVectorLayer(
"?query=select count(*) as 'Nbr de cables',CAPACITE as 'Capacité',ETAT as 'Etat', sum(ROUND(LONGUEUR_R)) as 'Longueur (m)'from CABLES group by NOM ORDER BY NOM desc",
"Cable_Table",
"virtual",
)
Bpe_Table = QgsVectorLayer(
"?query=select count(*) as 'Nb du BPE' from BPE group by ETAT ORDER BY ETAT desc",
"Bpe_Table",
"virtual",
)
Nb_Troncons = QgsVectorLayer(
"?query=SELECT sum ( CASE WHEN (TYPE_STRUC='TRANCHEE' AND PROPRIETAI='FT') THEN 1 ELSE 0 END) as 'Nb_Tronçons' from SUPPORT",
"Nb_Troncons",
"virtual",
)
QgsProject.instance().addMapLayer(PT_Table)
QgsProject.instance().addMapLayer(Support_Table)
QgsProject.instance().addMapLayer(Cable_Table)
QgsProject.instance().addMapLayer(Bpe_Table)
QgsProject.instance().addMapLayer(Nb_Troncons)
And now I want to create temporary vector layers with specific rows and columns but with the values of the existing layers