Select to view content in your preferred language

Route solve not returning true shape

365
5
06-06-2024 11:29 AM
JillianStanford
Frequent Contributor

Hello!

I've having trouble setting the correct parameters to return a route that follows the shape of my network.

In the screenshot below, the thin grey lines are my road network, the green line is the shape that's generated when I solve for a route using the ArcGIS Pro tools and the purple line is the shape that's returned when I solve for the same route using the Network Analyst python module.

JillianStanford_0-1717697800246.png

The relevant parts of my code are below. Almost all the parameters are left default, except that I've set the travel mode to "Walking" (I've tried different travel modes, they all return a funky line) and I've made sure to set the route shape type to "True Shape".

arcpy.nax.MakeNetworkDatasetLayer(oc_network_dataset, nd_layer_name)
nd_travel_modes = arcpy.nax.GetTravelModes(nd_layer_name)
travel_mode = nd_travel_modes["Walking"]
route = arcpy.nax.Route(nd_layer_name)
route.travelMode = travel_mode
route.routeShapeType = arcpy.nax.RouteShapeType.TrueShape
stopsCur = route.insertCursor(arcpy.nax.RouteInputDataType.Stops, ["Name", "SHAPE@"], append = False)
....
stopsCur.insertRow([on_from, row2[1]])
....
stopsCur.insertRow([on_from, row3[1]])
result = route.solve()
if result.solveSucceeded:
with result.searchCursor(arcpy.nax.RouteOutputDataType.Routes, ["SHAPE@"]) as resultCursor:
    for r in resultCursor:
        insertCursor.insertRow('Routed from UL Intersection points', r[0]))

 

Is there a parameter on the Route that I'm missing? Or am I not handling the results correctly?

Thank you!

Jill

0 Kudos
5 Replies
TonyAlmeida
Frequent Contributor

The variables oc_network_dataset, nd_layer_name, on_from, row2, and row3 are not defined, and you need an indentation in line 14.

 

maybe,

on_from = "Stop1"
row2 = ("Stop2", arcpy.Point(1, 1))
row3 = ("Stop3", arcpy.Point(2, 2))

# Create the route object
route = arcpy.nax.Route(nd_layer_name)
route.travelMode = arcpy.nax.GetTravelModes(nd_layer_name)["Walking"]
route.routeShapeType = arcpy.nax.RouteShapeType.TrueShape

# Create an insert cursor for stops
stopsCur = route.insertCursor(arcpy.nax.RouteInputDataType.Stops, ["Name", "SHAPE@"], append=False)
stopsCur.insertRow([on_from, arcpy.PointGeometry(arcpy.Point(0, 0))])
stopsCur.insertRow([row2[0], row2[1]])
stopsCur.insertRow([row3[0], row3[1]])

# Solve the route
result = route.solve()
if result.solveSucceeded:
    with result.searchCursor(arcpy.nax.RouteOutputDataType.Routes, ["SHAPE@"]) as resultCursor:
        for r in resultCursor:
            # Assuming you have an insertCursor for the output feature class
            insertCursor.insertRow(["Routed from UL Intersection points", r[0]])
0 Kudos
JillianStanford
Frequent Contributor

Thanks for your reply.

I just included the relevant parts of my code, not the whole thing, so those variables are defined elsewhere.

The code runs without error.

Thanks!

0 Kudos
TonyAlmeida
Frequent Contributor

So how did you solve the problem?

JillianStanford
Frequent Contributor

When I solve it, I'll let you know! 🙂

0 Kudos
JillianStanford
Frequent Contributor

I never did solve this problem but changed my workflow.

My original script looped through records, parsing a text field into two addresses, geocoding the addresses and then generating a route between them. The output was a polyline feature class of the routes.

What I ended up doing is modifying the script to output the addresses as points. I then import those into my route layer as stops and execute all of the routes at once using the Pro tools. All of the routes produced by this workflow follow the true shape of the road network.

This works for my purposes. If it comes up again and I can't work around it I will reach out to Support.

Thanks!

0 Kudos