1

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

Example:

# 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
  • 1
    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? – Vincent Bré Jan 17 at 7:56
  • 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. – Kadir Şahbaz Jan 17 at 8:03
  • 1
    I've added a feature request on "Getting layer reference using feature". – Kadir Şahbaz Jan 21 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.