import arcpy, sys, re, os from arcpy import env import string # Assign environment workspace env.workspace = r'C:\Users\karyn\Documents\My Documents\Business\GIS\ArcGIS\Learnings\Python\ivgn_loc_new_ID\127613066.gdb' #Create filter for selecting only features where there is a location code filter = "\"Location_Code\" <> ' '" #Define sample locations feature layer which require their IDs to be populated selected_feat = r'C:\Users\karyn\Documents\My Documents\Business\GIS\ArcGIS\Learnings\Python\ivgn_loc_new_ID\ivgn_locn_pt selection.lyr' #Define sample location featureclass fc = r'C:\Users\karyn\Documents\My Documents\Business\GIS\ArcGIS\Learnings\Python\ivgn_loc_new_ID\127613066.gdb\ivgn_locn_pt' #Define cursors selrows = arcpy.SearchCursor(selected_feat) orirows = arcpy.UpdateCursor(fc, filter) selrows2 = arcpy.UpdateCursor(selected_feat) print "variables assigned" #Get the gridcode for the sample locations that need updated - this needs updating to access this through a spatial join for selrow in selrows: fcgridcode = selrow.Grid_Code print "grid code assigned" + fcgridcode # iterate through fc to find where the grid code of the new sample locations equals the grid code of the existing sample locations. Then # work out location code with the highest sequential number and asign the new location code to be the highest ID + 1 for orirow in orirows: origlocation_code = orirow.Location_Code #assign first 2 letters of location code to the varible (which is the grid code) origgridcode = origlocation_code[:2] print "orig" + origgridcode print "fc" + fcgridcode if origgridcode == fcgridcode: # Iterate through rows and get highest ID values print "gridcode match " + origgridcode + fcgridcode for row1 in orirows: high_id = 0 high_idfull = row1.Location_Code high_id1 = int(high_idfull[:3]) if high_id < high_id1: high_id = high_id1 print high_id # Iterate through rows and update values for selrow2 in selrows2: i = high_id1 i += 1 selrow2.Location_Code = fcgridcode + "_" + str(high_id1) selrows2.updateRow(selrow2) print high_id del selrow, selrows, orirow, orirows, selrows2 print "script complete"
bump