I`m trying to find a way to add an online layer to my local map, through Python. I know how to grab the layer info, but don't know how to use it.

onlinelayer = gis.content.search(mylayer)[0]
itemid= onlinelayer.id

So I got the item ID, but how I apply it? Is there something similar to addDataFromPath()?

  • You need to designate your pro project (i.e. aprx), and then designate your map object (i.e. 'm'). I believe you need to then do something like 'myitem = gis.content.get(itemid)' and then use that to do something like 'm.addLayer(myitem)'. I might be thinking of jupyter notebooks, but I believe something along those lines should get you closer. – alexGIS Oct 4 at 18:54
  • My code is over 600 lines long, its a python toobox, I just wrote the relevant part. m.addLayer(myitem) does not work tho. Maybe I am not writing the correct variable. How do I get the "myitem" you mentioned? Using gis.content.get(itemid)? Because that also did not work. – Gustavo Oct 4 at 18:59
  • What library did you import to get access to that Search function? Any imports needed to run a code snippet should be part of a code snippet. – PolyGeo Oct 4 at 19:59
  • The map.addLayer(Layer) command comes from arcpy and can only accept references to existing layers in Pro maps or layer files (.lyrx, .lyr). It won't accept an item reference from the arcgis module: "The layer that is added can reference an already existing layer in a the same project or separate project, or reference a layer file (.lyr or .lyrx) on disk. A reference to a layer can be a single layer, a group layer with multiple sublayers, or a collection of root-level layers and group layers if referencing a .lyrx file. " pro.arcgis.com/en/pro-app/arcpy/mapping/map-class.htm – alexGIS Oct 4 at 20:39

This worked for me:

aprx = arcpy.mpArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]
itemid = '2ec9f27bea254a428e4eb70e7650672d'
m.addDataFromPath("https://mycounty.maps.arcgis.com/home/item.html?id=" + itemid)

The layer gets added to the Pro map.

  • Perfect, that is exactly what I was looking for! – Gustavo Oct 4 at 20:48
  • 1
    Oh good! Glad that works :-) – alexGIS Oct 4 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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