I'm absolutely new to Python and programming. How can I merge selected features in one DBF table (polygons) using Python in ArcGIS 10.2 for Desktop?

Here's what I need:

enter image description here

Here's what I've got:

arcpy.Dissolve_management(S2_merge1, S2_dissolve5, "Name5", "", MULTI_PART", "DISSOLVE_LINES")

arcpy.JoinField_management(S2_dissolve5, "Name5", S2_merge1, "Name5", "Name1")

This works but I need to do this without dissolve command - You see I need to merge selected features in one shp/dbf like I do this in Editor session. I can't find this type of toolbox/command in ArcGIS

And is there any possibility to configure the Dissolve tool to get shp-theme with all fields what was in original shapefile like it was in ArcView 3.2?

  • In your example, when Name5 has the value As3, your input table has three potential values for field Name1: A, B, and C. Your output table chose the value A. Is there a logic you'd like when there are multiple possible values? – Emil Brundage Jan 6 '16 at 17:30
  • I need two solutions - fist - i need simple merge with fist value ( A ) in row and second objective is to merge values in these rows into one merged row - A+B+C – Fox Jan 7 '16 at 21:29

Since you are new to python and arcpy, I will say that this problem is more complicated than the steps you usually do in ArcMap. However, if you have a large table or a large number of shapefiles, arcpy will save you lots of time.

The proper (IMO) way to do this is with the .union function for geometry objects and arcpy.da.InsertCursor for the attributes. Without an exact description it's hard to code exactly what you need, but I'll give it a shot.

Some helpful links:

Alternative to 'Editors Merge' feature using Arcpy


Possible to add multiple fields in single arcpy statement?

# First, create an output feature class with the desired schema
arcpy.CreateFeatureClass_management(r'C:\Path\To\Output', 'output.shp', 'POLYGON', spatial_reference='S2_merge1')

fields = [
('NAME5', 'TEXT'),
('NAME1', 'TEXT'),
# ... Add fields as necessary]

for field in fields:
    arcpy.AddField_management(*(r'C:\Path\To\Output\output.shp',) + field)

# Now that we have the output dataset, we will populate it in 2 steps
# Step 1: Merge all the geometries for shapes that have the same Name5 value
# Step 2: For each set of merged geometries, populate the Name1
# attribute with the unique letters of all the Name1 attributes for that set.
# In your example, As3 would have 'ABC'

# Get unique values for the column you want to 'dissolve' on
unique_names = sorted({r[0] for r in arcpy.da.SearchCursor('S2_merge1', 'Name5')})
# This will give you a list like ['As1', 'As2', 'As3']

# Next you need to merge the geometries that share the same Name5 
with arcpy.da.InsertCursor(r'C:\Path\To\Output\output.shp', ['SHAPE@', 'Name1', 'Name5']) as icur:
    for name5 in unique_names:
        query = "Name5 = '{}'".format(name5) # For example Name5 = 'As1'
        geoms_to_merge =  [r[0] for r in arcpy.da.SearchCursor('S2_merge1', 'SHAPE@', query) ]
        merged_names = sorted({r[0] for r in arcpy.da.SearchCursor('S2_merge1', 'Name1', query) })
        parent_geom = geoms_to_merge[0]
        if len(geoms_to_merge) > 1:
            child_geoms = geoms_to_merge[1:]
            for geom in child_geoms:
                # Union each child geometry with the parent
                parent_geom = parent_geom.union(geom)
        name1 = ''.join([i for i in merged_names])
        insert_row = (parent_geom, name1, name5)

# This should give you an ouput table that has rows equal to the number of unique values in the Name5 column.

I had a similar problem and was able to solve it using the previous answer, however, there are some small things that i had to correct from the code KJYDavis provided.

The first is only a typo:

unqiue_names instead of unique_names

### Get unique values for the column you want to 'dissolve' on
unique_names = sorted({r[0] for r in arcpy.da.SearchCursor('S2_merge1', 'Name5')})

The second is in the query:

query = "Name5 = {}".format(name5) #did not work for me

query = "  'Name5'  =  '{}'   ".format(name5) # worked 

The third is in the last line:

icur.insertRow(row) # did not work

icur.insertRow(insert_row) # worked

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.