2

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] > thisArea0.9 and perceel.attributes()[idx] < thisArea1.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

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Nick
  • 21
  • 1
  • you missed to add fields to the memory layer, basically like described in this post: https://gis.stackexchange.com/a/205956/87346 – eurojam Feb 16 '21 at 16:50
  • @eurojam And I think you should add "vl.updateExtents()" before adding map to Qgis. "updateExtents():Update the extents for the layer. This is necessary if features are added/deleted or the layer has been subsetted." – Gakovî Feb 16 '21 at 18:41

0 Answers0