Select to view content in your preferred language

A Tool to Find "ALL" Web Maps a Feature Layer is in: Script is in description

406
2
01-30-2025 08:45 AM
Status: Open
Nick_Creedon
Frequent Contributor

Please Read: Run script at your own risk

I have seen a lot of people asking if there is a way to see how many maps a layer is in. As an administrator, there is definitely some frustration in deleting layers and then finding out you didn't replace it in all of the maps. I think this script is a step in the right direction but it is not perfect, I receive a InsecureRequestWarning and Adding Certificate verification is strongly advised with the webmaps names under the warning

I want to share my findings with you all, maybe someone else has created something similar. Tag my post to an existing one and or share their post here please.

 

Here is what you need to watch out for:

Nick_Creedon_1-1738254132783.png

Here is a partial screenshot out the printed message:

Nick_Creedon_0-1738254069154.png

 

 

  

from arcgis.gis import GIS
import pandas as pd

# Connect to ArcGIS Online
gis = GIS("home")

# Specify the name of your hosted feature layer
feature_layer_name = "Feature_Layer_Name_Here"

# Search for the feature layer item by name
search_results = gis.content.search(query=f'title:"{feature_layer_name}" AND owner:{gis.users.me.username}',
                                    item_type="Feature Layer")

# Check if the search returned any results
if not search_results:
    print(
        f"Error: Could not find a feature layer with the name '{feature_layer_name}'. Please check the name and try again.")
else:
    # Get the feature layer item
    feature_layer_item = search_results[0]

    # Print the details of the found item
    print(f"Found feature layer: {feature_layer_item.title}")
    print(f"Item ID: {feature_layer_item.id}")
    print(f"Item URL: {feature_layer_item.url}")


    # Function to search and count maps
    def search_and_count_maps(feature_layer_item):
        # Search for all web maps in the organization
        web_maps = gis.content.search(query='type:"Web Map"', max_items=1000)

        # Initialize counter and list for maps that include the feature layer
        map_count = 0
        map_list = []

        # Iterate through each web map and inspect its layers
        for web_map in web_maps:
            try:
                map_data = web_map.get_data()
                if 'operationalLayers' in map_data:
                    for layer in map_data['operationalLayers']:
                        if 'url' in layer and feature_layer_item.url in layer['url']:
                            map_count += 1
                            map_list.append({
                                'Map Title': web_map.title,
                                'Map ID': web_map.id
                            })
                            break  # No need to check other layers in this map
            except:
                continue  # In case of an error, skip this map and continue with the next one

        return map_count, map_list


    # Count the maps and get list of maps
    map_count, map_list = search_and_count_maps(feature_layer_item)

    # Print the result
    print(f"The feature layer is included in {map_count} web maps.")

    # Optionally, create a DataFrame to store more detailed information
    if map_list:
        df = pd.DataFrame(map_list)
        print("\nList of Maps:")
        print(df.to_string(index=False))
    else:
        print("No maps found for this feature layer.")

 

2 Comments
DavidSolari

If you're already querying every web map in your org, why not build a comprehensive table?

from arcgis.gis import GIS, Item

def build_layer_lookup(portal: GIS, query: str = "*") -> dict[Item, list[tuple[str | None, str | None]]]:
    retval = {}
    for web_map in portal.content.search(query, item_type="Web Map", max_items=10000):
        retval[web_map] = []            
        data = web_map.get_data()
        for layer in data.get("operationalLayers", []) + data.get("tables", []):
            retval[web_map].append((layer.get("itemId", None), layer.get("url", None)))
    return retval

portal = GIS("Pro")
lookup = build_layer_lookup(portal)
table = []
for web_map, layers in lookup.items():
    for layer_id, layer_url in layers:
        table.append((layer_id, layer_url, web_map.id))
Nick_Creedon

A comprehensive table is a great idea, it would be a really cool way to view all of your maps and data within the maps. If I am reading that right. Someone might not want to see a table of all webmaps and layers in the maps if they are trying to locate one layer but there are definitely cases when seeing everything is beneficial.