I'm trying to loop through each row in table "Sites_Copy" and look at "OID1" and "OID2". Then take these and loop through "BLPUs_PolygonNeighbors" and compare these to "src_OBJECTID" and "nbr_OBJECTID".
If "OID1" matches "src_OBJECTID" and "OID2" doesn't match "nbr_OBJECTID", then copy the row and insert it to "Sites_Copy" and calculate "OID3" to be "nbr_OBJECTID".
However, this code doesn't seem to be doing it. Currently it doesn't even like the getValue. I'm in arcGIS 10.1, any help greatly appreciated.
import arcpy, os, sys from arcpy import env arcpy.env.workspace = "C:\Users\Ant\Documents\ArcGIS\ITN.gdb" edit = arcpy.da.Editor(r'C:\Users\Ant\Documents\ArcGIS\ITN.gdb') edit.startEditing(False,True) DISSOLVED2 = "Sites_Copy" with arcpy.da.UpdateCursor(DISSOLVED2,["OID1","OID2"]) as cursor: for row in cursor tabcur = arcpy.InsertCursor(DISSOLVED2) n_row = tabcur.newRow() TABLE = "BLPUs_PolygonNeighbors" searchRows = arcpy.SearchCursor(TABLE,["src_OBJECTID","nbr_OBJECTID"]) blpu1 = row.getValue("OID1") blpu2 = row.getValue("OID2") for searchRow in searchRows: searchRow1 = searchRow.getValue("src_OBJECTID") searchRow2 = searchRow.getValue("nbr_OBJECTID") if searchRow1 == blpu1 and searchRow2 <> blpu2: tabcur.insertRow(n_row) arcpy.CalculateField_management(DISSOLVED2, "OID3", searchRow2) edit.stopEditing(True)
To explain what I am doing - I have a layer of polygons, which I want to combine into larger and larger polygons by adding their neighbors one by one. I want to qualify these potential merges with if statements, such as whether the existing polygon is below a certain size then keep adding polygons. Eventually, I want to be able to take one layer of polygons, calculate all of their neighbors, then run a script that will keep adding neighboring polygons one by one based on certain criteria not having been met (such as size, flood risk), up to a total of 15 potential merges. But I don't want any duplicates, as this could very quickly get out of hand.
So I've written the first part that takes the original polygons, calculates neighbors and dissolves all possible 2-polygon combinations and appends them to the table. I now want to do all the possible 3-polygon combinations, then 4 etc up to 15.
Is this any closer? I'm not sure how to change the insert row part, it should copy the line being looked at in Sites_Copy and append it the end of Site_Copy. Then the updatecursor can calculate field OID3 hopefully.
DISSOLVED2 = "Sites_Copy" with arcpy.da.SearchCursor(DISSOLVED2,["OID1","OID2"]) as cursor: for row in cursor: tabcur = arcpy.InsertCursor(DISSOLVED2) n_row = tabcur.newRow() TABLE = "BLPUs_PolygonNeighbors" searchRows = arcpy.da.SearchCursor(TABLE,["src_OBJECTID","nbr_OBJECTID"]) blpu1 = row blpu2 = row for searchRow in searchRows: searchRow1 = searchRow searchRow2 = searchRow if searchRow1 == blpu1 and searchRow2 <> blpu2: tabcur.insertRow(n_row) arcpy.da.UpdateCursor(DISSOLVED2, "OID3", searchRow2)