Select to view content in your preferred language

Sorting before continuing

2218
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
15 Replies
TomKearns
Frequent Contributor
Ok, here is working code to choose the appropriate fid.  Thanks for the help everybody.


            uCursor = arcpy.gp.UpdateCursor (stream_in,"","","", "LINKNO D")
            urow = uCursor.next()
            fidStart = urow.FID
            print "fid start"
            print fidStart
0 Kudos
TomKearns
Frequent Contributor
That still didn't answer the question about sorting before processing though.  Now I can grab the fid of the highest value but I am still interested in sorting the whole table based on the values in the LINKNO column.
0 Kudos
RhettZufelt
MVP Notable Contributor


            uCursor = arcpy.gp.UpdateCursor (stream_in,"","","", "LINKNO D")
            urow = uCursor.next()
            fidStart = urow.FID
            print "fid start"
            print fidStart




Not sure what you mean here.  They way you have it above, the uCursor object IS your entire table sorted by LINKNO descending.  the uCursor.next() just grabs the next (in this case the first) value.

for row in uCursor:
    print row.FID

  Should print all your FID's sorted descending.



R_
0 Kudos
TomKearns
Frequent Contributor
Yes but my origional goal was to sort the whole table based on the LINKNO attribute and then run the rest of the script with the table sorted as such (the script creates a new attribute and fills it according to other variables).  That is why my initial cursor was in an UpdateCursor as opposed to SearchCursor.  What I was finding was that the table was no longer sorted when I proceded with the script. In thinking about it now, it makes sense due to it being only the cursor that is sorted, not the table. 

To restate my problem hopefully to improve clarity:

I am interested in sorting my table by an attribute and then proceding with my script.  I was attempting to do it using an UpdateCursor but found when I called the FIDs that the table was not sorted.  Ideally I could accomplish the sorting without creating a new .shp.
0 Kudos
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.?
0 Kudos
TomKearns
Frequent Contributor
My problem may have been that I was exiting the update cursor and expecting the order to stick. I am not sure how to stay within the cursor, I have bunch of for loops that run after the reordering.

The rest of the code looks at the variable I am sorting by and another and then assigns a name to the line.  It then queries the next row and if it is part of the link it assigns it the same name, all the way down to 0.  Then it starts over at then next link without a name and repeats.  The cause for the question is that the longest line of links needed to be named first, they always have the highest LINKNO but do not necessaraly fall at FID 0. 


LINKNO UPLINK NAME
449         448      a
448         446      a
447         445      b
446         410      a
445         403      b         and so on.

I gave in and just created a new .shp with "arcpy.Sort_management", it adds hastle but once coded it will be fine.
0 Kudos