4

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

3 Answers 3

3

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.

1

You need to keep track of the row number and the column number.

When you reach the end of the row (column number =100) then increment the row number and start the column number at 1.

Keep going until you have completed the last row.

2
  • Sorry, I just edited the example code. could you please give a hint what to change exactley on the code?
    – juhuli
    Oct 11, 2012 at 13:31
  • Simpler: the row is one greater than the integer part of OBJECTID/100.
    – whuber
    Oct 15, 2012 at 15:55
1

Thanks for the answers. I solved the problem via the coordiantes of the cells/polygons. I summarized the x and y coordinate, exported it to excel, added 1-100 and ABC and joined it back to the feature class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.