[GIS] Loop stops before expected without error message

arcgis-10.0arcpy

I try to run a script that clips all the feature class of a geodatabase by the successive features of a clip layer (named pochoir).

After Accessing geometry with SearchCursor in ArcPy and copy only selected with CopyFeature_management, the test of the intersection is know working.

In the following code, all the "for row in rows" is working. The only issue is that the script stops just after processing the first fc in fcList.
For the first fc processed, there was three rounds. The two first were True for the intersecting test while the third was not. The two first were well clipped and the third was well ignored. But I think something block after the if or the else statement because next fc is not run.

I used try/except but no error were recorded.

Here is the code :

arcpy.env.workspace = r"Z:\Documents\SIG\tests\synthese_tocorrect.gdb"
fcList = arcpy.ListFeatureClasses()
pochoir = r"Z:\Documents\SIG\tests\decoupe.shp"#Clip layer with multiple entities
rows = arcpy.SearchCursor(pochoir)
outFolder = r"Z:\Documents\SIG\tests\sorties"
count = 0
for fc in fcList:
    for row in rows:
        secteur = row.secteur
        where_clause = "secteur = '{0}'".format(secteur)
        arcpy.MakeFeatureLayer_management(pochoir, "ptmp", where_clause) #Create current clip layer
        arcpy.MakeFeatureLayer_management(fc, "toselect")#Create tmp layer to use in the select by location (no feature class allowed)
        arcpy.SelectLayerByLocation_management("toselect", "INTERSECT", "ptmp")# testing the intersect
        outName = "{0}_{1}_{2}".format(fc, secteur, count)

        if arcpy.Describe("toselect").FIDSet: #If overlap = true, do the clip
            out_poly = os.path.join(outFolder, outName)
            arcpy.Clip_analysis("toselect", "ptmp", out_poly)
            arcpy.Delete_management("ptmp")
            arcpy.Delete_management("toselect")
            count = count + 1
        else:#Else, delete the tmp layers so they could be re-created for the next fc and the next row
            arcpy.Delete_management("ptmp")
            arcpy.Delete_management("toselect")
            count = count + 1 ## The script stop here

Best Answer

The rows of the pochoir cursor can only be read once with a for loop. You need to recreate the cursor for each new feature class. Therefore the code needs to be modified to:

arcpy.env.workspace = r"Z:\Documents\SIG\tests\synthese_tocorrect.gdb"
fcList = arcpy.ListFeatureClasses()
pochoir = r"Z:\Documents\SIG\tests\decoupe.shp"#Clip layer with multiple entities
outFolder = r"Z:\Documents\SIG\tests\sorties"
count = 0
for fc in fcList:
    rows = arcpy.SearchCursor(pochoir)
    for row in rows:
       # Continue with the rest of your code ...

You should convert your code to use the data access cursors if you are scripting for ArcGIS 10.1 or later. It is 10 times faster than the cursor syntax you are using.