arcpy.na.AddFieldToAnalysisLayer(in_network_analysis_layer, sub_layer, field_name, field_type, {field_precision}, {field_scale}, {field_length}, {field_alias}, {field_is_nullable})
|
Name
|
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
|
Specifies the field type that will be used in the creation of the new field.
LONG—The field type will be long. Long fields support whole numbers between -2,147,483,648 and 2,147,483,647.
TEXT—The field type will be text. Text fields support a string of characters.
FLOAT—The field type will be float. Float fields support fractional numbers between -3.4E38 and 1.2E38.
DOUBLE—The field type will be double. Double fields support fractional numbers between -2.2E308 and 1.8E308.
SHORT—The field type will be short. Short fields support whole numbers between -32,768 and 32,767.
DATE—The field type will be date. Date fields support date and time values.
BLOB—The field type will be BLOB. BLOB fields support data stored as a long sequence of binary numbers. You need a custom loader or viewer or a third-party application to load items into a BLOB field or view the contents of a BLOB field.
GUID—The field type will be GUID. GUID fields store registry-style strings consisting of 36 characters enclosed in curly brackets.
BIGINTEGER—The field type will be big integer. Big integer fields support whole numbers between -(253) and 253.
TIMEONLY—The field type will be time only. Time only fields support time values with no date values.
DATEONLY—The field type will be date only. Date only fields support date values with no time values.
TIMESTAMPOFFSET—The field type will be timestamp offset. Timestamp offset fields support a date, time, and offset from a UTC value.
|
String
|
|
field_precision
(Optional)
|
Legacy:
This parameter is deprecated and maintained only for backward compatibility.
|
Long
|
|
field_scale
(Optional)
|
Legacy:
This parameter is deprecated and maintained only for backward compatibility.
|
Long
|
|
field_length
(Optional)
|
The length of the field. This sets the maximum number of allowable characters for each record of the field. If no field length is provided, a length of 255 will be used.
The field length applies only to text fields.
|
Long
|
|
field_alias
(Optional)
|
The alternate name for the field. 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.
Legacy:
The GetNASublayer function can be used to retrieve the sublayers of a network analysis layer. It was introduced in ArcGIS Pro 2.7. In earlier software versions, the best way to retrieve a sublayer object of a network analysis layer was to use the listLayers method of the network analysis Layer object using the sublayer name as a wildcard.
# 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:
#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))