AnsweredAssumed Answered

Issues using arcpy for network analyst tasks

Question asked by Greg_King on Apr 25, 2016
Latest reply on Apr 26, 2016 by GSCUser85


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

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
time the loop iterates through a separate 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

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 =
    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],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___