Get all maps that contain a feature layer - Python API

9229
11
Jump to solution
10-07-2020 06:38 AM
TanGnar
Frequent Contributor

Is it possible to find all of the maps that contain any one feature layer in ArcGIS Online using the Python API? For example, if there is a feature layer using frequently across the organization, and I want to find out which maps are using that layer.

I see some documentation about finding relationships between items here. The Map2Service seems to work in one direction - find the services in the map. But is there a way to do this the other direction? 

1 Solution

Accepted Solutions
JoshuaSharp-Heward
Frequent Contributor

Hi Tanner,

I wrote a script to do this last year: gis-administration/search_webmaps_for_services.py at master · joshsharpheward/gis-administration · G... which I designed to be run from ArcGIS Pro, you can download the gp toolbox in that repo to use it straight away! Otherwise if you want to run it from another environment you can copy the code from the script and just modify the 'gis = GIS("pro")' and 'services = arcpy.GetParameter(0)' lines. Note my script also scans web applications in your portal/agol to see if the service is being used in any configured searches, but this can be stripped out easily.

View solution in original post

11 Replies
MehdiPira1
Esri Contributor

Hi Tanner Arrington‌,

You can use reverse direction in related items method to get the web maps in which a feature layer is being used, however, you need to establish a relationship using (add_relationship) with the web maps first.

webmap1 = gis.content.get('webmap ID')
webmap_related_item = gis.content.get('Feature Layer ID')

webmap1.add_relationship(rel_item= webmap_related_item, rel_type= 'Map2Service')
webmap_related_item.related_items('Map2Service', 'reverse')‍‍‍‍‍‍

and conversely, forward direction provides the feature layers (collections) used in the web map.

I hope that helps.

JoshuaSharp-Heward
Frequent Contributor

Hi Tanner,

I wrote a script to do this last year: gis-administration/search_webmaps_for_services.py at master · joshsharpheward/gis-administration · G... which I designed to be run from ArcGIS Pro, you can download the gp toolbox in that repo to use it straight away! Otherwise if you want to run it from another environment you can copy the code from the script and just modify the 'gis = GIS("pro")' and 'services = arcpy.GetParameter(0)' lines. Note my script also scans web applications in your portal/agol to see if the service is being used in any configured searches, but this can be stripped out easily.

TanGnar
Frequent Contributor

Super helpful, thanks a bunch for sharing! 

0 Kudos
JoshuaSharp-Heward
Frequent Contributor

Thanks Tanner, glad I could help out! 

0 Kudos
fklotz
by
Regular Contributor

Hi @TanGnar, I'm a little late to this, but:

You can do this in the ArcGIS python API without adding a relationship by looping through the layers in the maps.  The benefits of this method are that it can be done in a standard notebook on ArcGIS online (no credit usage!) without editing your content or adding relationships or can be used in your local python environment without using up an ESRI license.

""" This code is meant to be run in a standard python notebook in ArcGIS Online.  
If you are using it outside of that environment, it may require modification.

Requirements:  ArcGIS Python API, ArcGIS Online Organizatation containing maps
"""

from arcgis.mapping import WebMap
from arcgis.gis import GIS
gis = GIS("home")

def find_maps_with_layer(lyr_to_find, map_item_query='1=1'):
    """
    Searches for items using the map_item_query, then searches each each map 
    found for any layers who's url contains the lyr_to_find
    
    Note that the web map search is limited to 10000 items below
    
    Author: Farley Klotz
    :param lyr_to_find: url of layer to find (or portion of url)
    :param map_item_query: AGO item query to limit web maps searched
    """
    web_map_items = gis.content.search(query=map_item_query, item_type="Web Map", max_items=10000)
    print(f"Searching {len(web_map_items)} web maps")
    maps_with_layer = []
    maps_without_layer = []
    for item in web_map_items:
        found_it = False
        wm = WebMap(item)
        lyrs = wm.layers
        for lyr in lyrs:
            found_it = lyr_to_find in lyr.url
        if found_it:
            print(f'{item.title} contains the layer')
            display(item)
            maps_with_layer.append(item)
        else:
            maps_without_layer.append(item)
    print(f"Found {len(maps_with_layer)} maps which contain the layer")
    print(f"Found {len(maps_without_layer)} maps which do not contain the layer")
    return maps_with_layer, maps_without_layer
    

