Query ArcGIS Online Point Features in a WebMap based on a Polygon Feature using 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 webmap by adding in a layer based on a selection! We will get our selected features (points) from an intersection with a polygon from another 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. We also need the FeatureLayer class and the intersects method from the geometry filters.
				
					## 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
## import FeatureLayer class to access the layers
from arcgis.features import FeatureLayer
## import the intersects filter to use with the feature layer query() method
from arcgis.geometry.filters import intersects
				
			

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("WM_ITEM_ID")

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

Access the WebMap Layers and Create FeatureLayer Objects

Let’s think about what we need to achieve. We want to dig into the geometry of a Feature in a Feature Layer. Accessing a WebMap layer alone won’t quite cut it as you cannot get access to features and therefore geometry via this avenue. We need to access the Feature Layer that is referenced by the layer in the WebMap. The WebMap object has a get_layer() method where we can supply the layer name to retrieve the layer of interest. We will get our point layer and polygon layer as a FeatureLayer object.

				
					## access the point layer of the WebMap and create a FeatureLayer object
pt_wml_url = webmap.get_layer(title = "PT_LAYER_TITLE").url
pt_fl = FeatureLayer(pt_wml_url, agol)

## access the polygon layer of the WebMap and create a FeatureLayer object
ply_wml_url = webmap.get_layer(title="PLY_LAYER_TITLE").url
ply_fl = FeatureLayer(ply_wml_url, agol)
				
			

Query the Polygon Layer to get the Geometry

We call the query() method on our polygon FeatureLayer object and access the geometry of the single (first) geometry returned from the FeatureSet (list).

				
					## query the polygon feature layer and get the geometry of the area of interest
query = ply_fl.query(where="SQL_STATEMENT")
ply_geom = query.features[0].geometry
				
			

Query the Point Layer to get a FeatureSet that Interescts the Polygon

We call the query() method on our point FeatureLayer object and supply the geometry of our polygon to the geometry_filter parameter.

				
					## query the point feature layer for any points that intersect the polygon
query = pt_fl.query(
    geometry_filter = intersects(ply_geom)
)
				
			

Define the new layer name and symbology

When we add the layer to our WebMap we want to name it and define symbology. We define these setting in a dictionary.

				
					## define a dictionary for the FeatureSet we want to add to the WebMap
## we set the 'title' which will be the name of the layer in the webmap
## and we define the 'renderer' which is the point symbology
lyr_dict = {
    "title" : "NEW_LAYER_TITLE",
    "renderer" : {
        "type": "simple",
        "symbol": {
            "type": "esriSMS",
            "color": [
                250,
                0,
                0,
                217
            ],
        "angle": 0,
        "xoffset": 0,
        "yoffset": 0,
        "size": 6,
        "style": "esriSMSCircle",
        "outline": {
            "type": "esriSLS",
            "color": [
                247,
                109,
                109,
                128
            ],
            "width": 1.125,
            "style": "esriSLSSolid"
            }
        }
    }

}
				
			

Add the FeatureSet to the WebMap

Call the WebMap object add_layer() method and supply the FeatureSet as the layer to add and the dictionary to the options parameter.
				
					## add the query (FeatureSet) object to the webmap with the lyr_dict options
webmap.add_layer(
    layer = query,
    options = lyr_dict
)
				
			

Update the WebMap

Update the WebMap object to commit our changes and add the layer to the WebMap.
				
					## update the webmap object
webmap.update()
				
			

The Workflow in Action

Below is a map with two layers, a point layer (NIAH) and a polygon layer (County Boundaries)

We run the script and are interested in points that intersect the county of Dublin. These get added to the map as the red circles.

If we turn the NIAH layer off we can see more clearly that our selected features have been added to the map.

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.

All the code in one place

You can find the entire code workflow below with links to important components in the documentation that were used.

				
					## 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
## import FeatureLayer class to access the layers
from arcgis.features import FeatureLayer
## import the intersects filter to use with the feature layer query() method
from arcgis.geometry.filters import intersects

################################################################################
## API Reference Links:
##  https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#gis
##  https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.ContentManager.get
##  https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#webmap
##  https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#arcgis.mapping.WebMap.get_layer
##  https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#featurelayer
##  https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#arcgis.features.FeatureLayer.query
##  https://developers.arcgis.com/python/api-reference/arcgis.geometry.filters.html#intersects
##  https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#featureset
##
## Description:
##  Select points based on a polygon and add to WebMap
##
## API Version: 2.2.0.1
##
################################################################################

## access AGOL
agol = GIS("home")

## get the item content that represents our webmap
wm_item = agol.content.get("WM_ITEM_ID")

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

## access the point layer of the WebMap and create a FeatureLayer object
pt_wml_url = webmap.get_layer(title = "PT_LAYER_TITLE").url
pt_fl = FeatureLayer(pt_wml_url, agol)

## access the polygon layer of the WebMap and create a FeatureLayer object
ply_wml_url = webmap.get_layer(title="PLY_LAYER_TITLE").url
ply_fl = FeatureLayer(ply_wml_url, agol)

## query the polygon feature layer and get the geometry of the area of interest
query = ply_fl.query(where="SQL_STATEMENT")
ply_geom = query.features[0].geometry

## query the point feature layer for any points that intersect the polygon
query = pt_fl.query(
    geometry_filter = intersects(ply_geom)
)


## define a dictionary for the FeatureSet we want to add to the WebMap
## we set the 'title' which will be the name of the layer in the webmap
## and we define the 'renderer' which is the point symbology
lyr_dict = {
    "title" : "NEW_LAYER_TITLE",
    "renderer" : {
        "type": "simple",
        "symbol": {
            "type": "esriSMS",
            "color": [
                250,
                0,
                0,
                217
            ],
        "angle": 0,
        "xoffset": 0,
        "yoffset": 0,
        "size": 6,
        "style": "esriSMSCircle",
        "outline": {
            "type": "esriSLS",
            "color": [
                247,
                109,
                109,
                128
            ],
            "width": 1.125,
            "style": "esriSLSSolid"
            }
        }
    }

}

## add the query (FeatureSet) object to the webmap with the lyr_dict options
webmap.add_layer(
    layer = query,
    options = lyr_dict
)

## update the webmap object
webmap.update()
				
			

Leave a Comment

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