Add Field to Analysis Layer (Network Analyst)

License Level:BasicStandardAdvanced

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.

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 occured on line %i" % tb.tb_lineno
    print str(e)

Environments

Related Topics

Licensing Information

ArcGIS for Desktop Basic: Yes
ArcGIS for Desktop Standard: Yes
ArcGIS for Desktop Advanced: Yes
1/20/2015