I am writing a script with ArcPy to look at modelling closed roads and the length of possible diversions. This is being done with the network analyst tools. The basic process I am using is to:

  • Create a new route analysis
  • Add route end points and a polygon barriers to block the road in question
  • Solve the problem to create the result of an alternative route to the closed road.
  • Save this result to a geodatabase feature class. I have created a script that works well for a single polyline feature. However, when I put this code into a ‘for loop’ to run over multiple polylines, I get errors. The resulting feature class has route results for all of the polylines in my input feature class. I don’t get a separate feature class for each polyline in my input feature class. I think it may have something to do with removing the end points and barrier polygons from my route layer after each iteration of the loop.

Can someone have a quick look at this code and let me know what the issue may be?

import arcpy
from arcpy import na
from arcpy import env

env.overwriteOutput = True

#___set global variables__

# road segments layer
roadSegs = r'Z:\BNE\Projects\246000\246777-00 Prioritising road segments\Work\Internal\GIS\Data\ALL_SEGMENTS_PROCESSING\Diversions_Assessment.gdb\State_Road_Network_Results\Sample_Segment_Five_Digit_IDs'
# network dataset (local road network, state road network, freight road network).  Needs to have a 'time' attribute for each link
networkDS = r'Z:\BNE\Projects\246000\246777-00 Prioritising road segments\Work\Internal\GIS\Data\ALL_SEGMENTS_PROCESSING\Diversions_Assessment.gdb\State_Road_Network\Qld_State_Road_Network_CL_ND'
# temp geodatabase for working layers
tempFGDB = r'Z:\BNE\Projects\246000\246777-00 Prioritising road segments\Work\Internal\GIS\Data\ALL_SEGMENTS_PROCESSING\Diversions_TEMP.gdb'
# output route line object
routeFC = r'Z:\BNE\Projects\246000\246777-00 Prioritising road segments\Work\Internal\GIS\Data\ALL_SEGMENTS_PROCESSING\Diversions_Assessment.gdb'

#Loop through each segment in the feature class to calculate the diversion
for segment in roadSegs:

# Get the segment ID value from the segment line layer
segIDCursor = arcpy.UpdateCursor(roadSegs)

for segRow in segIDCursor:
    segID = segRow.getValue('Segment_ID')

    #___create the working layers for the assessment___

    # Local loop variables
    segsEnds = tempFGDB + '\RoadSegsEndPts_'+ str(segID)[:5]
    endsBuffer = segsEnds + '_Buffer100m_'+ str(segID)[:5]
    segsErase = tempFGDB + '\RoadSegsErase_'+ str(segID)[:5]
    segsEraseBuffer = tempFGDB + '\SegmentClosurePolygon_'+ str(segID)[:5]

    # Convert road segment to points (end)

    # Create the blockage geometry:
    # Buffer the end points by 100m
    arcpy.Buffer_analysis(segsEnds,endsBuffer,'100 Meters')
    # Erase road segment line by 100m point buffers
    # Buffer the erased road segment line by 50m - POLYGON BARRIER
    arcpy.Buffer_analysis(segsErase,segsEraseBuffer,'50 Meters')

    #___setup and solve the route scenario layer___

    # MakeRouteLayer tool
    routeLayer = arcpy.MakeRouteLayer_na(networkDS,'ReRoute','Time')
    routeOutput = routeLayer.getOutput(0)
    subLayerNames = arcpy.na.GetNAClassNames(routeOutput)
    routesLayerName = subLayerNames["Routes"]
    # AddLocations - End Points
    arcpy.AddLocations_na(routeLayer,'Stops',segsEnds,'','100 Meters','','SHAPE','','CLEAR','SNAP','','','')
    # AddLocations - barrier polygon
    arcpy.AddLocations_na(routeLayer,'Polygon Barriers',segsEraseBuffer,'', '#','','SHAPE','','','','','INCLUDE','')
    # Solve the route scenario
    # Save the result as a Feature Layer (set the file name as the 'segment ID' + 'network type')
    RoutesSubLayer = arcpy.mapping.ListLayers(routeOutput, routesLayerName)[0]
    arcpy.management.CopyFeatures(RoutesSubLayer,routeFC + '\State_Road_Network_Results\Deviation_Result_Segment_' + str(segID)[:5])

    # variable for the resulting deviation layer.  SEE IF THIS CAN BE INCLUDED IN THE LOCAL VARIABLES ABOVE
    routeDeviation = routeFC + '\State_Road_Network_Results\Deviation_Result_Segment_' + str(segID)[:5]

    print routeDeviation

    # Add a field to the deviation layer and add in the segment ID from the segment line layer
    deviateCursor = arcpy.UpdateCursor(routeDeviation)
    for devRow in deviateCursor:

    # Delete the temporary working files and objects

    del segIDCursor
    del segRow

    del segsEnds
    del endsBuffer
    del segsErase
    del segsEraseBuffer

    del deviateCursor
    del devRow

# ___Close Loop___


closed as off-topic by PolyGeo Oct 26 '16 at 7:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • As it stands I think that there is too much code for this question to be answered here, which I suspect is why no comments or answers have been offered in just over 6 months. I think it will be worth you reviewing meta.gis.stackexchange.com/questions/4312/… It can be difficult to extract code snippets but the benefit is usually quicker answers and a deeper understanding of your own code. – PolyGeo Oct 26 '16 at 7:54