描述
用于向网络分析图层的子图层添加字段。
使用方法
该工具通常与添加位置工具配合使用,从而将输入要素中的字段传递到子图层。例如,如果想要将输入要素中名为 UniqueID 的字段传递到“服务区”图层的“设施点”子图层,请使用该工具先将 UniqueID 字段添加到“设施点”子图层,然后使用添加位置工具中的字段映射为 UniqueID 字段提供输入值。
可以为网络分析图层的任意子图层添加字段。
语法
arcpy.na.AddFieldToAnalysisLayer(in_network_analysis_layer, sub_layer, field_name, field_type, {field_precision}, {field_scale}, {field_length}, {field_alias}, {field_is_nullable})
参数 | 说明 | 数据类型 |
in_network_analysis_layer | 要添加新字段的网络分析图层。 | Network Analyst Layer |
sub_layer | 要添加新字段的网络分析图层的子图层。 | String |
field_name | 要添加到网络分析图层的指定子图层中的字段名称。 | String |
field_type | 在创建新字段时所使用的字段类型。
| String |
field_precision (可选) | 可存储在字段中的位数。所有位都将被计算在内,而无论其处于小数点的哪一侧。 参数值仅对数值字段类型有效。 | Long |
field_scale (可选) | 可存储在字段中的小数位数。此参数仅可用于浮点型和双精度数据字段类型。 | Long |
field_length (可选) | 要添加的字段的长度。它为字段的每条记录设置最大允许字符数。此参数仅适用于文本类型的字段。 | Long |
field_alias (可选) | 指定给字段名称的备用名称。此名称用于描述含义隐晦的字段名称。此参数仅适用于地理数据库。 | String |
field_is_nullable (可选) | 指定该字段是否可包含空值。空值与零或空字段不同,仅支持地理数据库中的字段。
| Boolean |
派生输出
名称 | 说明 | 数据类型 |
output_layer | 已更新的网络分析图层。 | 网络分析图层 |
代码示例
下面的 Python 窗口脚本演示了如何将 UniqueID 字段添加到服务区网络分析图层的设施点子图层。
arcpy.na.AddFieldToAnalysisLayer("Service Area", "Facilities", "UniqueID",
"LONG")
下面的独立 Python 脚本演示了如何使用 AddFieldToAnalysisLayer 函数将 StationID 字段从输入的消防站要素传递到通过服务区分析计算出的 2、3、5 分钟服务区面要素。StationID 字段可用于将消防站要素的其他属性连接到服务区面要素。
旧版本:
GetNASublayer 功能可用于检索网络分析图层的子图层。它是在 ArcGIS Pro 2.7 中引入的。在以前的软件版本中,检索网络分析图层的子图层对象的最佳方法是使用网络分析 Layer 对象的 listLayers 方法,该方法将子图层名称用作通配符。
# Name: AddFieldToAnalysisLayer_Workflow.py
# Description: Transfers the Address field from the input fire station
# features to the 2-,3-, and 5-minute service area polygon features
# calculated from a service area analysis. The Address field can
# be used to join other attributes from the fire station features
# to the service area polygon features.
# Requirements: Network Analyst Extension
#Import system modules
import arcpy
from arcpy import env
import os
try:
#Check out 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 = "FireStationsCoverage"
travel_mode = "Driving Time"
cutoffs = [2, 3, 5] #minutes
field_to_add = "Address"
field_type = "TEXT"
facilities = os.path.join(input_gdb, "Analysis", "FireStations")
search_tolerance = "2 Miles"
out_featureclass = os.path.join(output_dir, "Output.gdb",
"FireStationsCoverageArea")
#Create a new service area analysis layer. For this scenario, the default
#value for all the remaining parameters statisfies the analysis requirements
result_object = arcpy.na.MakeServiceAreaAnalysisLayer(network, layer_name,
travel_mode, cutoffs=cutoffs)
#Get the layer object from the result object. The service area layer can now
#be referenced using the layer object.
layer_object = result_object.getOutput(0)
#Get the names of all the sublayers within the service area layer.
sublayer_names = arcpy.na.GetNAClassNames(layer_object)
#Stores the layer names that we will use later
facilities_layer_name = sublayer_names["Facilities"]
polygons_layer_name = sublayer_names["SAPolygons"]
#Add the Address field to the Facilities sublayer of the service area layer.
#This is done before loading the fire stations as facilities so that the
#Address values can be transferred from the input features to the
#Facilities sublayer.
arcpy.na.AddFieldToAnalysisLayer(layer_object, facilities_layer_name,
field_to_add, field_type)
#Add the fire station features as Facilities and map the Name and the
#Address fields from the fire station features to the Name and Address
#properties on the Facilities sublayer
field_mappings = arcpy.na.NAClassFieldMappings(layer_object,
facilities_layer_name)
field_mappings['Name'].mappedFieldName = "Name"
field_mappings['Address'].mappedFieldName = "Address"
arcpy.na.AddLocations(layer_object, facilities_layer_name, facilities,
field_mappings, search_tolerance)
#Solve the service area layer
arcpy.na.Solve(layer_object)
#Get the layer objects for all the sublayers within the service area layer
facilities_sublayer = layer_object.listLayers(facilities_layer_name)[0]
polygons_sublayer = layer_object.listLayers(polygons_layer_name)[0]
#Transfer the Address field from the Facilities sublayer to the Polygons
#sublayer of the service area layer since we wish to export the polygons.
#The FacilityID field in the Polygons sublayer is related to the ObjectID
#field in the Facilities sublayer.
arcpy.management.JoinField(polygons_sublayer, "FacilityID",
facilities_sublayer, "ObjectID", field_to_add)
#Export the Polygons sublayer to a feature class on disk.
arcpy.management.CopyFeatures(polygons_sublayer, out_featureclass)
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))
环境
许可信息
- Basic: 是
- Standard: 是
- Advanced: 是