|
POST
|
Joshua Bixby, I always learn something from your code. Thanks.
... View more
09-26-2019
11:14 AM
|
2
|
1
|
1331
|
|
POST
|
I like Joshua Bixby 's code as it is simple. When you are in the UpdateCursor, just use the first 6 characters of the parcel id to check if it is in the original list, then set the flag. Here is that approach for you to test. import arcpy
from collections import Counter
tbl = r'C:/Temp/ParcelNumberSample2.dbf'
# read parcel id into counter
parcel_count = Counter(
(pid[:6] for pid, in arcpy.da.SearchCursor(tbl,"PARCEL_NO"))
)
original = [] # an empty list for original parcels
for pid,cnt in parcel_count.items():
if cnt == 1: # origional parcels will only have a count of 1
original.append(pid)
# print original
with arcpy.da.UpdateCursor(tbl,["PARCEL_NO","Org_Par"]) as cursor:
for row in cursor:
if row[0][:6] in original: # trim row[0] to 6 characters and check if in original list
# print row[0]
row[1] = 'Orig'
# cursor.updateRow(row) # you can just update the flagged records by uncommenting this line and deleting the remaining lines
else: # or add an else to set non-original fields to a value
row[1] = 'N'
cursor.updateRow(row) If you find that you do need the object id to do some other things, here's and updated version of my code that may give you some ideas. I used where OBJECTID IN ( nnn, nnn) in my SQL; you may need to make adjustments for your version. I was also testing with a file geodatabase where you might be testing with a shapefile. import arcpy
tbl = r'C:/Temp/ParcelNumberSample2.dbf' # use your table name
# read oid and parcel_id into dictionary
parcels = [[r[0],r[1]] for r in arcpy.da.SearchCursor(tbl, ['PARCEL_NO', 'OBJECTID'])] # use your field names
# print parcels
# empty dictionaries for parents and children
parentFound = {} # format: { parent parcel id : object id }
childFound = {} # format: { parent parcel id : ( object id, count of items) }
for p in parcels:
parent = p[0][:6]
# print parent
if len(p[0]) > len(parent):
if parent in childFound.keys():
childFound[parent] = (p[1], 1 + childFound[parent][1] )# this will be last child found (by objectid)
else:
childFound[parent] = (p[1], 0)
else:
parentFound[parent] = p[1] # this will be last parent found (by objectid)
# print parentFound
# print childFound
objID = [] # empty list for object ids
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
objID.append(parentFound )
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
objID.append(childFound [0])
# print sorted(objID)
where = "OBJECTID IN ({})".format(str(sorted(objID)).strip('[]')) # substitute name of object id field
with arcpy.da.UpdateCursor(tbl,["OBJECTID","Org_Par"], where_clause=where) as cursor:
for row in cursor: # this will just update the rows where an original parcel id was found
print row
row[1] = 'Orig'
cursor.updateRow(row)
... View more
09-25-2019
11:35 PM
|
3
|
1
|
1331
|
|
POST
|
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?
... View more
09-25-2019
10:03 AM
|
0
|
0
|
2231
|
|
POST
|
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.
... View more
09-25-2019
09:35 AM
|
2
|
3
|
2231
|
|
POST
|
You might try: d = {'16304760420000': (1536639.422987029, 7422834.91819194),
'16304760430000': (1536594.1241931021, 7422763.591562942),
'16304760440000': (1536511.8395807743, 7422768.880266264),
'16304760450000': (1536457.3127869368, 7422770.376654357)}
for k in d.keys(): # something like this for lines 7-8 in your code
print k, d [0], d [1] # parcel_id, X, Y
... View more
09-24-2019
11:56 AM
|
1
|
3
|
5127
|
|
POST
|
Is the original parcel always 6 characters? If so, would slicing work? >>> parcel = 'R00062010'
>>> original = parcel[:6]
>>> original
'R00062'
>>> subparcel = parcel[6:]
>>> subparcel
'010'
>>> len(subparcel)
3
... View more
09-23-2019
04:27 PM
|
0
|
9
|
2821
|
|
POST
|
Without seeing the actual query you are using, I assume you are specifying a specific object id number in your where clause ( OBJECTID = 123 ) which would only return one record. You can remove any reference to the object id in your where clause to retrieve all records; or use greater than or less than to get a group of rows ( OBJECTID > 0 ).
... View more
09-20-2019
02:42 PM
|
1
|
0
|
5425
|
|
POST
|
I was having success with the following: import arcpy
outFeature = 'D:/GIS Folder/HighwayDistricts.gdb/TEST'
features = [
'C:/Temp/HighwayDistricts.gdb/HWY1',
'C:/Temp/HighwayDistricts.gdb/HWY2',
'C:/Temp/HighwayDistricts.gdb/HWY3',
'C:/Temp/HighwayDistricts.gdb/HWY4',
'C:/Temp/HighwayDistricts.gdb/HWY5'
]
fields = [
['STREET "STREET" true true false 50 Text 0 0,First,#,{0};', '{0},STREET,0,11'],
['ROAD_TYPE "ROAD_TYPE" true true false 70 Text 0 0,First,#,{0};', '{0},ROAD_TYPE,0,11'],
['RD_Owner "RD_Owner" true true false 255 Text 0 0 ,Join,#,{0}', '{0}, RD_Owner,-1,-1']
]
fmap = ''
for fld in fields:
fm = []
for fc in features:
fm.append(fld[1].format(fc))
fmap = fmap + fld[0].format(','.join(fm))
print fmap
print
print ';'.join(features)
arcpy.Merge_management(
inputs= ';'.join(features), # semicolon delimited
output= outFeature,
field_mappings= fmap
) The print statements output the following: STREET "STREET" true true false 50 Text 0 0,First,#,C:/Temp/HighwayDistricts.gdb/HWY1,STREET,0,11,C:/Temp/HighwayDistricts.gdb/HWY2,STREET,0,11,C:/Temp/HighwayDistricts.gdb/HWY3,STREET,0,11,C:/Temp/HighwayDistricts.gdb/HWY4,STREET,0,11,C:/Temp/HighwayDistricts.gdb/HWY5,STREET,0,11;ROAD_TYPE "ROAD_TYPE" true true false 70 Text 0 0,First,#,C:/Temp/HighwayDistricts.gdb/HWY1,ROAD_TYPE,0,11,C:/Temp/HighwayDistricts.gdb/HWY2,ROAD_TYPE,0,11,C:/Temp/HighwayDistricts.gdb/HWY3,ROAD_TYPE,0,11,C:/Temp/HighwayDistricts.gdb/HWY4,ROAD_TYPE,0,11,C:/Temp/HighwayDistricts.gdb/HWY5,ROAD_TYPE,0,11;RD_Owner "RD_Owner" true true false 255 Text 0 0 ,Join,#,C:/Temp/HighwayDistricts.gdb/HWY1,RD_Owner,-1,-1,C:/Temp/HighwayDistricts.gdb/HWY2,RD_Owner,-1,-1,C:/Temp/HighwayDistricts.gdb/HWY3,RD_Owner,-1,-1,C:/Temp/HighwayDistricts.gdb/HWY4,RD_Owner,-1,-1,C:/Temp/HighwayDistricts.gdb/HWY5,RD_Owner,-1,-1
C:/Temp/HighwayDistricts.gdb/HWY1;C:/Temp/HighwayDistricts.gdb/HWY2;C:/Temp/HighwayDistricts.gdb/HWY3;C:/Temp/HighwayDistricts.gdb/HWY4;C:/Temp/HighwayDistricts.gdb/HWY5 My test values were slightly different than yours as I am not completely sure of your set-up. I ran this outside ArcMap, hence the path to the features. If you have a map with the layers added, you could substitute the layer names for the layer path in ArcMap's Python window. I think the '0 ,11' and '-1,-1' in your field map indicate the start and end positions of the data being transferred. 0,11 would be the first 12 characters of the field's data, and -1,-1 would take the whole field. In my experience, if the field data being transferred is bigger than the target field size, an error will occur. I have not tested for spaces in feature names/paths, nor have I tested where renaming the field is attempted with field mapping. Hope this helps.
... View more
09-05-2019
08:36 PM
|
0
|
3
|
4543
|
|
POST
|
Field mappings have always been a bit confusing for me. To see how various tools use mappings, I will put some test features into ArcMap and then run a tool like Merge and then copy the Python snippet of the tool's last run under Geoprocessing > Results. This will show all the parameters used for a specific running of the tool. Then you can see how the mappings were formatted and create a script that will format your variables in a similar fashion. Using that approach, it looks like the field mappings would be something like: STREET "STREET" true true false 50 Text 0 0,First,#,Feature1,STREET,0,11,Feature2,STREET,0,11,Feature3,STREET,0,11; where Feature1, Feature2 and Feature3 would be the 3 layers that are being merged into the STREET field of the new, merged feature. (I am assuming the 0, 11 are the correct values used by the features you are merging.)
... View more
09-05-2019
02:56 PM
|
0
|
0
|
4543
|
|
POST
|
A trick that I use is to put an apostrophe in Excel's first row cell containing the zip code that you want to be interpreted as text, ie. '12345.
... View more
09-05-2019
09:46 AM
|
1
|
3
|
3032
|
|
POST
|
I haven't worked with Portal, so I'm not sure how it would work as a web tool. I suggest that you post a new question and include a link to this one for reference. Can you provide a bit more about what you want your tool to do?
... View more
09-04-2019
09:52 AM
|
0
|
5
|
2730
|
|
POST
|
I think the "true true false" is using the right case based on my experiences with field mapping. I think the field you are mapping needs the feature name included as in: STREET "STREET" true true false 50 Text 0 0,First,#,{0},{0}.STREET,0,11; {0}.STREET
... View more
09-04-2019
09:37 AM
|
0
|
0
|
4543
|
|
POST
|
To remove case sensitivity when comparing text the .upper() method is often used; it converts all letters in a string to upper case. If both strings are using the same case, it is easier to check for a case insensitive match. To illustrate, note the capital F in field1's value: >>> field1 = "myField"
>>> field2 = "myfield"
>>> if field1 == field2:
... print "match"
...
>>> if field1.upper() == field2.upper():
... print "MATCH"
...
MATCH
>>> field1.upper()
'MYFIELD'
>>> field2.upper()
'MYFIELD'
>>>
... View more
09-03-2019
08:59 PM
|
0
|
0
|
1833
|
|
POST
|
In line 7, you are using 'f.name.upper()' to force field names to upper case for a comparison. I don't see where you are forcing field names to upper case when you append them to 'keepList'. So, perhaps change line 4 to: keepList.append(item.upper())
... View more
09-03-2019
02:17 PM
|
1
|
2
|
1833
|
|
POST
|
There is also the .required property. I haven't tested it, but it probably returns a True/False. This would be a more generic test. if feild.required is false: # perhaps: == 'False'
... View more
08-29-2019
11:54 AM
|
1
|
1
|
4739
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 10-27-2016 02:23 PM | |
| 1 | 09-09-2017 08:27 PM | |
| 2 | 08-20-2020 06:15 PM | |
| 1 | 10-21-2021 09:15 PM | |
| 1 | 07-19-2018 12:33 PM |
| Online Status |
Offline
|
| Date Last Visited |
02-12-2026
07:13 PM
|