4

I'm writing a qgis plugin which needs a fixed-distance-buffer algorithm and must add the buffer result to the map layers of the software.

I have the following code:

# these are my import list
import os.path

import qgis.core
# Initialize Qt resources from file resources.py
import resources
import processing
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
from PyQt4.QtGui import QAction, QIcon

and the main part in run() method is:

    # region adding vector layers
    layers = self.iface.legendInterface().layers()
    layer_list = []
    for layer in layers:
        if layer.type() == qgis.core.QgsMapLayer.VectorLayer:
            layer_list.append(layer.name())
        else:
            break
    # region crime inputs and crime instance


    self.cInput = self.dlg.comboBox_input_Crime
    self.cInput.clear()
    self.cInput.addItems(layer_list)

after the if statement in run() method:

self.pic = self.cInput.currentIndex()
self.selectedCrime = layers[self.pic]
buff=  processing.runalg('qgis:fixeddistancebuffer', layers[self.pic], 100, 50, False, None)
qgis.core.QgsMapLayerRegistry.instance().addMapLayer(buff)

now when I run my plugin, after a progress bar appears, it shows an error of TypeError: QgsMapLayerRegistry.addMapLayer(QgsMapLayer, bool addToLegend=True): argument 1 has unexpected type 'dict'!

please explain what's the problem?

4

you are trying to load as a layer a vector object called buff. As you can see in the qgis:fixeddistancebuffer doc the output is dictionary with "OUTPUT" entry :

buff['OUTPUT']u'/var/folders/sr/g1qdy1dd5t57862r5yr0g4lc0000gn/T/processingbe0227f51efb4870b2f53dac7abc2c67/ccaa1966d99c4339ab669c9a76281cb4/OUTPUT.shp'

So when you try to load this vector as a layer with

qgis.core.QgsMapLayerRegistry.instance().addMapLayer(buff)

Python returns an exception cause addMapLayer() method is waiting for a QgsMapLayer class which could be a vectorLayer in your case or rasterLayer as well.

so, create a new vectorLayer and populate it with your new shape OUTPUT and push it to your QgsMapLayerRegistry :

buffLyr =  QgsVectorLayer(buff['OUTPUT'], "buffer", "ogr")    
qgis.core.QgsMapLayerRegistry.instance().addMapLayer(buffLyr)
| improve this answer | |
  • the solution above gives this error: 'TypeError: QgsVectorDataProvider.addFeatures(list-of-QgsFeature) -> (bool, list-of-QgsFeature): argument 1 has unexpected type 'list' – Afshin Salehi Oct 21 '17 at 23:09
  • 1
    my fault. because you have only one buff feature, use addfeature instead of addfeatures. I edited the post – Hugo Roussaffa - GeoDatup Oct 22 '17 at 6:42
  • i'm sorry but it gives : TypeError: QgsVectorLayer.addFeature(QgsFeature, bool alsoUpdateExtent=True): argument 1 has unexpected type 'dict' now. – Afshin Salehi Oct 22 '17 at 8:05
  • 1
    I am sorry cause i didn't test on my side, I learned something that processing algorithm output are dict (as your error show). So i change again the code to feet your need and I tested this time, with a shorter code. according your code is correct this should work now, and I hope you will understand why. – Hugo Roussaffa - GeoDatup Oct 22 '17 at 8:48

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.