Select to view content in your preferred language

Can't turn off hierarchy in scripts

147
2
Jump to solution
2 weeks ago
StephenRhone
Occasional Contributor

Good morning,

I am attempting to script a series of closest-facility analyses but have been unable to deactivate the hierarchy setting in the script.  I've turned to AI as many of us do these days, and two AI engines have advised to pull the analysis layer's properties using arcpy.na.GetSolverProperties, then set the useHierarchy setting to either True or False as needed.  When I add this language to my script, it doesn't error out, so it appears to be valid code, but whichever way I set it in the script, the resulting route data is the same.

I should mention that I'm able to deactivate the hierarchy if I run the analysis manually in ArcGIS Desktop, and that it's successful there - my routes take about 13 minutes to run without hierarchy, as opposed to 2 minutes if I use the script which uses hierarchy.  While they take longer to run, I've been asked for the non-hierarchy outputs as the resulting routes are more direct.  Has anyone been able to run network analysis in a script with the hierarchy deactivated, and if so, how were you able to do so?  Thanks in advance.

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
MelindaMorang
Esri Regular Contributor

Hierarchy is a property of the travel mode used for the analysis, and you can control it in a script by modifying a travel mode object.

Here's the documentation for the travel mode object: https://pro.arcgis.com/en/pro-app/latest/arcpy/network-analyst/travelmode-nax.htm

# Retrieve a travel mode from the network
tm_orig = arcpy.nax.GetTravelModes(network)["Driving Time"]
# Copy it and turn off hierarchy
tm_updated = arcpy.nax.TravelMode(tm_orig)
tm_updated.useHierarchy = "NO_HIERARCHY"

# Use the travel mode for a Closest Facility analysis using arcpy.nax
cf = arcpy.nax.ClosestFacility(network)
cf.travelMode = tm_updated

# OR (not recommended)
# Use the travel mode for a Closest Facility analysis using a layer
lyr = arcpy.na.MakeClosestFacilityAnalysisLayer(network, travel_mode=tm_updated).getOutput(0)

 

Note that we don't recommend using network analysis layers in Python scripting workflows.  For a faster and more Pythonic experience, use the arcpy.nax solver classes to solve your analysis.  This documentation explains the workflow for running a network analysis in Python using arcpy.nax.  Here is the documentation for the ClosestFacility class.

Further note: If you just need the travel times or distances and don't need the geometry of the route taken or the driving directions, you can get what you need, and get it faster, using OD Cost Matrix.

View solution in original post

2 Replies
MelindaMorang
Esri Regular Contributor

Hierarchy is a property of the travel mode used for the analysis, and you can control it in a script by modifying a travel mode object.

Here's the documentation for the travel mode object: https://pro.arcgis.com/en/pro-app/latest/arcpy/network-analyst/travelmode-nax.htm

# Retrieve a travel mode from the network
tm_orig = arcpy.nax.GetTravelModes(network)["Driving Time"]
# Copy it and turn off hierarchy
tm_updated = arcpy.nax.TravelMode(tm_orig)
tm_updated.useHierarchy = "NO_HIERARCHY"

# Use the travel mode for a Closest Facility analysis using arcpy.nax
cf = arcpy.nax.ClosestFacility(network)
cf.travelMode = tm_updated

# OR (not recommended)
# Use the travel mode for a Closest Facility analysis using a layer
lyr = arcpy.na.MakeClosestFacilityAnalysisLayer(network, travel_mode=tm_updated).getOutput(0)

 

Note that we don't recommend using network analysis layers in Python scripting workflows.  For a faster and more Pythonic experience, use the arcpy.nax solver classes to solve your analysis.  This documentation explains the workflow for running a network analysis in Python using arcpy.nax.  Here is the documentation for the ClosestFacility class.

Further note: If you just need the travel times or distances and don't need the geometry of the route taken or the driving directions, you can get what you need, and get it faster, using OD Cost Matrix.

StephenRhone
Occasional Contributor

I forgot about the NAX module - thanks for the tip!

0 Kudos