mcdka004

Create a unique ID for new features based on a gridcode and sequential number

Discussion created by mcdka004 on Sep 16, 2012
Latest reply on Apr 30, 2018 by mtorres_training
Hi,

I am very new to Python (this is in fact my first attempt at scripting) so I am hoping someone could please give me some guidance.  Any help would be greatly appreciated!:confused:
Thanks very much

I would like to be able to autopopulate the unique ID of a sample locations featureclass.  The unique ID is based on the grid square that each sample location intesects (grid squares are saved in a separate grid featureclass) and a sequential number. eg sample locations in grid square 'A1' will be assigned a unique ID of A1_1, A1_2, A1_3 and so on.  The script will be run whenever new features are added and require the next unique sequential ID's to be calculated.

I have had an attempt but I am not sure I have the logic correct (and definitely not all of the coding) and may need to start from scratch.

Bits of this script are from a previous forum which i have adapted.  The script is not updating the [Location_Code] which is the unique ID field at the moment. If I unindent the last 'for statement' at the bottom it does update the [Location_Code] field but only with the same value for each sample location so it seems like the indenting was incorrect.  I have also not incorporated a spatial join yet and am selecting based on a pre calculated [Grid_Code] field but ideally it would be nice to have this in the same script.  The print statements are merely for me to work out what is going on.

Thanks again,
Karyn

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"

Outcomes