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")
SINGLE_PART
toMULTI_PART
in yourarcpy.Dissolve_management()
in_memory
. For example:tempLayer = r'C:\\Temp\\Planning\\Planning_Out\\tempLayer.shp'
can be converted totempLayer = "in_memory/temp"
.