Unfortunately, the two layers don't cover the same size of area and therefore don't have the same number of attributes. That's the reason I only saw the following attempt, instead of simply zip them. So until now I tried to find attribute value matches between two layers by running a for loop through one layer within a for loop through the other one, to be able to check with an if statement, if the two layers attribute values are identical or not... It's not working correctly! And also not efficient enough, since I have to check all streets of an entire city (do I have something wrong? But I don't get an error message...?)
Or is there a more elegant way I don't know yet to complete and speed up this task?
here my code so far:
def addFeatures(): polylines = QgsMapLayerRegistry.instance().mapLayersByName('polylines') itlayer = QgsMapLayerRegistry.instance().mapLayersByName('itpoint') tomaxspeed = itlayer.fieldNameIndex('tempo') tolanes = itlayer.fieldNameIndex('lanes') fspeed = polylines.fieldNameIndex('maxspeed') flanes = polylines.fieldNameIndex('lanes') itlayer.startEditing() for feat in itlayer.getFeatures(): for ft in polylines.getFeatures(): frommaxspeed = ft.attribute('maxspeed') fromlanes = ft.attribute('lanes') if ft.attribute('name') == feat.attribute('name'): if frommaxspeed: #print (maxspeed) itlayer.changeAttributeValue(feat.id(), tomaxspeed, frommaxspeed) if fromlanes: itlayer.changeAttributeValue(feat.id(), tolanes, fromlanes) itlayer.commitChanges() itlayer.updateFields()
fixed line 18 and it works now as it should (stupid failure). But still, the perfomance is terrible and QGIS is not reacting for about 15 min while executing this function.
Doesn't seem to me, that it should be like this!?
Therefore, does PyQGIS offer a different and more efficient way?