2

I am trying to combine all features of a Polygon layer in QGIS in a python plugin, and am running into this error when I try to run the combine method:

"AttributeError: 'NoneType' object has no attribute 'combine' Python version: 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)]

QGIS version: 2.10.1-Pisa Pisa, d20c5b7"

I've looked at this question before but am not sure what I am doing incorrectly:

from qgis.core import *
from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
from PyQt4.QtGui import QAction, QIcon, QFileDialog
# Initialize Qt resources from file resources.py
import resources, processing
# Import the code for the dialog
from save_attributes_dialog import SaveAttributesDialog
import os.path

Here is my code for the run method:

def run(self):
    layers = self.iface.legendInterface().layers()
    layer_list = []
    for layer in layers:
        layer_list.append(layer.name())
        self.dlg.comboBox.addItems(layer_list)
    # show the dialog
    self.dlg.show()
    # Run the dialog event loop
    result = self.dlg.exec_()
    # See if OK was pressed
    if result:

        selectedLayerIndex = self.dlg.comboBox.currentIndex()
        selectedLayer = layers[selectedLayerIndex]

        features = selectedLayer.getFeatures()
        geoms = QgsGeometry()

        for feature in features:
            geoms = geoms.combine(feature.geometry())

I ran the loop just printing out the areas of each feature before which worked fine, so I'm guessing it is able to access the features without a problem, but am not sure what to do about the NoneType issue.

3

At least one of the polygon features has no or invalid geometry. You can provoke this error as follows:

geoms = QgsGeometry()
g1 = QgsGeometry()
geoms = geoms.combine(g1)
# no problem, despite empty geometry

geoms = geoms.combine(None)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'combine'

I suggest some consistency check before calling combine like this:

geoms = geometry()
for feature in features:
    geom = feature.geometry()
    if geom:
        err = geom.validateGeometry()
        if not err:
            geoms = geoms.combine(geom)
        else:
            print '%d geometry errors detected (feature %d)' % (len(err), feature.id()) 
  • After implementing the check for consistency I am getting the same error at the combine method: 'geoms = geoms.combine(geom) AttributeError: 'NoneType' object has no attribute 'combine'' I tested it on another shapefile also and gave the same results. – left_side_drive Mar 24 '16 at 21:04
  • 1
    Ok, then I guess that there are polygons with holes (donut). I just tested some some simple polygons and a donut. And when it comes to the donut the error occures. – Detlev Mar 24 '16 at 21:22
2

Detlev, after looking at one of your other posts I realized I just needed to initialize the empty geometry object as:

geoms = QgsGeometry.fromWkt('GEOMETRYCOLLECTION()')

The information about validating geometries will be useful for me too. Thank you!

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.