You can perform network analysis in Python using the Network Analyst module, arcpy.nax. This module provides access to six analysis types. For each analysis (also known as a solver), you work with two objects specific to that analysis: an object that allows you to initialize the analysis, set analysis settings, load the inputs, and execute the analysis and another object that allows you to work with the analysis results after you execute the analysis.
You can modify the analysis settings, such as the travel mode, using the properties of analysis object. The analysis object also has several methods used for loading inputs and executing the analysis. The result object has methods that allow you to access and save the analysis outputs.
See a complete list of properties and methods for the ServiceArea analysis object and the ServiceAreaResult result object.
Remarque :
This topic uses Service Area analysis as an example; however, the information provided here can be applied to any type of network analysis.
To perform network analysis, follow this five-step workflow:
- Initialize the analysis.
- Set properties for the analysis.
- Load the inputs.
- Solve the analysis.
- Work with the results.
Initialize the analysis
To initialize the analysis, you must specify a road network. The options for the road network are as follows:
- A network dataset—This option requires you to have the ArcGIS Network Analyst extension and a network dataset. You can specify the network dataset using its full catalog path or the name of a network dataset layer created from a network dataset using the MakeNetworkDatasetLayer function.
- A portal service—This option requires an ArcGIS Online account with routing privileges and sufficient credits or a portal with ArcGIS Enterprise routing service configured. You must be signed in to the portal before running the script.
Conseil :
For best performance when using a network dataset, use the name of a network dataset layer when initializing your analysis. If you use a catalog path, the network dataset is opened each time the analysis is initialized. Opening a network dataset is time-consuming, as datasets contain advanced data structures and tables that are read and cached. A network dataset layer opens the dataset one time and performs better when the same layer is used.
The following code snippet shows how to initialize a service area analysis using a network dataset:
import arcpy
nd_path = "C:/data/NorthAmerica.gdb/Routing/Routing_ND"
nd_layer_name = "NorthAmerica"
# Create a network dataset layer. The layer will be referenced using its name.
arcpy.nax.MakeNetworkDatasetLayer(nd_path, nd_layer_name)
# Instantiate a ServiceArea analysis object.
service_area = arcpy.nax.ServiceArea(nd_layer_name)
The following code snippet shows how to initialize a service area analysis using ArcGIS Online:
import arcpy
# Instantiate a ServiceArea analysis object.
service_area = arcpy.nax.ServiceArea("https://www.arcgis.com/")
The following code snippet shows how to initialize a service area analysis using a portal with ArcGIS Enterprise routing services configured:
import arcpy
# Instantiate a ServiceArea analysis object.
service_area = arcpy.nax.ServiceArea("https://myportal.mysite.com/portal/")
Set properties for the analysis
After you initialize the analysis, you must set the properties for the analysis. To determine the properties that are supported by an analysis object and understand how a given property can influence the analysis, see the help topic for the particular object (available under the Classes category in the help). Many of the properties are set using Python enumeration objects, a set of symbolic names representing some constant values. For example, the time units for the analysis can be set to minutes using the Minutes member from the arcpy.nax.TimeUnits enumeration. The help topic for an object describes whether a property needs to be set as an enumeration. It also specifies the name of the enumeration to use.
The most important property to set for any analysis is the travel mode used for the analysis. To set the travel mode, you first must get the list of travel modes supported by the network dataset. Use the GetTravelModes function to do this. From the supported list, select the travel mode for the analysis using its name. The GetTravelModes function returns a dictionary in which key is the travel mode name and value is the travel mode object.
The following code snippet shows how to set the properties for the service area analysis. The snippet assumes that you have already initialized the service area analysis object.
# Get the desired travel mode for the analysis.
nd_travel_modes = arcpy.nax.GetTravelModes(nd_layer_name)
travel_mode = nd_travel_modes["Driving Time"]
# Set properties.
service_area.timeUnits = arcpy.nax.TimeUnits.Minutes
service_area.defaultImpedanceCutoffs = [5, 10, 15]
service_area.travelMode = travel_mode
service_area.outputType = arcpy.nax.ServiceAreaOutputType.Polygons
service_area.geometryAtOverlap = arcpy.nax.ServiceAreaOverlapGeometry.Split
Load the inputs
Before you execute the analysis, you must load the input locations to be used for the analysis. Depending on your analysis type, you must load one or more locations into one or more input data types supported by that analysis. For example, to perform service area analysis, you must load one or more facilities—locations around which you generate service area polygons and service area lines—into the ServiceAreaInputDataType.Facilities data type.
You can load the inputs in two ways. If your input locations are already available as feature classes or tables, you can load them using the load method on the analysis object. If you are reading input locations from other data sources, such as a .csv file containing latitude and longitude values, or if you have input locations as geometry objects, you can load the inputs using the insertCursor method on the analysis object.
Solve the analysis
To execute the analysis, call the solve method. This method returns a result object that can be used to work with results from the analysis, such as exporting the results to a feature class.
If the analysis did not find any results—for example, if a route cannot be found between your input stops—the solve method does not raise a Python exception. To determine whether the analysis produced a valid result, use the solveSucceeded property of the result object.
Licence :
If your network data source is a local network dataset and not a URL to a service, an ArcGIS Network Analyst extension license is required for solve to be successful. Check out the license before you call the solve method.
The following code snippet shows how to execute an analysis:
# Check out the Network Analyst extension license.
arcpy.CheckOutExtension("network")
# Solve the analysis.
result = service_area.solve()
Work with the results
After executing the analysis, use the result object obtained from the solve method to work with the results. The result object allows you to determine whether the analysis succeeded or failed (using the solveSucceeded property) and whether the analysis produced a partial solution (using the isPartialSolution property). If the analysis failed, you can determine why using the solverMessages method.
The following code snippet shows how to execute the analysis and export the results to a feature class:
# Solve the analysis.
result = service_area.solve()
# Export the results to a feature class. If the analysis failed print all the
# messages.
if result.solveSucceeded:
result.export(arcpy.nax.ServiceAreaOutputDataType.Polygons, output_polygons)
else:
arcpy.AddError("Analysis failed")
# Print all the warning messages.
for message in result.solverMessages(arcpy.nax.MessageSeverity.Warning):
arcpy.AddWarning(message[-1])
# Print all the error messages.
for message in result.solverMessages(arcpy.nax.MessageSeverity.Error):
arcpy.AddError(message[-1])
There are several ways to access and work with the results of an analysis. For example, you can export your results to a feature class or read through the rows one by one using a cursor.
Vous avez un commentaire à formuler concernant cette rubrique ?