Context: the script selects features from a layer which overlaps another one above a certain percentage and creates a new layer with these selected features. I do not manage to copy the attributes from the attributes table, only the ID is reported.
What am i doing wrong?
Here is the code:
mapcanvas = iface.mapCanvas()
layers = mapcanvas.layers()
#for Severity
feats_lyr1 = [ feat for feat in layers[0].getFeatures() ]
#for Basins800
feats_lyr2 = [ feat for feat in layers[1].getFeatures() ]
selected_feats = []
print "processing..."
for i, feat1 in enumerate(feats_lyr1):
for j, feat2 in enumerate(feats_lyr2):
if feat1.geometry().intersects(feat2.geometry()):
area1 = feat1.geometry().intersection(feat2.geometry()).area()
area2 = feat2.geometry().area()
crit =area1/area2
if crit > 0.8:
selected_feats.append(feat2)
epsg = layers[0].crs().postgisSrid()
uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer""&index=yes"
mem_layer = QgsVectorLayer(uri,
"mem_layer",
"memory")
lyr = mem_layer.dataProvider()
for i, feat in enumerate(selected_feats):
feat.setAttributes(selected_feats[i].attributes())
lyr.addFeatures([feat])
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
print "done..."