Select to view content in your preferred language

Create sequential numbers in a field in ArcMap using Python in the Field Calculator based on longitude

3445
3
07-21-2022 02:46 PM
Sean_Wray
Frequent Contributor

I'd like to calculate an ID field (or some other numeric field) with consecutive numbers starting with 1. The order of this field will be based on the longitude field since I want to number my points from South to North.

I found this https://support.esri.com/en/technical-article/000011137 but it is based on the record number and not a field in the data. I have  put a portion of my table below.

Is this possible? This has stumped me for years!

Sean_Wray_0-1658439899619.png

 

Tags (2)
3 Replies
DavidHoy
Esri Contributor

I dont have the code, but using python, you could create a list of all objectid,y values,

sort the list ascending (northern hemisphere) and then step through the list and update the corresponding feature's "ID" field

0 Kudos
AndyAnderson
Frequent Contributor

I assume you mean latitude rather than longitude if you want to order it from south to north?

If you don’t mind creating a new dataset, you can use the Sort tool:

https://pro.arcgis.com/en/pro-app/2.9/tool-reference/data-management/sort.htm

It will generate a new ObjectID in order of latitude (or any other existing field). Works both as a tool and in a script.

If you don’t want to generate a new dataset and your data is stored in a geodatabase, then you can create an UpdateCursor with an SQL ORDER BY … clause, and write an ordered index into a new field. This won’t work with a shapefile, though:

with arcpy.da.SearchCursor(data, "NewIndex", sql_clause=(None, "ORDER BY Latitude")) as cursor:

    index = 1    
    for row in cursor:
        row[0] = index
        cursor.updateRow(row)
        index += 1

https://pro.arcgis.com/en/pro-app/2.9/arcpy/data-access/updatecursor-class.htm

— Andy

VinceE
by
Frequent Contributor

Just a note, you are using arcpy.da.SearchCursor in your code here, when I think you intended to use an UpdateCursor.

0 Kudos