Reputation: 143
I need a line of code that will issue new sequential id's based on the order I've prescribed to my data. Essentially, after having sorted my data to my liking, the original Object_Id's are no longer in a meaningful order to me. In my table I created a new field (short integer). In the pre logic script box of the field calculator I attempted the following:
counter = 0
def uniqueID():
global counter
counter += 1
return counter
But it didn't seem to work.. A little sample of what my table looks like is as follows: (for visualization purposes)
Object_ID / MMSI / TIME / VESSEL / NEW_ID
1210 4444 01/10/10 01 NuLL
1801 4444 02/10/10 01 NuLL
303 4444 03/10/10 01 NuLL
2839 4444 03/10/10 01 NuLL
32100 5555 01/10/10 03 NuLL
34 5555 02/10/10 03 NuLL
In essence I need those NuLL's to now be 1, 2, 3, 4... etc etc. as my data is now ordered to my liking (first by MMSI number/within MMSI by date)
So, it would look as follows after: (notice the New_ID field is now populated)
Object_ID / MMSI / TIME / VESSEL / NEW_ID
1210 4444 01/10/10 01 1
1801 4444 02/10/10 01 2
303 4444 03/10/10 01 3
2839 4444 03/10/10 01 4
32100 5555 01/10/10 03 5
34 5555 02/10/10 03 6
how would I accomplish this? thanks
Upvotes: 0
Views: 20181
Reputation: 411
This fast solution simply uses Excel to create the sequential numbering on the sorted data, then joins the resulting table back in using ArcGIS:
Export your attribute table as a "Text File", being sure to add the .csv extension to the file name.
Open the .csv file in MS Excel.
Sort as desired.
In a new column in Excel, in row one type the number 1.
In row two, type the number 2.
Select both rows in that column and double-click the little box at bottom right of the selection to cause the numbering to continue sequentially down the column to the end of your populated rows.
Save the .csv.
Go back to ArcGIS and use "Add Data" to import the .csv you just created.
Right-click on the feature, select "Joins and Relates" -> "Join...".
Choose "Join attributes from a table".
Select the appropriate fields and the correct .csv file (the one you just imported)
Click "OK".
Finally, export the feature to a new feature to make the join permanent.
Upvotes: -2
Reputation: 1
I tried this,
I created a new field short integer Sort the data into my desired arrangement Export dbf as excel data, notice that it will export the way it was arranged in attribute table Fill out the numbering in excel (you will not be able to save it since its a dbf file) Copy the column of the sequence field in excel Go back to your attribute table, then paste the numbering there.
It worked since there's no python code available online
Upvotes: 0
Reputation: 1
I have tried the code (see below), but simply adds 1 to the FID value, not a serial sequence in the sorted order...
rows = arcpy.UpdateCursor("your attribute table")
counter = 1
for row in rows:
row.NEW_ID = counter
rows.updateRow(row)
counter += 1
Upvotes: 0
Reputation: 587
There's a field calculator sample in the ArcGIS Help that does exactly what you want.
http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004s000000
(If that link doesn't work for you just search for "calculate field examples" in the ArcGIS help).
Scroll down to "Accumulative and sequential calculations".
Upvotes: 2
Reputation: 1
you can use the updatecursor from the arcpy library
rows = arcpy.UpdateCursor("your attriubute table")
counter = 1
for row in rows:
row.NEW_ID = counter
rows.updateRow(row)
counter += 1
please make sure the correct data type
Upvotes: 0