ODCostMatrixSolverProperties

Summary

Provides access to analysis properties from an origin-destination (OD) cost matrix network analysis layer. The GetSolverProperties function is used to obtain an ODCostMatrixSolverProperties object from an OD cost matrix network analysis layer.

Discussion

The ODCostMatrixSolverProperties object provides read and write access to all the analysis properties of an OD cost matrix network analysis layer. The object can be used to modify the desired analysis properties of the OD cost matrix layer, and the corresponding layer can be resolved to determine the appropriate results. A new OD cost matrix layer can be created using the Make OD Cost Matrix Analysis Layer geoprocessing tool. Obtaining the ODCostMatrixSolverProperties object from a new OD cost matrix layer allows you to reuse the existing layer for subsequent analyses rather than create a layer for each analysis, which can be slow.

After modifying the properties on the ODCostMatrixSolverProperties object, the corresponding layer can be immediately used with other functions and geoprocessing tools. There is no refresh or update of the layer required to honor the changes modified through the object.

Properties

PropertyExplanationData Type
accumulators
(Read and Write)

Provides the ability to get or set a list of network cost attributes that are accumulated as part of the analysis. An empty list, [], indicates that no cost attributes are accumulated.

String
attributeParameters
(Read and Write)

Provides the ability to get or set the parameterized attributes to be used in the analysis. The property returns a Python dictionary. The dictionary key is a two-value tuple consisting of the attribute name and the parameter name. The value for each item in the dictionary is the parameter value.

Parameterized network attributes are used to model some dynamic aspect of an attribute's value. For example, a tunnel with a height restriction of 12 feet can be modeled using a parameter. In this case, the vehicle's height in feet should be specified as the parameter value. If the vehicle is taller than 12 feet, this restriction will then evaluate to True, thereby restricting travel through the tunnel. Similarly, a bridge could have a parameter to specify a weight restriction.

Attempting to modify the attributeParameters property in place won't result in updated values. Instead, you should always use a new dictionary object to set values for the property. The following two code blocks demonstrate the difference between these two approaches.

Do not attempt to modify the attributeParameters property in place; this coding method will not work.


solverProps.attributeParameters[('HeightRestriction', 'RestrictionUsage')] = "PROHIBITED"

Modify the attributeParameters property using a new dictionary object.


params = solverProps.attributeParameters
params[('HeightRestriction', 'RestrictionUsage')] = "PROHIBITED"
solverProps.attributeParameters = params
If the network analysis layer does not have parameterized attributes, this property returns None.

Dictionary
defaultCutoff
(Read and Write)

Provides the ability to get or set the default impedance value at which to cut off searching for destinations for a given origin. A value of None is used to specify that no cutoff should be used.

Double
defaultTargetDestinationCount
(Read and Write)

Provides the ability to get or set the number of destinations to find for each origin. A value of None is used to specify that all destinations should be found.

Integer
ignoreInvalidLocations
(Read and Write)

Specifies whether invalid input locations will be ignored. Typically, locations are invalid if they cannot be located on the network. When invalid locations are ignored, the solver will skip them and attempt to perform the analysis using the remaining locations.

  • SKIPInvalid input locations will be ignored so that the analysis will succeed using only valid locations. This value may also be specified using a Boolean value of True.
  • HALTInvalid locations will not be ignored and will cause the analysis to fail. This value may also be specified using a Boolean value of False.
String
impedance
(Read and Write)

Provides the ability to get or set the network cost attribute used as impedance. This cost attribute is minimized while determining the routes between origins and destinations.

String
outputPathShape
(Read and Write)

Controls whether a straight line is generated between each origin-destination pair in the output OD cost matrix. The following is a list of possible values:

  • NO_LINESNo shape is generated between origin-destination pairs. This is useful when you have a large number of origins and destinations and are interested only in the OD cost matrix table (and not the output line shapes).
  • STRAIGHT_LINESA single straight line between each of the origin-destination pairs is generated.
String
restrictions
(Read and Write)

Provides the ability to get or set a list of restriction attributes that are applied for the analysis. An empty list, [], indicates that no restriction attributes are used for the analysis.

String
solverName
(Read Only)

Returns the name of the solver being referenced by the network analysis layer used to obtain the solver properties object. The property always returns the string value OD Cost Matrix Solver when accessed from a ODCostMatrixSolverProperties object.

