<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Issues using arcpy for network analyst tasks in Python Snippets Questions</title>
    <link>https://community.esri.com/t5/python-snippets-questions/issues-using-arcpy-for-network-analyst-tasks/m-p/784186#M156</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Havn't been through your code, it is rather difficult to follow.&lt;/P&gt;&lt;P&gt;Consider using arcpy.da style cursor to access data.&lt;/P&gt;&lt;P&gt;If you need the separate features of roadSeg and to process them individually you could first read them into a dictionary.&lt;/P&gt;&lt;P&gt;Like&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;roadSegDict = {}
with arcpy.da.SearchCursor(fc, ["Segment_ID", "SHAPE@"]) as Cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in Cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; roadSegDict[row[0]] = row[1]&amp;nbsp; # row[0] is the Segment_ID, row[1] is the geometry.&lt;/PRE&gt;&lt;P&gt;In this scenario, Segment_ID must be unique.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then you can loop through the contents of roadSegDict.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for ID, geom in roadSegDict.iteritems():&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I was last messing with NA datasets, I couldn't find a "Create Network Dataset" tool to use in python.&lt;/P&gt;&lt;P&gt;It maybe there somewhere, but I couldn't find it.&lt;/P&gt;&lt;P&gt;Ended up having a pre created one (using the wizard), then deleting content, appending content, then solving.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 08:56:23 GMT</pubDate>
    <dc:creator>NeilAyres</dc:creator>
    <dc:date>2021-12-12T08:56:23Z</dc:date>
    <item>
      <title>Issues using arcpy for network analyst tasks</title>
      <link>https://community.esri.com/t5/python-snippets-questions/issues-using-arcpy-for-network-analyst-tasks/m-p/784183#M153</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am writing a script with ArcPy to look at modelling closed&lt;BR /&gt;roads and the length of possible diversions.&amp;nbsp; This is being done with the network analyst&lt;BR /&gt;tools.&lt;/P&gt;&lt;P&gt;The basic process I am using is to:&lt;/P&gt;&lt;UL style="list-style-type: disc;"&gt;&lt;LI&gt;Create a new route analysis&lt;/LI&gt;&lt;LI&gt;Add route end points and a polygon barriers to&lt;BR /&gt;block the road in question&lt;/LI&gt;&lt;LI&gt;Solve the problem to create the result of an alternative&lt;BR /&gt;route to the closed road.&lt;/LI&gt;&lt;LI&gt;Save this result to a geodatabase feature class.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;I have created a script that works well for a single&lt;BR /&gt;polyline feature.&amp;nbsp; However, when I put&lt;BR /&gt;this code into a ‘for loop’ to run over multiple polylines, I get errors.&amp;nbsp; The resulting feature class has route results&lt;BR /&gt;for all of the polylines in my input feature class.&amp;nbsp; I don’t get a separate feature class for each&lt;BR /&gt;time the loop iterates through a separate polyline in my input feature class.&lt;/P&gt;&lt;P&gt;I think it may have something to do with removing the end&lt;BR /&gt;points and barrier polygons from my route layer after each iteration of the&lt;BR /&gt;loop.&lt;/P&gt;&lt;P&gt;Can someone have a quick look at this code and let me know&lt;BR /&gt;what the issue may be?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Greg&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;from arcpy import na&lt;BR /&gt;from arcpy import env&lt;/P&gt;&lt;P&gt;env.overwriteOutput = True&lt;BR /&gt;arcpy.CheckOutExtension('Network')&lt;/P&gt;&lt;P&gt;#___set global variables__&lt;/P&gt;&lt;P&gt;# road segments layer&lt;BR /&gt;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'&lt;BR /&gt;# network dataset (local road network, state road network, freight road network).&amp;nbsp; Needs to have a 'time' attribute for each link&lt;BR /&gt;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'&lt;BR /&gt;# temp geodatabase for working layers&lt;BR /&gt;tempFGDB = r'Z:\BNE\Projects\246000\246777-00 Prioritising road segments\Work\Internal\GIS\Data\ALL_SEGMENTS_PROCESSING\Diversions_TEMP.gdb'&lt;BR /&gt;# output route line object&lt;BR /&gt;routeFC = r'Z:\BNE\Projects\246000\246777-00 Prioritising road segments\Work\Internal\GIS\Data\ALL_SEGMENTS_PROCESSING\Diversions_Assessment.gdb'&lt;/P&gt;&lt;P&gt;#Loop through each segment in the feature class to calculate the diversion&lt;BR /&gt;for segment in roadSegs:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get the segment ID value from the segment line layer&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; segIDCursor = arcpy.UpdateCursor(roadSegs)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for segRow in segIDCursor:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; segID = segRow.getValue('Segment_ID')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #___create the working layers for the assessment___&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Local loop variables&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; segsEnds = tempFGDB + '\RoadSegsEndPts_'+ str(segID)[:5]&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; endsBuffer = segsEnds + '_Buffer100m_'+ str(segID)[:5]&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; segsErase = tempFGDB + '\RoadSegsErase_'+ str(segID)[:5]&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; segsEraseBuffer = tempFGDB + '\SegmentClosurePolygon_'+ str(segID)[:5]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Convert road segment to points (end)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.FeatureVerticesToPoints_management(roadSegs,segsEnds,'BOTH_ENDS')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create the blockage geometry:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Buffer the end points by 100m&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Buffer_analysis(segsEnds,endsBuffer,'100 Meters')&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Erase road segment line by 100m point buffers&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Erase_analysis(roadSegs,endsBuffer,segsErase)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Buffer the erased road segment line by 50m - POLYGON BARRIER&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Buffer_analysis(segsErase,segsEraseBuffer,'50 Meters')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #___setup and solve the route scenario layer___&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # MakeRouteLayer tool&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; routeLayer = arcpy.MakeRouteLayer_na(networkDS,'ReRoute','Time')&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; routeOutput = routeLayer.getOutput(0)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; subLayerNames = arcpy.na.GetNAClassNames(routeOutput)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; routesLayerName = subLayerNames["Routes"]&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # AddLocations - End Points&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddLocations_na(routeLayer,'Stops',segsEnds,'','100 Meters','','SHAPE','','CLEAR','SNAP','','','')&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # AddLocations - barrier polygon&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddLocations_na(routeLayer,'Polygon Barriers',segsEraseBuffer,'', '#','','SHAPE','','','','','INCLUDE','')&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Solve the route scenario&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Solve_na(routeLayer,'SKIP','CONTINUE','')&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Save the result as a Feature Layer (set the file name as the 'segment ID' + 'network type')&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; RoutesSubLayer = arcpy.mapping.ListLayers(routeOutput, routesLayerName)[0]&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.management.CopyFeatures(RoutesSubLayer,routeFC + '\State_Road_Network_Results\Deviation_Result_Segment_' + str(segID)[:5])&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # variable for the resulting deviation layer.&amp;nbsp; SEE IF THIS CAN BE INCLUDED IN THE LOCAL VARIABLES ABOVE&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; routeDeviation = routeFC + '\State_Road_Network_Results\Deviation_Result_Segment_' + str(segID)[:5]&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #routeDeviation.strip('.0')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print routeDeviation&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add a field to the deviation layer and add in the segment ID from the segment line layer&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(routeDeviation,'Seg_ID','TEXT','','',50)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; deviateCursor = arcpy.UpdateCursor(routeDeviation)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for devRow in deviateCursor:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; devRow.setValue('Seg_ID',segID)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; deviateCursor.updateRow(devRow)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Delete the temporary working files and objects&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management('im_memory')&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del segIDCursor&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del segRow&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del segsEnds&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del endsBuffer&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del segsErase&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del segsEraseBuffer&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del deviateCursor&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del devRow&lt;/P&gt;&lt;P&gt;# ___Close Loop___&lt;/P&gt;&lt;P&gt;arcpy.CheckInExtension('Network')&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Apr 2016 09:49:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/issues-using-arcpy-for-network-analyst-tasks/m-p/784183#M153</guid>
      <dc:creator>GregKing1</dc:creator>
      <dc:date>2016-04-25T09:49:58Z</dc:date>
    </item>
    <item>
      <title>Re: Issues using arcpy for network analyst tasks</title>
      <link>https://community.esri.com/t5/python-snippets-questions/issues-using-arcpy-for-network-analyst-tasks/m-p/784184#M154</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Firstly,&lt;/P&gt;&lt;P&gt;use the advanced editor and post the code with python syntax highlighting.&lt;/P&gt;&lt;P&gt;That might make it a bit easier to read.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Those paths are very complex and contain spaces and other nasty bits. Have you tried running this locally instead?&lt;/P&gt;&lt;P&gt;What is happening here:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#Loop through each segment in the feature class to calculate the diversion
