Migrating arcpy.na to ArcGIS Pro

Python scripts authored with ArcGIS Desktop using arcpy.na will need to be modified before they will run in ArcGIS Pro. Additionally, you may need to take special considerations if you need your scripts to remain backward compatible with ArcGIS Desktop. The sections below highlight the necessary changes and considerations.

Using Python 3

ArcGIS Pro uses Python 3. It is possible that you will need to modify some standard Python syntax. If you want to run stand-alone Python scripts you are required to install 64-bit Python.

Creating network analysis layers

Starting in ArcGIS Pro, network analysis layers can be created using a new set of geoprocessing tools. The old tools for creating network analysis layers have been deprecated. The Make Route Layer tool has been replaced by the Make Route Analysis Layer tool, and similarly for the other network analysis layer types. The new tools provide enhanced functionality, such as the ability to work with travel modes and to use either a local network dataset or a portal service to solve the analysis. The deprecated tools will continue to work in ArcGIS Pro, but you should only use them if you need your script to be backward compatible with ArcGIS Desktop or versions of ArcGIS Pro earlier than 1.2.

Setting the workspace environment

In ArcGIS Pro, network analysis layers store their data on disk in file geodatabase feature classes. When creating a new network analysis layer in a Python script (using arcpy.na.MakeRouteAnalysisLayer(), for example), you must first explicitly set the workspace environment to a file geodatabase where you want the layer's data to be stored, using arcpy.env.workspace = "<path to file gdb>". When the layer is created, a new feature dataset containing the appropriate sublayer feature classes will be added to this file geodatabase.

Working with layer files

Layer files authored with ArcGIS Desktop have a .lyr extension, and layer files created with ArcGIS Pro have a .lyrx extension. Existing .lyr network analysis layer files can still be used in scripts in ArcGIS Pro, but they will be converted automatically to the new layer format and their data saved to feature classes on disk. Prior to using a .lyr network analysis layer file in your script, you must explicitly set the workspace environment to a file geodatabase where you want the layer's data to be stored, using arcpy.env.workspace = "<path to file gdb>". If you wish to save an existing .lyr network analysis layer file in the new .lyrx format, use arcpy.management.SaveToLayerFile(). Regardless of where you save the .lyrx file, the layer's data will remain in the file geodatabase specified in the workspace environment.

Working with layer objects

The arcpy.mapping module is now arcpy.mp, and these changes affect how you work with network analysis layer objects in scripts. Specifically, the syntax has changed for accessing sublayers, retrieving layer objects from layer files, and saving layer objects as layer files.

To create a layer object from an existing .lyrx (or .lyr) network analysis layer file, use arcpy.mp.LayerFile(lyrxPath).listLayers()[0]. Note that arcpy.mapping.ListLayers() has been replaced with layer_object.listLayers(), and arcpy.mp.LayerFile should be used instead of arcpy.mapping.Layer. The listLayers method returns a list of layer objects contained in the .lyrx file; the first item is always the network analysis layer object, and the remaining items are the sublayers. To save a network analysis layer object to a .lyrx, use NA_layer_object.saveACopy("<path to output .lyrx>").

To access a specific sublayer of a network analysis layer, you can use a wildcard in listLayers to filter the results, if you know the name of the sublayer you wish to access: facilities_sublayer = NA_layer_object.listLayers(facilities_layer_name)[0]. If you do not know the sublayer's name, or you want to ensure that your script works on localized versions of ArcGIS Pro, you can obtain the sublayer names using arcpy.na.GetNAClassNames as you did in ArcGIS Desktop. You can no longer access sublayers using a string path, such as "Route/Stops", for the Stops sublayer in a network analysis layer named Route.

Rubriques connexes