3

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

RESULT

layers

1
  • You might want to provide an example of what you currently get with your script, and how that differs from your expected result, this would make it easier to understand the issue and provide some suggestions
    – Kasper
    Commented Dec 26, 2022 at 11:19

1 Answer 1

3

I can suggest using the materialize() method of the QgsFeatureSource class. The below script also utilizes setFilterFids() and setSubsetOfAttributes() methods.

Note: this is available since QGIS 3.0

from qgis.core import QgsProject

layer = QgsProject.instance().mapLayersByName("YOUR_LAYER_NAME")[0]

rows = [0, 5]  # specify ids of required features
columns = [0, 1, 2, 3]  # specify indexes of required columns

temp_layer = layer.materialize(
    QgsFeatureRequest().setFilterFids(rows).setSubsetOfAttributes(columns)
)

QgsProject.instance().addMapLayer(temp_layer)

References:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.