ArcGIS – Field Calculator Python Numbering Rows of Fishnet

arcgis-desktopfield-calculatorpython-parser

I have a grid of 100*100 cells. I want to add to each cell a number representing the row it is in (like in excel). I added a field and want try to solve that with field calculator:

The code is something like this:

def test( OBJECTID ):

 if OBJECTID > 0 and OBJECTID < 101:
     return 1

 elif OBJECTID > 100 and OBJECTID < 201:
     return 2

 elif OBJECTID > 200 and OBJECTID < 301:
     return 3

 elif OBJECTID > 300 and OBJECTID < 401:
     return 4

 elif OBJECTID > 400 and OBJECTID < 501:
     return 5

 elif  OBJECTID > 500 and OBJECTID < 601:
     return 6

 elif  OBJECTID > 600 and OBJECTID < 701:
     return 7

This works good, however I want to automate it a little bit. The return value shall automatically be counted to the next number.

I tried to solve this with autoincrement. Though it adds a number per cell and not per row. Any ideas how to solve that?

Meanwhile I figured, that the cells of each row have obviously the same coordinates and with the help of another guy I have now this code. However I receive only NULL values for the fields. Any ideas what is wrong

def test( X_KORD_ED ):
    var = 0
    old_i = object() 

    for i in X_KORD_ED: 
        if i != old_i:
            var += 1
            old_i = i
        yield i, var

Best Answer

Calculate based on the shape Xmin minus the feature class Xmin (the origin), the divide by the cell width.

So The function will be something like:

get_rownum(!shape.extent.XMin!, !shape.extent.XMax!)

And the code block will be something like:

def get_rownum(xmin, xmax):
    return (xmin - origin) // (xmax - xmin) #fill in the origin

For help see some examples of using field calculator.

Related Question