Add Field to Analysis Layer (Network Analyst)

Summary

Adds a field to a sublayer of a network analysis layer.

Usage

Syntax

AddFieldToAnalysisLayer_na (in_network_analysis_layer, sub_layer, field_name, field_type, {field_precision}, {field_scale}, {field_length}, {field_alias}, {field_is_nullable})
ParameterExplanationData Type
in_network_analysis_layer

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.

  • LONGNumeric values without fractional values within a specific range.
  • TEXTNames or other textual qualities.
  • FLOATNumeric values with fractional values within a specific range.
  • DOUBLENumeric values with fractional values within a specific range.
  • SHORTNumeric values without fractional values within a specific range; coded values.
  • DATEDate and/or Time.
  • BLOBImages or other multimedia.
String
field_precision
(Optional)

Describes the number of digits that can be stored in the field. All digits are counted no matter what side of the decimal they are on.

The parameter value is valid only for numeric field types.

Long
field_scale
(Optional)

Sets 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 option is only applicable on fields of type text or blob.

Long
field_alias
(Optional)

The alternate name given to the field name. This name is used to give more descriptive names to cryptic field names. The field alias parameter only applies to geodatabases and coverages.

String
field_is_nullable
(Optional)

A geographic feature where there is no associated attribute information. These are different from zero or empty fields and are only supported for fields in a geodatabase.

  • NON_NULLABLEThe field will not allow null values.
  • NULLABLEThe field will allow null values. This is the default.
Boolean

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.

import arcpy
arcpy.AddFieldToAnalysisLayer_na("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, 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 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 
# Author: ESRI

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

    
    
    
    
    
                                  
                                  

Environments

Related Topics

Licensing Information

ArcView: Requires Network Analyst
ArcEditor: Requires Network Analyst
ArcInfo: Requires Network Analyst

Published 6/7/2010