2

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]).

enter image description here

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>
mgri
  • 16,159
  • 6
  • 47
  • 80
freeski_52
  • 135
  • 1
  • 6
  • Why arent you using the Dissolve tool? – BERA Jul 06 '17 at 08:09
  • Using dissolve every dissolve process requires a Output shapefile and with this amount of data i would create hundrets of new shapefiles....which at the end i need again bring together – freeski_52 Jul 06 '17 at 09:19
  • and i am not sure but you can't define selected features as dissolve Input as far i know.... – freeski_52 Jul 06 '17 at 09:40

1 Answers1

1

I didn't test the following code but, since it seems you are able to create the selection successfully, I only edited the code part where you need to merge the features:

"""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():
            if geom == None:
                 geom = feature.geometry()
             else:
                geom = geom.combine(feature.geometry()) 
             """ After this step the geom need to be assigned to a
                 QgsFeature  object and added to a QgsVectorObject
                 So something like the following code:"""
        newFeature = QgsFeature()
        values = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]  
        newFeature.setGeometry(geom)
        newFeature.setAttributes(values)
        myWorkLay.startEditing()
        myWorkLay.addFeature(newFeature, True)
        myWorkLay.removeSelection()
        myWorkLay.commitChanges()
        geom = None

        myWorkLay.removeSelection()
freeski_52
  • 135
  • 1
  • 6
mgri
  • 16,159
  • 6
  • 47
  • 80
  • I tested the suggested change with having no effect on my expected result. Still no merge is exectuted on the selected features – freeski_52 Jul 06 '17 at 10:35
  • @freeski_52 Are you sure that the selection is correctly obtained? Are you able to print all the features (i.e. you feature variable) within it? – mgri Jul 06 '17 at 10:39
  • 1
    @freeski_52 Moreover, the result in the code is a geometry object and not a feature, so how did you evaluate it? The above code doesn't create any feature, so you can't graphically see the expected result. – mgri Jul 06 '17 at 10:40
  • Yes i am pretty sure with my selection => i add the resulting output to my post (see above): Here is my output => 10 => the result of print myWorkLay.selectedFeatureCount() and below my selected feature.geometry() – freeski_52 Jul 06 '17 at 11:00
  • How I am able to store the geometry object as a feature than => in my understanding the code merges all selected Features to a new "merged" single feature? or how i am able to do this after i get the merged geometry object? – freeski_52 Jul 06 '17 at 11:05
  • 1
    @freeski_52 The code merges all the geometries in a single geometry, but doesn't create a feature. See here for creating a new feature. You only need to use setGeometry(geom). – mgri Jul 06 '17 at 12:18
  • 1
    you was right, geom= geom.combine(feature.geometry) returns a new QgsGeometry object which need to be referenced to a QgsFeature and further to a qgsVectorLayer object...and this is the solution! – freeski_52 Jul 07 '17 at 10:54
  • @freeski_52 I didn't add the code part for adding the features because it seemed you wanted to focus only on the merging process. However, I will leave your edit on my answer (even if there are faster ways for adding the features, but this is not the purpose of the question). – mgri Jul 07 '17 at 12:15