NAClassFieldMappings

描述

提供 NAClassFieldMap 对象的 Python 字典,用于映射网络分析图层内网络分析类的属性字段名称,或设置该属性的默认值。字典关键字是网络分析类属性名称,值为 NAClassFieldMap 对象。

讨论

添加位置地理处理工具中,NAClassFieldMappings 对象或其字符串用作 field_mappings 参数的输入。NAClassFieldMappings 对象中所包含的 NAClassFieldMap 对象提供获取或设置网络分析类各属性的默认值以及对与属性关联的映射字段名称的访问权。

语法

NAClassFieldMappings
 (network_analyst_layer, sub_layer_name, {use_location_fields}, {list_candidate_fields})
参数说明数据类型
network_analyst_layer

该变量引用一个从网络分析图层获取的 Layer 对象。该对象可以源自地图文档中的现有图层或向网络分析图层文件指定目录路径作为 Layer 类的参数。可以使用 Layer 对象上的 isNetworkAnalystLayer 属性来确认某个给定的 Layer 对象是否为网络分析图层。

Layer
sub_layer_name

将要创建字段映射的子图层名称。名称必须针对特定网络分析图层类型有效。对于给定的网络分析图层,可使用 GetNAClassNames 功能确定子图层名称。

String
use_location_fields

指定是否为网络位置属性与其他属性一起创建字段映射。

(默认值为 False)

Boolean
list_candidate_fields
[list_candidate_fields,...]

用于生成映射字段名称的 Field 对象列表。可以使用 ListFields 功能从给定的要素类或表中获取参数值。如果未指定参数,则仅使用适当属性的默认值创建字段映射。

(默认值为 None)

Field

代码示例

NAClassFieldMappings 示例 1(Python 窗口)

以下脚本显示如何将消防站作为设施点载入到现有服务区图层中,并使用 NAClassFieldMappings 对象在加载设施点时指定 10 分钟的延迟。它假设已对现有地图文档添加了基于旧金山地区网络数据集创建的名为 Fire Stations Coverage 的服务区网络分析图层,并添加了名为 FireStations 的要素图层。

#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 示例 2(工作流)

该示例演示了在将天气条件作为减速障碍的情况下,如何寻找商店间的最佳路径。它说明如何在 NAClassFieldMappings 对象中使用“添加位置”工具加载天气图层作为面障碍,和商店位置作为停靠点。

# 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))