Select to view content in your preferred language

How to access arcgis online for routing service from arcpy

969
2
04-08-2014 03:17 PM
TakeoShibata
Emerging Contributor
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)
https://github.com/deelesh/batch-geocoding-python/blob/master/BatchGeocoding.py

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
arcpy.CheckOutExtension("network")

#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
arcpy.na.Solve(routeLayer)

#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
arcpy.na.Solve(routeLayer)

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

arcpy.AddMessage("Completed")
Tags (2)
0 Kudos
2 Replies
KevinHibma
Esri Regular Contributor
Here's the two help topics which should get you started. The only part I'm not sure of is the authorization. I'd need to make time to try it, but I think you can use these services with a simple token (that you must obtain). This is a very "simple" generate token function here: https://gist.github.com/khibma/7485243

This help page explains the input parameters to the service for the REST endpoint of the online service: http://resources.arcgis.com/en/help/arcgis-rest-api/index.html#/Route_service_with_asynchronous_exec...

This help topic has 2 sections in it: http://resources.arcgis.com/en/help/main/10.2/#/Using_a_geoprocessing_service_in_Python_scripts/0057...
The first shows how to Import a toolbox from an online source. The second shows how to use the service from its REST end point. Use whatever method makes the most sense to you in regards to using the online service.
0 Kudos
TakeoShibata
Emerging Contributor
Hi Kevin

Thank you.
I am now looking at
http://resources.arcgis.com/en/help/main/10.2/#/Using_a_geoprocessing_service_in_Python_scripts/0057...
and the ImportToolbox in arcpy wat seems to be the resonable way if I understand correct.
(REST way, I send and I have to decode JSON reply strings and create feature from it manually, right?)

In arcpy way sample,
How to find the exact function name and parameters?
Below example, the function name
Viewshed_viewshedAlias
is used and I think that is the one imported from http://sampleserver1.arcgisonline.com/

Is that way I can do with routing service and out put the line feature in ArcPY way?
If I use ArcPy way instead of Rest way if possible, how to access the service?
According to the help of ImportToolbox
URL;servicename;{username};{password}

Thank you!
=====
import arcpy
import time

arcpy.ImportToolbox("http://sampleserver1.arcgisonline.com/ArcGIS/services;Elevation/ESRI_Elevation_World", "viewshedAlias")

result = arcpy.Viewshed_viewshedAlias(r'c:\data\inputPoint.shp', "10000 Kilometers")

while result.status < 4:
    print result.status
    time.sleep(0.2)
print "Execution Finished"

result.getMessages()
arcpy.CopyFeatures_management(result.getOutput(0), 'localResult.shp')
0 Kudos