0

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)

EDIT:

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.

EDIT:

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[0]
        blpu2 = row[1]
        for searchRow in searchRows:
            searchRow1 = searchRow[0]
            searchRow2 = searchRow[1]
            if searchRow1 == blpu1 and searchRow2 <> blpu2:
                tabcur.insertRow(n_row)
                arcpy.da.UpdateCursor(DISSOLVED2, "OID3", searchRow2)

1 Answer 1

1

You appear to be mixing up your cursors quite badly. You start using a da update cursor and using it should be more like this:

blpu1, blpu2 = row

or

blpu1 = row[0]
blpu2 = row[1]

But you never actually change the values in any of the points, so you should really only use a search cursor for it. You then use an old (non da) Search Cursor for the second part which I am sure is confusing you. Personally I would use da.SearchCursor and access it like the above with [] so it all is one type of cursor.

Really not sure what you are doing at the end with the search cursor and the calculate field. If you are trying to add a value into "OID3" in the current row you are in, you need to add that to the UpdateCursor and then do it that way not using CalculateField. Looks like you are also trying to add rows from the second feature back into the first? That can be done with an InserCursor, but not the way you are doing it.

Can you explain what you are trying to do and why? That would help greatly with understanding why you are doing certain things. Hopefully this is enough information to allow you to keep working though. If not, there is a lot more great information on da cursors here.

Edit: Also why are you opening an edit session? All of the paths are hard coded so it doesn't look like there would be a purpose to the edit session. The cursors can do whatever they need to do without an edit session. They are mainly just for being able to undo edits you are doing by hand, and that ends when you end the edit session.

2
  • ok thanks I'll have a look through, quite new to all of this. i started editing because of an error message I was getting that seemed to want an edit. i think i read that if you have nested cursors you should open an edit session. Commented Aug 22, 2013 at 15:44
  • the insert row is supposed to take the row that is being used in Sites_Copy, append it to the bottom of Sites_Copy and calculate field OID3 as being nbr_OBJECTID in the other table Commented Aug 22, 2013 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.