Hi All,
I have a function (copied/pasted below) that does the following:
Based on some testing, the Intersect_analysis step intermittently (every 10-20 times the tool is run) returns an empty temporary feature class (doing a GetCount of it returns 0 features). If the temp feature class is empty, then there is no “max” overlap value, so in turn function returns an error.
def get_proj_ctype(in_project_fc, commtypes_fc):
'''Get project community type, based on which community type has most spatial overlap with project'''
temp_intersect_fc = os.path.join(arcpy.env.scratchGDB, 'temp_intersect_fc') # don't use "memory", use scratch GDB
arcpy.Intersect_analysis([in_project_fc, commtypes_fc], temp_intersect_fc, "ALL",
0, "LINE")
item_cnt = arcpy.GetCount_management(temp_intersect_fc)[0]
arcpy.AddMessage("Project segments after intersecting with comm types: {}".format(item_cnt))
len_field = 'SHAPE@LENGTH'
fields = ['OBJECTID', len_field, params.col_ctype]
ctype_dist_dict = {}
with arcpy.da.SearchCursor(temp_intersect_fc, fields) as cur:
for row in cur:
ctype = row[fields.index(params.col_ctype)]
seg_len = row[fields.index(len_field)]
if ctype_dist_dict.get(ctype) is None:
ctype_dist_dict[ctype] = seg_len
else:
ctype_dist_dict[ctype] += seg_len
try:
maxval = max([v for k, v in ctype_dist_dict.items()])
proj_ctype = [k for k, v in ctype_dist_dict.items() if v == maxval][0]
return proj_ctype
except:
raise ValueError("ERROR: No Community Type identified for project.")
Any thoughts as to what's going on?
Further information:
Got to ask the obvious question, when someone draws that line is it possible they have drawn a line that does not intersect any of your polygons? May be they are looking at the data at a zoomed out scale and what they think they are drawing on is actually missing the polygons?
Good Question, but no, in every test done, the line has been drawn within at least one of the polygons.
OK one thing your function should be doing but it is not is to test if the input parameters actually have any data to intersect. I assume in_project_fc is the line layer? I would check that actually has data in it. If you find it does not then this would indicate the issue is outside this function and at that point nobody can help you as we are not seeing the full code.
On a side note you should always use the syntax highlighter when adding code as it makes it easier to read and in the case of python helps identify indentation issues. You can ALWAYS amend your original question...