Get all maps that contain a feature layer - Python API

517
5
Jump to solution
10-07-2020 06:38 AM
TannerArrington
New Contributor III

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
Occasional 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

5 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
Occasional 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

TannerArrington
New Contributor III

Super helpful, thanks a bunch for sharing! 

0 Kudos
JoshuaSharp-Heward
Occasional Contributor

Thanks Tanner, glad I could help out! 

0 Kudos
fklotz
by
New Contributor II

Hi @TannerArrington, 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)

 

0 Kudos