7

I have 3 groups of layers, layer names are the same in the 3 groups as shown in this image below:

enter image description here:

I want to make a table of 4 fields:

  1. layer names of group (1),
  2. layer lengths of group (1),
  3. Feature count of group (2),
  4. Feature count of group (3),

I am trying with this code:

#Create an empty table in memory and add fields
newtable = QgsVectorLayer("None", "Layer Length", "memory")
provider = newtable.dataProvider()
provider.addAttributes([QgsField('Layer Name', QVariant.String),
                        QgsField('Length', QVariant.Double),\
                        QgsField('Rl Count', QVariant.Double),\
                        QgsField('SL Count', QVariant.Double)])
newtable.updateFields()

group_names = ['group1', 'group2']

for name in group_names:
    group = root.findGroup(name)
    layers = [layer.layer() for layer in group.children()]
    
    for lyr in layers:
        total_length = sum([r.geometry().length() for r in lyr.getFeatures()])
        f = QgsFeature()
        f.setAttributes([lyr.name(), total_length])
        provider.addFeatures([f])

group_names = ['RL', 'SL']

for name in group_names:
    group = root.findGroup(name)
    layers = [layer.layer() for layer in group.children()]
    
    for lyr in layers:
        Featurecount = lyr.featureCount() 
        f = QgsFeature()
        f.setAttributes([lyr.name(), Featurecount])
        provider.addFeature(f)
    
QgsProject.instance().addMapLayer(newtable)

the code gives me the following table, How can I edit the code to assign the feature count numbers to the layer having the same name? as here in this image:

table I get

1 Answer 1

6

I assume all layers in the groups have the same order

root = QgsProject.instance().layerTreeRoot()

#Create an empty table in memory and add fields
newtable = QgsVectorLayer("None", "Layer Length", "memory")
provider = newtable.dataProvider()
provider.addAttributes([QgsField('Layer Name', QVariant.String),
                        QgsField('Length', QVariant.Double),
                        QgsField('RL Count', QVariant.Double),
                        QgsField('SL Count', QVariant.Double)])
newtable.updateFields()

group = root.findGroup("group1")
rl = root.findGroup("RL")
sl = root.findGroup("SL")
stack = [group.children(), rl.children(), sl.children()]

for glyr, rlyr, slyr in zip(*stack):
    total_length = sum([r.geometry().length() for r in glyr.layer().getFeatures()])
    rl_count = rlyr.layer().featureCount()
    sl_count = slyr.layer().featureCount()
    
    f = QgsFeature()
    f.setAttributes([glyr.name(), total_length, rl_count, sl_count])
    provider.addFeatures([f])
    
QgsProject.instance().addMapLayer(newtable, False)
root.insertLayer(0, newtable)

enter image description here

1
  • It's exactly what I'm looking for, It will help me a lot, I'm thankful for you, bro! Jan 26, 2022 at 8:59

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.