NAClassFieldMappings

Zusammenfassung

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

Auswertung

The NAClassFieldMappings object or its string representation is used as input to the field_mappings parameter in the Add Locations geoprocessing tool. 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.

Syntax

NAClassFieldMappings
 (network_analyst_layer, sub_layer_name, {use_location_fields}, {list_candidate_fields})
ParameterErklärungDatentyp
network_analyst_layer

A variable that references a Layer object obtained from a network analysis layer. It can be derived from existing layers in a map document or by specifying the catalog path to the network analysis layer file as an argument to the Layer class. The isNetworkAnalystLayer property on the Layer object can be used to identify if a given Layer object is a network analysis layer.

Layer
sub_layer_name

The sublayer name for which the field mappings are to be created. The name must be valid for the particular network analysis layer type. For a given network analysis layer, the sublayer name can be determined using the GetNAClassNames function.

String
use_location_fields

Specifies whether to create the field mappings for the network location properties along with other properties.

(Der Standardwert ist False)

Boolean
list_candidate_fields
[list_candidate_fields,...]

A list of Field objects that are used to generate the mapped field names. The argument value can be obtained from a given feature class or table using the ListFields function. If the argument is not specified, then the field mappings are created with only the default values for the appropriate properties.

(Der Standardwert ist None)

Field

Codebeispiel

NAClassFieldMappings Example 1 (Python window)

The following script shows how to load fire stations as facilities into an existing service area layer and specify a delay of 10 minutes when loading the facilities using the NAClassFieldMappings object. It assumes that a service area network analysis layer called Fire Stations Coverage, created from the tutorial network dataset for the San Francisco region, and a feature layer called FireStations are already added to an existing map document.

#Get the service area layer called "Fire Stations Coverage" from the map
doc = arcpy.mp.ArcGISProject('current')
map_obj = doc.listMaps()[0]
sa_layer = map_obj.listLayers("Fire Stations Coverage")[0]

#Get the list of fields from the FireStations feature layer in the map
fields = arcpy.ListFields("FireStations")

#Get the facilities sublayer name from the service area layer. Note that we are 
#not using a string called "Facilities" because the sublayer name can be
#different if using ArcGIS on a non-English operating system.
facilities_sublayer_name = arcpy.na.GetNAClassNames(sa_layer)["Facilities"]

#Create a field mappings object for facilities sublayer based on the fields from
#FireStations layer
field_mappings = arcpy.na.NAClassFieldMappings(sa_layer, 
                                        facilities_sublayer_name, False, fields)

#Get the field map corresponding to the "Attr_TravelTime" property of facilities
field_map = field_mappings["Attr_TravelTime"]

#Set a delay of 10 minutes for the facilities
field_map.defaultValue = 10

#Load the fire stations as service area facilities using the field mappings
arcpy.na.AddLocations(sa_layer, facilities_sublayer_name, "FireStations",
                        field_mappings)
NAClassFieldMappings Example 2 (workflow)

The example shows how to find the best route between some store locations considering weather conditions as slowdown areas. It illustrates how to use the NAClassFieldMappings object while loading the weather polygons as polygon barriers and store locations as stops using the Add Locations tool.

# Name: NAClassFieldMappings_workflow_01.py
# Description: Find the fastest route to visit several stores when part of the
#              road network has been slowed due to bad weather. Use
#              NAClassFieldMappings to map fields from the input data and to
#              set default values for some parameters.
# Requirements: Network Analyst Extension

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

try:
    
    #Check out the Network Analyst extension license

    #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")
    polygon_barriers = os.path.join(input_gdb, "Analysis",
                                    "WeatherSlowDownAreas")
    stops = os.path.join(input_gdb, "Analysis", "Stores")
    travel_mode = "Driving Time"
    layer_name = "WeatherRoute"
    output_layer_file = os.path.join(output_dir, layer_name + ".lyrx")
    
    #Create a new route layer
    route_layer = arcpy.na.MakeRouteAnalysisLayer(network, layer_name,
                                         travel_mode).getOutput(0)
    
    #Get na class names based on the layer
    na_classes = arcpy.na.GetNAClassNames(route_layer, "INPUT")
    
    #Create field mappings for loading barriers as scaled cost polygon barriers
    #with a slow down of 40%
    field_mappings = arcpy.na.NAClassFieldMappings(route_layer,
                                                  na_classes["PolygonBarriers"])
    field_mappings["BarrierType"].defaultValue = 1
    #Figure out the layer's impedance attribute
    solver_props = arcpy.na.GetSolverProperties(route_layer)
    impedance = solver_props.impedance
    field_mappings["Attr_" + impedance].defaultValue = 1.4
    #Load weather polygons as slow down barriers
    arcpy.na.AddLocations(route_layer, na_classes["PolygonBarriers"],
                              polygon_barriers, field_mappings)
    
    #Get a list of field objects from the stores feature class
    stores_fields = arcpy.ListFields(stops)
    #Create field mappings for loading stops based on the field names from the
    #stores
    stops_field_mappings = arcpy.na.NAClassFieldMappings(route_layer,
                                                na_classes["Stops"],
                                                False, stores_fields)
    
    #Load stops using the field mappings
    arcpy.na.AddLocations(route_layer, na_classes["Stops"], stops,
                            stops_field_mappings)
    
    #Solve the route
    arcpy.na.Solve(route_layer)
    
    #Save the solved service area layer as a layer file on disk
    route_layer.saveACopy(output_layer_file)

    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 occured on line %i" % tb.tb_lineno)
    print(str(e))