2

I'm using arcpy in ArcGIS 10.0. My code is supposed to test the intersection between all layer in a geodatabase by group of 2, and if so, erase one feature of each couple by the other depending on a reliability field. But I have an issue with stopping the listing of the list and I get the error "list index out of range". Here is the way I do the job by couple of layer :

import arcpy, os
arcpy.env.workspace = r'Z:\Documents\SIG\tests\agregation_auto\erase.gdb'
fcList = arcpy.ListFeatureClasses() 
outFolder = r"Z:\Documents\SIG\tests\agregation_auto"
count = 0 #help select the a feature class
cnt = 1 #help select the b feature class
tList = len(fcList) #Count the number of elements in the list
for fc in fcList:
    if arcpy.Exists("temp"):
            desca = arcpy.Describe("temp")
            descb = arcpy.Describe(fcList[cnt])
            namea = desca.baseName # Get the name of the a feature to name the out_poly
            nameb = descb.baseName # Get the name of the b feature to name the out_poly
            arcpy.MakeFeatureLayer_management(os.path.join(arcpy.env.workspace, temp), "fca")
            arcpy.MakeFeatureLayer_management(os.path.join(arcpy.env.workspace, fcList[cnt]), "fcb")
            arcpy.SelectLayerByAttribute_management("fca","NEW_SELECTION")
            arcpy.SelectLayerByLocation_management("fcb","INTERSECT","fca")

    else:    
            desca = arcpy.Describe(fcList[count])
            descb = arcpy.Describe(fcList[cnt])
            namea = desca.baseName # Get the name of the a feature to name the out_poly
            nameb = descb.baseName # Get the name of the b feature to name the out_poly
            arcpy.MakeFeatureLayer_management(os.path.join(arcpy.env.workspace, fcList[count]), "fca")
            arcpy.MakeFeatureLayer_management(os.path.join(arcpy.env.workspace, fcList[cnt]), "fcb")
            arcpy.SelectLayerByAttribute_management("fca","NEW_SELECTION")
            arcpy.SelectLayerByLocation_management("fcb","INTERSECT","fca")

    if arcpy.Describe("fcb").FIDSet: #Check for selection
            rowa = arcpy.SearchCursor("fca")
            rowb = arcpy.SearchCursor("fcb")
    for row in rowa:
            fiabia = row.fiabilité #get the int value of reliability (all entities of a layer have the same)
    for rows in rowb:
            fiabib = rows.fiabilité
    if fiabia > fiabib: 
            out_poly = os.path.join(outFolder, nameb + "_er_" + namea + str(count) + "_" + str(cnt))
            tmp = arcpy.Erase_analysis("fcb","fca", out_poly)
    if fiabia < fiabib:
            out_poly = os.path.join(outFolder, namea + "_er_" + nameb + str(count) + "_" + str(cnt))
            tmp = arcpy.Erase_analysis("fcb","fca", out_poly)

    if count == tList - 1:
            break
    elif cnt == tList:
            count = count + 1
            cnt = count + 1
    else:
            cnt = cnt + 1



del rowa, rowb, row, rows, desca, descb, namea, nameb, fc, fcList
arcpy.SelectLayerByAttribute_management("fca", "CLEAR_SELECTION")
arcpy.Delete_management("fca")
arcpy.Delete_management("fcb")

To help understanding the above, imagine the following list :

list = [0,1,2,3,4]

The loop will do the job for 0 with 1 first, then for 0 with 2, 0 with 3, 0 with 4.Then, 1 with 2, 2 with three, 3 with 4 and so on until count = 3 and cnt = 4 where I want to stop the process to avoid the out of range list.

Any idea ?

6
  • Should the if statement be indented one more level? What line is giving you the error?
    – artwork21
    Dec 9 '14 at 13:57
  • Yes it's a mistake when copying code on the forum. I'm correcting this right know.
    – Samy-DT
    Dec 9 '14 at 13:59
  • Can you explain or further detail what are you trying to do? I am confused by your sentence: > My code is supposed to test the intersection between all layer in a geodatabase by group of 2, and if so, erase one feature of each couple by the other depending on a reliability field.
    – dassouki
    Dec 9 '14 at 14:08
  • Yes, I know it's difficult to explain. My aim is to combine the best part of some features of a geodatabase in one feature class. A good layer has a reliability field value superior to a worse layer. To do this, I must erase the worst features by the better ones when there is intersection before combining layers. This script is supposed to do the erase but I didn't wrote this part of the code here because the error deals only with the list and its increment.
    – Samy-DT
    Dec 9 '14 at 14:20
  • @Samy-DT , I just realized that your indentations are different in the top code you have four spaces while it's eight on the bottom. In addition, I would recommend you update your question to reflect your above comment.
    – dassouki
    Dec 9 '14 at 14:22
3

This should work better with nested for loops based on the list length, instead of iterating through the entire list.

for count in range(tList-1):
    for cnt in range(tList):
        arcpy.MakeFeatureLayer_management(os.path.join(workspace, fcList[count]),"fca")
        arcpy.MakeFeatureLayer_management(os.path.join(workspace, fcList[cnt]), "fcb")
        #other code (test intersection, choose which one must be erased by the other)
1
  • it's ok for the list range problem. My code need still some corrections to do exactly what I want but I'll search more by myself before posting again.
    – Samy-DT
    Dec 9 '14 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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