I'm working with a dataset of center pivot irrigation sites for my work and I have a unique ID for each land owner (Land_Owner_ID) as well as a number of center pivots associated with each owner (CP_ID). Ignore the certificate number, as it is irrelevant to my issue.
For each unique Land_Owner_ID I need to reset my CP_ID to 1 for the 1st instance of the unique Land_Owner_ID, then add 1 to CP_ID and set that as the next CP_ID for the same instance of Land_Owner_ID.
As a result, in the case of Land_Owner_ID 3 I should have CP_ID's 1, 2, and 3, and then again for Land_Owner_ID 4 I should have CP_ID's 1, 2, and 3.
I am somewhat of a python beginner, but I've been trying to do as much of my work via python scripts/tools as possible to flesh out my knowledge and experience.
Here is what I have come up with so far:
#This script sorts the field LandOwner_ID low to high and then populates the
# CP_ID and CP_LandOwner_ID_Combined fields
import arcpy
import os
from arcpy import env
env.overwriteOutput = True
arcpy.env.workspace = r"D:/Irrigation_Data/WaterUseData.gdb"
CP_shp = "All_CP_With_Masterlist_Version1_Copy"
out_path = arcpy.env.workspace
out_shp = os.path.join(out_path,"
{}".format("All_CP_With_Masterlist_Version1_SortedCopy"))
sort_field = [["Land_Owner_ID","ASCENDING"]]
#Sort Land_Owner_ID from low to high
arcpy.Sort_management(CP_shp,out_shp,sort_field)
landOwner_ID_Field = "Land_Owner_ID"
CP_ID_Field = "CP_ID"
updateFields = (landOwner_ID_Field,CP_ID_Field)
with arcpy.da.UpdateCursor(out_shp,updateFields) as cursor:
for row in cursor:
#Reset the CP_ID for each new landOwner_ID to 1
CP_ID_Count = 1
#Store owner_ID in a variable
landOwner_ID_Storage = 1
landOwner_ID = row[0]
If current landOwnerID equals the ID I am testing for, while that condition is true, set the current CP ID and count up 1, continue to do so until a new landOwner_ID is registered. I'm sure I have some syntax or arrangement of my code out of whack here.
if landOwner_ID == landOwner_ID_Storage:
while landOwner_ID == landOwner_ID_Storage:
row[1] = CP_ID_Count
CP_ID_Count += 1
cursor.updateRow(row)
else:
landOwner_ID_Storage += 1
