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.
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?
-
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.SMiller– SMiller2018年10月27日 15:47:23 +00:00Commented Oct 27, 2018 at 15:47
-
1It 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.SMiller– SMiller2018年10月27日 15:48:08 +00:00Commented 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 ObjectIDRudini– Rudini2018年10月27日 15:52:00 +00:00Commented 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.SMiller– SMiller2018年10月27日 15:55:20 +00:00Commented Oct 27, 2018 at 15:55
-
If you have 100 records, 101-ascending value will do.FelixIP– FelixIP2018年10月27日 18:50:19 +00:00Commented Oct 27, 2018 at 18:50
2 Answers 2
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)
-
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 itRudini– Rudini2018年10月27日 18:03:24 +00:00Commented 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?? thanksRudini– Rudini2018年10月28日 03:03:30 +00:00Commented 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?Bera– Bera2018年10月28日 06:16:07 +00:00Commented 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..Rudini– Rudini2018年10月28日 06:50:58 +00:00Commented Oct 28, 2018 at 6:50
-
fc need to be complete path and name of shapefile, for example
r'C:\folder\shapefile.shp'
Bera– Bera2018年10月28日 06:59:16 +00:00Commented Oct 28, 2018 at 6:59
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.
Explore related questions
See similar questions with these tags.