1

I'm working on a tool that identifies features and repairs them using an existing tool. At the moment I am struggling to put my idea into code however. The purpose is to use a search cursor from a Select Features By Location layer to identify and then select features in the original data and execute another tool. As of right now, my code looks something like this (in ArcMap 9.3):

Junctions_Lyr = "Junctions_Lyr"
gp.MakeFeatureLayer_management(Junctions, Junctions_Lyr, "", "", "")
gp.SelectLayerByLocation_management(Junctions_Lyr, "INTERSECT", Corrupt_Lyr, "", "NEW_SELECTION")
gp.SaveToLayerFile_management(Junctions_Lyr, "Junctions.lyr", Workspace)
rows = gp.SearchCursor(Junctions, "OBJECTID")
curs = gp.SearchCursor(Junctions_Lyr, "OBJECTID")
for row in rows:
    if row.OBJECTID = curs.OBJECTID:
        # Something to select the feature and run clean up tool.
        rows.next()
    else:
        rows.next()

Essentially, this will (hopefully) select features in the Junctions Feature class if they intersect with a corrupted layer. They will then be selected and saved to file. The original feature class will then have a search cursor run through it, and, if the Object ID matches with one from the Saved Layer file, it will select the feature and run the clean up tool.

Am I approaching this correctly? I think my inability to properly articulate this is making the issue a lot more difficult to resolve.

5
  • I'm not that familiar with the gpscripting environment, but it doesn't appear you're looping through curs, just rows.
    – recurvata
    Sep 3, 2015 at 15:50
  • I would want to loop through rows but compare it with curs...if I'm making that clear. It would be a comparison, looking for the value of one cursor in another.
    – E.Riv15
    Sep 3, 2015 at 17:14
  • Well, since you're not looping through curs, curs.OBJECTID will never change. You're comparing each row to the same value. If that's really what you want, you don't need a search cursor for curs. You may also want to look at the Find Identical tool if 9.3 and your license level provides it. Btw, it's not a great idea to use object ids in this way, because they can change in some cases. They're guaranteed to be unique, not static.
    – recurvata
    Sep 3, 2015 at 17:24
  • You make a good point. I'll review the code and data to see if there is a better way to distinguish the feature I intend to clean up. Thanks!
    – E.Riv15
    Sep 3, 2015 at 18:58
  • You can add a new field and copy the OBJECTID to it if necessary.
    – recurvata
    Sep 4, 2015 at 12:03

2 Answers 2

1

A much more efficient method than in my first answer is to iterate through Junctions once and store all the values in a python list. Then iterate through Junctions_lyr and proceed with those values found in the list.

Junctions_Lyr = "Junctions_Lyr"
gp.MakeFeatureLayer_management(Junctions, Junctions_Lyr, "", "", "")
gp.SelectLayerByLocation_management(Junctions_Lyr, "INTERSECT", Corrupt_Lyr, "", "NEW_SELECTION")
gp.SaveToLayerFile_management(Junctions_Lyr, "Junctions.lyr", Workspace)

values = []

rows = gp.SearchCursor(Junctions, "OBJECTID")
row = rows.next ()
while row:
    values += [row.OBJECTID]
    row.next ()

curs = gp.SearchCursor(Junctions_Lyr, "OBJECTID")
cur = curs.next ()
while cur:
    if cur.OBJECTID in values:
        # Something to select the feature and run clean up tool.
    cur = curs.next ()
1
  • I think this is exactly what I was looking for. I was unsure how to apply this type of task in 9.3. Thank you!
    – E.Riv15
    Sep 4, 2015 at 20:23
0

Create your first cursor, and then for each row, iterate through your second to find the matching OID. This will acomplish your goal, though I suggest finding a Select Layer By Attribute equivalent if it exists in gp to speed things up.

I think this will do it:

Junctions_Lyr = "Junctions_Lyr"
gp.MakeFeatureLayer_management(Junctions, Junctions_Lyr, "", "", "")
gp.SelectLayerByLocation_management(Junctions_Lyr, "INTERSECT", Corrupt_Lyr, "", "NEW_SELECTION")
gp.SaveToLayerFile_management(Junctions_Lyr, "Junctions.lyr", Workspace)

rows = gp.SearchCursor(Junctions, "OBJECTID")
row = rows.next ()
while row:

    curs = gp.SearchCursor(Junctions_Lyr, "OBJECTID")
    cur = curs.next ()
    while cur:
        if row.OBJECTID = cur.OBJECTID:
            # Something to select the feature and run clean up tool.
        cur = curs.next ()

    row = rows.next()
1
  • Thanks for the input. I'll be re-writing the code today and hoping this works.
    – E.Riv15
    Sep 4, 2015 at 14:41

Your Answer

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

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