Merging/dissolving all features within feature classes in geodatabase using ArcPy

443
5
Jump to solution
11-30-2020 02:57 AM
AdilToorawa1
New Contributor II
I have a geodatabase with multiple feature classes. In each feature class there are multiple features. I would like to merge/dissolve them so there is only one feature per feature class (keeping the same output location)

Can someone provide an example of a a script that would loop through each feature class within the gdb and perform the process below.

Manual method: Edit > Select All > Merge > Save/Stop Edit

 

I have attached an image of the gdb with feature classes that i want to loop through.

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DavidIralu
Esri Contributor

Hi, I think you can approach it with the 'Dissolve' tool which is also exposed as a python function.

You could go with more fine-grained manipulations through the arcpy.Geometry class and it's respective methods but it might be overkill for now.

Bearing in mind that the output will be a new featureclass, you can choose to output to an 'in-memory' or 'physical' featureclass in the fGDB - choosing to write the in-memory featureclass to the fGDB later.

1. set your file GDB as the workspace with: 

 

 

arcpy.env.workspace = //path/to/Test.gdb

 

 

(replace the full path to your Test.gdb above).

2. Next you would need to loop through each featureclass in your fGDB, but first list all featureclasses inside it.

3. Dissolve on each featureclass and choose to dissolve on a particular field or leave the default. (ensure you leave the parameter 'MULTI_PART' so that a multipart feature is written).

4. Keep a reference to the original featureclass (restore it later)

5. delete the original featureclass

6. Rename the dissolved feature class back to your original.

 

So, together:

 

 

import arcpy
import sys

fileGDB = "C:\\path\\to\\test.gdb"

arcpy.env.workspace = fileGDB

featureclasses = arcpy.ListFeatureClasses()


for fc in featureclasses:
    original_featureclass = fc
    dissolved_featureclass = fc + '_dissolved'
    try:
        print("original featureclass: {0}".format(original_featureclass))
        arcpy.management.Dissolve(fc, dissolved_featureclass, None, None, "MULTI_PART", "DISSOLVE_LINES")
        print("dissolved featureclass: {0}".format(dissolved_featureclass))
    except Exception:
        e = sys.exc_info()[1]
        print(e.args[0])
        
    try:
        arcpy.Delete_management(fc)
        print("original featureclass deleted: {0}".format(fc))
    except Exception:
        e = sys.exc_info()[1]
        print(e.args[0])
        
    try:
        
        arcpy.Rename_management(dissolved_featureclass, original_featureclass)
        print("original featureclass restored: {0}".format(original_featureclass))
    except Exception:
        e = sys.exc_info()[1]
        print(e.args[0])

 

 

 

All the best.

------

Edit: after I returned to this I saw Dan's post and yes, you need to dissolve on an attribute.

I'm not sure what it dissolves on if you leave the dissolve field blank - probably the geometries.

It would be worthwhile getting to know the nuances in the arcpy.Geometry class. 

View solution in original post

5 Replies
DanPatterson
MVP Notable Contributor

merge and dissolve are different processes, with the latter removing shared boundaries in polygons as in the "bird on dog" images below.

dissolve_sq2_0.pngdissolve_sq2_1.png

yes it can be done, but your image doesn't open


... sort of retired...
0 Kudos
AdilToorawa1
New Contributor II

Thanks for your response. Im not sure why it doesnt, ill try re uploading. In the meantime dissolve would be the preferred option so theres on "multipart" polygon. 

0 Kudos
DanPatterson
MVP Notable Contributor

To dissolve in arcmap/pro, you need a common attribute to dissolve on.  You could add a field with a common value if you don't have one, first and use it as the dissolve field.  You then follow this up with a union to produce a multipart polygon.

The example I have shown is for my own geometry constructor which does it without those requirements.  I will blog about it soon when I am done testing and it will be available then.  In the interim, use the process as describe above


... sort of retired...
DavidIralu
Esri Contributor

Hi, I think you can approach it with the 'Dissolve' tool which is also exposed as a python function.

You could go with more fine-grained manipulations through the arcpy.Geometry class and it's respective methods but it might be overkill for now.

Bearing in mind that the output will be a new featureclass, you can choose to output to an 'in-memory' or 'physical' featureclass in the fGDB - choosing to write the in-memory featureclass to the fGDB later.

1. set your file GDB as the workspace with: 

 

 

arcpy.env.workspace = //path/to/Test.gdb

 

 

(replace the full path to your Test.gdb above).

2. Next you would need to loop through each featureclass in your fGDB, but first list all featureclasses inside it.

3. Dissolve on each featureclass and choose to dissolve on a particular field or leave the default. (ensure you leave the parameter 'MULTI_PART' so that a multipart feature is written).

4. Keep a reference to the original featureclass (restore it later)

5. delete the original featureclass

6. Rename the dissolved feature class back to your original.

 

So, together:

 

 

import arcpy
import sys

fileGDB = "C:\\path\\to\\test.gdb"

arcpy.env.workspace = fileGDB

featureclasses = arcpy.ListFeatureClasses()


for fc in featureclasses:
    original_featureclass = fc
    dissolved_featureclass = fc + '_dissolved'
    try:
        print("original featureclass: {0}".format(original_featureclass))
        arcpy.management.Dissolve(fc, dissolved_featureclass, None, None, "MULTI_PART", "DISSOLVE_LINES")
        print("dissolved featureclass: {0}".format(dissolved_featureclass))
    except Exception:
        e = sys.exc_info()[1]
        print(e.args[0])
        
    try:
        arcpy.Delete_management(fc)
        print("original featureclass deleted: {0}".format(fc))
    except Exception:
        e = sys.exc_info()[1]
        print(e.args[0])
        
    try:
        
        arcpy.Rename_management(dissolved_featureclass, original_featureclass)
        print("original featureclass restored: {0}".format(original_featureclass))
    except Exception:
        e = sys.exc_info()[1]
        print(e.args[0])

 

 

 

All the best.

------

Edit: after I returned to this I saw Dan's post and yes, you need to dissolve on an attribute.

I'm not sure what it dissolves on if you leave the dissolve field blank - probably the geometries.

It would be worthwhile getting to know the nuances in the arcpy.Geometry class. 

View solution in original post

AdilToorawa1
New Contributor II

Thank you Dan and David,

 

The script does it. I just need to tweak it a little.

Very much appreciated.

0 Kudos