I'm trying to calculate total stream length for multiple, overlapping watersheds (i.e., each watershed contains the watersheds upstream of it). I have a flowline layer that I've run through the Intersect tool so that they end on the boundaries of the watersheds (not the overlapping watersheds because that created multiples of the same flowlines). My idea was to create a cursor that would iterate through the watershed table, select the individual watershed, then use the select-by-location tool to select the only the stream segments within the selected watershed. However, every time I try this, I get the error: "Error in executing tool." Here's the code so far:
watershed = "Sampled_Watersheds"
flowline = "Flowline_Intersect2"
with arcpy.da.SearchCursor(watershed, "Stream_Length") as cursor:
for row in cursor:
arcpy.SelectLayerByAttribute_management(watershed, "NEW_SELECTION", [row])
arcpy.SelectLayerByLocation_management(flowline, "COMPLETELY_WITHIN", watershed, "NEW_SELECTION")
After, I get the selection, I want to add the total stream length to the watershed table. Would something like the follow work?
arcpy.CalculateField_management(watershed, "Stream_Length", "SUM(!Flowline_Streams.Length_km!)","PYTHON")