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,"
sort_field = [["Land_Owner_ID","ASCENDING"]]

#Sort Land_Owner_ID from low to high

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

         landOwner_ID_Storage += 1

I suggest creating a dictionary of landOwnder_ID's to keep track of what cpid you are on.


landOwnerDict = {}
with arcpy.da.UpdateCursor(out_shp,updateFields) as cursor:
    for row in cursor: # the landownerID has already been used, use the next cpid
        if row[0] in landOwnerDict.keys():
            row[1] = landOwnerDict[row[0]]
            landOwnerDict[row[0]] += 1
        else: #Store a new LandownerID and start counting at 1
            landOwnnerDict.update({row[0]: 1})
            row[1] = 2

If the owner data is all sequential, how about just a set of two counters (one to hold the current owner ID, and one to hold the pivot ID):

owner_counter = 0
cp_counter = 1
with arcpy.da.UpdateCursor(out_shp,updateFields) as cursor:
    for row in cursor:

         # Check if the Owner ID of this row matches the owner counter
         # (which gives the previous owner)
         if row[0] == owner_counter

             # if it does, this owner already has center pivots associated with it
            # and the CP_ID is the last CP_ID plus one
            row[1] = cp_counter + 1

            # and we need to update the cp counter
            cp_counter = cp_counter + 1

    else if row[0] != cp_counter
        # if the Owner ID of this row is not the same as the last row
        # this is a new owner and the cp_counter should be reset to 1
        row[1] = 1
        cp_counter = 1

Not the answer you're looking for? Browse other questions tagged or ask your own question.