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!"
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
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.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!"
inPts = "C:\ArcGIS\Temp\Tester.shp" animalField = "AnimalNum" sortField = "MSTTime" arcpy.AddField_managment(inPnts, "LAST_FLAG", "SHORT") animalDateDict = {} searchRows = arcpy.da.SearchCursor(inPnts, [OID@, animalField, sortField]) for searchRow in searchRows: oidValue, animalValue, sortValue = searchRow if animal in animalDateDict: animalDateDict[animal].append(sortValue, oidValue) else: animalDateDict[animal] = [(sortValue, oidValue)] del searchRow, searchRows lastOidsList = [max(animalDict[animal])[-1] for animal in animalDict] oidFieldName = arcpy.Describe(inPnts).oidFieldName queryExp = oidFieldName + " in (" + ",".join(str(lastOid) for lastOid in lastOidsList) + ")" updateRows = arcpy.da.UpdateRows(inPnts, ["LAST_FLAG"], queryExp) for updateRow in updateRows: updateRow[0] = 1 updateRows.updateRow(updateRow) del updateRow, updateRows
If speed is your concern, are your sort fields indexed? Python may not take full advantage of them, but the Summary Statistics with a Join and Select where < MAX_MSTTime then field calculator then invert selection then field calculator would finish this is under 2 minutes for 20,000 records. If you don't want a permanent output do the Summary Statistics to an in memory table.You are reinventing the wheel from my perspective, since the above workflow has been optimized (at least as of 10.1). It will beat the performance of any sorted Python cursor easily, since those cursor are not optimized at all for using indexes in my experience.
Actually, I put something similar to this in my scripts:debug = "n" if debug == "y":print "Processing ",infc
debug = "n" if debug == "y":print "Processing ",infc
debug = False ... if debug: print "Processing ", infc
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
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
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()
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
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")
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()
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.