How To: Create Network Analyst attribute restrictions using Python
In many cases when using Network Analyst it may be desirable to restrict use of certain edges of the network. For example, only roads that can be walked along or only roads that allow delivery vehicles should be traversed. At ArcMap 10.1, Python can be used to create attribute restrictions. Instructions provided in this article describe the steps to create field-based attribute restrictions using Python scripting.
The following sample workflow shows how to create a restriction where two fields are evaluated. The restriction is created for edges that are both pedestrian friendly and not a highway. This is just a sample and the code can be configured to work on one field or more than two. The key is having fields in place from which to evaluate.
In this case the network dataset contains two fields with Boolean values. One field for pedestrian traffic and another describing an edge as a highway. In order for the edge to be traversed, it must be both pedestrian and not a highway.
A network dataset must be created or exist that contains fields to be used as evaluators. If creating a new network dataset, the evaluators are created in the specify attributes page. This is the same as the attributes tab of an existing network dataset properties.
Click the Add button to add a new attribute. Give the attribute a name, and choose the usage type of Restriction. Ensure that restriction usage is set to prohibited.
Highlight the newly created attribute and click the Evaluators button. Click the drop-down for type and change both From-To and To-From to 'Field'. Select the From-To attribute and click the expression button. This opens a window to add the Python field evaluator.
The Python script must now be entered for both evaluators. The following code sample shows the syntax for using a Python expression to evaluate two fields. The code returns whether an edge is restricted or not based on field values. Ensure that the Parser is set to Python.
Code: def PedestrianOnly(pedestrian, highway): restricted = False if pedestrian in ('N'): restricted = True elif highway in ('Y'): restricted = True return restricted
Click the Verify button to ensure the syntax is correct. Use the same expression for both From-To and To-From directions.
Apply the settings and finish the Network Dataset wizard or apply the settings to the existing network dataset. Build or rebuild the network dataset if necessary. The new attribute restriction can now be tested. Add the network dataset to a map document and use the Identify tool on an edge that should not be prohibited due to the restriction.
When solving a network analysis layer, this restriction can be turned on and off in the solver layer properties.