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

2224
4
Jump to solution
12-16-2016 12:01 PM
LauraCremin
New Contributor II

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

0 Kudos
1 Solution

Accepted Solutions
MelindaMorang
Esri Regular Contributor

Unfortunately, no, there is no way to automatically carry over fields from the input into the output Lines sublayer.  You have to use a join.

The last code sample on the Make OD Cost Matrix Layer documentation page gives an example of doing something very similar to this, using joins:

Make OD Cost Matrix Layer—Help | ArcGIS Desktop 

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

Moved to the https://community.esri.com/community/gis/analysis/network-analyst?sr=search&searchId=0c554793-8ecc-4...‌ place to increase your chances of getting an answer

LauraCremin
New Contributor II

Thank you! 

0 Kudos
MelindaMorang
Esri Regular Contributor

Unfortunately, no, there is no way to automatically carry over fields from the input into the output Lines sublayer.  You have to use a join.

The last code sample on the Make OD Cost Matrix Layer documentation page gives an example of doing something very similar to this, using joins:

Make OD Cost Matrix Layer—Help | ArcGIS Desktop 

LauraCremin
New Contributor II

Thank you for the notes and pointing out the example! Looks like there's no way to avoid the join, which for some reason is running very slowly (although I may try an update cursor with dictionaries and compare times). 

0 Kudos