Select to view content in your preferred language

Trouble Consuming 11.3 Routing Service In Arcpy Script

420
5
Jump to solution
02-10-2025 02:40 PM
__JackCharde__
Regular Contributor

Hey Everyone,

I have a routing service published to my 11.3 Enterprise portal (via map image layer w/ Network Analysis capability checked) and I am attempting to utilize the REST endpoint in an arcpy script that I will eventually publish as a web tool. I know I could use a local ND in a script and publish the GP tool which would include the ND, but the client wants the routing service separate so they can update it at will w/o updating the published tool. Anyway, this service has a single analysis layer for calculating service areas and I am using the arcpy.nax module to call the service endpoint and instantiate a ServiceArea() class.

 

 

service_url = r'https://<my_domain>'

svc_area_solver = arcpy.nax.ServiceArea(service_url)

 

 

 When line 3 runs, I get:

 

 

ValueError: Cannot use 'https://<my_domiain>' for network analysis.

 

 

 

I am using the following example from this documentation on consuming the routing service in arcpy:

 

import arcpy

# Instantiate a ServiceArea analysis object.
service_area = arcpy.nax.ServiceArea("https://myportal.mysite.com/portal/")

 

I am assuming, since the URL here is only to the portal, that the URL to the service area layer of the published routing service needs to be applied to the Directions and Routing section of the portal org settings (Organization > Utility Services > Directions and Routing > Paste service area layer URL in the Service Area box of the Network Analysis Services section; see below image). I have done this and I receive the aforementioned ValueError exception.

__JackCharde___0-1739227077900.png

We are using ArcGIS Server Advanced, so I know network analysis comes with it by default. I am also signed into Portal with the main site administrator credentials, so it shouldn't be due to a lack of privileges. Wondering if anyone has any insight into what I may be doing wrong or missing. Thanks!

- Jack C

 

 

0 Kudos
1 Solution

Accepted Solutions
MaxZeng
Esri Contributor

Hey Jack,

arcpy.nax API only takes the portal url and internally it uses the helper service (like the UI you showed) to discover the correct service to use based on the object being initialized. So providing a direct service url wouldn't work, unless you are publishing to standalone server, and here is the documentation for that: https://pro.arcgis.com/en/pro-app/latest/arcpy/network-analyst/use-routing-services-from-a-standalon...

That being said, another problem in your workflow, is that you published a map image layer with network analyst capability enabled. arcpy.nax API, when initialized with portal URL, can only use geoprocessing service based routing service. So you need to use publish routing services workflow to publish routing services on the portal, which takes care of publishing the service and registering the helper services on the portal. This documentation shows two ways you can publish routing services, https://enterprise.arcgis.com/en/server/latest/publish-services/windows/publish-routing-services.htm. Since you use Enterprise 11.3, you can use the configure routing services UI to publish routing services on the Enterprise. 

A third point to your workflow, since you try to publish a script tool that runs on the server and reference the portal's routing service, so this is what I would suggest you to do, this takes away the need to hardcode the portal url.

 

portal_url = arcpy.GetActivePortalURL()
service_area = arcpy.nax.ServiceArea(portal_url)

 

 

Hope this helps, if you run into issue in this workflow, let me know. Thanks!

View solution in original post

5 Replies
MaxZeng
Esri Contributor

Hey Jack,

arcpy.nax API only takes the portal url and internally it uses the helper service (like the UI you showed) to discover the correct service to use based on the object being initialized. So providing a direct service url wouldn't work, unless you are publishing to standalone server, and here is the documentation for that: https://pro.arcgis.com/en/pro-app/latest/arcpy/network-analyst/use-routing-services-from-a-standalon...

That being said, another problem in your workflow, is that you published a map image layer with network analyst capability enabled. arcpy.nax API, when initialized with portal URL, can only use geoprocessing service based routing service. So you need to use publish routing services workflow to publish routing services on the portal, which takes care of publishing the service and registering the helper services on the portal. This documentation shows two ways you can publish routing services, https://enterprise.arcgis.com/en/server/latest/publish-services/windows/publish-routing-services.htm. Since you use Enterprise 11.3, you can use the configure routing services UI to publish routing services on the Enterprise. 

A third point to your workflow, since you try to publish a script tool that runs on the server and reference the portal's routing service, so this is what I would suggest you to do, this takes away the need to hardcode the portal url.

 

portal_url = arcpy.GetActivePortalURL()
service_area = arcpy.nax.ServiceArea(portal_url)

 

 

Hope this helps, if you run into issue in this workflow, let me know. Thanks!

__JackCharde__
Regular Contributor

@MaxZeng,

Thank you for the information! I must have missed in my research what you state about how to utilize a service in arcpy.nax. Good to know and I'll give it a go and see how I fare.

I am only developing this for a client on my 11.3 enterprise deployment. They are still at 10.9.1, so that Portal UI for publishing won't work and I'll need to use the command line utility to publish.

I'll definitely avoid hardcoding the portal URL and instead use GetActivePortalURL(); good reminder!

Do you know, is the routing service I published (map image layer w/ NA capability) only usable by other ArcGIS APIs, like the Maps SDK for JS?

0 Kudos
MaxZeng
Esri Contributor

Yes the other APIs can consume it. Besides APIs, the only out of box client that can consume the map image layer w/NA capability, is directions widget. And it consumes the route service.

__JackCharde__
Regular Contributor

Hey @MaxZeng,

That worked beautifully! I was able to successfully publish and then instantiate the ServiceArea() class. Thanks again for the assistance and knowledge.

 

MaxZeng
Esri Contributor

Sounds great, glad to help!

0 Kudos