Select to view content in your preferred language

Simple way to export a Layer List for a Web Map to Text

3985
18
05-25-2017 09:47 AM
BrianO_keefe
Regular Contributor II

Is there a simple way to export the layer list from a web map (built with WAB) to a text file?

18 Replies
anonymous55
Occasional Contributor II

Hello @JoshuaSharp-Heward 
I am getting this error when I am using your code on AGOL notebook

 

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)‍‍‍‍‍‍‍‍

 

Error:

  Input In [8]
    print(lyr.title)‍‍‍‍‍‍‍‍
                    ^
SyntaxError: invalid non-printable character U+200D
0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

Hi,

It looks to me like there was an issue with the copy pasting of the code - try deleting the whole line "print(lyr.title)" and re-typing it manually, and let me know if that resolves it!

anonymous55
Occasional Contributor II

Thanks @JoshuaSharp-Heward 

sorry for late respond. I manually re-type it and it is working. Just one more question
Do you know how can I modifie your code to get URL of each layers in webmap 

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

No worries, glad to hear it's working now!

You can replace "lyr.title" with "lyr.url" to print out the url of a layer. Best to do it in an if statement like so:

if hasattr(lyr, "url"):
   print(lyr.url)

as there are some layer types that don't have "url" attribute (for example, vector tile services have a 'styleUrl' instead) and if you don't have that step then the code will fail when it hits one of those layers. Also note that this simple bit of code above to print out the layers in a map doesn't handle group layers, I'd have to fetch another couple lines of code to deal with that if you need that functionality.

TukFull
New Contributor

Would it be a hassle to provide an example that pulls group layers as well?

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

Hi,

Do you mean code that can look within group layers to pull out the layers within? If so, I've got a function that can handle those as well! Works for any number of levels of grouping.

 

def unnestlayers(layer):
    wmlayers = []
    for lyr in layer.layers:
        try:
            if hasattr(lyr, "layers"):
                wmlayers += unnestlayers(lyr)
            else:
                wmlayers.append(lyr)
        except:
            print("Something went wrong")

    return wmlayers

 

Then just input the webmap and it should spit out a list of the actual layers

 

layers = unnestlayers(web_map)

 

TukFull
New Contributor

This worked great, thank you!

0 Kudos
anonymous55
Occasional Contributor II

@JoshuaSharp-Heward Thanks this is pretty good.
sorry to bother you with this. Do you know how can I run this for my whole arcgis online webmaps? Instead of manually copy paste ItemId for each webmap.

Thanks again for your help

0 Kudos
JoshuaSharp-Heward
Occasional Contributor III

Hi,

You can loop through all of the webmaps in your arcgis online by using the following code:

web_maps = gis.content.search(query="", item_type="Web Map", max_items=10000)
for item in web_maps:
   wm = WebMap(item)

Then use the rest of the code that you have been using! If you have any issues you can refer back to this code in github that I put together which searches through web maps for a particular web service https://github.com/joshsharpheward/gis-administration/blob/master/search_webmaps_for_services.py

Cheers,

Josh

0 Kudos