python - merge polys in arcmap

793
7
09-18-2019 12:38 PM
GailMorrison
New Contributor II

Greetings!  I have feature data sets that have 1+ polys. The ArcMap merge tool works great. How do I script this type of processing using Python? 

Thx! Gail

0 Kudos
7 Replies
Jeremy_Moore
MVP

Take a look at arcpy.Merge_management() which will combine your FeatureClasses into one FeatureClass as a single polygon

Merge—Data Management toolbox | ArcGIS Desktop

You could also use arcpy.Append_management() however this will combine your FeatureClasses into a single FeatureClass without merging into a single polygon.

Append—Data Management toolbox | ArcGIS Desktop

Then if you needed to combine features in a FeatureClass into a single polygon you want to use arcpy.Dissolve_management()

Dissolve—Data Management toolbox | ArcGIS Desktop 

3 different tools that do essentially the same thing visually on different data types.

If you need help on how to use these tools please don't hesitate to ask. There are no dumb question.

Hope this helps

GailMorrison
New Contributor II

I don't think I asked the question correctly.  The above answer is for merging datasets.  I need a script to merge polygons within each dataset.   

0 Kudos
Jeremy_Moore
MVP

Just to make sure we are on the same page:

When you say dataset are you referring to a feature dataset, which is a collection of related feature classes that share a common coordinate system? Or are you referring to polygon features in a single feature class?

I apologize if I'm being dense

0 Kudos
GailMorrison
New Contributor II

Oh, you are not being dense.  I am being sloppy with my verbaige.  I  am referring to polygon features in a single feature class

0 Kudos
Jeremy_Moore
MVP

Alright, so polygon features in a feature class. You would want to use arcpy.Dissolve_management()

Dissolve—Data Management toolbox | ArcGIS Desktop 

If you need a code example let me know.

0 Kudos
GailMorrison
New Contributor II

The catch is that I need to keep all the fields in the attribute table. Unless I calculate all the field values to be the same, this won't work for what I need.  Example:  Fields = sqmi, sqft, sqkm, name.  If I dissolve on Name, the other 3 fields drop out of the feature class.  

0 Kudos
Jeremy_Moore
MVP

Sorry for the delay in getting back to you. I have been playing around with some other tools to try to find a solution for you. Once the geometry has been combined and the attributes limited down to a single row, do you need any information in that row?

I have been able to preserve your fields. Try this script and see how it works for you

import arcpy

inFeatureClass = r"C:\Users\JohnDoe\Documents\ArcGIS\Projects\General\General.gdb\dissolve_polygons"
outFeatureClass = r"C:\Users\JohnDoe\Documents\ArcGIS\Projects\General\General.gdb\dissolve_test"
listFields = arcpy.ListFields(inFeatureClass)

print("Dissolving polys")
arcpy.Dissolve_management(inFeatureClass, outFeatureClass)

print("Adding Fields")
for addField in listFields:
    try:
        arcpy.AddField_management(outFeatureClass, addField.name, addField.type, "", "", addField.length, "", "", "")
    except:
        continue
0 Kudos