I created a fishnet grid and need to give the individual polygons ID values based on their associated property names and location relative to each other.

I was able to achieve the above results with the following code that I adapted from a previously asked question.


def autoIncrement(string_prefix):
    global rec
    pStart = 1 #adjust start value, if req'd 
    pInterval = 1 #adjust interval value, if req'd
    if (rec == 0): 
        rec = pStart 
        rec = rec + pInterval 
        return string_prefix + str(rec)


However, I cant figure out how to relate it to the Y_Coordinates instead of the OBJECTID.

  • When you say ID values based on their location relative to one another, are you planning on incorporated the x and y coordinate values into this ID? – reevesii Aug 27 '18 at 19:19
  • 1
    Perhaps try gis.stackexchange.com/questions/193681/… – FelixIP Aug 27 '18 at 19:31
  • I need the ID numbers to be sequential based on their location relative to one another. The xy coordinates was my solution to that problem. However, I couldn't figure out how to adapt the code in the page you mentioned to order it the way I wanted. – Linden Aug 27 '18 at 19:42

Im not sure this is what you are asking, but to add sequential ID's from top left to bottom right (like reading text) this will work:

import arcpy

fc = r'C:\data.gdb\features123'
some_prefix = 'AP'
field_to_update = 'Grid_ID'

coords = [[round(i[0],0),round(i[1],0),i[2]] for i in arcpy.da.SearchCursor(fc,['SHAPE@X','SHAPE@Y','OID@'])]
coords.sort(key=lambda k: (k[1],-k[0]), reverse=True)
order = [i[2] for i in coords]
d = {k:v for (v,k) in list(enumerate(order))}

with arcpy.da.UpdateCursor(fc,["OID@",field_to_update]) as cursor:
    for row in cursor:
        row[1] = some_prefix+str(d[row[0]]+1)

If you want to use your X and Y field instead of the feature centroid replace SHAPE@X,SHAPE@Y with the name of them. You also might need to not round the coordinates to 0 decimals like i do since my coordinate system is in meters and you might have decimal degrees. For example change round(i[0],0) to i[0] or change the number of decimals.

enter image description here

| improve this answer | |

It may be less a case of adapting the code and more about running the same code on a table that you have already sorted. This can be achieved very easily with the Sort tool (http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/sort.htm Note: limited functionality without an advanced licence) which will allow you to define your Y-Coordinate attribute as the Sort value- or even based on spatial relationships- then run your code on the output.

It's worth noting that if you simply want to assign Index values to your records based on NW to SE spatial ordering, I found a fantastic answer to do just that on this thread: https://community.esri.com/thread/182343-autonumber-irregular-grid (Darren Wien's answer).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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