One of the most important parts of performing network analysis with the arcpy.nax module is accessing the analysis outputs. When you solve your analysis using the solve method, a solver result object is returned, and this result object has several methods that can be used to retrieve the outputs in the following ways:
- Save the analysis results to a feature class with the export method.
- Directly read the results with the searchCursor method.
- Save the analysis results to a .zip file that can be used in other applications, such as ArcGIS Navigator.
- Save the analysis results to a layer file or package for debugging purposes.
This topic describes each of these options in more detail so you can choose the one appropriate for your needs.
This topic uses Route analysis as an example; however, the information provided here can be applied to any type of network analysis.
Save results to a feature class with the export method
To save your analysis results directly to disk as they are returned by the solver, use the export method. All fields and rows for the designated output type are included.
The following code snippet shows how to save analysis results to disk using the export method.
# Solve the analysis result = route.solve() # Save results to disk using the export method output_feature_class = "C:/data/io.gdb/Routes" result.export(arcpy.nax.RouteOutputDataType.Routes, output_feature_class)
Learn more about the export method for route analysis in the Methods section of the RouteResult object's documentation.
Read results with the searchCursor method
The searchCursor method can be used to directly access outputs row by row. This is useful if you only need to retrieve specific fields from the output or if you want to insert the results directly into some other system without saving the entire output table to disk.
For example, perhaps in your Route analysis, you only need to retrieve the total miles traveled for each route so you can include that information in a report. Use the searchCursor method to retrieve the Total_Miles field for each row.
The following code snippet shows how to retrieve the values of certain fields in the output using the searchCursor method. In this example, the names of routes longer than 10 miles are printed to the console.
# Solve the analysis result = route.solve() # Retrieve specific fields of interest using a searchCursor for row in result.searchCursor(arcpy.nax.RouteOutputDataType.Routes, ["Name", "Total_Miles"]): # Retrieve the name and mileage for each route route_name = row route_miles = row # Print the route's name if the route's total mileage is greater than 10 miles if route_miles > 10: print(route_name)
You can access the geometry of outputs using some special shape tokens. For example, using the SHAPE@JSON token allows you to retrieve a JSON representation of the shape. Use the SHAPE@XY token to retrieve a tuple of X and Y coordinates for a point in the spatial reference specified by the solver result object's spatialReference property.
Use the result object's fieldNames method to retrieve a list of fields included in the analysis output class. Learn more about the fieldNames method for Route analysis output in the Methods section of the RouteResult object's documentation.
Learn more about the searchCursor method for Route analysis in the Methods section of the RouteResult object's documentation.
Retrieve a route data .zip file with the saveRouteData method
For Route, Closest Facility, and Vehicle Routing Problem analysis, you can export the result to a route data .zip file. The .zip file can be shared as route layer items in ArcGIS Online or ArcGIS Enterprise using the Share As Route Layers tool.
A route layer item can be used by various applications, such as ArcGIS Navigator to provide route guidance for field workers, the Directions pane in Map Viewer to further customize the route contained in the route layer, or ArcGIS Pro to create a new route analysis layer from a route layer.
The following code snippet shows how to save a route data .zip file with the saveRouteData method and share it to your portal with the ShareAsRoute Layers function.
# Solve the analysis result = route.solve() # Export the results to a route data .zip file out_zip = "C:/data/RouteData.zip" result.saveRouteData(out_zip) # Share the route data zip file as route layers to your portal arcpy.nax.ShareAsRouteLayers( out_zip, summary='Tuesday restaurant inspection routes', tags='Tuesday', route_name_prefix='TuesdayRestaurants', portal_folder_name='RouteLayers', share_with='MYGROUPS', groups='Drivers' )
To successfully run the saveRouteData method, the allowSaveRouteData property on the solver object must be set to True.
Learn more about the saveRouteData method for Route analysis in the Methods section of the RouteResult object's documentation.
Save a layer file or package with the saveAsLayerFile method
You can save your analysis settings and results to a layer file or layer package using the saveAsLayerFile method and open and examine the resulting layer in ArcGIS Pro. This is primarily intended for debugging your analysis.
You can choose to save as either a .lyr layer file compatible with either ArcGIS Desktop or ArcGIS Pro or a .lpkx layer package compatible with ArcGIS Pro only. The output type depends on the file extension you use when specifying the method's file_name parameter. Layer packages include the analysis data but do not include the network data source.
Layer files with the .lyr extension do not have a travel mode property. The individual settings making up the travel mode will be preserved in the saved layer, but the name of the travel mode used for the analysis will be lost.
The following code snippet shows how to save the analysis results as a layer package using the saveAsLayerFile method.
# Solve the analysis result = route.solve() # Save the results to a layer package for debugging purposes out_lpkx = "C:/data/RouteAnalysis.lpkx" result.saveAsLayerFile(out_lpkx)
To successfully run the saveAsLayerFile method, the allowSaveLayerFile property on the solver object must be set to True.
Learn more about the saveAsLayerFile method for Route analysis in the Methods section of the RouteResult object's documentation.