Summary
Makes an origin–destination (OD) cost matrix network analysis layer and sets its analysis properties. An OD cost matrix analysis layer is useful for representing a matrix of costs going from a set of origin locations to a set of destination locations.
Usage
After creating the analysis layer with this tool, you can add network analysis objects to it using the Add Locations tool, solve the analysis using the Solve tool, and save the results on disk using the Save To Layer File tool.
-
When using this tool in geoprocessing models, if the model is run as a tool, the output network analysis layer must be made a model parameter; otherwise, the output layer is not added to the contents of the map.
Syntax
MakeODCostMatrixLayer(in_network_dataset, out_network_analysis_layer, impedance_attribute, {default_cutoff}, {default_number_destinations_to_find}, {accumulate_attribute_name}, {UTurn_policy}, {restriction_attribute_name}, {hierarchy}, {hierarchy_settings}, {output_path_shape}, {time_of_day})
Parameter | Explanation | Data Type |
in_network_dataset | The network dataset on which the OD cost matrix analysis will be performed. | Network Dataset Layer |
out_network_analysis_layer | Name of the OD cost matrix network analysis layer to create. | String |
impedance_attribute | The cost attribute to be used as impedance in the analysis. | String |
default_cutoff (Optional) | Default impedance value at which to cut off searching for destinations for a given origin. If the accumulated impedance becomes higher than the cutoff value, the traversal stops. The default can be overridden by specifying the cutoff value on the origins. | Double |
default_number_destinations_to_find (Optional) | Default number of destinations to find for each origin. The default can be overridden by specifying a value for the TargetDestinationCount property on the origins. | Long |
accumulate_attribute_name [accumulate_attribute_name,...] (Optional) | A list of cost attributes to be accumulated during analysis. These accumulation attributes are purely for reference; the solver only uses the cost attribute specified by the Impedance Attribute parameter to calculate the route. For each cost attribute that is accumulated, a Total_[Impedance] property is added to the routes that are output by the solver. | String |
UTurn_policy (Optional) | The U-Turn policy at junctions. Allowing U-turns implies the solver can turn around at a junction and double back on the same street. Given that junctions represent street intersections and dead ends, different vehicles may be able to turn around at some junctions but not at others—it depends on whether the junction represents an intersection or dead end. To accommodate this, the U-turn policy parameter is implicitly specified by how many edges connect to the junction, which is known as junction valency. The acceptable values for this parameter are listed below; each is followed by a description of its meaning in terms of junction valency.
If you need a more precisely defined U-turn policy, consider adding a global turn delay evaluator to a network cost attribute, or adjusting its settings if one exists, and pay particular attention to the configuration of reverse turns. Also, look at setting the CurbApproach property of your network locations. | String |
restriction_attribute_name [restriction_attribute_name,...] (Optional) | A list of restriction attributes to apply during the analysis. | String |
hierarchy (Optional) |
The parameter is not used if a hierarchy attribute is not defined on the network dataset used to perform the analysis. In such cases, use "#" as the parameter value. | Boolean |
hierarchy_settings (Optional) | Network Analyst Hierarchy Settings | |
output_path_shape (Optional) |
No matter which output shape type is chosen, the best route is always determined by the network impedance, never Euclidean distance. This means only the route shapes are different, not the underlying traversal of the network. | String |
time_of_day (Optional) | Indicates the departure time from origins. If you have chosen a traffic-based impedance attribute, the solution will be generated given dynamic traffic conditions at the time of day specified here. A date and time can be specified as 5/14/2012 10:30 AM. Instead of using a particular date, a day of the week can be specified using the following dates:
| Date |
Derived Output
Name | Explanation | Data Type |
output_layer | The newly created network analysis layer. | Network Analyst Layer |
Code sample
MakeODCostMatrixLayer example 1 (Python window)
Execute the tool using only the required parameters.
network = "C:/Data/Paris.gdb/Transportation/ParisMultimodal_ND"
arcpy.na.MakeODCostMatrixLayer(network, "DrivetimeCosts", "DriveTime")
MakeODCostMatrixLayer example 2 (Python window)
Execute the tool using all parameters.
network = "C:/Data/Paris.gdb/Transportation/ParisMultimodal_ND"
arcpy.na.MakeODCostMatrixLayer(network, "DrivetimeCosts", "DriveTime", 10, 20,
["Meters", "DriveTime"], "NO_UTURNS",
["Oneway"], "USE_HIERARCHY", "", "NO_LINES")
MakeODCostMatrixLayer example 3 (workflow)
The following stand-alone Python script demonstrates how the MakeODCostMatrixLayer tool can be used to create an origin-destination cost matrix for delivery of goods from the warehouses to all stores within a 10-minute drive time.
# Name: MakeODCostMatrixLayer_Workflow.py
# Description: Create an origin-destination cost matrix for delivery of goods
# from the warehouses to all stores within a 10-minute drive time
# and save the results to a layer file on disk. Such a matrix can
# be used as an input for logistics, delivery and routing analyses.
# Requirements: Network Analyst Extension
#Import system modules
import arcpy
from arcpy import env
try:
#Check out the Network Analyst extension license
arcpy.CheckOutExtension("Network")
#Set environment settings
env.workspace = "C:/data/Paris.gdb"
env.overwriteOutput = True
#Set local variables
inNetworkDataset = "Transportation/ParisMultimodal_ND"
outNALayerName = "WarehouseToStoreDrivetimeMatrix"
impedanceAttribute = "Drivetime"
searchTolerance = "1000 Meters"
accumulateAttributeName = ["Meters"]
inOrgins = "Analysis/Warehouses"
inDestinations = "Analysis/Stores"
outLayerFile = "C:/data/output" + "/" + outNALayerName + ".lyr"
#Create a new OD Cost matrix layer. We wish to find all stores within a 10
#minute cutoff. Apart from finding the drive time to the stores, we also
#want to find the total distance. So we will accumulate the "Meters"
#impedance attribute.
outNALayer = arcpy.na.MakeODCostMatrixLayer(inNetworkDataset, outNALayerName,
impedanceAttribute, 10, "",
accumulateAttributeName)
#Get the layer object from the result object. The OD cost matrix layer can
#now be referenced using the layer object.
outNALayer = outNALayer.getOutput(0)
#Get the names of all the sublayers within the OD cost matrix layer.
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
#Stores the layer names that we will use later
originsLayerName = subLayerNames["Origins"]
destinationsLayerName = subLayerNames["Destinations"]
#Load the warehouse locations as origins using a default field mappings and
#a search tolerance of 1000 Meters.
arcpy.na.AddLocations(outNALayer, originsLayerName, inOrgins, "",
searchTolerance)
#Load the store locations as destinations and map the NOM field from stores
#features as Name property using field mappings
fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, destinationsLayerName)
fieldMappings["Name"].mappedFieldName = "NOM"
arcpy.na.AddLocations(outNALayer, destinationsLayerName, inDestinations,
fieldMappings, searchTolerance)
#Solve the OD cost matrix layer
arcpy.na.Solve(outNALayer)
#Save the solved OD cost matrix layer as a layer file on disk with relative
#paths
arcpy.management.SaveToLayerFile(outNALayer,outLayerFile,"RELATIVE")
print "Script completed successfully"
except Exception as e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print "An error occurred on line %i" % tb.tb_lineno
print str(e)
MakeODCostMatrixLayer example 4 (workflow)
The following stand-alone Python script demonstrates how to access sublayers, join input and output layers, and transfer field values from input origins and destinations to the output Lines layer.
# Name: MakeODCostMatrixLayer_Workflow2.py
# Description: Find the travel time to the closest hospital from each census
# tract and join the travel time and hospital name to the input
# tracts.
# Requirements: Network Analyst Extension
import datetime
#Import system modules
import arcpy
from arcpy import env
try:
#Check out the Network Analyst extension license
arcpy.CheckOutExtension("Network")
#Set environment settings
env.workspace = "C:/Data/SanFrancisco.gdb"
env.overwriteOutput = True
#Set inputs and outputs
inNetworkDataset = "Transportation/Streets_ND"
inOrigins = "Analysis/TractCentroids"
inDestinations = "Analysis/Hospitals"
outNALayerName = "HospitalsOD"
outTracts_withOD = "Analysis/TractCentroids_withOD"
#Define some OD cost matrix analysis settings
#Optimize based on travel time
impedanceAttribute = "TravelTime"
#Calculate the total distance, even though the analysis is optimizing time
accumulate_attrs = ["Meters"]
#Find only the closest hospital
num_hospitals_to_find = 1
#Set the time of day for the analysis to 6PM on a generic Monday.
start_time = datetime.datetime(1900, 1, 1, 18, 0, 0)
#Don't output line shapes (output Lines will still list travel times)
out_lines = "NO_LINES"
#Create a new OD cost matrix layer.
outODResultObject = arcpy.na.MakeODCostMatrixLayer(inNetworkDataset,
outNALayerName, impedanceAttribute,
default_number_destinations_to_find=num_hospitals_to_find,
accumulate_attribute_name=accumulate_attrs,
output_path_shape=out_lines, time_of_day=start_time)
#Get the layer object from the result object. The OD layer can
#now be referenced using the layer object.
outNALayer = outODResultObject.getOutput(0)
#Get the names of all the sublayers within the OD layer.
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
#Store the layer names for later use
originsLayerName = subLayerNames["Origins"]
destinationsLayerName = subLayerNames["Destinations"]
#The input census tract data has a unique ID field that can be transferred
#to the analysis layer. Add the field, and then use field mapping to
#transfer the values.
arcpy.na.AddFieldToAnalysisLayer(outNALayer, originsLayerName,
"Tract_ID", "TEXT")
fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, originsLayerName)
fieldMappings["Tract_ID"].mappedFieldName = "ID"
#Load the census tracts as origins.
arcpy.na.AddLocations(outNALayer, originsLayerName, inOrigins,
fieldMappings, "",
exclude_restricted_elements = "EXCLUDE")
#Map the input hospital NAME field to a new Hospital_Name field in
#Destinations
arcpy.na.AddFieldToAnalysisLayer(outNALayer, destinationsLayerName,
"Hospital_Name", "TEXT")
fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer,
destinationsLayerName)
fieldMappings["Hospital_Name"].mappedFieldName = "NAME"
#Load the hospitals as desinations.
arcpy.na.AddLocations(outNALayer, destinationsLayerName, inDestinations,
fieldMappings, "",
exclude_restricted_elements = "EXCLUDE")
#Solve the OD layer
arcpy.na.Solve(outNALayer)
#Get sublayers
#arcpy.mapping.ListLayers returns a list of layer objects containing the NA
#layer itself (item 0) and each of the sublayers. Put these in a dictionary
#with the sublayer names as the keys
subLayers = dict((lyr.datasetName, lyr) for lyr in arcpy.mapping.ListLayers(outNALayer)[1:])
OriginsSubLayer = subLayers["Origins"]
DestinationsSubLayer = subLayers["Destinations"]
LinesSubLayer = subLayers["ODLines"]
#Transfer the tract ID from the input Origins to the output Lines
arcpy.management.JoinField(LinesSubLayer, "OriginID",
OriginsSubLayer, "ObjectID", "Tract_ID")
#Transfer the hospital name from the input Destinations to the output Lines
arcpy.management.JoinField(LinesSubLayer, "DestinationID",
DestinationsSubLayer, "ObjectID", "Hospital_Name")
#Transfer fields of interest (hospital name, TravelTime cost, and other
#accumulated costs) from the output Lines to the input census tracts
#feature class using the Tract_ID field
output_impedance_fieldname = "Total_" + impedanceAttribute
fields_to_transfer = ["Hospital_Name", output_impedance_fieldname]
for field in accumulate_attrs:
fields_to_transfer.append("Total_" + field)
arcpy.management.CopyFeatures(inOrigins, outTracts_withOD)
arcpy.management.JoinField(outTracts_withOD, "ID",
LinesSubLayer, "Tract_ID", fields_to_transfer)
print "Script completed successfully"
except Exception as e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print "An error occurred on line %i" % tb.tb_lineno
print str(e)
Environments
Licensing information
- Basic: Yes
- Standard: Yes
- Advanced: Yes