More

Problems with nested loops - How to start from 0?

Problems with nested loops - How to start from 0?


I have :

  • 12 lines feature classes (lineFC)
  • A polygon feature class (grid)
  • Another polygon feature class with much bigger polygons (gridDivision)

The process:

  • Select one lines FC from a list
  • Select one gridDivision
  • Select all polygons in grid inside gridDivision
  • Select lines inside gridDivision
  • Make spatial join between lines and grid

This is done for all gridDivision. There is a loop with a SearchCursor that looks for the oid. The result is 5 spatial join FC

Once this is done all 5 spatial joins are merged and converted to a raster.

The problem:

This works fine for the first line feature class from the list. The second time the loop runs the oid doesn't start from 0 but it stays at the last value. So I don't get 5 spatial joins but only one.

WHY?

I have tried cursor.reset() but it didn't work.

I have tried with different indentations of the loops but it didn't work either.

# Import import arcpy import time import re # Set timer from datetime import datetime startTime = datetime.now() # Set environment arcpy.env.workspace = r"E:DensityMapsDensityMapsTest.gdb" arcpy.env.overwriteOutput = True # Set local variables and make layers gridDivision = arcpy.MakeFeatureLayer_management("GridDivisionTEST", "GridDivisionTEST_lyr") grid = arcpy.MakeFeatureLayer_management(r"E:DensityMapsDensityMaps1Km_1.gdbGrid1km_Clip_EraseOutsideBaltic", "Grid1km_Clip_EraseOutsideBaltic_lyr") matchOption = "INTERSECT" # Make list of all lines linesList = arcpy.ListFeatureClasses("*Lines_Project") #print linesList # Go through all lines feature classes for lineFC in linesList: print "THIS IS THE LINE FILE: "+lineFC with arcpy.da.SearchCursor(gridDivision, ["[email protected]"]) as cursor: for oid in cursor: print (oid[0]) #print "THIS IS THE OID " +str(oid[0]) # Prepare to make the query to select one big square from GridDivision at a time descObj = arcpy.Describe(gridDivision) field = descObj.OIDFieldName df = arcpy.AddFieldDelimiters(gridDivision,field) query = df + " = " + str(oid[0]) #print query #Select squares of gridDivision starting with 1 arcpy.SelectLayerByAttribute_management(gridDivision, "NEW_SELECTION", query) print "GridDivision number "+str(oid[0])+" selected: "+str(datetime.now() - startTime) # Select grid inside gridDivision arcpy.SelectLayerByLocation_management(grid, "WITHIN", gridDivision) print "Grid selected: "+str(datetime.now() - startTime) # Make layer of the selection. (This is to hold the selection) arcpy.MakeFeatureLayer_management(grid, "grid_lyr") # Select lines inside gridDivision and add them to the previous selection print "Selecting "+lineFC+" inside GridDivision "+str(oid)+"… " arcpy.SelectLayerByLocation_management("grid_lyr", matchOption, lineFC, "", "ADD_TO_SELECTION") print lineFC+ " selected: "+str(datetime.now() - startTime) # Make Spatial Join print "Making SpatialJoin between "+lineFC+"and grid inside GridDivision "+str(oid)+"… " arcpy.SpatialJoin_analysis(grid, lineFC, lineFC+"_SpatialJoin_"+str(oid[0]), "JOIN_ONE_TO_MANY", "", "", matchOption) print "Spatial Join of grid division "+ str(oid) +"and "+lineFC+" done: "+str(datetime.now() - startTime)+"
" # Select pieces of Spatial Join fileList = arcpy.ListFeatureClasses(lineFC+"_SpatialJoin*") # Search the name of the month in lines string month = re.search('scope_ais_(.+?)_Lines_Project', lineFC).group(1) #print month # Merge them print "Merging the spatial joins of "+month+"… " arcpy.Merge_management(fileList, month+"_Merged") print month+" merged: "+str(datetime.now() - startTime)+"
" #Delete spatial join feature classes to save space in disk for deleteSpatialJoinFC in fileList: arcpy.Delete_management(deleteSpatialJoinFC) print "Deleted Spatial Joins to save space in disk" # Dissolve it print "Dissolving "+month+"… " arcpy.Dissolve_management(month+"_Merged", month+"_Dissolve", "TARGET_FID", [["Join_Count", "SUM"]]) print "Dissolved: "+str(datetime.now() - startTime)+"
" #Delete merged feature classes to save space in disk arcpy.Delete_management(month+"_Merged") print "Deleted merged feature class to save space in disk"+"
" # Make raster print "Making "+month+" raster… " arcpy.FeatureToRaster_conversion(month+"_Dissolve","SUM_Join_Count", month+"_2014_Raster") print "RASTER "+month+" READY!!: "+str(datetime.now() - startTime)+"
" #Print end time print "Finished "+str(datetime.now() - startTime)