# Parameters:
lyr_to_find = 'https://services.arcgis.com/XN3gLesriRY2KtI6/arcgis/rest/services/my_service_layer/FeatureServer'
map_item_query = "Title:my map title"

# lets see some results:
maps_with_layer, maps_without_layer = find_maps_with_layer(lyr_to_find, map_item_query)

 

Farley
JoëlHempenius3
Frequent Contributor

Hi @fklotz ,

Nice script, saved me a lot of time writing it myself. 

Small improvement at line 30:

 

 

        for lyr in lyrs:
            if 'url' in lyr and not found_it:                
                found_it = lyr_to_find.lower() in lyr.url.lower()     
            elif 'layerType' in lyr and lyr.layerType =="GroupLayer":
                for sublyr in lyr.layers:
                    if 'url' in sublyr and not found_it:                
                        found_it = lyr_to_find.lower() in sublyr.url.lower()

 

 

 

I got an exception when it found a layer in my GIS without url. The first part of the if statement fixes this. And it should stop searching the webmap when it already found a match, otherwise another layer in the webmap might set the found_it variable to False again, the second part of my if statement should fix that. 

Edit: added support for the new Map Viewer Group Layers. Noticed that the script didn't find a map and this was due to a group layer.

Another Edit: found a case where an url was not found, because part of it was in uppercase. Updated my snippet to take care of this

-Joël Hempenius.

Languages: JavaScript, Python and Dunglish
AaronKoelker
Frequent Contributor

Thank you both @fklotz and @JoëlHempenius3 , works great and saved me some work as well.

I will add that I was getting an error when it hit certain web maps that contained uploaded shapefiles (Classic Map Viewer; and which have no url). To get around it I just wrapped Joëls whole bit into a try/except clause. I'm sure there's a more elegant way to handle that issue but this works and I can then investigate the map manually at that point if I suspect the layer I'm searching for is in there.

 

try:
    for lyr in lyrs:
        if 'url' in lyr and not found_it:                
            found_it = lyr_to_find.lower() in lyr.url.lower()     
        elif 'layerType' in lyr and lyr.layerType =="GroupLayer":
            for sublyr in lyr.layers:
                if 'url' in sublyr and not found_it:                
                    found_it = lyr_to_find.lower() in sublyr.url.lower()
except Exception as error:
    print(f'{item.title} led to an error')
    print(error)
    display(item)
    continue

 

-Aaron
ChrisWiebke
Regular Contributor

Thanks for posting this!  Thanks to all who contributed updates too!  I split the url so I could just look for the service name.  This way I didn't have to handle different urls based on if they were map or feature services, and if they were added to the map as a web layer or as a service.

                if 'url' in lyr and not found_it:
                    serviceUrlSplitList = lyr.url.split('/')
                    found_it = lyr_to_find in serviceUrlSplitList
                elif 'layerType' in lyr and lyr.layerType == "GroupLayer":
                    for sublyr in lyr.layers:
                        if 'url' in sublyr and not found_it:
                            serviceUrlSplitList = lyr.url.split('/')
                            found_it = lyr_to_find in serviceUrlSplitList

 

anonymous55
Frequent Contributor

Hello @ChrisWiebke @fklotz ,

I am looking for code which just go through each webmaps in my organization and give me url items inside of webmaps with title of urls. 

I want to create table like this:

webmap titleurl titleurl
NYC Crime mapCity Council Districtshttps://
 Map PLUTOhttps://
 police precinctshttps://
NYC Bike Share mapSchool Districtshttps://
 Subway Stopshttps://

 

note: some items are inside of group in webmap which I need to scan those to and return urls  

I have this code which I need to manually copy paste webmap id and also doesn't return url items inside group.

 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
from arcgis.mapping import WebMap
from arcgis.widgets import MapView
gis = GIS('home')

wmItem = gis.content.search(query= 'YourWebMapID')
print(wmItem)

wmItemId = "YourWebMapID" #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)

 

I will appreciate if someone can help me with this.

Thanks

0 Kudos