-2

In my plugin, when I click a button it will call the findTheCityNames function which is attached below and get features if there is a particular layer. What I want is if there is no specific layer in which it is getting features, it should give a warning message "No Valid layer found" instead python error "List object has no attribute getFeatures".

def findTheCityNames(self):
    self.dlg.Citycombo.clear()
    ct = self.dlg.Statecombo.currentText()
    st = "state='"+ct+"'"
    exp = QgsExpression(st)
    fidAndVal = {}        
    #QMessageBox.information(self.dlg, "info", st) 

    layer = self.iface.activeLayer()

    iterFeat = layer.getFeatures()

    for f in iterFeat:
        val = exp.evaluate(f)
        if bool(val):
            attrs = f.attributes()
            idx = layer.fieldNameIndex('name')
            cityName = f.attributes()[idx]
            fidInt = f.id()
            fidAndVal[fidInt] = cityName

    for x in fidAndVal.keys():
        self.dlg.Citycombo.addItem(fidAndVal[x])            

1 Answer 1

0

I think that this is most a python related question rather than a GIS question.

You can try with the "Try and except" in python sort of:

try:
   layer with possible problem
except:
   print this layer doesn't work.

Anyways I think is better to ask or search this in the python related StackExchange.

2
  • 2
    Python is case sensitive so perhaps change to try and except even though you are only presenting pseudo-code.
    – PolyGeo
    Feb 27, 2015 at 9:21
  • Thank you so much Gsanez. Really great! though my question is simple.
    – User123
    Feb 27, 2015 at 9:55

Your Answer

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

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