Solved! Go to Solution.
Here is the latest version....It is getting the job done but is still slow. I am intrigued by rzufelt's suggestion of using the searchcursor, but if I understand correctly the searchcursor is read only so I am not sure how I can then work with the LAST_PNT field?
I removed any print functions and it is still not much faster. I will try a combining the 2 sort fields into one to see if that speeds it up.lastValue = arcpy.SearchCursor(infc, "", "", "", OID + " D").next().getValue(myField) #Get 1st row in cursor - gets last value based on OID firstValue = arcpy.SearchCursor(infc, "", "", "", OID + " A").next().getValue(myField) #Get last row in cursor - gets first value based on OID
Working Scriptimport arcpy import os import types from arcpy import env #get Parameters from toolbox #-For Use with Script Toolbox #inPts = arcpy.getparameterastext(0) #Animalfield=arcpy.getparameterastext(1) #SortField=arcpy.getparameterastext(2) #-For Troubleshooting inPts = "C:\ArcGIS\Temp\Tester.shp" AnimalField = "AnimalNum" SortField = "MSTTime" #Add Field print "Beginning:" arcpy.DeleteField_management(inPts,"LAST_PNT") arcpy.AddField_management(inPts,"LAST_PNT","SHORT",) ThesortString = AnimalField + " A; " + SortField + " D" # Apply Sort rows = arcpy.UpdateCursor(inPts,"","","",ThesortString) print "Finished Sorting" i=0 lastRow = "Starter" for row in rows: #print i #i=i+1 if row.getValue(AnimalField) == lastRow: #row.setValue("LAST_PNT",0) lastRow=row.getValue(AnimalField) rows.updateRow(row) else: row.setValue("LAST_PNT",1) lastRow=row.getValue(AnimalField) rows.updateRow(row) del row del rows print "Completed!"
f __name__ == '__main__':
inPts = r"testLast.shp"
try:
count = arcpy.GetCount_management(inPts).getOutput(0)
lastRow = int(count) - 1
print lastRow
arcpy.AddField_management(inPts, "last", "SHORT","","", 3)
rows = arcpy.UpdateCursor(inPts)
for row in rows:
print row.getValue("FID")
if row.getValue("FID") == lastRow:
print row.getValue("FID")
print "Found it"
row.last = lastRow ##Put what you like here I like 1's or 0's but used last row num for testing
rows.updateRow(row)
print "done"
del rows
except:
print arcpy.GetMessages()
print traceback.print_exc()
import arcpy
import os
import types
from arcpy import env
#get Parameters from toolbox
#-For Use with Script Toolbox
#inPts = arcpy.getparameterastext(0)
#Animalfield=arcpy.getparameterastext(1)
#SortField=arcpy.getparameterastext(2)
#-For Troubleshooting
inPts = "C:\ArcGIS\Temp\Tester.shp"
AnimalField = "AnimalNum"
SortField = "MSTTime"
#Add Field
print "Beginning:"
arcpy.AddField_management(inPts,"LAST_PNT","TEXT",)
ThesortString = AnimalField + " A; " + SortField + " D"
# Apply Sort
rows = arcpy.UpdateCursor(inPts,"","","",ThesortString)
print "Finished Sorting"
thecount = arcpy.GetCount_management(inPts).getOutput(0)
i=0
lastRow = "Starter"
for row in rows:
print i
i=i+1
#print str(count)
#thecount = thecount - 1
if row.getValue(AnimalField) == lastRow:
row.setValue("LAST_PNT","FALSE")
lastRow=row.getValue(AnimalField)
rows.updateRow(row)
else:
row.setValue("LAST_PNT","TRUE")
lastRow=row.getValue(AnimalField)
rows.updateRow(row)
del row
del rows
lastValue = arcpy.SearchCursor(infc, "", "", "", OID + " D").next().getValue(myField) #Get 1st row in cursor - gets last value based on OID firstValue = arcpy.SearchCursor(infc, "", "", "", OID + " A").next().getValue(myField) #Get last row in cursor - gets first value based on OID
Does anybody have any ideas on how to speed this up?
arcpy.AddField_management(inPts,"LAST_PNT","TEXT",) arcpy.CalculateField_management(inPts,"LAST_PNT", "'TRUE'", "PYTHON")
Does anybody have any ideas on how to speed this up?import arcpy import os import types from arcpy import env #get Parameters from toolbox #-For Use with Script Toolbox #inPts = arcpy.getparameterastext(0) #Animalfield=arcpy.getparameterastext(1) #SortField=arcpy.getparameterastext(2) #-For Troubleshooting inPts = "C:\ArcGIS\Temp\Tester.shp" AnimalField = "AnimalNum" SortField = "MSTTime" #Add Field print "Beginning:" arcpy.AddField_management(inPts,"LAST_PNT","TEXT",) ThesortString = AnimalField + " A; " + SortField + " D" # Apply Sort rows = arcpy.UpdateCursor(inPts,"","","",ThesortString) print "Finished Sorting" thecount = arcpy.GetCount_management(inPts).getOutput(0) i=0 lastRow = "Starter" for row in rows: print i i=i+1 #print str(count) #thecount = thecount - 1 if row.getValue(AnimalField) == lastRow: row.setValue("LAST_PNT","FALSE") lastRow=row.getValue(AnimalField) rows.updateRow(row) else: row.setValue("LAST_PNT","TRUE") lastRow=row.getValue(AnimalField) rows.updateRow(row) del row del rows
debug = "n" if debug == "y":print "Processing ",infc
if now == previous:
row.setValue("LAST_PNT","FALSE")
print row.getvalue("LAST_PNT")
else:
row.setValue("LAST_PNT","TRUE")
print row.getvalue("LAST_PNT")
rows.UpdateRow(row) # note indent fix
row = rows.Next()
Thanks, your script didn't quite work for me but it got me to the point where I could get it working...The script below works for me...but there is now a performance issue. This seems to take much longer than it should to process. It probably takes about 3-4 minutes to process only 1000 records. I have similar functions in Excel that can do this in a few seconds. Given that my actual dataset has about 20,000+ records, I don't know how useful this script will really be. Does anybody have any ideas on how to speed this up?import arcpy import os import types from arcpy import env #get Parameters from toolbox #-For Use with Script Toolbox #inPts = arcpy.getparameterastext(0) #Animalfield=arcpy.getparameterastext(1) #SortField=arcpy.getparameterastext(2) #-For Troubleshooting inPts = "C:\ArcGIS\Temp\Tester.shp" AnimalField = "AnimalNum" SortField = "MSTTime" #Add Field print "Beginning:" arcpy.AddField_management(inPts,"LAST_PNT","TEXT",) ThesortString = AnimalField + " A; " + SortField + " D" # Apply Sort rows = arcpy.UpdateCursor(inPts,"","","",ThesortString) print "Finished Sorting" thecount = arcpy.GetCount_management(inPts).getOutput(0) i=0 lastRow = "Starter" for row in rows: print i i=i+1 #print str(count) #thecount = thecount - 1 if row.getValue(AnimalField) == lastRow: row.setValue("LAST_PNT","FALSE") lastRow=row.getValue(AnimalField) rows.updateRow(row) else: row.setValue("LAST_PNT","TRUE") lastRow=row.getValue(AnimalField) rows.updateRow(row) del row del rows
Actually, I put something similar to this in my scripts:debug = "n" if debug == "y":print "Processing ",infc
debug = False ... if debug: print "Processing ", infc