2

I am developing a plugin that checks for multipart geometry in a layer in qgis 2.6. I am geeting an error list object has no attribute getFeatures. My code snippet is attached below.

def run(self):
    """Run method that performs all the real work"""
    # show the dialog
    self.dlg.show()
    # Clear the QcomboBox before loading layers
    self.dlg.multipartcombo.clear()
    #self.dlg.multipartcombo.clear()
    layer = self.iface.mapCanvas().layers()
    for layer in layer:
        if layer.type() == QgsMapLayer.VectorLayer:
            self.dlg.multipartcombo.addItem( layer.name(), layer )
    # Run the dialog event loop
    result = self.dlg.exec_()
    # See if OK was pressed
    # Find all multipart features in the active layer
    if result:
        layer = self.dlg.multipartcombo.itemData(self.dlg.multipartcombo.currentIndex())
        iter = layer.getFeatures()
        geoms = []
        for feature in iter:
            geom = feature.geometry()
            if geom.isMultipart():
                layer.select(feature.id())
                geoms.append(geom)
                if result  == 1:
                    index = self.dlg.featurecombo.currentIndex()
                    layer = self.dlg.featurecombo.itemData(index)
                    if layer > 0:
                        QMessageBox.information(self.iface.mainWindow(), 'There are %i multipart features in this layer' % len(geoms))
                    else:
                        QMessageBox.information(self.iface.mainWindow(),"Warning!",'Load a layer!')
1

1 Answer 1

0

You need to specify which layer you want to get features from.

In your code, layers is a list of layers, whereas layer is a specific layer.

You can only call getFeatures() from a vector layer.

Additionally, you probably want to get the selected layer from your QComboBox. So, try something like this (didn't test it, probably needs some adjustment):

if result:
    layer = self.dlg.multipartcombo.itemData(self.dlg.multipartcombo.currentIndex())
    iter = layer.getFeatures()
    ...
0

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.