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

2469
6
Jump to solution
11-30-2020 02:57 AM
by Anonymous User
Not applicable
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
by Anonymous User
Not applicable

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

6 Replies
DanPatterson
MVP Esteemed 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
by Anonymous User
Not applicable

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 Esteemed 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...
by Anonymous User
Not applicable

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. 

NSamu
by
New Contributor III

Hi, I am working on a similar script and can answer your comment: "I'm not sure what it dissolves on if you leave the dissolve field blank - probably the geometries."

I assume that leaving the dissolve field blank does dissolve on the geometry--I just tested it and the output gave me one multipart feature. If you add statistics fields while leaving the dissolve field blank, it will also calculate those for the single output feature. I figured I'd share since it's quicker/simpler to leave the field blank than add and calculate a new field (and potentially need to delete it afterwards) to dissolve all--that's what I used to do and have seen others suggest in this forum since I don't think it's completely obvious that leaving the dissolve field blank will accomplish the same thing. 

0 Kudos
by Anonymous User
Not applicable

Thank you Dan and David,

 

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

Very much appreciated.

0 Kudos