How to access arcgis online for routing service from arcpy

Discussion created by takeofuture on Apr 8, 2014
Latest reply on Apr 10, 2014 by takeofuture
Hi I am trying to geocode addresses and make a route using arcpy and arcgis online.
I successfully complete the geocode using arcpy and arcgis online.
Using CSV file data and generate points data as file geodatabase into the PC.
(I found the sample below)

It works fine and the accuracy is far better than address locator using census data.

Now I try to find the sample or instruction to make a route using arcgisonline routing service.
I search but hard to find, I found network analyst sample like below and it works ok
with census street_ND for some address, though I got some run time errors.
Is there any way I can use arcgis online route service and save
the result into File Geodatabase or Shape file in local computer?
Thank you!

Below is some sample just using locat Street_ND from census but 30% of address results in the error when I geocode it using Census geocodor, so I would like to use ArcGIS route service to make a route from points layer

import arcpy

#Set up the environment
arcpy.env.overwriteOutput = True

#Set up variables
networkDataset = "C:\EsriPress\GIST1\Network Analyst\Tutorial\SanFrancisco.gdb\Transportation\Streets_ND"
stops = "C:\EsriPress\GIST1\Network Analyst\Tutorial\SanFrancisco.gdb\Analysis\Stores"
fastestRoute = "C:\EsriPress\GIST1\Network Analyst\Tutorial\SanFrancisco.gdb\FastestRoute"
shortestRoute = "C:\EsriPress\GIST1\Network Analyst\Tutorial\SanFrancisco.gdb\ShortestRoute"

#Make a new route layer using travel time as impedance to determine fastest route
routeLayer = arcpy.na.MakeRouteLayer(networkDataset, "StoresRoute", "TravelTime").getOutput(0)

#Get the network analysis class names from the route layer
naClasses = arcpy.na.GetNAClassNames(routeLayer)

#Get the routes sublayer from the route layer
routesSublayer = arcpy.mapping.ListLayers(routeLayer, naClasses["Routes"])[0]

#Load stops
arcpy.na.AddLocations(routeLayer, naClasses["Stops"], stops)

#Solve the route layer

#Copy the route as a feature class
arcpy.management.CopyFeatures(routesSublayer, fastestRoute)

#Get the RouteSolverProperties object from the route layer to modify the
#impedance property of the route layer.
solverProps = arcpy.na.GetSolverProperties(routeLayer)

#Set the impedance property to "Meters" to determine the shortest route.
solverProps.impedance = "Meters"

#Resolve the route layer

#Copy the route as a feature class
arcpy.management.CopyFeatures(routesSublayer, shortestRoute)