向分析图层添加字段 (网络分析)
摘要
向网络分析图层的子图层添加字段
用法
-
该工具通常与添加位置工具配合使用,以将输入要素中的字段传递到子图层。例如,如果想要将输入要素中名为 UniqueID 的字段传递到“服务区”图层的“设施点”子图层,请使用该工具先将 UniqueID 字段添加到“设施点”子图层,然后使用添加位置工具中的字段映射为 UniqueID 字段提供输入值。
-
可以为网络分析图层的任意子图层添加字段。
语法
AddFieldToAnalysisLayer_na (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 (可选) |
要添加的字段的长度。它为字段的每条记录设置最大允许字符数。此选项仅适用于文本或 blob 类型的字段。 | Long |
field_alias (可选) |
指定给字段名称的备用名称。此名称用于为含义隐晦的的字段名称指定更具描述性的名称。字段别名参数仅适用于地理数据库和 coverage。 | String |
field_is_nullable (可选) |
不存在关联属性信息的地理要素。它们与零或空字段不同,仅支持地理数据库中的字段。
| Boolean |
代码示例
AddFieldToAnalysisLayer 示例 1(Python 窗口)
下面的 Python 窗口脚本演示了如何将 UniqueID 字段添加到“服务区”网络分析图层的“设施点”子图层。
import arcpy
arcpy.AddFieldToAnalysisLayer_na("Service Area","Facilities","UniqueID","LONG")
AddFieldToAnalysisLayer 示例 2(工作流)
下面的独立 Python 脚本演示了如何使用 AddFieldToAnalysisLayer 函数将 StationID 字段从输入消防站要素传递到通过服务区分析计算出的 2、3、5 分钟服务区面要素。StationID 字段可用于将消防站要素的其他属性连接到服务区面要素。
# Name: AddFieldToAnalysisLayer_Workflow.py
# Description: Transfers the StationID field from the input fire station
# features to the 2,3,5 minute service area polygon features
# calculated from a service area analysis. The StationID 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
try:
#Check out the Network Analyst extension license
arcpy.CheckOutExtension("Network")
#Set environment settings
env.workspace = "C:/data/SanFrancisco.gdb"
env.overwriteOutput = True
#Set local variables
inNetworkDataset = "Transportation/Streets_ND"
outNALayer = "FireStationsCoverage"
impedanceAttribute = "TravelTime"
defaultBreakValues = "2 3 5"
fieldName = "StationID"
fieldType = "LONG"
inFeatures = "Analysis/FireStations"
fieldMappings = "Name StationName #; StationID StationID #"
searchTolerance = "2 Miles"
polygonLayer = outNALayer + "/" + "Polygons"
facilitiesLayer = outNALayer + "/" + "Facilities"
calculateField = "SAPolygons." + fieldName
expression = "!Facilities." + fieldName + "!"
outFeatures = outNALayer + "Area"
#Create a new service area analysis layer. For this scenario, the default
#value for all the remaining parameters statisfies the analysis requirements
arcpy.MakeServiceAreaLayer_na(inNetworkDataset, outNALayer,
impedanceAttribute,"",defaultBreakValues)
#Add a StationID field to the Facilities sublayer of the service area layer.
#This is done before loading the fire stations as facilities so that the
#StationID values can be transferred from the input features to the
#Facilities sublayer. The service area layer created previously is
#referred by its name.
arcpy.AddFieldToAnalysisLayer_na(outNALayer,"Facilities",fieldName,
fieldType)
#Add the fire station features as Facilities and map the Name and the
#StationID properties from the StationName and StationID fields.
arcpy.AddLocations_na(outNALayer,"Facilities",inFeatures,fieldMappings,
searchTolerance)
#Solve the service area layer
arcpy.Solve_na(outNALayer)
#Transfer the StationID field from Facilities sublayer to Polygons sublayer
#of the service area layer since we wish to export the polygons.
#First add "StationID" field to the Polygons sublayer
arcpy.AddFieldToAnalysisLayer_na(outNALayer,"Polygons",fieldName,fieldType)
#Join Facilities to Polygons based on FacilityID and ObjectID fields
arcpy.AddJoin_management(polygonLayer,"FacilityID",facilitiesLayer,
"ObjectID")
#Calculate the StationID field in Polygons based on the StationID field from
#facilities.
arcpy.CalculateField_management(polygonLayer,calculateField,expression,
"PYTHON")
#Remove the join so that the Polygons sublayer will not have all the fields
#from the Facilities sublayer when Polygons are exported to a feature class.
arcpy.RemoveJoin_management(polygonLayer,"Facilities")
#Export the Polygons sublayer to a feature class on disk.
arcpy.CopyFeatures_management(polygonLayer, outFeatures)
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)
环境
相关主题
7/10/2012