Select to view content in your preferred language

Sorting before continuing

1521
15
Jump to solution
07-16-2013 06:53 AM
TomKearns
Frequent Contributor
I am interested in sorting via an attribute before running my script.  Currently I am trying to use an update cursor:

arcpy.gp.UpdateCursor (in_dataset,"","","", "attribute D")

It does not appear to work when I use the next search cursor. I sort as above then call the next line, check the fid and it is still 0. I tried to refresh in an attempt to make the sort stick but as of yet it has not worked. 

Do I have to use "Sort_management"?  I would like to complete the operation without creating a new .shp.  It seems that the lock Sort_management puts on the file prevents it from writing over its self.

Thanks!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Notable Contributor
Your sorted updateCursor will allow you to "update" values in a field, and would be based on the "sorted" table.

I.e, if you setValue of a new field = 1000, then when you look at your table, after the update, the "last" record (highest FID) would be updated to 1000 (since you've done a cursor.next(), you have the "first" row (in decending order) selected so would actually be the "last" row of the table).

So, if you create the new attribute field before you establish the updateCursor, you can easily update the record based on the sorted table.  Just do your updates before you exit the sorted updateCursor.

however, you can't make a table sort of a feature class permenant without creating a new dataset.

The "order" of a shapefile is determined by the order that the features are added.  If you are actually after a "sorted" shapefile, not just a sorted table, you would want to use the arcpy.sort function which does create a new feature class.

So, could sort the shapefile into a new fc, delete your existing shapefile, then convert the FC back to shapefile with your original name.  That is, of course, if you really, for some reason need a shapefile, otherwise, you can just use the new FC (since output of Sort doesn't support shapefiles).

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000057000000
http://resources.arcgis.com/en/help/main/10.1/index.html#//001700000057000000

R_

If this doesn't get you going, maybe some input of what the other variables would be, and how you want it "filled" would give me a better idea.  Are these variables coming from other fields, static input, etc.?

View solution in original post

0 Kudos
15 Replies
MathewCoyle
Honored Contributor
Are you trying to use a da cursor?

sorted(arcpy.da.SearchCursor(table, 'atribute'))
0 Kudos
TomKearns
Frequent Contributor
I was not.  When I started the script I was unsure if it would be run on machines with only 10 so I used the old update cursor.
0 Kudos
TomKearns
Frequent Contributor
I see the .da has an editor session available.  If I use this will I not have to create a second .shp?

Something along the lines of:

 with arcpy.da.Editor(workspace) as edit:
        arcpy.Sort_management (in_dataset, out_dataset, sort_field)


If this is the case, how do I close the edit session?
0 Kudos
MathewCoyle
Honored Contributor
Is 'attribute' the name of your field? You should include more of your code to see what you are trying to accomplish.
0 Kudos
TomKearns
Frequent Contributor
The LINKNO attribute counts up to 449 and it is the line with 449 as the attribute that I want the script to read first.  When I call the fid it still reads as fid 0, not 115 as I was shooting for.


            arcpy.gp.UpdateCursor (stream_in,"","","", "LINKNO D")
            count1 = arcpy.gp.GetCount_management(stream_in)
            cursor = arcpy.SearchCursor(stream_in)
            row = cursor.next()
            fid = row.FID
            print "naming fid"
            print fid
0 Kudos
MathewCoyle
Honored Contributor
Are you trying to extract one particular row from your data? Or are you trying to start at an arbitrary row and continue from that?
0 Kudos
TomKearns
Frequent Contributor
I am trying to start at the highest LINKNO and the script will then move through the table row by row. This script will be run on multiple shape files and there is no way to tell ahead of time which fid the highest LINKNO will correspond to.
0 Kudos
RhettZufelt
MVP Notable Contributor
The LINKNO attribute counts up to 449 and it is the line with 449 as the attribute that I want the script to read first.  When I call the fid it still reads as fid 0, not 115 as I was shooting for.


            arcpy.gp.UpdateCursor (stream_in,"","","", "LINKNO D")
            count1 = arcpy.gp.GetCount_management(stream_in)
            cursor = arcpy.SearchCursor(stream_in)
            row = cursor.next()
            fid = row.FID
            print "naming fid"
            print fid


See this post for syntax using the sort in a normal cursor.  Examples for both hard coding the attribute and using a variable for it. http://forums.arcgis.com/threads/88186-To-select-a-record-based-on-the-highest-value-of-a-field

also, I see you are sorting your updateCursor, yet you are grabbing the cursor.next() on the cursor object created by the searchcursor.  I don't see a sort on the searchcursor, so should get the first fid each time.
Perhaps try setting the cursor = to the update cursor and see if it grabs the correct value.

R_
0 Kudos
TomKearns
Frequent Contributor
Thank you, I had just found that same thread a few minutes ago and am trying to grab the FID using it.  There is something wrong with my syntax though as it keeps crashing.  I understand what you were saying about calling the wrong cursor, and will look at that as well.

maxValue = arcpy.gp.SearchCursor(stream_in,"","","", "LINKNO D").next().getValue(FID)
0 Kudos