Skip to content

Instantly share code, notes, and snippets.

@cindygis
Created July 14, 2015 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cindygis/d4ee18f3030209fd49e0 to your computer and use it in GitHub Desktop.
Save cindygis/d4ee18f3030209fd49e0 to your computer and use it in GitHub Desktop.
Populates the next highest numbers for a feature class based on the previous max
import arcpy
lyr = arcpy.management.MakeFeatureLayer(r"C:\Some\Arb\Folder\work.gdb\ftr_wrp_Pipe")
field = "GIS_ID"
def getWhereQry(exp):
return """ "{0}" IS {1} """.format(field, exp)
# Gets the previous highest GIS ID value. Set n = 0 for new feature class
n = max(int(row[0].split("-")[1]) for row in arcpy.da.SearchCursor(lyr, field, where_clause=getWhereQry("NOT NULL")))
numstring = "00000"
prefix = "WRP"
sep = "-"
# Populate blank GIS IDs with new values
with arcpy.da.UpdateCursor(lyr, field, where_clause=getWhereQry("NULL")) as cursor:
for row in cursor:
n += 1
numstring_length = len(numstring) - len(str(n))
row[0] = prefix + sep + numstring[:numstring_length] + str(n)
cursor.updateRow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment