import arcpy, os
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "NEW_BiState_Grid400_IowaSP") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "GridIndexFeatures20") [0]
#alpha will be assigned a letter to rows2 update, there are 16
alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
name = str()
searchrow = 0
rows1 = arcpy.SearchCursor(mapLyr1, "", "", "Name")
rows2 = arcpy.UpdateCursor(mapLyr2, "", "", "tile")
rows = arcpy.UpdateCursor(mapLyr2)
for row in rows1:
name = row.getValue("Name")
print name
arcpy.SelectLayerByAttribute_management(mapLyr1, "NEW_SELECTION", '"FID" = %s' %searchrow)
searchrow = searchrow + 1
arcpy.SelectLayerByLocation_management(mapLyr2, "HAVE_THEIR_CENTER_IN", mapLyr1, 0, "ADD_TO_SELECTION")
for row in rows2:
row.tile = name + A
rows2.updateRow(row)
del mxd, rows, rows1, rows2, searchrowimport arcpy, os
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "NEW_BiState_Grid400_IowaSP") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "NEW_BiState_Grid100_IowaSP") [0]
#alpha will be assigned a letter to rows2 update, there are 16
place = 0
alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P']
name = str()
searchrow = 0
rows1 = arcpy.SearchCursor(mapLyr1, "", "", "Name")
rows2 = arcpy.UpdateCursor(mapLyr2, "", "", "tile")
rows = arcpy.UpdateCursor(mapLyr2)
for row in rows1:
name = row.getValue("Name")
print name
arcpy.SelectLayerByAttribute_management(mapLyr1, "NEW_SELECTION", '"FID" = %s' %searchrow)
searchrow = searchrow + 1
arcpy.SelectLayerByLocation_management(mapLyr2, "HAVE_THEIR_CENTER_IN", mapLyr1, 0, "ADD_TO_SELECTION")
for row in rows2:
row.tile = name + alpha[place]
rows2.updateRow(row)
place = place + 1
if place == 16:
place = 0
del mxd, rows, rows1, rows2, searchrowThanks. That's what I ended up doing. But I didn't realize you could use a defined variable as the index number in the list. I thought it was necessary to format the line with something like alpha[%d] %letterplace. Here's my final script, but it is taking FOREVER to run through all the features.import arcpy, os #set map doc and the layer to be used mxd = arcpy.mapping.MapDocument("Current") mapLyr1 = arcpy.mapping.ListLayers(mxd, "NEW_BiState_Grid400_IowaSP") [0] mapLyr2 = arcpy.mapping.ListLayers(mxd, "NEW_BiState_Grid100_IowaSP") [0] #alpha will be assigned a letter to rows2 update, there are 16 place = 0 alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'] name = str() searchrow = 0 rows1 = arcpy.SearchCursor(mapLyr1, "", "", "Name") rows2 = arcpy.UpdateCursor(mapLyr2, "", "", "tile") rows = arcpy.UpdateCursor(mapLyr2) for row in rows1: name = row.getValue("Name") print name arcpy.SelectLayerByAttribute_management(mapLyr1, "NEW_SELECTION", '"FID" = %s' %searchrow) searchrow = searchrow + 1 arcpy.SelectLayerByLocation_management(mapLyr2, "HAVE_THEIR_CENTER_IN", mapLyr1, 0, "ADD_TO_SELECTION") for row in rows2: row.tile = name + alpha[place] rows2.updateRow(row) place = place + 1 if place == 16: place = 0 del mxd, rows, rows1, rows2, searchrow
Is it necessary to define "row in rows2:" as "row2 in rows2" since row is in use by rows1? I thought "row" was a keyword used by arcpy, because I think trying "row2 in rows2" was giving me an error.
import arcpy, os
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr1 = arcpy.mapping.ListLayers(mxd, "NEW_BiState_Grid400_IowaSP") [0]
mapLyr2 = arcpy.mapping.ListLayers(mxd, "NEW_BiState_Grid100_IowaSP") [0]
#alpha will be assigned a letter to rows2 update, there are 16
place = 0
alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P']
searchrow = 0
rows1 = arcpy.SearchCursor(mapLyr1, "", "", "Name")
rowcount = int(arcpy.GetCount_management(mapLyr1).getOutput(0))
allrows = (rowcount + 0.0)
for row in rows1:
bigtile = str()
arcpy.SelectLayerByAttribute_management(mapLyr1, "NEW_SELECTION", '"FID" = %s' %searchrow)
bigtile = row.getValue("Name")
print bigtile
searchrow = searchrow + 1
prgrow = (searchrow + 0.0)
arcpy.SelectLayerByLocation_management(mapLyr2, "HAVE_THEIR_CENTER_IN", mapLyr1, 0, "ADD_TO_SELECTION")
rows2 = arcpy.UpdateCursor(mapLyr2, "", "", "", "FID")
for row2 in rows2:
row2.tile = bigtile + alpha[place]
rows2.updateRow(row2)
place = place + 1
if place == 16:
place = 0
arcpy.SelectLayerByAttribute_management(mapLyr1, "CLEAR_SELECTION")
arcpy.SelectLayerByAttribute_management(mapLyr2, "CLEAR_SELECTION")
prgrss = ((prgrow / allrows)*100.0)
arcpy.AddMessage("%d current row" %searchrow)
arcpy.AddMessage("%d total rows" %rowcount)
arcpy.AddMessage("%f percent completed" %prgrss)
arcpy.AddMessage("______________________________")
del mxd, row, rows1, row2, rows2, searchrow, place, bigtile, rowcount, prgrow, allrows clock1 = datetime.now() #beginning of loop
clock2 = datetime.now() end of loop
clock3 = ((clock2 - clock1) * rowsleft) # rowsleft is total records minus completed records
clock4 = clock3.strftime('%d Days, %h Hours, %M Minutes, %S Seconds')