Query for orginal numbers

1648
26
09-23-2019 03:48 PM
CCWeedcontrol
Occasional Contributor III

I am trying to query a filed to find original parcels, i would like to create a filled and mark the ones that are original and don't have not split. Currently the field is a string with a length of 11. Some parcels have 5, some 8 and some 10 - not including the alph letter. Hopefully i have explained good what I am needing, i am just not sure how to do this. At first i thought of doing unique numbers but some how it would need to know if the first 5 digits are used, there are some duplicate numbers as well. I am kind of lost on how to proceed, if anyone has done something similar could share some code please?

the attributes are as follows for example;

R00062 (Original parcel)--> has R00062010 (Split parcel, next one below) - this is not one i need marked.

R00062010 - Not Original parcel

R32073114 - Not Original parcel 

R32078 - Original parcel because there is no other parcel number associated with it, need this one mark in the new filed.

R32079 - Original parcel because there is no other parcel number associated with it,, need this one mark in the new filed.

R32081 - Original parcel because there is no other parcel number associated with it,, need this one mark in the new filed.

R32082012 - Not Original parcel 

R32083012 - Not Original parcel 

0 Kudos
26 Replies
CCWeedcontrol
Occasional Contributor III

I am not see the edit.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Line #06, I added the Counter function call

0 Kudos
CCWeedcontrol
Occasional Contributor III

Joshua i really appreciate the code you posted.

I am not sure if worked because i am having a hard time getting the a filed marked if it is an original but here are some things i noticed from the print.

For example the code printed R37422 and when i cross check it with the parcel's there is no R37422, there is in fact an R37422010. So i am not sure as to why it indicated that there was R37422 as an original when there is no R37422 in the parcel's. The same thing for R39509, there is no R39509 but there is a R39509500.

It appears as there is complete set of duplicate parcel numbers.

tbl = 'C:/Temp/ParcelNumberSample2.dbf'

parcel_count = Counter(
    (pid[:6] for pid, in arcpy.da.SearchCursor(tbl,"PARCEL_NO"))
)

for pid,cnt in parcel_count.items():
    if cnt == 1:
        print(pid)

with arcpy.da.UpdateCursor(tbl,["PARCEL_NO","Org_Par"]) as cursor:
    for row in cursor:
        if cnt == 1:
            row[1] = "Orig"
            cursor.updateRow(row)
    else:
        pass
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

In the case of duplicate numbers, assuming you are referring to duplicate "original" numbers, how do you decide which record to mark and which one not to mark?

0 Kudos
CCWeedcontrol
Occasional Contributor III

Your assumption is correct, i haven't thought about marking duplicate original numbers. If there is a duplicate but no child parcel numbers then I  would like this one marked as original parcel.

0 Kudos
CCWeedcontrol
Occasional Contributor III

I have attached some sample data.

0 Kudos
JoeBorgione
MVP Emeritus

I think Randy Burton‌'s comment is putting you in the right direction:  If you parse out the original parcel from those that have been split, you can get a list of parent parcels.  Use that list to plow through the entire data set.  When the id in the list is found in the data set, keep going; those parcel ids that are not in the list are the ones you want to flag as original...

That should just about do it....
0 Kudos
RandyBurton
MVP Alum

Regarding your question previously:

So i am not sure as to why it indicated that there was R37422 as an original when there is no R37422 in the parcel's. The same thing for R39509, there is no R39509 but there is a R39509500.

From your example file, you mentioned that a child would be an original if the first 6 characters had not been used in another parcel.  Joshua counts what would be parent codes. R39509500 would be considered an original as there are no other parcels starting with the same six characters.

For your project, I think it would be helpful to pair an object id with the parcel id.  Here's my test code using your example file.  Perhaps it will be helpful in this discussion.

