Hello Everyone,I am relatively new to python and need some help. I have a electrical dataset consisting of lines and poles. The purpose of my script is to spatially join poles that are snapped to secondary lines. Then I need to select the poles that do not contain at least one of the required assemblies, such as J, K, and UM5. The data contained in the pole's table in the Assemblies field might contain one or more assemblies separated by commas (e.g. 'C1', 'E1-1', 'F4-1', 'K11-8', 'M2-1')# import modules
import arcpy, os, sys, traceback, datetime, time
CURDATE = time.strftime("%Y%m%d_%H%M%S")#datetime.datetime.now() #datetime.date.today()
# identify workspace
arcpy.env.workspace = "E:\\Geog4500\\Project\\Data\\Saulsbury_214-Full.mdb"
arcpy.env.overwriteOutput = True
# define variables and output path
gdbFeatureDataset = "\\DRG"
outPath = "E:\\Geog4500\\Project\\Saulsbury_214-Full.mdb\\"
outFCjoined = outPath + os.sep + gdbFeatureDataset + os.sep + "PoleLines"
outFClines = "Poles"
# define variables
fcPole = gdbFeatureDataset + os.sep + "Pole"
poleLayer = 'Pole_Layer'
fcLine = gdbFeatureDataset + os.sep + "LineFeatures"
lineLayer = 'Line_Layer'
search_Distance = '0 FEET'
# identify log file
logpath = "E:\\Geog4500\\Project\\Data\\logfiles"
logfile1 = logpath + "log_" + str(CURDATE) + ".txt"
print("Logfile Name: " + logfile1)
if arcpy.Exists(logfile1):
arcpy.Delete_management(logfile1)
# Begin logging
log1 = open(logfile1, 'w')
log1.write("Spatially Join Poles to Line Sections & Identify Pole Assemblies\n")
# Start Geoprocessing
try:
#Create Pole Feature Layer
arcpy.MakeFeatureLayer_management(fcPole, poleLayer)
#Display feature count of point feature layer
countSelectedPoles = arcpy.GetCount_management(poleLayer)
print 'The number of selected Poles is ' + str(countSelectedPoles)
log1.write('The number of selected Poles is ' + str(countSelectedPoles) + "\n")
#Create Line Feature Layer
querySecondaryLines = "[Subtype] in (2,3)"
arcpy.MakeFeatureLayer_management(fcLine, lineLayer, querySecondaryLines)
#Display feature count of line feature layer
countTotalLines = arcpy.GetCount_management(fcLine)
print 'The total number of features in the LineFeatures feature class is ' + str(countTotalLines)
log1.write('The total number of features in the LineFeatures feature class is ' + str(countTotalLines) + "\n")
countSelectedLines = arcpy.GetCount_management(lineLayer)
print 'The total number of selected Secondary LineFeatures is ' + str(countSelectedLines)
log1.write('The total number of selected Secondary LineFeatures is ' + str(countSelectedLines) + "\n")
#Select Secondary Poles (poleLayer) that INTERSECT LineFeatures
arcpy.SelectLayerByLocation_management(poleLayer, "WITHIN_A_DISTANCE", lineLayer, "", "NEW_SELECTION")
#Display the number selected Secondary Poles (poleLayer) that INTERSECT LineFeatures (lineLayer)
selectedSecondaryPoles = arcpy.GetCount_management(poleLayer)
print 'The number of poles selected is ' + str(selectedSecondaryPoles)
log1.write('The number of poles selected is ' + str(selectedSecondaryPoles) + "\n")
# Create a copy of the Selected Poles to a feature class
try:
arcpy.CopyFeatures_management(poleLayer, gdbFeatureDataset + os.sep + "PoleLines_" + str(CURDATE))
print("Copied Selected Poles to a feature class.")
log1.write("Copied Selected Poles to a feature class." + "\n")
except:
print("Failed to copy selected poles to a feature class for later viewing.")
log1.write("Failed to copy selected poles to a feature class for later viewing." + "\n")
#if countSpatialJoin > 0:
if selectedSecondaryPoles > 0:
##Identify Selected Poles without "J", "K", and "UM5" values in ASSEMBLIES field
# Loop through each Selected Pole to query and identify Assumblies
print"Loop through each Selected Pole to query and identify Assumblies"
log1.write("Loop through each Selected Pole to query and identify Assumblies" + "\n")
#Create Update Cursor to update ISSUE field if Assemblies are missing
# Python Set (tuple) of Missing Assembly Values
requiredAssemblies = ("J", "K", "U")
# Update Cursor Based on Spatial Join Output
try:
uRows = arcpy.UpdateCursor(poleLayer, "", "", "", "")
update = True
print("Update Cursor Created.")
log1.write("Update Cursor Created." + "\n")
except:
update = False
print("Update Cursor Failed to be created.")
log1.write("Update Cursor Failed to be created." + "\n")
# Start looping of rows to update
if update:
for row in uRows:
# Create a value to indicate when AT LEAST ONE assembly has been found
assemblyIssue = True
assembliesFound = True
# select Assemblies and split the values to create a list
if row.ASSEMBLIES is not None:
print("Current row's Assemblies: ")
assemblies = row.ASSEMBLIES.split(",")
print(assemblies)
# Compare available Assemblies to ("J", "K", "U")
for assembly in assemblies:
if assembly in requiredAssemblies:
assemblyIssue = False
break
else:
assembliesFound = False
if assembliesFound == False:
if row.Issue == "-":
row.Issue = "No Assemblies Assigned to Pole"
elif row.Issue == "No Assemblies Assigned to Pole":
pass
else:
row.Issue = "No Assemblies Assigned to Pole; " + row.Issue
uRows.updateRow(row)
if assemblyIssue:
if row.Issue == "-":
row.Issue = "'J', 'K', or 'UM5' were not found"
elif row.Issue == "'J', 'K', or 'UM5' were not found":
pass
else:
row.Issue = "'J', 'K', or 'UM5' were not found; " + row.Issue
uRows.updateRow(row)
#Write information to log file
print("ObjectID: " + str(row.OBJECTID) + " does not have a value matching 'J', 'K', or 'UM5'.")
log1.write("ObjectID: " + str(row.OBJECTID) + " does not have a value matching 'J', 'K', or 'UM5'." + "\n")
else:
print("No Poles were spatially joined to the LineFeatures.")
log1.write("No Poles were spatially joined to the LineFeatures." + "\n")
print("Script Completed.")
log1.write("Script Completed." + "\n")
log1.close()
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + str(sys.exc_type) + ": " + str(sys.exc_value)+ "\n"
msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"
print("Scripted Ended with Errors")
log1.write("Scripted Ended with Errors" + "\n")
print msgs
arcpy.AddError(msgs)
log1.write(msgs)
print pymsg
arcpy.AddError(pymsg)
log1.write(pymsg + "\n")
print arcpy.GetMessages(1)
arcpy.AddMessage(arcpy.GetMessages(1))
log1.close()
Thanks in advance,Ed