I am working on a feature merge Operation, able to stick features together, if they have the same attributive values (e.g. TP_RKZ1 & TP_RKZ2 are empty so stick the multiple features to one single feature together if the following feature is not equal to the feature before [like in the Image]).
To get this behavior, I created the following code, which is actually already selecting the features in the mentioned way. The only missing part is the "merge process". Here I am not able to figure out the misbehavior of the used geom = geom.combine(feature.geometry)
syntax.
Any suggestions?
"""APPROACH"""
"""Loop comparing attribute ID_RKZ1 & ID_RKZ2 values of the actual feature with the following feature and add the feature to the selectedFeatures using the
myWorkLay.select () function and if it is not equal add last equal feature to the selectedFetures
and perform the merge of all equal features"""
for i in range(0,len(myAttributes)-1):
if myAttributes[i][1] == myAttributes[i+1][1] and myAttributes[i][2] == myAttributes[i+1][2]:
print "My items {0} | {1} are equal".format(myAttributes[i],myAttributes[i+1])
'Select feature because of feature-id'
myWorkLay.select([myAttributes[i][0]])
#print "%s"%myAttributes[i][0]
else:
print "!My items {0} | {1} are not equal!".format(myAttributes[i],myAttributes[i+1])
'Select feature because of feature-id'
myWorkLay.select([myAttributes[i][0]])
print myWorkLay.selectedFeatureCount()
#print myWorkLay.setSelectedFeatures()
'Iterate through all selected features and merge them'
for feature in myWorkLay.selectedFeatures():
print feature.geometry()
if geom == None:
geom = feature.geometry()
else:
geom = geom.combine(feature.geometry())
myWorkLay.removeSelection()
geom = None
Here the result of my selection and
10 ' => result of print myWorkLay.selectedFeatureCount()
' following the output of print feature.geometry()
<qgis._core.QgsGeometry object at 0x000000001C1742F0>
<qgis._core.QgsGeometry object at 0x000000001C174400>
<qgis._core.QgsGeometry object at 0x000000001C174378>
<qgis._core.QgsGeometry object at 0x000000001C1742F0>
<qgis._core.QgsGeometry object at 0x000000001C174400>
<qgis._core.QgsGeometry object at 0x000000001C174378>
<qgis._core.QgsGeometry object at 0x000000001C1742F0>
<qgis._core.QgsGeometry object at 0x000000001C174400>
<qgis._core.QgsGeometry object at 0x000000001C174378>
<qgis._core.QgsGeometry object at 0x000000001C1742F0>