I am creating a script tool and I am trying to handle a unique error message that is created when a csv file is not formatted correctly for the script.
When I use the script standalone, the error is properly picked up at the end of the try-except
with the last exception that should catch all other errors. However, when placed into ArcGIS Pro as a script tool, the error does not come up if the CSV is formatted incorrectly. Instead the tool runs to completion. The code segment is below. The last few lines are me trying to catch the error but it does not translate properly in the script tool itself. Any suggestions?
The CSV basically must contain 4 columns, first is system ID, then lat, long, and name as a string. For example, if I delete the name column, the tool will run to completion without showing the index error from not being able to select a field in the cursor. Index Error list out of range. Please excuse if there are wrong the indents in the code below that occurred when pasting in, this block does work...
try:
# create points from csv, checks values for errors
with arcpy.da.InsertCursor(out_points, ['SHAPE@', 'SYSTEM_ID', 'LAT', 'LONG', 'NAME']) as cursor:
point = arcpy.Point()
with open(csvfile) as f:
reader = csv.reader(f)
headers = next(reader)
for line in reader:
if float(line[2]) > 180 or float(line[2]) < -180:
print(arcpy.AddError('Longitude Coordinate must be in decimal degrees between -180 and 180'))
arcpy.Delete_management(out_points)
exit()
elif float(line[1]) > 90 or float(line[1]) < -90:
print(arcpy.AddError('Latitude Coordinate must be in decimal degrees between -90 and 90'))
arcpy.Delete_management(out_points)
exit()
else:
point.ID = line[0]
check = line[0]
point.X = line[2]
point.Y = line[1]
name = line[3]
geom = arcpy.PointGeometry(point)
unique_id = line[0]
cursor.insertRow((geom, unique_id, point.Y, point.X, name))
for row in cursor:
check_select_points = int(arcpy.GetCount_management(out_points).getOutput(0))
if check_select_points == 0:
arcpy.AddError('Latitude Coordinate must be in decimal degrees between -90 and 90')
arcpy.Delete_management(out_points)
exit()
except arcpy.ExecuteError:
print(arcpy.GetMessages())
exit()
except:
print('An Unknown error has occurred. KML file name may already exist in this folder')
exit()
1 Answer 1
I think your logic is badly flawed, this is how I understand your code.
You create an insert cursor, open a csv file, on the first line of data you check if longitude is between 180, if not delete the output file and exit script. If it is between 180 you then check if latitude is between 90, if not delete the output file and exit script.
If you have got past those two checks you create a geometry and insert into the output featureclass. This logic repeats until all of csv file is read.
You make an erroneous call on cursor
as if it is a search cursor, but its an insert cursor!
for row in cursor:
You then get a count on the number of ROWS and check if it is zero, but by virtue of getting to this line it can never be zero because to be zero rows you would have skipped all the previous logic, but the way you set it up is if your csv has incorrect lat/long you delete the output and exit so it would never get to that line.
You never test if the fields exist you are testing rows.
I would personally suggest you have a strong cup of coffee, scrap this logic and rethink your code as it's neither efficient or logical.