String
timeOfDay
(Read and Write)

Provides the ability to get or set the departure date and time from the origins. A value of None can be used to specify that no date and time should be used.

Instead of using a particular date, a day of the week can be specified using the following dates:

  • Today—12/30/1899
  • Sunday—12/31/1899
  • Monday—1/1/1900
  • Tuesday—1/2/1900
  • Wednesday—1/3/1900
  • Thursday—1/4/1900
  • Friday—1/5/1900
  • Saturday—1/6/1900

For example, to specify that the route from each origin should start at 8:00 a.m. on Wednesday, specify the value as datetime.datetime(1900, 1, 3, 8,0,0).

The timeZoneUsage parameter specifies whether the date and time refer to UTC or the time zone in which the orders are located.

DateTime
timeZoneUsage
(Read and Write)

Specifies the time zone of the timeOfDay parameter.

  • GEO_LOCALThe timeOfDay parameter refers to the time zone in which the origins are located.
  • UTCThe timeOfDay parameter refers to Coordinated Universal Time (UTC). Choose this option if you want to solve the analysis for a specific time, such as now, but aren't certain in which time zone the origins will be located.

When solving an OD cost matrix analysis that spans across multiple time zones and a start time is set, all origins must be in the same time zone.

String
travelMode
(Read Only)

Accesses the travel mode set on a network analysis layer as an arcpy.na.TravelMode object.

Object
useHierarchy
(Read and Write)

Controls the use of the hierarchy attribute while performing the analysis. The following is a list of possible values:

  • USE_HIERARCHY Use the hierarchy attribute for the analysis. Using a hierarchy results in the solver preferring higher-order edges to lower-order edges. Hierarchical solves are faster, and they can be used to simulate the preference of a driver who chooses to travel on freeways over local roads when possible—even if that means a longer trip. This option is applicable only if the network dataset referenced by the Network Analyst layer has a hierarchy attribute. A value of True can also be used to specify this option.
  • NO_HIERARCHYDo not use the hierarchy attribute for the analysis. Not using a hierarchy yields an exact route for the network dataset. A value of False can also be used to specify this option.
String
uTurns
(Read and Write)

Provides the ability to get or set the policy that indicates how the U-turns at junctions that could occur during network traversal between stops are being managed by the solver. The following is a list of possible values:

  • ALLOW_UTURNSU-turns are permitted at junctions with any number of connected edges.
  • NO_UTURNSU-turns are prohibited at all junctions, regardless of junction valency. Note that U-turns are still permitted at network locations even when this setting is chosen; however, you can set the individual network locations' CurbApproach property to prohibit U-turns there as well.
  • ALLOW_DEAD_ENDS_ONLYU-turns are prohibited at all junctions, except those that have only one adjacent edge (a dead end).
  • ALLOW_DEAD_ENDS_AND_INTERSECTIONS_ONLYU-turns are prohibited at junctions where exactly two adjacent edges meet but are permitted at intersections (junctions with three or more adjacent edges) and dead ends (junctions with exactly one adjacent edge). Often, networks have extraneous junctions in the middle of road segments. This option prevents vehicles from making U-turns at these locations.
String

Method Overview

MethodExplanation
applyTravelMode (travel_mode)

Updates the analysis properties of a network analyst layer based on a travel mode object. The updated network analyst layer can then be solved to complete the analysis.

Methods

applyTravelMode (travel_mode)
ParameterExplanationData Type
travel_mode

A variable that references a travel mode object derived from a network dataset. A list of travel mode objects can be obtained by calling the arcpy.na.GetTravelModes function.

Object

When a network analyst layer is created, it is assigned default values for all of its analysis properties. The individual analysis properties can be updated using a solver properties object obtained from the network analyst layer. A travel mode stores a predefined set of analysis settings that help to perform a particular analysis, such as a walking time travel mode that stores the analysis settings required to perform a time-based walking analysis.

Using the applyTravelMode method, all the analysis settings that are defined in a travel mode can be applied at once. After the analysis properties are updated, the network analyst layer can be solved to complete the analysis.

If there is an error when updating the solver properties, such as when the provided travel mode references properties that don't exist on the current network dataset or references properties that are no longer applicable to the network dataset that was used to create the network analyst layer corresponding to the solver properties object, no exceptions are raised. The method will execute successfully, but you will get errors when you try to solve such a network analyst layer.

