Select to view content in your preferred language

GRID, assign ROW/COL index values to each cell

2218
5
Jump to solution
09-24-2012 05:30 AM
JamesCrandall
MVP Frequent Contributor
Sorry for the cross-post, but the OP in the Python forum is not getting much more attention...

Perhaps I am missing the obvious here, but I am in need of (Python, VB or C#) a way to populate ROW, COL field values in a polygon grid. That is, starting at cell 1 (top left) and moving right, set the cell Row, Col values:

Row, Col Row, Col Row, Col
[1, 1] [1, 2] [1, 3]
[2, 1] [2, 2] [2, 3]
[3, 1] [3, 2] [3, 3]
[4, 1] [4, 2] [4, 3]

This is the same functionality found in the XTools Extension, and yes it works great!  However, I need to implement this same functionality in another tool that has additional automation requirements.

Any help or comments are appreciated!
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
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.

View solution in original post

0 Kudos
5 Replies
JakeSkinner
Esri Esteemed Contributor
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?
0 Kudos
JamesCrandall
MVP Frequent Contributor
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?


Hi Jake -- I *can* build the GRID with the Fishnet tool/toolbox.  Yes, there is an objectID, but I may or may not increment in that manner.  (some polygon layer(s) to be used as input may have been built with Arc/Info fishnet and generate as coverages.  But I am attempting to come up with a solution that can be done with existing ArcGIS 9.x and 10.x Toobox tools and custom tools --- however it needs to be is fine with me.

What I have so far is a messy method of clumisly grabbing Y-Axis to select the upper left polygon, then stores an point for the upper-right and lower-left of this polygon.  Then I can move right from the upper-right point (doing a select by location) and move down to the next row using the lower-left point (doing a select by location).

Again -- it's very clumsy and I still have not figured out the exact loop process/requirement.

Any additional input is appreciated!
0 Kudos
JamesCrandall
MVP Frequent Contributor
I think a change in approach is needed as this whole selecting by location of polygon extents is very messy.  Not saying it is not a viable way to go about it, and I will most certainly be able to utilize much of the code I have developed to this point, but this is just a complex way to go about things that calls for much simpler tactics.

New thinking is to implement similar functionality as found in the Fishnet Tool but extend it a bit to inlcude some additional automation (populate the Grid cells as we need them to be from my OP). Invoking LABELS, adding row/col columns, calculating these columns with fairly simple formulas and then populating a polygon FC with the values will work just fine and much more efficiently.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
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.
0 Kudos
JamesCrandall
MVP Frequent Contributor
Thanks for your ideas and input!

I came up with formulas for a field calculation process, so not sure how they will relate to looping over rows with an .UpdateCursor --- which I may end up having to use some of your approach below.

Also: the calcs can start at the Y-Axis cell or the Origin cell depending upon how the actual formula is written.

Thanks again and I will keep this thread updated with progress.

j

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.
0 Kudos