TransWikia.com

PyQGIS adding polygon features to temporary vector layer results in empty layer

Geographic Information Systems Asked on March 22, 2021

I’m trying to extract certain features from a layer and add them to a temporary layer to run an algorithm on in a later stage to limit processing time. Code below selects the correct features and adds them to the temporary layer, but when I check the layer, it shows no geometry and no attribute information even though I ask to copy these. It only gives the correct number of intersecting features (see image below). I’ve searched extensively for a solution for this, but can’t find an appropriate one.

What am I missing?

# create temporary layer
vl = QgsVectorLayer('Polygon', 'temporary_polygon', 'memory')
pr = vl.dataProvider()

idx = joinOutput['OUTPUT'].fields().indexFromName('OPPERVL')
for perceel in joinOutput['OUTPUT'].getFeatures():
    # to run code faster, limit to a features within a certain area interval
    if perceel.attributes()[idx] > thisArea*0.9 and perceel.attributes()[idx] < thisArea*1.1:
        for gw in GWPov.getFeatures():
            if perceel.geometry().intersects(gw.geometry()):
                # add intersecting feature to temporary layer
                feat = QgsFeature()
                feat.setGeometry(perceel.geometry())
                feat.setAttributes(perceel.attributes())
                pr.addFeature(feat)
                print('Test')

            else:
                continue
    else:
        continue

# control check, showing empty geometry and attribute table
QgsProject.instance().addMapLayers([vl])

Screenshot of features without attribute information

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP