Summary
Adds a field to a sublayer of a network analysis layer.
Usage
The tool is mostly used along with the Add Locations tool to transfer fields from the input features to the sublayers. For example, if you want to transfer a field called UniqueID from your input features into the Facilities sublayer of the Service Area layer, use this tool to first add the UniqueID field to the Facilities sublayer, and then use the field mappings in the Add Locations tool to provide input values for the UniqueID field.
Fields can be added to any of the sublayers of the network analysis layers.
Syntax
arcpy.na.AddFieldToAnalysisLayer(in_network_analysis_layer, sub_layer, field_name, field_type, {field_precision}, {field_scale}, {field_length}, {field_alias}, {field_is_nullable})
Parameter | Explanation | Data Type |
in_network_analysis_layer | The network analysis layer to which the new field will be added. | Network Analyst Layer |
sub_layer | The sublayer of the network analysis layer to which the new field will be added. | String |
field_name | The name of the field that will be added to the specified sublayer of the network analysis layer. | String |
field_type | The field type used in the creation of the new field.
| String |
field_precision (Optional) | The number of digits that can be stored in the field. All digits are counted regardless of which side of the decimal they are on. The parameter value is only valid for numeric field types. | Long |
field_scale (Optional) | The number of decimal places stored in a field. This parameter is only used in float and double data field types. | Long |
field_length (Optional) | The length of the field being added. This sets the maximum number of allowable characters for each record of the field. This parameter is only applicable to fields of type text. | Long |
field_alias (Optional) | The alternate name given to the field name. This name is used to describe cryptic field names. This parameter only applies to geodatabases. | String |
field_is_nullable (Optional) | Specifies whether the field can contain null values. Null values are different from zero or empty fields and are only supported for fields in a geodatabase.
| Boolean |
Derived Output
Name | Explanation | Data Type |
output_layer | The updated network analysis layer. | Network Analyst Layer |
Code sample
AddFieldToAnalysisLayer example 1 (Python window)
The following Python window script demonstrates how to add a UniqueID field to the Facilities sublayer of the Service Area network analysis layer.
arcpy.na.AddFieldToAnalysisLayer("Service Area", "Facilities", "UniqueID",
"LONG")
AddFieldToAnalysisLayer example 2 (workflow)
The following stand-alone Python script demonstrates how the AddFieldToAnalysisLayer function can be used to transfer the StationID 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 StationID field can be used to join other attributes from the fire station features to the service area polygon features.
# 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
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"
outNALayerName = "FireStationsCoverage"
impedanceAttribute = "TravelTime"
defaultBreakValues = "2 3 5"
fieldName = "Address"
fieldType = "TEXT"
inFeatures = "Analysis/FireStations"
searchTolerance = "2 Miles"
outFeatures = outNALayerName + "Area"
saFacilities = "Facilities"
saPolygons = "SAPolygons"
#Create a new service area analysis layer. For this scenario, the default
#value for all the remaining parameters statisfies the analysis requirements
outNALayer = arcpy.na.MakeServiceAreaLayer(inNetworkDataset, outNALayerName,
impedanceAttribute,"",
defaultBreakValues)
#Get the layer object from the result object. The service layer can now be
#referenced using the layer object.
outNALayer = outNALayer.getOutput(0)
#Get the names of all the sublayers within the service area layer.
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
#Stores the layer names that we will use later
facilitiesLayerName = subLayerNames[saFacilities]
polygonLayerName = subLayerNames[saPolygons]
#Get the layer objects for all the sublayers within the service area layer
#The first layer returned by ListLayers is the Service area layer itself
#which we don't want to use.
subLayers = {}
for layer in arcpy.mapping.ListLayers(outNALayer)[1:]:
subLayers[layer.datasetName] = layer
#Store the layer objects that we will use later
facilitiesLayer = subLayers[saFacilities]
polygonLayer = subLayers[saPolygons]
#Add a 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. The service area layer created previously is
#referred by the layer object.
arcpy.na.AddFieldToAnalysisLayer(outNALayer,facilitiesLayerName,fieldName,
fieldType)
#Add the fire station features as Facilities and map the Name and the
#Address properties from the Name and Address fields from fire station
#features using the field mappings.
fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, facilitiesLayerName)
fieldMappings['Name'].mappedFieldName = "Name"
fieldMappings['Address'].mappedFieldName = "Address"
arcpy.na.AddLocations(outNALayer,facilitiesLayerName,inFeatures,
fieldMappings, searchTolerance)
#Solve the service area layer
arcpy.na.Solve(outNALayer)
#Transfer the Address field from Facilities sublayer to Polygons sublayer
#of the service area layer since we wish to export the polygons. The
#FacilityID field in Polygons sub layer is related to the ObjectID field in
#the Facilities sub layer.
arcpy.management.JoinField(polygonLayer, "FacilityID",facilitiesLayer,
"ObjectID", fieldName)
#Export the Polygons sublayer to a feature class on disk.
arcpy.management.CopyFeatures(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 occurred on line %i" % tb.tb_lineno
print str(e)
Environments
Licensing information
- Basic: Yes
- Standard: Yes
- Advanced: Yes