0

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 cannot see the geometry of these newly added features on the map. What I am doing wrong?

7
  • Try layerObject.updateExtents(). Commented Jan 17, 2022 at 0:48
  • Tried your advice. Still the added features are not visible. :( Commented Jan 17, 2022 at 5:12
  • Replace the last 3 lines in your function with: layerObject.dataProvider().addFeatures(features)
    – Ben W
    Commented Jan 17, 2022 at 8:50
  • You will find this section of the pyqgis developer cookbook useful: docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/…
    – Ben W
    Commented Jan 17, 2022 at 10:13
  • @Ben W, tried your advice. still added features are not visible. ;( Commented Jan 17, 2022 at 12:35

1 Answer 1

0

The code was fine. The issue was with rule-based symbology. Fibre Cable layer had an attribute rule-based symbology. I added the required attributes to the added features and features were visible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.