Create a List of Layer from Map running into issues where instance has no attributes

732
7
04-06-2022 01:29 PM
Kayden75
New Contributor III

I grabbed code from JoshuaSharp-Heward here Simple way to export a Layer List for a Web Map to... - Esri Community

and tweaked it a little to output the layer url with the layer name. Unfortunately I keep running into an issue where one of the items has no attribute and it causes the program to fail. I am trying to write in an exception but don't know how. any help would be appreciated. The exact error is "layer" instance has no attribute 'url'

from arcgis.gis import GIS
from arcgis.mapping import WebMap
gis = GIS('pro')# if running this from Notebook for ArcGIS in AGOL/Enterprise, replace this line with gis = GIS('home')
wmItemId = "" #put the id of the webmap in here
wmItem = gis.content.get(wmItemId)
wm = WebMap(wmItem)
print('\033[1m' +"MapName" )
for lyr in wm.layers:
print('\033[0m' + lyr.title + "\n" + lyr.url)

Tags (1)
0 Kudos
7 Replies
jcarlson
MVP Esteemed Contributor

You could do a full-blown try/except, but you really just want to include the URL if it exists. Including a succinct little ternary operator to look for the 'url' key in the dict is very simple:

[f"{lyr['title']} | {lyr['url'] if 'url' in lyr else 'No URL'}" for lyr in wm.layers]

Returns

['Records | https://maps.co.kendall.il.us/server/rest/services/Hosted/Change_Record_View/FeatureServer/0',
'Parcels | https://maps.co.kendall.il.us/server/rest/services/Hosted/Change_Record_View/FeatureServer/1']

 I don't actually have a no-URL layer to test this on, but I assume it would apply to a sketch layer or something?

- Josh Carlson
Kendall County GIS
Kayden75
New Contributor III

Awesome this works great. Thank You! Quick question, how do I add a /n to this? It isn't taking. Do I need to add a print in front of 

[f"{lyr['title']} | {lyr['url'] if 'url' in lyr else 'No URL'}" for lyr in wm.layers]

0 Kudos
Kayden75
New Contributor III

Unfortunately after testing it, it only works when I do one map at a time.

0 Kudos
Kayden75
New Contributor III

I got it to work by adding 

try:
print('\033[0m' + lyr.title + "\n" + lyr.url)
except:
print("No Url")

 

Thank you for your help

0 Kudos
jcarlson
MVP Esteemed Contributor

Oh, were you looking to do this for more than one map at a time? It wasn't clear in the original post.

Also, is there a reason you need it to be broken by a newline?

- Josh Carlson
Kendall County GIS
0 Kudos
Kayden75
New Contributor III

it's a little jank but I use FME to read in every map on an account then have it write out in python script then run the whole thing in arcgis pro so I know all of the layers for every map in our account. I needed a new line so when I copy and paste the output it goes into excel where the newline makes it easy to read.

Thank you for your help!

anonymous55
Occasional Contributor II

Hello
I have this code which is working with single item id. But I need to run this on all webmaps on my content or org. I am using notebook on AGOL
any idea how can I do this?

from arcgis.gis import GIS
from arcgis.mapping import WebMap
gis = GIS('home')# if running this from Notebook for ArcGIS in AGOL/Enterprise, replace this line with gis = GIS('home')
wmItemId = "myid" #put the id of the webmap in here
wmItem = gis.content.get(wmItemId)
wm = WebMap(wmItem)
for lyr in wm.layers:
    print(lyr.title)
    print(lyr.url)
0 Kudos