Need script to select each feature, one at a time

3054
5
11-02-2012 04:36 AM
johnodonnell
New Contributor
Hello all-

I'm having a tough time getting started on what should be a relatively simple script. I have a feature class consisting of sinks in a geometric network. I need my script to initially select the first record, perform some functions, finish those functions, then move to the next record, and repeat until the end. I've set up a search cursor to cycle through the records, but my problem is making a feature layer from the "current" record in the search cursor. Any input on how I could do this? Thanks, John
Tags (2)
0 Kudos
5 Replies
markdenil
Occasional Contributor III
You want to use your cursor to build a list of the individual features in the fc
Then use the list to cycle through the fc, picking each in turn.

#  make a list of the feature IDs
theSource = r"C:\some.gdb\FeatureClass"
cur = arcpy.SearchCursor(theSource)
theIdList = []
for row in cur:
    theID = row.OBJECTID
    if theID not in theIdList:
 theIdList.append(theID)
del cur

#  count the entries in the list
idCount = len(idList)

for each in range(idCount):
    print "%s of %s" % (each + 1, idCount)
    #   build a feature where clause
    segWhere = '"OBJECTID" = %s' % idList[each]

    #   make a layer of each feature in fc
    if arcpy.Exists("MyTempLayer"):
        arcpy.Delete_management(("MyTempLayer")

    arcpy.MakeFeatureLayer_management(theSource,
          "MyTempLayer",
          segWhere)
    ###  Do stuff to that feature here....
0 Kudos
johnodonnell
New Contributor
Thanks for the reply! I think this will work.

After writing more script, I'm coming across another issue. I'm basically performing a TRACE_UPSTREAM on each feature in the feature class. That will produce a selection set of upstream structures and conveyances. I want to take the unique FacilityID from the selected sink and update the selected upstream structures with that value. I have set up an update cursor to update the records, but I'm getting an error when trying to get the value from the selected sink. I guess once the feature is in a list I can no longer use the getValue command. So should I store this value in the list as well?

Thanks again for any input.
0 Kudos
markdenil
Occasional Contributor III
investigate either nested lists of all the field values you need from each record:
allrecordsList = [[field1, field2, field3], [field1, field2, field3], [field1, field2, field3], ...]

or, try a dictionary for a similar thing.
0 Kudos
johnodonnell
New Contributor
I've built a lot upon the above suggestions and currently have a script that will:

1. Add all sink features to a list.
2. Use the list to cycle through the features and select them one at a time.
3. Create feature layer from this selection.
4. Use the feature layer as a sink to trace the geometric network.
5. Update the selected (traced) features in a networked feature class with the FacilityID of the first selected sink.

I need for the script to eliminate the selection and start fresh with the next feature in line, trace from there, and update all the upstream selected features. However, as of now it will not produce the intended results. What's confusing is I get different messages/results every time I run it. Could anyone please take a look and see if I'm missing something obvious? Thanks, John

import arcpy
arcpy.env.overwriteOutput=True
arcpy.env.workspace = "D:\FacID_Python\SO_CSS_Master_9x_GDB.gdb"

sinkFeatures = "D:\\FacID_Python\\SO_CSS_Master_9x_GDB.gdb\\SO_CSS_Master\\Export_Output"
dCatchBasin = "D:\\FacID_Python\\SO_CSS_Master_9x_GDB.gdb\\SO_CSS_Master\\DCatchBasin_Master"
geometricNetwork = "D:\\FacID_Python\\SO_CSS_Master_9x_GDB.gdb\\SO_CSS_Master\\SO_CSS_Master_Net"
mxd = arcpy.mapping.MapDocument("D:\FacID_Python\Working.mxd")

sinkList = []

rows = arcpy.SearchCursor(sinkFeatures)
row = rows.next()

while row:
    featureID = row.OBJECTID
    if featureID not in sinkList:
        sinkList.append(featureID)
    row = rows.next()
del rows

print sinkList

sinkCount = len(sinkList)

for each in range(sinkCount):
    print "%s of %s" % (each +1, sinkCount)
    sinkWhere = '"OBJECTID" = %s' % sinkList[each]

    if arcpy.Exists("TempSinkLayer"):
        arcpy.Delete_management("TempSinkLayer")


    arcpy.MakeFeatureLayer_management(sinkFeatures, "TempSinkLayer", sinkWhere)

    cursor = arcpy.SearchCursor("TempSinkLayer")
    sink = cursor.next()
    while sink:
        facID = sink.getValue("FACILITYID")
        print facID
        sink = cursor.next()
    del sink
    del cursor


    try:
        outGroupLayer = "Trace" + facID
        arcpy.SetFlowDirection_management(geometricNetwork, "WITH_DIGITIZED_DIRECTION")
        arcpy.TraceGeometricNetwork_management(geometricNetwork, outGroupLayer, "TempSinkLayer", "TRACE_UPSTREAM", "", "", "", "", "", "", "" ,"", "", "", "", "", "", "")
    except:
        print "Can't trace"

    try:
        dCatchLayer = facID + "dCatch"
        arcpy.SelectData_management(outGroupLayer, "DCatchBasin_Master")
        arcpy.MakeFeatureLayer_management("DCatchBasin_Master", dCatchLayer)
        result = int(arcpy.GetCount_management(dCatchLayer).getOutput(0))
        print result
    except:
        print "can't make feature layer or access selection"


    try:
        dCatchs = arcpy.UpdateCursor(dCatchLayer)
        dCatch = dCatchs.next()
        fieldID = "DnComboFacID"

        while dCatch:
            dCatch.setValue(fieldID, facID)
            dCatchs.updateRow(dCatch)
            dCatch = dCatchs.next()

        del dCatch
        del dCatchs

    except:
        print "can't update rows"


    try:
        arcpy.Delete_management(dCatchLayer)
    except:
        print "can't delete the feature layer"

    try:
        arcpy.SelectLayerByAttribute_management("DCatchBasin_Master", "CLEAR_SELECTION")
        for df in arcpy.mapping.ListDataFrames(mxd):
            for lyr in arcpy.mapping.ListLayers(mxd, "", df):
                if lyr.name == "Trace" + facID:
                    arcpy.mapping.RemoveLayer(df, lyr)
                else:
                    pass

    except:
        print "can't delete the group layer"





0 Kudos
johnodonnell
New Contributor
I got it working! For anyone interested, the following script will take a point feature class representing sinks in a geometric network, select each one at a time, trace upstream from each, and update a field in an upstream feature class with a unique ID of the sink it traces to. Cheers!

import arcpy
arcpy.env.overwriteOutput=True
arcpy.env.workspace = "D:\FacID_Python\SO_CSS_Master_9x_GDB.gdb"

sinkFeatures = "D:\\FacID_Python\\SO_CSS_Master_9x_GDB.gdb\\SO_CSS_Master\\SManhole_Master"
dCatchBasin = "D:\\FacID_Python\\SO_CSS_Master_9x_GDB.gdb\\SO_CSS_Master\\DCatchBasin_Master"
geometricNetwork = "D:\\FacID_Python\\SO_CSS_Master_9x_GDB.gdb\\SO_CSS_Master\\SO_CSS_Master_Net"
mxd = arcpy.mapping.MapDocument("D:\FacID_Python\Working.mxd")

sinkList = []

rows = arcpy.SearchCursor(sinkFeatures)
row = rows.next()

while row:
    featureID = row.OBJECTID
    if featureID not in sinkList:
        sinkList.append(featureID)
    row = rows.next()
del rows

print sinkList

sinkCount = len(sinkList)

for each in range(sinkCount):
    print "%s of %s" % (each +1, sinkCount)
    sinkWhere = '"OBJECTID" = %s' % sinkList[each]

    if arcpy.Exists("TempSinkLayer"):
        arcpy.Delete_management("TempSinkLayer")


    arcpy.MakeFeatureLayer_management(sinkFeatures, "TempSinkLayer", sinkWhere)

    cursor = arcpy.SearchCursor("TempSinkLayer")
    sink = cursor.next()
    while sink:
        facID = sink.getValue("FACILITYID")
        print facID
        sink = cursor.next()
    del sink
    del cursor


    try:
        outGroupLayer = str("Trace" + facID)
        arcpy.SetFlowDirection_management(geometricNetwork, "WITH_DIGITIZED_DIRECTION")
        arcpy.TraceGeometricNetwork_management(geometricNetwork, outGroupLayer, "TempSinkLayer", "TRACE_UPSTREAM", "", "", "", "", "", "", "" ,"", "", "", "", "", "", "")
    except:
        print "Can't trace"

    try:
        dCatchLayer = str(facID + "dCatch")
        arcpy.SelectData_management(outGroupLayer, "DCatchBasin_Master")
        arcpy.MakeFeatureLayer_management("DCatchBasin_Master", dCatchLayer)
        result = int(arcpy.GetCount_management(dCatchLayer).getOutput(0))
        print result

        dCatchs = arcpy.UpdateCursor(dCatchLayer)
        dCatch = dCatchs.next()
        fieldID = "DnComboFacID"

        while dCatch:
            dCatch.setValue(fieldID, facID)
            dCatchs.updateRow(dCatch)
            dCatch = dCatchs.next()

        del dCatch
        del dCatchs

        arcpy.Delete_management(dCatchLayer)
        arcpy.Delete_management("DCatchBasin_Master")
        arcpy.SelectLayerByAttribute_management("DCatchBasin_Master", "CLEAR_SELECTION")
        ##for df in arcpy.mapping.ListDataFrames(mxd):
            ##for lyr in arcpy.mapping.ListLayers(mxd, "", df):
                ##if lyr.name == "Trace" + facID:
                    ##arcpy.mapping.RemoveLayer(df, lyr)
                ##else:
                    ##pass

    except:
        print "not working"







PS- the code works within PyScripter, but for some reason gets wonky results when run within ArcMap.
0 Kudos