Select to view content in your preferred language

URL of selected map service layer in TOC

3963
4
06-15-2015 11:57 AM
TommyStålnacke
Emerging Contributor

Using Python I'm trying to get the source path of a selected layer in ArcMap (10.3). The trouble I'm having is with layers from a AGS map service (10.0). I'm not getting the whole URL to the map service endpoint by using the layer class and serviceProperties. Any ideas of how to get the rest service endpoint of a selected layer in ArcMap?

Thank you,

Tommy

0 Kudos
4 Replies
OwenEarley
Frequent Contributor

Try the following:

Open a new map document and add the Hurricanes map service:

http://sampleserver6.arcgisonline.com/arcgis/rest/services/Hurricanes/MapServer

Open the python command prompt and run the following code:

mapdoc = arcpy.mapping.MapDocument("CURRENT")
lyrs = arcpy.mapping.ListLayers(mapdoc)
for lyr in lyrs:
     if lyr.supports("serviceProperties"):
          sp = lyr.serviceProperties
          endpoint = "{0}/{1}/{2}".format(sp["URL"], lyr.name, sp["ServiceType"])
          print endpoint

You should end up with the map service URL:

map-service-url.png

0 Kudos
TommyStålnacke
Emerging Contributor

The problem though is when you rename the layer in ArcMap TOC. "lyr.name" then return the renamed layer name. So renaming the Hurricane map service gives me the following endpoint URL:

http://sampleserver6.arcgisonline.com/arcgis/services/renamed_layer/MapServer

Any other ideas?

Thank you,

Tommy

0 Kudos
OwenEarley
Frequent Contributor

The arcpy.mapping module does not seem to expose this.

If you check the Layer properties the original name is shown (even after renaming it in the ToC):

hurricanes.png

However the documentation for the arcpy.mapping Layer class shows no property that can be used to access this value unless you are using WMS service layers.

serviceProperties

(Read Only)

Keys for a web service dictionary

  • ServiceType —Property displaying the type of service. These include ImageServer, IMS, MapServer, TiledInternetLayer, WMS, and WCS.
  • URL —Property displaying the URL to the service. If the connection to ArcGIS for Server is through a local area network (LAN), this value will be null.
  • Server —Property displaying the server name. If the connection to ArcGIS for Server is through the Internet (HTTP), this value will be null.
  • UserName —Property displaying the user name used to access a secured service. If the service is not password protected, this property will be null.
  • ServiceName —IMS service layers only. Property displays the name of the IMS service.
  • WMSName —WMS service layers only. Property displays the text string for the WMS service used for machine-to-machine communication.
  • WMSTitle —WMS service layers only. Property displays the description title string for the WMS service.
  • Name —WMS service layers only. Property displays the text string for the WMS layer used for machine-to-machine communication.
  • Title —WMS service layers only. Property displays the description title string for the WMS layer.

Not sure if you can do this without heading down the ArcObjects path.

TommyStålnacke
Emerging Contributor

I took your advice and headed down the ArcObjects path, and I found a soultion there. Thank you.

0 Kudos