Is there a simple way to export the layer list from a web map (built with WAB) to a text file?
open the feature service in arcmap (in catalog you will see an option for feature services, then use arcpy to list the layers
Is there a way to do this for all services at once? Thanks!
Do you have access to ArcGIS Pro or ArcGIS Notebook (in AGOL or enterprise)? If so this can be done using the ArcGIS API for Python pretty easily!
Yes, I have both of those. Any help is appreciated!! Thank you!
Easy then!
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>mapping <SPAN class="keyword token">import</SPAN> WebMap gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'pro'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="comment token"># if running this from Notebook for ArcGIS in AGOL/Enterprise, replace this line with gis = GIS('home')</SPAN> wmItemId <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN> <SPAN class="comment token">#put the id of the webmap in here</SPAN> wmItem <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN>wmItemId<SPAN class="punctuation token">)</SPAN> wm <SPAN class="operator token">=</SPAN> WebMap<SPAN class="punctuation token">(</SPAN>wmItem<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> wm<SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">.</SPAN>title<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I wrote that to be run from the Python window in Pro, but see my note in line 3 about using it in ArcGIS Notebook. Just put the ID of the web map item into line 4 and it should print out all the layers for the given webmap.
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
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!
Thanks @JoshuaSharp-Heward
sorry for late respond. I manually re-type it and it is working. Just one more questionDo you know how can I modifie your code to get URL of each layers in webmap
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.
@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
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
Would it be a hassle to provide an example that pulls group layers as well?
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)
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.