Hello,
I have a script to perform a simple everyday task I have to do at work that had been working well (for years) until recently. The script looks through a GDB to find unprocessed point FCs and turns them into smooth lines. However, recently I have been receiving the following error:
Traceback (most recent call last):
File "E:\MapFiles\Trails\TrailConditionMonitoring\OSMP_TrailAlignData\PointsToLineArcPro.py", line 9, in <module>
gpsPointList = list((arcpy.ListFeatureClasses(feature_type=pnt)))
TypeError: 'NoneType' object is not iterable
I'm unsure why the script worked for so long then all of a sudden threw this error. Why could I iterate through the list of feature classes before, but not now? Any help would be appreciated! The script is posted below.
# A script that looks through a GDB to find point fcs that need to be processed into smooth lines and processes them
import arcpy
# set workspace
wksp = arcpy.env.workspace = arcpy.GetParameterAsText(0)
# define variables for point and line features
pnt = "Point"
gpsPointList = list((arcpy.ListFeatureClasses(feature_type=pnt)))
print(gpsPointList)
line = "Line"
existingLines = list((arcpy.ListFeatureClasses(feature_type=line)))
print(existingLines)
# set up loop to add smooth text to the end of the point feature classes
smooth = "Smooth"
strPoint = ""
checkPoints = []
for points in gpsPointList:
strPoint = points + smooth
checkPoints.append(strPoint)
print(checkPoints)
# Set up list variables to store already processed and unprocessed point features
processedPoints = []
unprocessedPoints = []
# set up to iterate through the point feature classes to find duplicates(processed points)
unPointStr = ""
for pointfc in checkPoints:
if pointfc in existingLines:
processedPoints.append(pointfc)
else:
unPointStr = pointfc[:-6]
unprocessedPoints.append(unPointStr)
# Use exception handling to stop code if no point fcs need to be processed
if len(unprocessedPoints) == 0:
raise Exception("All point feature classes have been processed.")
else:
print(unprocessedPoints, "\n", len(unprocessedPoints), "point feature classes ready to process.")
# Iterate through unprocessed point fcs to turn them into smooth lines
sr = arcpy.SpatialReference(6429) # Code for NAD1983 (2011) StatePlane Colorado North FIPS
project = ""
ptsToLine = ""
for unpro in unprocessedPoints:
try:
# Project points to match other city data
project = unpro + "project"
arcpy.Project_management(unpro, project, sr)
print(project)
# Turn points into line
ptsToLine = project + "Line"
linefield = "RID"
arcpy.PointsToLine_management(project, ptsToLine, linefield)
print(ptsToLine)
# Smooth the line for a nice finished product
smoothLine = unpro + "Smooth"
alg = "PAEK" # algorithm for smoothing
tolerance = 5 # tolerance for smoothing
arcpy.SmoothLine_cartography(ptsToLine, smoothLine, alg, tolerance)
print(smoothLine + " Processed")
# delete intermediate data
arcpy.Delete_management(ptsToLine)
arcpy.Delete_management(project)
# use iteration to create a trail name for a new field
trail = smoothLine[:-12]
trail2 = ""
for i in range(len(trail)):
if (trail[i].isupper()):
trail2 += " "
trail2 += trail[i]
else:
trail2 += trail[i]
trailName = trail2[1:]
print(trailName)
# use cursor to add trail name field to smooth line fc
nameField = "TrailName"
arcpy.AddField_management(smoothLine, nameField, "TEXT")
print("Trail Name field added")
with arcpy.da.UpdateCursor(smoothLine, nameField) as cursor:
for row in cursor:
row[0] = trailName
cursor.updateRow(row)
print(trailName + " added")
del cursor, row
except:
print("Something went wrong check your unprocessed point list" + "\n" + unprocessedPoints)
print("Processing Complete")
Hi, it sounds like there are no new unprocessed point feature classes and you need to introduce some code to manage cases when gpsPointList returns None. Perhaps some change upstream has impacted your workflow?
import sys
gpsPointList = arcpy.ListFeatureClasses(feature_type=pnt)
if gpsPointList is None:
gpsPointList = []
print("No gpsPoint feature classes. Exit script")
sys.exit()
else:
gpsPointList = list(gpsPointList)
print(gpsPointList)
# continue with script
i would start with checking your workspace. Also, if you arcpy.ListFeatureClasses() returns none, might be an issue. Try this,
import arcpy
# set workspace
wksp = arcpy.env.workspace = arcpy.GetParameterAsText(0)
print(f"Workspace: {wksp}")
# define variables for point and line features
pnt = "Point"
gpsPointList = arcpy.ListFeatureClasses(feature_type=pnt)
print(f"GPS Point List: {gpsPointList}")
if gpsPointList is None:
raise Exception("No point feature classes found in the workspace.")
gpsPointList = list(gpsPointList)
line = "Line"
existingLines = arcpy.ListFeatureClasses(feature_type=line)
print(f"Existing Lines: {existingLines}")
if existingLines is None:
raise Exception("No line feature classes found in the workspace.")
existingLines = list(existingLines)