Select to view content in your preferred language

Route solve not returning true shape

104
4
2 weeks ago
JillianStanford
Occasional Contributor III

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
4 Replies
TonyAlmeida
Occasional Contributor II

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
Occasional Contributor III

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
Occasional Contributor II

So how did you solve the problem?

0 Kudos
JillianStanford
Occasional Contributor III

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

0 Kudos