Route "Name" field

2897
7
Jump to solution
03-25-2016 07:47 PM
Highlighted
New Contributor

Hi All,

I am trying to Solve best routes between each OD (origin-destination) pair. I want  the route "Name" field to be populated by the value in the field "PRMT_ID" of the origins. PRMT_ID field is in the destination point feature also.I am using python code as below: The route solution works fine, except that the "Name" field of the route only says Location 1-Location 2. It doesn't populate it with the value in the "PRMT_ID" field as I was expecting. I wonder if anyone can advise on how to make the route layer show the "PRMT_ID" value instead of saying generic Location 1-Location 2

Appreciate any help!

I wrote the code referring to the MakeRouteLayer example 4.

and below are a few lines

Make Route Layer—Help | ArcGIS for Desktop

(ArcGIS 10.3.1)

fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, stopsLayerName)

fieldMappings["Name"].mappedFieldName = "PRMT_ID"

arcpy.na.AddLocations(outNALayer, stopsLayerName, inOrigins, fieldMappings,"")

arcpy.na.AddLocations(outNALayer, stopsLayerName, inDestinations, fieldMappings,"")

arcpy.na.Solve(outNALayer)

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Esri Regular Contributor

Ah, okay.  Let me first clear up one point of confusion:

If you want to model multiple routes (one route between each origin and destination) in your Route layer, the correct field to map your ID to in the Stops sublayer is the RouteName field.  Stops with the same RouteName value end up in the same route.

The "Name" in the Stops sublayer field just the name of each stop.  In the Routes sublayer, the route's Name field is just a concatenation of the stop names, unless you specified a RouteName in Stops.  Yeah, a little confusing.

I still don't know why your code isn't working, but you can try using the old-fashioned string method instead of using NAClassFieldMappings.

fieldMappings = "RouteName PRMT_ID #;"

arcpy.na.AddLocations(outNALayer, stopsLayerName, inOrigins, fieldMappings,"")

View solution in original post

Reply
0 Kudos
7 Replies
Highlighted
Esri Regular Contributor

That code looks like it should work, in theory.  Is your "PRMT_ID" field a string field?  I'm actually not sure how the field mapping would behave if you mapped a field to Name that was, say, an integer.

Highlighted
New Contributor

I tried both ways..it was a long integer type, initially. Then I also tried it with text field, but still did not work.

Reply
0 Kudos
Highlighted
New Contributor

in the example code field mappings, example 4  has used "RouteName", while example 3 show it as "Name". It my Route layer output the field name was "Name". The code didn't show any error whether I used "RouteName" or "Name"

MakeRouteLayer example 4 (workflow)

........................................

fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, stopsLayerName)

fieldMappings["RouteName"].mappedFieldName = "Commuter_Name"

------------------------------------------------------------------------

MakeRouteLayer example 3 (workflow)

..................

fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, stopsLayerName)

fieldMappings["Name"].mappedFieldName = "Address"

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

I am not sure it would show an error, it may just ignore it completely, try it manually and see what happens

Highlighted
New Contributor

as you suggested,I executed the code manually within arcmap python window. As before, the Stops layer name field shows the 2 locations as Location1, Location2 and the Routes layer show Name as Location1-Location2.

If fieldmapping worked correctly, I would assume, the Stops layer also should have shown its Name field value as  the mappedFieldName "PRMT_ID" and in Routes layer.

(I know it had worked as expected when I was using model builder tool for route solve a few months ago, but when it used as python code it did not)

Reply
0 Kudos
Highlighted
Esri Regular Contributor

Ah, okay.  Let me first clear up one point of confusion:

If you want to model multiple routes (one route between each origin and destination) in your Route layer, the correct field to map your ID to in the Stops sublayer is the RouteName field.  Stops with the same RouteName value end up in the same route.

The "Name" in the Stops sublayer field just the name of each stop.  In the Routes sublayer, the route's Name field is just a concatenation of the stop names, unless you specified a RouteName in Stops.  Yeah, a little confusing.

I still don't know why your code isn't working, but you can try using the old-fashioned string method instead of using NAClassFieldMappings.

fieldMappings = "RouteName PRMT_ID #;"

arcpy.na.AddLocations(outNALayer, stopsLayerName, inOrigins, fieldMappings,"")

View solution in original post

Reply
0 Kudos
Highlighted
New Contributor

Thank you Melinda!  Your explanation about when to use "RouteName"   (or when not use "Name") helped me a long way..

Now I understand what was wrong within my code. I had used cursors to get the geometry of the OD pairs and used it as Stops as I needed to treat each OD pair differently depending on their specific attributes. Now when I changed the code to use the stops featureclass itself, the RouteLayer attributes populated with RouteName field as expected

Reply
0 Kudos