Using an existing feature, is there a way/method to get the layer which contains that feature?


# new empty feature which doesn't belong to any layer
new_feat = QgsFeature()


layer = iface.activeLayer()
features = layer.getFeatures()    
# An existing feature which belongs to a layer
feat = next(features) # feat is connected to active layer

# At this point, how to get layer using feature?
# For example:
print(feat.layer()) # or something
  • 3
    There is no method in the QGIS API for get the name of the layer which contains the feature. Since you identify a layer and then browse through the features in that layer, why don't you query the layer name directly? Jan 17, 2020 at 7:56
  • 1
    There is a post asking similar question for QGIS 2. I thought there was a short way in QGIS 3. I need to use in Advanced Python field calculator. Jan 17, 2020 at 8:03
  • 3
    I've added a feature request on "Getting layer reference using feature". Jan 21, 2020 at 9:57


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.