If the travel_mode parameter does not reference a travel mode object or a string, a TypeError exception is raised. If the travel_mode parameter references a string and the string cannot be internally converted to a valid string representation of a travel mode object, a ValueError exception is raised.

Code sample

ODCostMatrixSolverProperties example 1 (stand-alone script)

The script shows how to update the OD Cost Matrix solver properties through a full workflow. This is based on the tutorial network dataset of the San Francisco region.

# Name: ODSolverProperties_Workflow.py
# Description: Finds the full matrix of destribution centers and stores and then
#              a second solve that only finds the closest destribution center
# Requirements: Network Analyst Extension

# Import system modules
import arcpy
from arcpy import env
import os
import datetime

try:
    # Check out the Network Analyst license if available.
    # Fail if the Network Analyst
    # license is not available.
    if arcpy.CheckExtension("network") == "Available":
        arcpy.CheckOutExtension("network")
    else:
        raise arcpy.ExecuteError("Network Analyst Extension license is not available.")

    # Set environment settings
    output_dir = "C:\Data"

    # The NA layer's data will be saved to the workspace specified here
    env.workspace = os.path.join(output_dir, "Output.gdb")
    env.overwriteOutput = True

    # Set local variables
    input_gdb = "C:/Data/SanFrancisco.gdb"
    network = os.path.join(input_gdb, "Transportation", "Streets_ND")
    layer_name = "StoresToDCs"
    travel_mode = "Driving Time"
    in_origins = os.path.join(input_gdb, "Analysis/Stores")
    in_destinations = os.path.join(input_gdb, "Analysis/CentralDepots")
    full_matrix = os.path.join(output_dir, "Output.gdb", "full_matrix")
    closest_DC = os.path.join(output_dir, "Output.gdb", "closest_DC")

    # Create a new Origin Destination layer.
    result_object = arcpy.na.MakeODCostMatrixAnalysisLayer(network,
                                        layer_name, travel_mode)

    # Get the layer object form the result object. The origin destination layer
    # can now be referenced using the layer object.
    layer_object = result_object.getOutput(0)

    # Get the names of all the sublayers within the Origin Destination layer.
    sub_layer_names = arcpy.na.GetNAClassNames(layer_object)
    # Store the layer names that we will use later
    origins_layer_name = sub_layer_names["Origins"]
    destination_layer_name = sub_layer_names["Destinations"]
    lines_layer_name = sub_layer_names["ODLines"]

    # Load the stores locations as origins.
    arcpy.na.AddLocations(layer_object, origins_layer_name, in_origins,
                          "", "")

    # Load the distribution center location as destinations
    arcpy.na.AddLocations(layer_object, destination_layer_name, in_destinations,
                          "", "")

    # Solve the origin destination layer
    arcpy.na.Solve(layer_object)

    # Save the resulting line sublayer
    arcpy.management.CopyFeatures(lines_layer_name, full_matrix)

    # Get the solver properties object from the origin destination layer
    solverProps = arcpy.na.GetSolverProperties(layer_object)

    # Update the destination count to 1
    solverProps.defaultTargetDestinationCount = 1

    # Solve the origin destination layer
    arcpy.na.Solve(layer_object)

    # Save the resulting line sublayer
    arcpy.management.CopyFeatures(lines_layer_name, closest_DC)


except Exception as e:
    # If an error occurred, print line number and error message
    import traceback
    import sys
    tb = sys.exc_info()[2]
    print ("An error occurred on line %i" % tb.tb_lineno)
    print (str(e))
ApplyTravelMode example 2 (Python window)

This script shows how to apply the TruckingTime travel mode to an existing layer.

#Get the OD cost matrix layer object from a layer named "OD Matrix" in
#the map
doc = arcpy.mp.ArcGISProject('current')
map_obj = doc.listMaps()[0]
od_layer = map_obj.listLayers('OD Matrix')[0]

#Get the Trucking Time travel mode from the network dataset
desc = arcpy.Describe(od_layer)
travel_modes = arcpy.na.GetTravelModes(desc.network.catalogPath)
trucking_mode = travel_modes["Trucking Time"]

#Apply the travel mode to the analysis layer
solver_properties = arcpy.na.GetSolverProperties(od_layer)
solver_properties.applyTravelMode(trucking_mode)