6

I want to get the path of a layer in my plugin, in the console it works fine but in my plugin it gives me the error AttributeError: 'NoneType' object has no attribute 'dataProvider'

 def run(self):
"""Run method that performs all the real work"""
# show the dialog
self.dlg.show()
mapCanvas = self.iface.mapCanvas()
self.dlg.comboBox.clear()
for i in range(mapCanvas.layerCount()-1,-1,-1):
    layer = mapCanvas.layer(i)
    layerName = layer.name()
    self.dlg.comboBox.addItem(layerName)


# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result == 1:

    index = self.dlg.comboBox.currentIndex()
    layer = self.dlg.comboBox.itemData(index)
    layer1 = os.path.dirname( unicode( layer.dataProvider().dataSourceUri() ) )       
    t = "C:/Users/slop2.tif"
    processing.runalg("gdalogr:slope", layer1, 1, True, True, False, 1.0, t)

    pass
1
  • Hi Nour. Welcome to gis.se. Please add some explanation about how you populate your comboBox. The way the question currently is formulated it is not possible to answer it. Commented Apr 30, 2015 at 7:32

2 Answers 2

3

I prefer to use QgsMapLayerComboBox objects. Next, It is the snipped code in my plugin for getting the pixel value of a raster under a point. There are implemented two QgsMapLayerComboBox objects: one for raster layers and another one for vector layers (filtered with QgsMapLayerProxyModel):

   def __init__(self, iface):
        """Constructor."""
        self.wcb1 = QgsMapLayerComboBox(self.dlg)
        self.wcb1.setFixedWidth(172)
        self.wcb1.move(170,28)
        self.wcb1.setFilters( QgsMapLayerProxyModel.RasterLayer )
        self.wcb1.enabledChange(True)

        self.wcb2 = QgsMapLayerComboBox(self.dlg)
        self.wcb2.setFixedWidth(172)
        self.wcb2.move(170,68)
        self.wcb2.setFilters( QgsMapLayerProxyModel.VectorLayer )
        self.wcb2.enabledChange(True)

    def run(self):
        """Run method that performs all the real work"""
        rlayer = self.wcb1.currentLayer()
        vlayer = self.wcb2.currentLayer()

It works as you can see at the next images.

With the filters, each layer is automatically selected in the adequate QgsMapLayerComboBox:

enter image description here

After click in OK, "the real work" is done. At the attribute table of vector point layer was created a "value" field and filled with raster values:

enter image description here

1

In your code, layer is only the layer name that you put into the combobox. It's NOT an actual vector layer instance. That's why you get an error that layer doesn't have a provider.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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