NAClassFieldMappings

Summary

Provides a dictionary of NAClassFieldMap objects that are used to map field names or set default values for the properties of a network analysis input class. The dictionary keys are the network analysis class property names, and the values are the NAClassFieldMap objects.

Discussion

The NAClassFieldMappings object is obtained using the fieldMappings method on a network analysis solver class object and can be used as input to the field_mappings parameter of the load method. The NAClassFieldMap objects contained within the NAClassFieldMappings object provide access to get or set the default value and the mapped field name associated with each property of the network analysis class.

Learn more about best practices for setting up analysis inputs

Code sample

The following script shows how to use the NAClassFieldMappings and NAClassFieldMap objects to control load behavior in an origin destination cost matrix analysis.

# An example showing how to perform origin destination cost matrix analysis using inputs from feature classes.
import arcpy
arcpy.CheckOutExtension("network")

nds = "C:/data/NorthAmerica.gdb/Routing/Routing_ND"
nd_layer_name = "Routing_ND"
input_origins = "C:/data/io.gdb/Origins"
input_destinations = "C:/data/io.gdb/Destinations"
output_lines = "C:/data/io.gdb/ODCostMatrixLines"

# Create a network dataset layer and get the desired travel mode for analysis
arcpy.na.MakeNetworkDatasetLayer(nds, nd_layer_name)
nd_travel_modes = arcpy.na.GetTravelModes(nd_layer_name)
travel_mode = nd_travel_modes["Driving Time"]

# Pre-calculate network location fields on the inputs to speed up analysis later
search_tolerance = "5000 Meters"
search_criteria = [["Streets", "SHAPE"], ["Streets_ND_Junctions", "NONE"]]
arcpy.nax.CalculateLocations(input_origins, nds, search_tolerance, search_criteria, travel_mode=travel_mode)
arcpy.nax.CalculateLocations(input_destinations, nds, search_tolerance, search_criteria, travel_mode=travel_mode)

# Instantiate a OriginDestinationCostMatrix solver object
odcm = arcpy.nax.OriginDestinationCostMatrix(nd_layer_name)
# Set properties
odcm.travelMode = travel_mode
odcm.lineShapeType = arcpy.nax.LineShapeType.NoLine

# Load inputs using field mappings, including the pre-calculated location fields
field_mappings_origins = odcm.fieldMappings(arcpy.nax.OriginDestinationCostMatrixInputDataType.Origins, True)
# Map the "Nombre" field in the input data to the "Name" property of the Origins class
field_mappings_origins["Name"].mappedFieldName = "Nombre"
# If the "Nombre" field is blank, set the default name to "Origen"
field_mappings_origins["Name"].defaultValue = "Origen"
odcm.load(arcpy.nax.OriginDestinationCostMatrixInputDataType.Origins, input_origins, field_mappings_origins)
# Load destinations
field_mappings_destinations = odcm.fieldMappings(arcpy.nax.OriginDestinationCostMatrixInputDataType.Destinations, True)
odcm.load(arcpy.nax.OriginDestinationCostMatrixInputDataType.Destinations, input_destinations, field_mappings_destinations)

# Solve the analysis
result = odcm.solve()
# Export the results to a feature class
if result.solveSucceeded:
    result.export(arcpy.nax.OriginDestinationCostMatrixOutputDataType.Lines, output_lines)
else:
    print("Solved failed")
    print(result.solverMessages(arcpy.nax.MessageSeverity.All))