I have a layer with catchment polygons. For those that are smaller than a certain size, I want to merge with an adjoining polygon (namely the one downstream). In the attribute table I have a GridID, which i use to identify different polygons, and I can identify the downstream polygon by that attribute.
How can I, in this case inside a SearchCursor, merge these two polygons together?
I have tried with an UpdateCursor and arcpy.Dissolve_management() without getting it to work. Since Dissolve requires two tables with a value in common, I suppose I could just add a new field and make sure that this is the case for each iteration. But I can't use the same file as input and output (obviously), and I would like to get the file updated right away. If not, I might merge polygons that are of sufficient size since it has been merged with another earlier in the process, and after that meet the criterion.
In the case of an UpdateCursor, I think this is mainly for updating field values and not the geometry itself?
Is there a (more sufficient) method for this?
# inpgs = Catchment polygons shapefile
where1 = '"' + 'Shape_Area' + '" < 5000'
polyrows = arcpy.SearchCursor(inpgs, where1)
for prow in polyrows:
grd1 = prow.GridID
...
# grd1 = GridID on the current polygon
# grd2 = GridID on the polygon downstream
where4 = '"' + 'GridID' + '" = ' + str(grd2)
polyrows2 = arcpy.UpdateCursor(inpgs, where4)
for prow2 in polyrows2:
arcpy.Dissolve_management(inpgs, outpgs, ???)
inpgs = outpgs # ?
I use ArcInfo 10.0, Python 2.6 on Windows 7.
EDIT: I have now implemented the changes dmahr suggested, and made some more myself. That resulted in the following code:
# Create dictionary for all geometries
polyrows = arcpy.SearchCursor(inpgs)
geometryDictionary = {}
for prow in polyrows:
geometryDictionary[prow.GridID] = prow.Shape
del prow, polyrows
# Create dictionary (nDD) which contains the GridIDs of the downstream catchment area
# as value and the current catchment area's GridID as key.
nextrows = arcpy.SearchCursor(indrln)
nextDownDictionary = {}
nDD = {}
for nrow in nextrows:
nextd = nrow.NextDownID
nextDownDictionary[nrow.GridId] = nrow.NextDownID
for k in dict.keys((nextDownDictionary)):
clause = '"' + 'HydroID' + '" = ' + str(nextDownDictionary[k])
nr = arcpy.SearchCursor(indrln, clause)
for n in nr:
nDD[k] = n.GridID
del n, nr
where1 = '"' + 'Shape_Area' + '" < 5000'
polyrows = arcpy.SearchCursor(inpgs, where1)
arcpy.Copy_management(inpgs, outpgs)
# Merge the small polygon into the downstream polygon
for prow in polyrows:
grd1 = prow.GridID
grd2 = nDD.get(grd1)
if grd2:
# grd1 = GridID on the current polygon
# grd2 = GridID on the polygon downstream
geometry1 = prow.Shape
geometry2 = geometryDictionary[grd2]
arcpy.Merge_management([geometry1, geometry2], tempMerged)
arcpy.Dissolve_management(tempMerged, tempPgs)
arcpy.Append_management(tempPgs, outpgs, "NO_TEST")
grids.append(prow.GridID)
# After appending the dissolved polygons to the output file, the row for the merged polygon is being deleted
for g in grids:
whereU = '"GridID" = ' + str(g)
upd = arcpy.UpdateCursor(outpgs, whereU)
for up in upd:
upd.deleteRow(up)
del prow, polyrows
del up, upd

