1

I have Feature Point DATA. I need to add some sequence number to the attribute of each point, based on their location in the display map. In my attribute it'S ordered in the opposite way.

Direction i want to add

enter image description here

In attribute table its start from there..

I tried to descending the ObjectID first and put this code from ESRI but the result follows ObjectID sequence..

rec=0 
def autoIncrement(): 
 global rec 
 pStart = 1 
 pInterval = 1 
 if (rec == 0): 
 rec = pStart 
 else: 
 rec += pInterval 
 return rec

I tried to search how to do add sequence on descending way. I found some advise from this but still cant do because it's too complicated for my data. My data just contains ObjectID and shape.

So what I want to do is to create sequence number opposite way from ObjectID or create descending sequence number for the simple.

How do I do it?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Oct 27, 2018 at 15:16
6
  • If you were to sort by the objectid column in descending order, would this give you the appropriate sequence number? There is a sort tool, which would change the underlying objectID. Commented Oct 27, 2018 at 15:47
  • 1
    It looks like you have two separate questions. To get better responses and to prevent the question from being put on hold, please remove your second question and start a new one. You can link it back to this one. Commented Oct 27, 2018 at 15:48
  • edited.. thanks.. for your comment above.. i tried to descending order first and apply the formula from EsRi but still the result shows the number based on ObjectID Commented Oct 27, 2018 at 15:52
  • Not at a computer right now to check, but there's a tool to sort which changes the underlying data, not the right click sort in the attribute table. Commented Oct 27, 2018 at 15:55
  • If you have 100 records, 101-ascending value will do. Commented Oct 27, 2018 at 18:50

2 Answers 2

1

You can try sort tool and sort in order from left to right:

Reorders, in ascending or descending order, records in a feature class or table based on one or multiple fields.

Use the spatial_sort_method UL : —Sorting starts at upper left corner. Im not sure you will get the correct order though.

If not you can use arcpy. Execute in the Python window:

import arcpy
sequence_field = 'SomeFieldName' #Change
fc = r'C:\data.gdb\points' #Change
arcpy.AddField_management(in_table=fc, field_name=sequence_field, field_type='SHORT')
all_points = [i for i in arcpy.da.SearchCursor(fc,['OID@','SHAPE@X'])] #list all points as pairs of objectid and x coord
all_points.sort(key=lambda x: x[1]) #sort by x coord
update_dictionary = {i[1]:i[0] for i in enumerate([j[0] for j in all_points])} #Create a dictionary of objectid and sequential number using enumerate function
#Update field with sequence
with arcpy.da.UpdateCursor(fc,['OID@',sequence_field]) as cursor:
 for row in cursor:
 row[1] = update_dictionary[row[0]]
 cursor.updateRow(row)
answered Oct 27, 2018 at 17:02
7
  • Is it the same tool as smiller mention?? From your code, do i just need to change the sequence field and the database? And in 'somefield name' is it refering to the field what i want to order? In this case 'objectID' field.. im sorry for ask to many question about phyton cz im very beginner with it Commented Oct 27, 2018 at 18:03
  • Hi Bera thanks for the help.. your code is working for me.. but i have a little question for you.. when i put your code in my arcpy and change the 'somefieldname' & the file directory its come out with error.. it said: Runtime error Traceback (most recent call last): File "<string>", line 14, in <module> RuntimeError: An invalid SQL statement was used. [SELECT OBJECTID, Order FROM STA3].. but if i just change the file directory it works perfectly.. i checked line 14, and didnt understand what is the wrong with that.. could you help me?? thanks Commented Oct 28, 2018 at 3:03
  • I dont understand you. If it is working there should be nothing wrong with the code. What exactly do you use for fc? Commented Oct 28, 2018 at 6:16
  • So i changed "somefieldname" to "neworder" and i changed the directory to directory of my shp file.. when i ran the process the error like i mention above showed.. Commented Oct 28, 2018 at 6:50
  • fc need to be complete path and name of shapefile, for example r'C:\folder\shapefile.shp' Commented Oct 28, 2018 at 6:59
0

Try the tool mentioned in this article https://support.esri.com/en/technical-article/000012218, the Sort tool in Data Management. You can sort the feature class / shapefile permanently, which changes the underlying attribute table. Note this is different from doing the sort operation in the attribute table itself, as that just changes the view.

It's possible this won't work on the objectid field itself. If that's the case, then I'd start by creating a separate field and populating it with the objectid, then resort by this second field.

answered Oct 27, 2018 at 15:56
2
  • thanks.. i tried that tool but it cant sort based on objectID field Commented Oct 27, 2018 at 16:00
  • Ok, added suggestion based on that. Commented Oct 27, 2018 at 16:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.