Route "Name" field

3653
8
Jump to solution
03-25-2016 07:47 PM
ElsitMandal
New Contributor II

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)

0 Kudos
1 Solution

Accepted Solutions
MelindaMorang
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

0 Kudos
8 Replies
MelindaMorang
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.

ElsitMandal
New Contributor II

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

0 Kudos
ElsitMandal
New Contributor II

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"

0 Kudos
DanPatterson_Retired
MVP Emeritus

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

ElsitMandal
New Contributor II

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)

0 Kudos
MelindaMorang
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,"")

0 Kudos
ElsitMandal
New Contributor II

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

0 Kudos
MelindaMorang
Esri Regular Contributor

Hello Elsit and anyone else reading this thread who might be doing similar workflows.

Esri’s Network Analyst Team is doing some research about customer workflows involving solving routes or calculating travel times and distances between known or preassigned pairs of origins and destinations.  We’d like to better understand our customers’ needs in this area and may use this information to design and develop improved tools and workflows.

I know this post is very old, but if this workflow is still relevant to you and if you have a few minutes and are willing, could you please fill out the attached survey and return it to the e-mail address included inside the document?  Alternatively, you can reach out to me (my e-mail address is in the document) to set up a meeting.

Thank you so much!!

0 Kudos