How to add Route Layer to Map using Arcpy.nax, Pro 2.5?

1124
4
Jump to solution
08-10-2020 02:41 PM
jacquelinewillan1
New Contributor III

What is the best way to generate a route, that can be saved as a route, and displayed on the map, using Pro 2.5? I created a route using the arcpy.nax module (which I found is faster than using the arcpy.na to Make a Route Analysis Layer), and was able to save as a layer file:

routeLyr = result.saveAsLayerFile(WorkSpace + os.path.sep +  RouteName + ".lyr"), but then I want to add it to the map and choose the colors. When I tried the code below, I got an error: on the line for the mp.LayerFile:

curPrj = arcpy.mp.ArcGISProject("CURRENT")
curMap = curPrj.listMaps("*")[0]

layerPath = WS + os.path.sep + RouteName + ".lyr"
lf = arcpy.mp.LayerFile(layerPath)

I also tried: 

lf = arcpy.mp.LayerFile(routeLyr ), but got an error for that too.  In 10.6 my users liked the fact that they could generate some routes and then go back later and add barriers, and to do that I can't just export the route to a feature class. Is there an alternate method to save/export the route? 

Any help on how to use python to display the layer or an alternate way to save/export a route would be greatly appreciated!

0 Kudos
1 Solution

Accepted Solutions
MelindaMorang
Esri Regular Contributor

Try something like this:

layer_path = "D:\myDir\Route.lyr"
result.saveAsLayerFile(layer_path)
arcpy.SetParameterAsText(5, layer_path)

I'm not 100% sure if that will work.

There are a couple of things to be aware of with regards to the nax solver objects as saveAsLayerFile.  First, the saved layer file is a .lyr file in the old ArcMap style, not a .lyrx in the Pro style.  If you try to use .lyrx in the output path, it will fail.  You must use .lyr.

Since the output file is a .lyr file, I don't know if it will actually work with SetParameter() or SetParameterAsText() because that might be expecting a Pro-style layer file or layer object.  As I said, the intention of these saved layer files from the nax solver objects is for debugging, not for what you're currently doing.

Another thing you could try is reconstituting the layer file as a Pro-style layer object after you've saved it out.

layer_path = "D:\myDir\Route.lyr"
result.saveAsLayerFile(layer_path)
layer_object = arcpy.mp.LayerFile(layer_path).listLayers()[0]
arcpy.SetParameter(5, layer_object)‍‍‍

I don't know if that will work either, though.

View solution in original post

4 Replies
MelindaMorang
Esri Regular Contributor

I'm guessing from your questions that you are writing a python script tool that you intend to run as a geoprocessing tool within ArcGIS Pro.  Is this correct?

If this is the case, then the proper way to add the output network analysis layer to the map is not to use the mapping module to add it explicitly but to simply add an output parameter to the script tool.  Let me explain further.

First, when setting up your tool parameters in the tool dialog, create an output parameter.  Set the type to Network Analysis Layer.  Make a note of the index for this parameter.  You can also set up a symbology layer for this parameter.  By specifying a symbology layer here, it will automatically apply this layer's symbology to the derived output when it adds it to the map.

When you run the tool, it should automatically add the layer to the map.

If you don't want the users of your tool to specify the path to the output .lyr file, but instead you want to handle that internally, you can make the parameter definition of type "Derived".  In the script, once you have your output layer path, assign the layer to that parameter using arcpy.SetParameter().

Note: if your ultimate goal is to create fully-functioning network analysis layers as the output of your tool, the arcpy.nax module might not be the right choice.  It might be better to use the classic Make Route Analysis Layer, Add Locations, Solve workflow.  The saveAsLayerFile() method for the nax solver objects is primarily meant for debugging.

0 Kudos
jacquelinewillan1
New Contributor III

Yes I am writing a python script tool that will be run as a geoprocessing tool within ArcGIS Pro. What you suggest sounds like that might be what I need. I had tried creating an output parameter of type Network Analysis Layer (with Type Derived, Direction Output), but at that point I didn't know how to get it to be added to the map (in 10.6 I had used the derived parameter and it was automatically added to the Table of Contents - and random symbology). When I tried it in Pro, it was not automatically added to the Table of Contents, so I was trying other ways to implement it. I do want it handled internally. I'm not clear on the line of code for specifying the output layer path would that be like:

RouteLyr = arcpy.SetParameter(5,"D:\myDir") with 5 being the index of the parameter? Would I also put the name like:

"D:\myDir\Route.lyrx"? I tried the line without the lyrx name and there was no error, but when I tried the next line:

lyrFile = arcpy.mp.LayerFile(RouteLyr) I get an error message:

TypeError: Required argument 'layer_file_path' (pos 1) not found

What am I missing?  Thanks for your help!  I'd rather not use the Make Route Analysis Layer method because it took much longer and my users are used to performing a Solve in a few seconds not waiting 25-some seconds for the Make Route Analysis Layer, Add Locations, and Solve. Thanks!

ValueError: Route
0 Kudos
MelindaMorang
Esri Regular Contributor

Try something like this:

layer_path = "D:\myDir\Route.lyr"
result.saveAsLayerFile(layer_path)
arcpy.SetParameterAsText(5, layer_path)

I'm not 100% sure if that will work.

There are a couple of things to be aware of with regards to the nax solver objects as saveAsLayerFile.  First, the saved layer file is a .lyr file in the old ArcMap style, not a .lyrx in the Pro style.  If you try to use .lyrx in the output path, it will fail.  You must use .lyr.

Since the output file is a .lyr file, I don't know if it will actually work with SetParameter() or SetParameterAsText() because that might be expecting a Pro-style layer file or layer object.  As I said, the intention of these saved layer files from the nax solver objects is for debugging, not for what you're currently doing.

Another thing you could try is reconstituting the layer file as a Pro-style layer object after you've saved it out.

layer_path = "D:\myDir\Route.lyr"
result.saveAsLayerFile(layer_path)
layer_object = arcpy.mp.LayerFile(layer_path).listLayers()[0]
arcpy.SetParameter(5, layer_object)‍‍‍

I don't know if that will work either, though.

jacquelinewillan1
New Contributor III

The second one worked - thanks for your help!

0 Kudos