Tell me more ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to extend some concepts in the QGIS Workshop tutorial. Basically, I would like to check if the layer is of type "QGSVectorLayer". In the QGIS Python console, I am able to type

canvas = qgis.utils.iface.mapCanvas()
cLayer = canvas.currentLayer()
type(cLayer).__name__

with no problem. However, when attempting the same from the plugin, I get the following error message:

if type(self.cLayer).name == "QgsVectorLayer": NameError: global name 'cLayer' is not defined

The code is as follows:

def handleLayerChange(self, layer):
        self.cLayer = self.canvas.currentLayer()
        if self.cLayer:
            if type(self.cLayer).__name__ == "QgsVectorLayer":
                self.provider = self.cLayer.dataProvider()
share|improve this question

1 Answer

up vote 1 down vote accepted

You don't need to compare it using strings or the name of the type. You should just do:

 layer.type() == QgsMapLayer.VectorLayer
share|improve this answer
Thanks, this is much better. I did end up getting the other method to work by converting it to a string; if str(type(self.cLayer).__name__) == "QgsVectorLayer" – user1420372 May 9 at 22:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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