parcels = [ [1, 'R29681011'], [2, 'R29681113'], [3, 'R29681114'], [4, 'R29681115'], [5, 'R29681116'],
    [6, 'R29681117'], [7, 'R29681118'], [8, 'R29681119'], [9, 'R29681120'], [10, 'R29681121'],
    [11, 'R29681122'], [12, 'R29681122B'], [13, 'R29681122C'], [14, 'R29681122D'], [15, 'R29682011'],
    [16, 'R29682012'], [17, 'R29682013'], [18, 'R29688011'], [19, 'R29825'], [20, 'R29825010A'],
    [21, 'R29825010B'], [22, 'R29826'], [23, 'R29826011'], [24, 'R29826011A'], [25, 'R29826011B'],
    [26, 'R29826011C'], [27, 'R29826011D'], [28, 'R29826012'], [29, 'R29826012A'], [30, 'R29826013'],
    [31, 'R29826014'], [32, 'R29826015'], [33, 'R29826016'], [34, 'R29833'], [35, 'R29834'],
    [36, 'R29834010'], [37, 'R29835'], [38, 'R29836'], [39, 'R29836010'], [40, 'R29842010'],
    [41, 'R29844'], [42, 'R29846'], [43, 'R29850'], [44, 'R29850010'], [45, 'R29852'],
    [46, 'R29852010'],
    ]

parentFound = {} # empty dictionary
childFound = {}

# order by objectid
for p in parcels:
    parent = p[1][:6]
    # print parent
    if len(p[1]) > len(parent):
        if parent in childFound.keys():
            childFound[parent] = (p[0], 1 + childFound[parent][1] )# this will be last child found (by objectid)
        else:
            childFound[parent] = (p[0], 0)
    else:
        parentFound[parent] = p[0] # this will be last parent found (by objectid)

# print parentFound
# print childFound

for k in parentFound.keys():
    if k not in childFound.keys():
        print k, parentFound[k], "parent is orig"  # this is the parcelid and objectid of parent parcels, add to a list

for k in childFound.keys():
    if k not in parentFound.keys():
        if childFound[k][1] < 1:
            print k, childFound[k][0], "child is orig" # this would be the parent parcelid that was NOT found, add to the list also

# output:
R29835 37 parent is orig
R29833 34 parent is orig
R29844 41 parent is orig
R29846 42 parent is orig
R29842 40 child is orig
R29688 18 child is orig
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The idea would be to loop through your parcel ID's to locate the "originals".  The object id of the originals would be added to a list which would be used with an update cursor to set the original flag field.

RandyBurton
MVP Alum

I was wondering, what would be the "original" if a parent parcel id such as "R29835" appeared more than once in your parcel file?  Would you want to use the first or last occurrence for the original?

0 Kudos
CCWeedcontrol
Occasional Contributor III

I am confused as to how you are got the parcel numbers into the list, i am trying this based on what you posted but get parcels = [row[2] for row in arcpy.da.SearchCursor(fc, field1)] IndexError: tuple index out of range. I thought about duplicates since it doesn't matter which one i can first occurrence. There is two fields before the PARCEL_NO field.

fc = 'C:/Temp/ParcelNumberSample2.dbf'
field1 = "PARCEL_NO" 

parcels = [row[2] for row in arcpy.da.SearchCursor(fc, field1)]


parentFound = {} # empty dictionary
childFound = {}

# order by objectid
for p in parcels:
    parent = p[1][:6]
    # print parent
    if len(p[1]) > len(parent):
        if parent in childFound.keys():
            childFound[parent] = (p[0], 1 + childFound[parent][1] )# this will be last child found (by objectid)
        else:
            childFound[parent] = (p[0], 0)
    else:
        parentFound[parent] = p[0] # this will be last parent found (by objectid)

# print parentFound
# print childFound

for k in parentFound.keys():
    if k not in childFound.keys():
        print (k, parentFound, "parent is orig")  # this is the parcelid and objectid of parent parcels, add to a list

for k in childFound.keys():
    if k not in parentFound.keys():
        if childFound[1] < 1:
            print (k, childFound[0], "child is orig") # this would be the parent parcelid that was NOT found, add to the list also

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos