2

I want to update over 100 MXDs by adding 4/5 ArcGIS Online/ArcGIS REST Services directory layers. I am unsure whether this is possible using Python and can only find documentation on how to access ArcGIS Online layers via the ArcGIS API and Jupyter Notebooks. How can I add data from ArcGIS Online via Python to a MXD or even how to use ArcGIS Online layers within ArcPy?

Sample code I've tried so far:

from arcgis.gis import GIS

trailheads_id = '7ff2ea905ad343f98dfd09a723938784'

gis = GIS()

trailheads_item = gis.content.get(trailheads_id)
trailheads_item


import arcpy  
# Hook into the map document  
mxd = arcpy.mapping.MapDocument(r"Path\To\MapDocument")  

# Hook into the data frame where you want to add the layer  
df  = arcpy.mapping.ListDataFrames(mxd)[0] 
2
  • Welcome to GIS SE. As a new user, please be sure to take the short tour to learn about the site's focused Q&A format. For questions about code, please edit to tell us what is/is not happening when you run the code and also list the text of any error messages you are receiving.
    – Andy
    Commented Jan 6, 2020 at 12:40
  • Your code doesn't actually attempt to extract the URL from the item or use it to add a new layer to the ArcMap document.
    – Vince
    Commented Jan 6, 2020 at 13:23

1 Answer 1

2

The only way to add a layer using Python/arcpy to ArcMap is using the arcpy.mapping.AddLayer function. This function works with mapping layers.

No matter what you do in the ArcGIS for Python API, you'll never get back an arcpy mapping layer object. Meaning: you cannot simply do a search using the Python API, and pass that reference through to arcpy.mapping.AddLayer. These pieces just aren't functionality compatible.

Now an arcpy.mapping.Layer object is either a true layer object, or a pointer to a layer file (.lyr). I suspect your only chance at making this work is if you had a layer file already created that referenced a web service. Then, using a function to update the data source of said layer file with a new URL, you'd then add this layer to your map. However, I'm doubtful you can actually update the data source of a layer file with a new web service URL. The doc doesn't show any samples that do this, nor hint at the possibility.

In short, you'd need to the following in your code (and even then, I'm skeptical it'll work):

  • Get the web service URL out of the item you've found. This would be from your trailheads_item
  • Create a layer file from a layer in your map that points to a webservice. This will be your template layer file (.lyr)
  • Create a reference to this layer file as a layer object
  • Try to update the datasource of this layer object with the URL you've previously extracted using the Python API
  • Add this updated layer file/object to your MXD. Save. Open the next MXD and continue on...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.