[GIS] Use arcpy.da.searchcursor to select rows to dissolve

arcgis-10.1arcpy

I'm quite new to ArcGIS and hope someone can help me.

I have one table that lists which polygons are contiguous with each other called BLPUsCavPlaceContigBLPUs that looks like this:

OBJECTID   blpuid  contigid  shape-length
    1         1        7          38
    2         1        16         42
    3         2        8          14

I then have a table of all the polygons called BLPUsCavPlace where the objectID is the blpuid and contigid:

OBJECTID   SHAPE   SHAPE_length  SHAPE_area
    1     polygon    126.846       986.093

Therefore I know which can merge with which, such as 1 and 7. I want to iterate through the first table, and for each row the cursor will take the blpuid and contigid and select these rows (OBJECTIDs) in BLPUsCavPlace, then it will dissolve them and add them to BLPUsCavPlaceDissolved.

So it would look at BLPUsCavPlaceContigBLPUs and select 1 and 7 in BLPUsCavPlace, then it would do the next row etc

The end result is that I have a polygon layer that contains all the possible dissolved polygons for each polygon within BLPUsCavPlace i.e. 1 + 7, 1 + 16, 2 + 8

I've been looking at arcpy.da.searchcursor which could retrieve the field values, but how would I then use the blpuid and contigid to select the rows in BLPUsCavPlace?

Any help is greatly appreciated!!

so i can run this:

fc = "BLPUsCavPlaceContigBLPUs"
fields = ["blpuid", "contigid"]

with arcpy.da.SearchCursor(fc, fields) as cursor:
    for row in cursor:
        print("{0}, {1}".format(row[0], row[1]))

to get the field values, i then want to take the two values and use both as objectid in the other table and run dissolve on them

Best Answer

Here is a rough draft of what I think you are after. Use the cursor to get the OBJECTID's that you need. I don't think that the da.SearchCursor is needed. The values from the cursor are used to form a SQL where clause in making a feature layer with only the two needed polygons. From there, dissolve with appropriate name. You will need to fit this your needs and give it some testing and error handling, but should give you a start.

import arcpy

arcpy.env.workspace = "...\\your_gdb"

FC = "BLPUsCavPlace"
TABLE = "BLPUsCavPlaceContigBLPUs"
FIELDS = ["blpuid", "contigid"]

with arcpy.SearchCursor(TABLE, FIELDS) as cursor:
    for row in cursor:
        OID1 = str(row[0])
        OID2 = str(row[1])
        SQL = "OBJECTID = %s OR OBJECTID = %s"%(OID1, OID2)
        arcpy.MakeFeatureLayer_management(FC, "TEMP_LYR", SQL)
        OUTPUT_NAME = "Dissolve_%s_%s"%(OID1, OID2)
        arcpy.Dissolve_management("TEMP_LYR", OUTPUT_NAME)
        arcpy.Delete_management("TEMP_LYR")

it may be the with/as, so try:

rows = arcpy.SearchCursor(TABLE, FIELDS)
for row in rows:
    OID1 = str(row[0])
    OID2 = str(row[1])
    SQL = "OBJECTID = %s OR OBJECTID = %s"%(OID1, OID2)
    arcpy.MakeFeatureLayer_management(FC, "TEMP_LYR", SQL)
    OUTPUT_NAME = "Dissolve_%s_%s"%(OID1, OID2)
    arcpy.Dissolve_management("TEMP_LYR", OUTPUT_NAME)
    arcpy.Delete_management("TEMP_LYR")

EDIT: this one is almost there, just having a problem with the output name as in the comment below

FC = "BLPUsCavPlace"
TABLE = "BLPUsCavPlaceContigBLPUs"
FIELDS = ["blpuid", "contigid"]
with arcpy.da.SearchCursor(TABLE, FIELDS) as cursor:
    for row in cursor:
        OID1 = str(int(row[0]))
        OID2 = str(int(row[1]))
        SQL = "OBJECTID = %s OR OBJECTID = %s"%(OID1, OID2)
        arcpy.MakeFeatureLayer_management(FC, "TEMP_LYR", SQL)
        OUTPUT_NAME = "Dissolve_%s_%s"%(OID1, OID2)
        arcpy.Dissolve_management("TEMP_LYR", OUTPUT_NAME)
        arcpy.Delete_management("TEMP_LYR")