This printed messages might also help understand the problem. You can see that after the message:

RASTER april READY!!: 0:00:24.593000

the new file scope_ais_august_Lines_Project starts but the OID is 7 and not 0

THIS IS THE LINE FILE: scope_ais_april_Lines_Project 1 GridDivision number 1 selected: 0:00:01.656000 Grid selected: 0:00:01.687000 Selecting scope_ais_april_Lines_Project inside GridDivision (1,)… scope_ais_april_Lines_Project selected: 0:00:01.843000 Making SpatialJoin between scope_ais_april_Lines_Projectand grid inside GridDivision (1,)… Spatial Join of grid division (1,)and scope_ais_april_Lines_Project done: 0:00:05.265000 2 GridDivision number 2 selected: 0:00:05.297000 Grid selected: 0:00:05.312000 Selecting scope_ais_april_Lines_Project inside GridDivision (2,)… scope_ais_april_Lines_Project selected: 0:00:05.469000 Making SpatialJoin between scope_ais_april_Lines_Projectand grid inside GridDivision (2,)… Spatial Join of grid division (2,)and scope_ais_april_Lines_Project done: 0:00:08.891000 5 GridDivision number 5 selected: 0:00:08.922000 Grid selected: 0:00:08.937000 Selecting scope_ais_april_Lines_Project inside GridDivision (5,)… scope_ais_april_Lines_Project selected: 0:00:09.094000 Making SpatialJoin between scope_ais_april_Lines_Projectand grid inside GridDivision (5,)… Spatial Join of grid division (5,)and scope_ais_april_Lines_Project done: 0:00:12.511000 6 GridDivision number 6 selected: 0:00:12.527000 Grid selected: 0:00:12.558000 Selecting scope_ais_april_Lines_Project inside GridDivision (6,)… scope_ais_april_Lines_Project selected: 0:00:12.714000 Making SpatialJoin between scope_ais_april_Lines_Projectand grid inside GridDivision (6,)… Spatial Join of grid division (6,)and scope_ais_april_Lines_Project done: 0:00:16.136000 7 GridDivision number 7 selected: 0:00:16.168000 Grid selected: 0:00:16.183000 Selecting scope_ais_april_Lines_Project inside GridDivision (7,)… scope_ais_april_Lines_Project selected: 0:00:16.355000 Making SpatialJoin between scope_ais_april_Lines_Projectand grid inside GridDivision (7,)… Spatial Join of grid division (7,)and scope_ais_april_Lines_Project done: 0:00:19.777000 Merging the spatial joins of april… april merged: 0:00:21.365000 Deleted Spatial Joins to save space in disk Dissolving april… Dissolved: 0:00:23.249000 Deleted merged feature class to save space in disk Making april raster… RASTER april READY!!: 0:00:24.593000 THIS IS THE OID 7 THIS IS THE LINE FILE: scope_ais_august_Lines_Project 7 GridDivision number 7 selected: 0:00:24.624000 Grid selected: 0:00:24.640000 Selecting scope_ais_august_Lines_Project inside GridDivision (7,)… scope_ais_august_Lines_Project selected: 0:00:24.796000 Making SpatialJoin between scope_ais_august_Lines_Projectand grid inside GridDivision (7,)… Spatial Join of grid division (7,)and scope_ais_august_Lines_Project done: 0:00:28.259000 Merging the spatial joins of august… august merged: 0:00:28.884000 Deleted Spatial Joins to save space in disk Dissolving august… Dissolved: 0:00:29.633000 Deleted merged feature class to save space in disk Making august raster… RASTER august READY!!: 0:00:30.930000 THIS IS THE OID 7 Finished 0:00:30.930000

This is because you create a selection on yourgridDivisionlayer. The cursor then only iterates through selected features.

Clear your selection after your cursor.

# Go through all lines feature classes for lineFC in linesList: print "THIS IS THE LINE FILE: "+lineFC with arcpy.da.SearchCursor(gridDivision, ["[email protected]"]) as cursor: for oid in cursor: print (oid[0]) #print "THIS IS THE OID " +str(oid[0])… other code… # Clear selection <--------- NEW CODE arcpy.SelectLayerByAttribute_management(gridDivision, "CLEAR_SELECTION")