for segment in roadSegs:

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get the segment ID value from the segment line layer
&amp;nbsp;&amp;nbsp;&amp;nbsp; segIDCursor = arcpy.UpdateCursor(roadSegs)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for segRow in segIDCursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; segID = segRow.getValue('Segment_ID')&lt;/PRE&gt;&lt;P&gt;What is the nature of roadSegs? This does not seem to be a list, it points to something on disk (I think).&lt;/P&gt;&lt;P&gt;Then you open an Update cursor, but actually just reads the data.&lt;/P&gt;&lt;P&gt;Then it reads everything, so segID will always be the last one in segIDCursor..&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:56:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/issues-using-arcpy-for-network-analyst-tasks/m-p/784184#M154</guid>
      <dc:creator>NeilAyres</dc:creator>
      <dc:date>2021-12-12T08:56:20Z</dc:date>
    </item>
    <item>
      <title>Re: Issues using arcpy for network analyst tasks</title>
      <link>https://community.esri.com/t5/python-snippets-questions/issues-using-arcpy-for-network-analyst-tasks/m-p/784185#M155</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Neil,&lt;/P&gt;&lt;P&gt;Thanks for your reply.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I was initially testing this code I was using a single polyline feature.&amp;nbsp; The code works well for a single feature and I get the result I am looking for.&amp;nbsp; So the file paths I have set here work fine in this situation.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The issue is when I put the main body of code into the 'for loop' I'm not getting the results I thought I would be.&amp;nbsp; My input feature class has multiple polylines representing road centrelines.&amp;nbsp; What I would like to get is a separate feature class with the result from the network analyst operation for each feature in the input feature class .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;'roadSegs' is the feature class with the road polyline features.&amp;nbsp; Within this feature class is an attribute called 'Segment_ID' with the ID value for each road polyline.&lt;/P&gt;&lt;P&gt;Following your comment below, how can I setup the cursor so it only reads the ID value for the polyline being processed in each particular iteration of the loop?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, when I run a network analyst operation using python, should I delete the network dataset at the end of each iteration of the loop?&amp;nbsp; Then create a new network dataset at the start of the next iteration?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks for your assistance.&lt;/P&gt;&lt;P&gt;Greg&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Apr 2016 10:18:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/issues-using-arcpy-for-network-analyst-tasks/m-p/784185#M155</guid>
      <dc:creator>GregKing1</dc:creator>
      <dc:date>2016-04-26T10:18:10Z</dc:date>
    </item>
    <item>
      <title>Re: Issues using arcpy for network analyst tasks</title>
      <link>https://community.esri.com/t5/python-snippets-questions/issues-using-arcpy-for-network-analyst-tasks/m-p/784186#M156</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Havn't been through your code, it is rather difficult to follow.&lt;/P&gt;&lt;P&gt;Consider using arcpy.da style cursor to access data.&lt;/P&gt;&lt;P&gt;If you need the separate features of roadSeg and to process them individually you could first read them into a dictionary.&lt;/P&gt;&lt;P&gt;Like&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;roadSegDict = {}
with arcpy.da.SearchCursor(fc, ["Segment_ID", "SHAPE@"]) as Cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in Cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; roadSegDict[row[0]] = row[1]&amp;nbsp; # row[0] is the Segment_ID, row[1] is the geometry.&lt;/PRE&gt;&lt;P&gt;In this scenario, Segment_ID must be unique.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then you can loop through the contents of roadSegDict.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for ID, geom in roadSegDict.iteritems():&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I was last messing with NA datasets, I couldn't find a "Create Network Dataset" tool to use in python.&lt;/P&gt;&lt;P&gt;It maybe there somewhere, but I couldn't find it.&lt;/P&gt;&lt;P&gt;Ended up having a pre created one (using the wizard), then deleting content, appending content, then solving.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:56:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/issues-using-arcpy-for-network-analyst-tasks/m-p/784186#M156</guid>
      <dc:creator>NeilAyres</dc:creator>
      <dc:date>2021-12-12T08:56:23Z</dc:date>
    </item>
  </channel>
</rss>

