Problem with Using Python to change Travel Mode Impedance in Pro 2.5

1300
7
Jump to solution
08-12-2020 07:51 AM
jacquelinewillan1
New Contributor III

Hi,

am trying to test the ability to change the impedance on a TravelMode using python:

travelModes = arcpy.nax.GetTravelModes(NDLayerName)
travelMode = travelModes["Travel Mode OnRoad"]
travelMode.impedance = "Shortest"
arcpy.AddMessage("impedance = " + str(travelMode.impedance))

However, either I am doing something wrong or else maybe there is a problem in Pro 2.5 with changing the impedance using python or a problem with the impedance being held in memory?  I've tried numerous times to change the impedance without the route using the changed impedance, and then it worked.  Then when I tried to change the impedance to something else - "OnRoad', now it is stuck on "Shortest". I tried saving the project, closing Pro, reopening Pro and the project, then  running the solve again, but with the impedance set to "OnRoad", the route being created is for the Shortest. The TravelMode actually has "OnRoad" as the impedance, so I tried deleting the line setting

travelMode.impedance = "OnRoad" (since without that, the actual TravelMode setting should be used, and the route created is still using the 'shortest' instead of OnRoad.  Any ideas what is wrong? Or is this a bug in 2.5?  Thanks!

0 Kudos
1 Solution

Accepted Solutions
MelindaMorang
Esri Regular Contributor

That's because travelMode = travelModes("OnRoad") is a syntax error.  You need to use square brackets to retrieve an item in a dictionary. travelMode = travelModes["OnRoad"]

View solution in original post

7 Replies
MelindaMorang
Esri Regular Contributor

I can't really follow your workflow from your description, but I suggest you follow the last code sample on this page: TravelMode—ArcGIS Pro | Documentation 

Rather than editing the travel mode retrieved from the network directly, try instantiating an entirely new travel mode using the network's travel mode as a template.  Then change the impedance for the new travel mode and see if that works.

jacquelinewillan1
New Contributor III

Thanks, that is the documentation I was following to change the impedance. The one line of code:

travelMode.impedance = "new impedance"

should change the impedance according to the documentation, but when tested, it is not changing the impedance, so that appears to be a bug unless I'm missing something.

However I would like to try instantiating a new travel mode if that would work, but must have the syntax wrong as I get the error:

KeyError: '<Travel Mode OnRoad>'

from using this line of code:

travelMode = travelModes["<Travel Mode OnRoad>"]

I also tried: travelMode = travelModes({"<Travel Mode OnRoad>"})

but that gave a different error.

Where do the < > brackets go? Thanks for your help!

0 Kudos
MelindaMorang
Esri Regular Contributor

The brackets are added automatically to the name.  You don't need to do anything with those.

# Get all existing travel modes from the network dataset
travel_modes = arcpy.nax.GetTravelModes(network)

# Get the existing "Driving Time" travel mode to use as a template
existing_tm = travel_modes["Driving Time"]

# Construct a new TravelMode object from the existing travel mode
new_travel_mode = arcpy.nax.TravelMode(existing_tm)

# Update the new travel mode's impedance
new_travel_mode.impedance = "MyImpedance"

# Use the new travel mode object when constructing a OD Cost Matrix object
arcpy.nax.OriginDestinationCostMatrix(network)
od_object.travelMode = new_travel_mode‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
MelindaMorang
Esri Regular Contributor

I'm still not entirely sure what your workflow is, but if you're using saveAsLayerFile() and then adding the saved .lyr file to the map and looking at the travel mode, it's possible the travel mode name displayed will be incorrect or inconsistent. That's because .lyr files are the old ArcMap layers that do not have a concept of travel mode.  I believe the settings should be consistent, but the name might be off.  I'm not really sure.

This is why I suggest that you use the older MakeLayer, Add Locations, Solve workflow n your script. The nax solver objects just really aren't designed to work with layers.

0 Kudos
jacquelinewillan1
New Contributor III

Thanks for looking at my problem. I think the new travel mode will work and I will give it a try, however before I got your response I tried adding a new travel mode to the network dataset, manually (by opening the properties of the datset), and now I get a new error: 

line of code: travelMode = travelModes("OnRoad")
TypeError: 'dict' object is not callable

This is the same line of code that was giving no error before. I tried 'Build Network Dataset' to see if this would take care of the problem, but it didn't. Any thoughts? If needed I will rebuild the network again, but I was hoping there is an easier fix.  Thanks!

0 Kudos
MelindaMorang
Esri Regular Contributor

That's because travelMode = travelModes("OnRoad") is a syntax error.  You need to use square brackets to retrieve an item in a dictionary. travelMode = travelModes["OnRoad"]

jacquelinewillan1
New Contributor III

Thanks, I saw after I sent the email that I had replaced the square brackets by mistake. I just tried your suggestion for the new travel mode and it worked. Thanks so much for your help!

0 Kudos