Solved! Go to Solution.
fc = "fishnet" def updateROW(x, y, z, n): while x <= 25: rows = arcpy.UpdateCursor(fc) for row in rows: if row.OID == x and x > y and x <= z: row.ROW = n x += 1 rows.updateRow(row) else: y += 5 z += 5 n += 1 row.ROW = n x += 1 rows.updateRow(row) def updateCOLUMN(): x = 1 n = 1 while x <= 25: rows = arcpy.UpdateCursor(fc) y = x for row in rows: if row.OID == y and y <= 25 and n < 6: row.COLUMN_ = n y += 5 rows.updateRow(row) x += 1 n += 1 updateROW(1, 0, 5, 1) updateCOLUMN()
Hi James,
How was the polygon GRID created? Did you use the 'Create Fishnet' tool in ArcToolbox? If not, is there an ObjectID that increments from left to right? And, is there an equal amount of rows and columns?
fc = "fishnet" def updateROW(x, y, z, n): while x <= 25: rows = arcpy.UpdateCursor(fc) for row in rows: if row.OID == x and x > y and x <= z: row.ROW = n x += 1 rows.updateRow(row) else: y += 5 z += 5 n += 1 row.ROW = n x += 1 rows.updateRow(row) def updateCOLUMN(): x = 1 n = 1 while x <= 25: rows = arcpy.UpdateCursor(fc) y = x for row in rows: if row.OID == y and y <= 25 and n < 6: row.COLUMN_ = n y += 5 rows.updateRow(row) x += 1 n += 1 updateROW(1, 0, 5, 1) updateCOLUMN()
Here is an example of some code that I got to work after running the Fishnet tool. I created a two new fields called 'ROW' and 'COLUMN_' and then used the following functions to update them:fc = "fishnet" def updateROW(x, y, z, n): while x <= 25: rows = arcpy.UpdateCursor(fc) for row in rows: if row.OID == x and x > y and x <= z: row.ROW = n x += 1 rows.updateRow(row) else: y += 5 z += 5 n += 1 row.ROW = n x += 1 rows.updateRow(row) def updateCOLUMN(): x = 1 n = 1 while x <= 25: rows = arcpy.UpdateCursor(fc) y = x for row in rows: if row.OID == y and y <= 25 and n < 6: row.COLUMN_ = n y += 5 rows.updateRow(row) x += 1 n += 1 updateROW(1, 0, 5, 1) updateCOLUMN()
You will need to update some of the variables depending on the size of your Fishnet. The above example is executed on a 5x5 fishnet.
For the updateROW function, the 'while' loop is the total number of polygons, and the 'y' and 'z' increment values are based on the number of rows.
For the updateCOLUMN function, the 'while' loop is the total number of polygons, the 'y' variable in the 'if' statement is the total number of polygons, 'n < 6' represents one less the number of columns, and the 'y' variable is incremented by the number of columns.
The only problem with the above code is that it starts at the bottom left corner of the fishnet feature class rather than the top left. You would need to populate a field with values of the OID field in descending value, then use that field in the function.
I have not thoroughly tested this, and there is a probably a more elegant way to do this, but this may help you get started.