AnsweredAssumed Answered

Network Analyst Field Mapping with arcpy- How to carry fields from origins and destinations to lines sublayer?

Question asked by LCremin on Dec 16, 2016
Latest reply on Jan 9, 2017 by LCremin

Hi- I am fairly new to Network Analyst, and have a question about using field mapping for network analyst layers. 

I am using a python script (with arcpy.na), running on Desktop 10.4/10.5

I am trying to use field mappings to carry certain fields (for IDs and attributes) from input destinations and origins features to the lines layer. I am doing this to retain the IDs and to avoid a join step. 

I have figured out how to map these fields into the destinations sublayer (want SID and VALUE fields) and the origins sublayer (want BID field). 

None of these fields, however, are carried on after the solve step, and any field mapping I tried with the lines layer didn't work (inputs valid only for layers that exist before the solve). 

Is there a way to select which fields in the destinations and origins layers get carried on after the solve, and ultimately appear in the lines sublayer?

 

Thank you for any information (or corrections!)- I'm not quite sure what I'm missing here, if it's possible, or if there's a different approach. My code is below along with a screenshot of the fields I'm trying to transfer. 

Laura

 

 

# Process: Make OD Cost Matrix Layer
OD_matrix = "ODMatrix"
outNA_layer = arcpy.na.MakeODCostMatrixLayer(streets, OD_matrix, cost) #, "Driving Time", "", "", "", "", "NO_LINES")
layer_object = outNA_layer.getOutput(0)

 

#Process: Add Destinations #Works to add and map SID and VALUE fields to destinations sublayer
field_name = "VALUE"
field_type = "FLOAT"
arcpy.na.AddFieldToAnalysisLayer(layer_object, destinations_layer_name, field_name, field_type)
arcpy.na.AddFieldToAnalysisLayer(layer_object, destinations_layer_name, "SID", "TEXT")

field_mappings = arcpy.na.NAClassFieldMappings(layer_object, destinations_layer_name)
field_mappings["VALUE"].mappedFieldName = "VALUE"
field_mappings["SID"].mappedFieldName = "Name"
arcpy.na.AddLocations(layer_object, destinations_layer_name, stores, field_mappings) 

 

#Process: Add Origins  #Works to add and map BID fields to origins sublayer
arcpy.na.AddFieldToAnalysisLayer(layer_object, origins_layer_name, "BID", "SHORT")
field_mappings = arcpy.na.NAClassFieldMappings(layer_object, origins_layer_name)
field_mappings["BID"].mappedFieldName = "BID"
arcpy.na.AddLocations(layer_object, origins_layer_name, origins, field_mappings) 

 

#Are there any field mappings options for lines before the solve? Lines table only has fields FID, Shape#, Name, OriginID, DestinationID, and Total_Minutes. Want SID, VALUE, and BID to appear.

# Process: Solve

#arcpy.na.AddFieldToAnalysisLayer(layer_object, lineslayer_name, "SID", "TEXT")
#field_mappings = arcpy.na.NAClassFieldMappings(layer_object, lineslayer_name) ##Does not work. Tried from inputs directly and from destination and origin sublayers
#field_mappings["SID"].mappedFieldName = "SID"


solvestart = time.time()
arcpy.na.Solve(layer_object, "SKIP", "TERMINATE")
arcpy.AddMessage("Matrix solved: {} s".format(time.time()-solvestart))

subLayers = {} 
for layer in arcpy.mapping.ListLayers(layer_object)[1:]: 
   subLayers[layer.datasetName] = layer 
linesLayer = subLayers["ODLines"] 
arcpy.SaveToLayerFile_management(layer_object, "LayerOutput" 
arcpy.management.CopyFeatures(linesLayer, lineslayer_name )

 

Origins, Destinations, and Lines layer fields- how to transfer between?

 

Matt Beyers

Outcomes