3

I want to check that a particular features exists or not in getFeature().

Suppose in attribute table I have ID, Name, CAT, and Type columns. Now I want to check that CATEGORY column is exists or not in features.

I use this but this is not working.

for poi in layerPOI.getFeatures():
    if "CATEGORY"  in layerPOI.getFeatures():

and this as well. It producing keyError

if poi["CATEGORY"]#As category not exists it giving error here

1 Answers1

5

To check if 'CATEGORY' exists as a field:

layer = iface.activeLayer()

field = 'CATEGORY' 

index = layer.fieldNameIndex(field)

if index == -1:
    print "field doesn't exist"
else:
    print "field exists"
xunilk
  • 29,891
  • 4
  • 41
  • 80