Guest User

Untitled

a guest
Oct 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. layers = self.iface.mapCanvas().layers()
  2.  
  3. found = false
  4. for feature in layer.getFeatures():
  5. try:
  6. feature.attribute("fieldName") # Throws exception if it does not exist
  7. found = true
  8. except KeyError as e:
  9. # all features have hte same fields, so if it doesn't exists in the first feature, it won't exist in the others
  10. break
  11. finally:
  12. # all features have the same fields, so only need to check the first
  13. break
  14.  
  15. field_name = "my_field"
  16. layer = iface.activeLayer()
  17.  
  18. field_index = layer.fields().indexFromName(field_name)
  19.  
  20. if field_index == -1:
  21. print("The field {} does not exist in layer {}!".format(field_name, layer.name()))
  22. else:
  23. print("The field {} exists in layer {}!".format(field_name, layer.name()))
Add Comment
Please, Sign In to add comment