Web maps are an essential tool for displaying geographic data on a web platform, making it easy to share information with others. However, it can be challenging to determine which web maps are using a specific feature layer in ArcGIS Online (AGOL). This can be especially difficult if there are many web maps and web apps in your AGOL organization. Fortunately, we can use Python to search through all the web apps and web maps in an organization to find those that reference a particular layer.
In this blog post, we will go over a Python script that can search through an AGOL organization to find web maps that use a single feature layer, and we will explain how to use it.
from arcgis.gis import GIS
import pandas as pd
# Connect to AGOL
gis = GIS("home")
# Layer ID to search for and its URL
find_id = 'abc123def456'
find_url = gis.content.get(find_id).url
# Pull list of all web maps in portal
webmaps = gis.content.search('', item_type='Web Map', max_items=-1)
# Pull list of all web apps in portal
webapps = gis.content.search('', item_type='Application', max_items=-1)
# Create empty list to populate with results
app_list = []
# Check each web app for matches
for w in webapps:
try:
# Get the JSON as a string
wdata = str(w.get_data())
# Check if URL is directly referenced
criteria = [
wdata.find(find_url) > -1,
# Check if any matching maps are in app
any([wdata.find(i) > -1 for i in matches])
]
# If layer is referenced directly or indirectly, append app to list
if any(criteria):
app_list.append(w)
# Some apps don't have data, so we'll just skip them if they throw a TypeError
except:
continue
# Print results as a dataframe
pd.DataFrame([{'title':a.title, 'id':a.id, 'type':a.type} for a in app_list])
We start by importing the necessary libraries and connecting to AGOL. Next, we specify the ID of the feature layer we are interested in and its URL. We then use the search()
function to retrieve all the web maps and web apps in our AGOL organization. The script then iterates through each web app, searching for a match between the app and the feature layer we are interested in. Finally, the script generates a Pandas dataframe that lists all the web apps that reference the feature layer.
By using this script, we can quickly identify all the web maps and web apps in our AGOL organization that reference a particular feature layer, saving us a significant amount of time and effort.
Step-By-Step
Here are step-by-step instructions for finding web maps that use a single feature layer in ArcGIS Online using a Notebook:
- Open your ArcGIS Online account and navigate to the
Notebooks
tab on the left-hand side of the screen. - Click the
New Notebook
button and selectPython 3
as your notebook type. - Give your notebook a name and description, then click
Create Notebook
. - In the first cell of your notebook, copy and paste the following code:
- Replace the
find_id
variable with the ID of the feature layer you want to search for. You can find this ID by navigating to the feature layer in your AGOL account and copying the last part of the URL. For example, if the URL ishttps://www.arcgis.com/home/item.html?id=abc123def456
, the ID isabc123def456
. - Run the code by clicking the
Run
button or by pressingShift + Enter
on your keyboard. The script will search through all the web maps and web apps in your AGOL organization and generate a Pandas dataframe that lists all the web apps that reference the feature layer. - If you want to export the results as a CSV file, add the following code to a new cell:
output_file = 'webapps_using_feature_layer.csv'
df.to_csv(output_file, index=False)
print(f'Saved results to {output_file}')
- Replace
webapps_using_feature_layer.csv
with the desired name of your output file. - Run the new cell by clicking the
Run
button or by pressingShift + Enter
on your keyboard. The results will be exported as a CSV file and saved to your notebook’s home directory.
That’s it! By following these steps, you can easily find web maps that use a single feature layer in ArcGIS Online using a Notebook.