# Ordering data in sequential order based on coordinates

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.

``````rec=0

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
else:
rec = rec + pInterval
return string_prefix + str(rec)

autoIncrement('AP')
``````

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
• 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),round(i,0),i] for i in arcpy.da.SearchCursor(fc,['SHAPE@X','SHAPE@Y','OID@'])]
coords.sort(key=lambda k: (k,-k), reverse=True)
order = [i 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 = some_prefix+str(d[row]+1)
cursor.updateRow(row)
``````

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)` to `i` or change the number of decimals. 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.