I have two feature classes that I want to create a table from doing a join and update on. However, I don't want to alter the original feature classes but instead would like to create layer files to perform the join and update processes. I drafted a python script that creates the final output table of these operations just fine but noticed it is also joining the join features (streets) to the original target feature class (GIM) and updating the table. This may cause problems as these target feature classes need to retain their schemas. I thought making layer files and running the geoprocesses on them would prevent this. Here is my code:
import arcpy
# deignate output location and input variables
outPath = raw_input("Enter output workspace gdb: ")
GIM = raw_input("Enter geocoding information manager: ")
streets = raw_input("Enter last year's street file: ")
outName = raw_input("Enter an output name: ")
# initiate geoprocesses
try:
print "create layer files to run geoprocesses and select by location queries on"
arcpy.MakeFeatureLayer_management(GIM, "gimLyr")
arcpy.MakeFeatureLayer_management(streets, "strLyr")
print "made lyr files"
print "Select by location on streets for data reduction"
# Select by location args
distance = 40
unit = str(distance) + " Feet"
arcpy.SelectLayerByLocation_management("strLyr", "WITHIN_A_DISTANCE", "gimLyr", unit)
print "Select by location succeeded"
arcpy.MakeFeatureLayer_management("strLyr", "targetStrLyr")
print "Add fields for street name comparison"
fieldLength = 10
arcpy.AddField_management("gimLyr", "Cust1stStWord","TEXT",field_length=fieldLength)
arcpy.AddField_management("gimLyr", "Street1stStWord","TEXT",field_length=fieldLength)
joinFields = ["LINK_ID","ST_NM_BASE"]
print "Join layers"
arcpy.JoinField_management("gimLyr", "Ref_ID", "targetStrLyr", "LINK_ID", joinFields)
print "Make new layer file from join"
arcpy.MakeFeatureLayer_management("gimLyr","gimLyrJoin")
f1 = "Address"
f2 = "Cust1stStWord"
f3 = "ST_NM_BASE"
f4 = "Street1stStWord"
f5 = "Ref_ID"
v1 = 10000
alphas = ('FIRST','SECOND','THIRD','FOURTH','FIFTH','SIXTH','SEVENTH','EIGHTH','NINTH','TENTH')
numbers = ('1ST','2ND','3RD','4TH','5TH','6TH','7TH','8TH','9TH','10TH')
alpha_num = zip(alphas,numbers)
print "Running cursors"
with arcpy.da.UpdateCursor("gimLyrJoin",[f1, f2, f3, f4]) as cursor:
for row in cursor:
# fix null values
if row[2] == None:
row[2] = " "
cursor.updateRow(row)
continue
elif len(row[0].split(" ")[1]) == 1 and row[0].split(" ")[2] != "ST":
row[1] = row[0].split(" ")[2][0:3]
# ST_NM_BASE seems to not include prefix so okay to 0 index
row[3] = row[2].split(" ")[0][0:3]
# now look for street names that start with SAINT in customer records and change them to ST
elif row[0].split(" ")[1] == "SAINT":
row[1] = "ST "
row[3] = row[2].split(" ")[0][0:3]
#Change remaining records to first three letters of street name
else:
row[1] = row[0].split(" ")[1][0:3]
row[3] = row[2].split(" ")[0][0:3]
cursor.updateRow(row)
# fix street name discrepancies between alpha and numeric entries
with arcpy.da.UpdateCursor("gimLyrJoin",[f1, f2]) as cursor:
for row in cursor:
f1_elements = row[0].split(" ")[0:]
for x in alpha_num:
if x[0] in f1_elements:
row[1] = x[1]
cursor.updateRow(row)
q2 = """{0} <> {1} OR {2} < {3}""".format(f2,f4,f5,v1)
print "Making final table"
arcpy.TableToTable_conversion("gimLyrJoin", outPath, outName, q2)
print "succeeded!"
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
finally:
arcpy.Delete_management("gimLyr")
arcpy.Delete_management("strLyr")
arcpy.Delete_management("targetStrLyr")
arcpy.Delete_management("gimLyrJoin")
Is there something I'm missing that would cause both the layer file and the original feature class to be updated?
in_memory
is nice for this) and do your work on that. – Paul Jan 13 '17 at 20:04