3

I have a simple code that is pulling together polygon shapefiles from a Windows folder and merging them together into a single shapefile. In the same code, I want to add a dissolve command. My fields are "Name" and "Status". If there are two polygons with the same Name (i.e. 2 different rows with the same Name), I want to dissolve by Name to create unique rows of data.

I am using ArcGIS 10.1. Please see my code below:

import arcpy, os, time

arcpy.env.workspace = r'C:\\Temp\\Planning\\Planning_In\\Validated'
fcs = arcpy.ListFeatureClasses(feature_type='Polygon')

fcOut = r'C:\\Temp\\Planning\\Planning_Out\\ProjectMerge.shp'
if fcOut in fcs:fcs.remove(fcOut)


if len(fcs) > 0:
    if arcpy.Exists(fcOut):
        fcArchived = os.path.join(r'C:\\Temp\\Planning\\Planning_Out\\Archive', 'Archived_{}.shp'.format(time.strftime('%Y%m%d')))
        os.makedirs(os.path.dirname(fcArchived))
        arcpy.Copy_management(fcOut, fcArchived)
        arcpy.Delete_management(fcOut)

print fcs

arcpy.Merge_management(fcs, fcOut)

# Set local variables
inFeatures = r'C:\\Temp\\Planning\\Planning_Out\\ProjectMerge.shp'
tempLayer = r'C:\\Temp\\Planning\\Planning_Out\\tempLayer.shp'
expression = arcpy.AddFieldDelimiters(inFeatures, "Name") + " <> ''"
outFeatureClass = r'C:\\Temp\\Planning\\Planning_Out\\ProjectDissolve.shp'
dissolveFields = ["Name"]


# Execute MakeFeatureLayer and SelectLayerByAttribute.  This is only to exclude 
#  features that are not desired in the output.
arcpy.MakeFeatureLayer_management(inFeatures, tempLayer)
arcpy.SelectLayerByAttribute_management(tempLayer, "NEW_SELECTION", expression)

# Execute Dissolve using LANDUSE and TAXCODE as Dissolve Fields
arcpy.Dissolve_management(tempLayer, outFeatureClass, dissolveFields, "", 
                          "SINGLE_PART", "DISSOLVE_LINES")

I end up with a shapefile, but it is not dissolved. Also, my "Status" field that is in the existing, singular shapefiles is not carried over.

The statuses of all projects will be the same. For example, if there are two polygons with Name= 1415-006, they will both have the same status before the dissolve takes place. If I use the following code and also dissolve by "Status", I have the "Status" field in the dissolve shapefile but still, the items are not dissolved.

# Set local variables
inFeatures = r'C:\\Temp\\Planning\\Planning_Out\\ProjectMerge.shp'
tempLayer = r'C:\\Temp\\Planning\\Planning_Out\\tempLayer.shp'
expression = arcpy.AddFieldDelimiters(inFeatures, "Name") + " <> ''"
outFeatureClass = r'C:\\Temp\\Planning\\Planning_Out\\ProjectDissolve.shp'
dissolveFields = ["Name", "Status"]


# Execute MakeFeatureLayer and SelectLayerByAttribute.  This is only to exclude 
#  features that are not desired in the output.
arcpy.MakeFeatureLayer_management(inFeatures, tempLayer)
arcpy.SelectLayerByAttribute_management(tempLayer, "NEW_SELECTION", expression)

# Execute Dissolve using LANDUSE and TAXCODE as Dissolve Fields
arcpy.Dissolve_management(tempLayer, outFeatureClass, dissolveFields, "", 
                          "SINGLE_PART", "DISSOLVE_LINES")
5
  • Is your field named 'ProjectName' or 'Name'. The code uses 'Name', but your description says 'ProjectName'. – Tom Mar 29 '16 at 16:45
  • As for preserving your Status field: how would you like it preserved? Are the statuses all the same for a given project? If so, you could either dissolve on both ProjectName and Status, or you could use Status as a statistics field (first or last). See: pro.arcgis.com/en/pro-app/tool-reference/data-management/… – Tom Mar 29 '16 at 16:46
  • 6
    Do your features overlap/intersect? If not then this may be why it's not dissolving - you'd need to change SINGLE_PART to MULTI_PART in your arcpy.Dissolve_management() – Midavalo Mar 29 '16 at 17:13
  • As a side note, you may be able to increase the efficiency of this by converting your temp data to in_memory. For example: tempLayer = r'C:\\Temp\\Planning\\Planning_Out\\tempLayer.shp' can be converted to tempLayer = "in_memory/temp". – Aaron Mar 29 '16 at 18:34
  • @Midavalo It looks like you comment provided and answer that would be accepted. I think you should post it as one. – PolyGeo Jul 19 '16 at 1:31
2

To dissolve down to Single-Part features your input features need to intersect (touching edges or overlapping), even when dissolving on an attribute. If the features don't intersect at all then the output will be the same as the input.

If your features do not intersect you will need to set your dissolve to output Multi-Part features. This will dissolve down to a single feature (per dissolve field value) even if there are gaps between the feature parts.

See How Dissolve (Data Management) works

To change your code to output Multi-part features, change SINGLE_PART to MULTI_PART:

# Execute Dissolve using LANDUSE and TAXCODE as Dissolve Fields
arcpy.Dissolve_management(tempLayer, outFeatureClass, dissolveFields, "", "MULTI_PART", "DISSOLVE_LINES")

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.