Add/Update Feature Layer Filter in ArcGIS Online with the ArcGIS API for Python

Table of Contents

Introduction

The ArcGIS API for Python is a powerful Python library that allows users to interact with and automate tasks in ArcGIS Online (or Portal). The API is excellent for programmatically creating, maintaining, and updating webmaps and feature layers in ArcGIS Online. In this post we will focus on updating a filter in a webmap layer! 

arcgis modules

The API provides access to your organisations ArcGIS Online via the GIS class in the gis module. This GIS class is the gateway to ArcGIS Online. We will need to import the WebMap class to create a WebMap object. The WebMap class is a part of the mapping module. This mapping module provides components for working with 2D and 3D maps and scenes, and also includes classes for map layers such as the MapFeatureLayer, MapImageLayer, and VectorTileLayer.
				
					## import GIS which provides the gateway entry to your AGOL
from arcgis.gis import GIS
## import WebMap from the mapping module
from arcgis.mapping import WebMap
				
			

Accessing ArcGIS Online

Our first port of call is to access your ArcGIS Online via the GIS class. There are a handful of ways to achieve access, if you are logged into your ArcGIS Online in ArcGIS Pro you can simply use "home", otherwise, another common way is to provide the ArcGIS Online URL, followed by your username and password.
				
					## Access AGOL
agol = GIS("home")
				
			
				
					## Access AGOL
agol = GIS(
    url = "https://your_organisation.maps.arcgis.com/",
    username = "Your_Username",
    password = "Your_Password"
)
				
			

Create a WebMap Object

We create the WebMap object from an Item object.
				
					## access the WebMap Item
wm_item = agol.content.get("WebMap_Item_ID")

## create a WebMap object
webmap = WebMap(wm_item)
				
			

Access the WebMap Layer and Update the Filter

The WebMap object has a get_layer() method where we can supply the layer name to retrieve the layer of interest. The WebMap Layer has a property named ‘layerDefinition’ which in turn has a property that can be associated with the layerDefinition called ‘definitionExpression’. The definitionExpression is where the filter criteria are stored.
				
					## get the layer of interest
lyr = webmap.get_layer(title="Name of layer as it appears in the WebMap")
## set the filter
lyr.layerDefinition.definitionExpression = "FIELDNAME = 'attribute'" # for text field
				
			

Update the WebMap object

We call the update() method on our WebMap object to commit the updated filter.
				
					## update the webmap
webmap.update()
				
			

Updating a Filter in Action!

Below is a map in ArcGIS Online currently showing the county boundaries of Ireland. 

We will run the previous code snippet providing a WebMap_Item_ID and setting the title parameter for the get_layer() method to County Boundaries (the name of the layer), and we will set the expression as per below.

				
					## get the layer of interest
lyr = webmap.get_layer(title="County Boundaries")
## set the filter
lyr.layerDefinition.definitionExpression = "COUNTY = 'KILDARE'"
				
			

We can see that the filter has been applied (with a map refresh).

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

Leave a Comment

Your email address will not be published. Required fields are marked *