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>
  • Why arent you using the Dissolve tool? – BERA Jul 6 '17 at 8: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 6 '17 at 9:19
  • and i am not sure but you can't define selected features as dissolve Input as far i know.... – freeski_52 Jul 6 '17 at 9:40
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()
  • 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 6 '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 6 '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 6 '17 at 10:40
  • 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 6 '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 7 '17 at 10:54

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.