Skip to main content
1 of 2

Copying features from one layer into another in PyQGIS using addFeatures

I am writing a plugin for QGIS 3.8

I have two layers,

  • drop_layer and
  • Fibre Cable.

I want to copy all features from the drop_layer and append to Fibre cable layer.

I use a function to create drop_layer using some processing tools. Then i use following function "merge_to_original_Fibre_layer" to copy all features of this "drop_layer" to another existing layer "Fibre cable". All these functions are part of the main class.

def merge_to_original_Fibre_layer(self):
    dlayer = self.drop_layer.getFeatures()
    layerObject = QgsProject.instance().mapLayersByName("Fibre Cable")[0]
    features = []
    for f in dlayer:
        features.append(f)
    layerObject.startEditing()
    layerObject.addFeatures(features)
    layerObject.commitChanges()

When i run the code, all the features of "drop_layer" are getting copied to "Fibre Cable" layer. I see this when I open the attribute table of the layer "Fibre Cable". Even i am able to zoom map to these features.

The problem is i can not see the geometry of these newly added features on the map. What I am doing wrong?