Create view from Hosted Feature Layer and define Area of Interest

03-27-2019 08:06 AM
MVP Regular Contributor
4 1 457

Define Area of Interest on Hosted Feature Layer View using ArcGIS API for Python

Let's say you are responsible for a Hosted Feature Layer with data for the whole country and you want to share these data with regional teams in such a way that they are able to see only records in their own region. For many practical reasons you don't want to divide your data over multiple Hosted Feature Layers. You want to create regional views on the data instead.

You can draw these regions manually, following the procedure on this page: But hey, you know your regions, and you don't want to draw them by hand. Instead you want to use existing geometry and create the views automatically.

In the example below we use data from the Netherlands. We have a data set with police stations ('Politiebureaus') and one with provinces ('Provincies (Bestuurlijke Grenzen 2019)')  - with the ArcGIS Online Assistant we have copied these data sets to our own environment.

See the script below how we create twelve views - one for each province - showing only the police stations within the provincial boundaries

Hope this helps!

P.S. for another example - setting a view definition - you might have a look at this blog by Earl Medina.

## ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Script:
## Goal: to define an area of interest on multiple hosted feature layer views using input geometry
## Author: Egge-Jan Polle - Tensing GIS Consultancy
## Date: March 27, 2019
## ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# This script should be run within a specific ArcGIS/Python environment using the batch file below
# (This batch file comes with the installation of ArcGIS Pro)
# "C:\Program Files\ArcGIS\Pro\bin\Python\scripts\propy.bat"

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
from provide_credentials import provide_credentials

username, password = provide_credentials()

my_agol = GIS("", username, password)

# The Hosted Feature Layer containing a countrywide dataset
police_stations = my_agol.content.get('<itemID>')
police_stations_flc = FeatureLayerCollection.fromitem(police_stations)

# The Hosted Feature Layer containing the regional division
provinces = my_agol.content.get('<itemID>').layers[0].query()

# Get the Spatial Reference
spat_ref = provinces.spatial_reference

# Loop through the regional division to create the views
for index, province in enumerate(provinces):
prov_name = provinces.features[index].attributes['Provincienaam'].upper().replace('-','_')
view_name = + "_" + prov_name + "_View"
# Get the geometry for the regions
view_geom = provinces.features[index].geometry.get('rings')
new_view = police_stations_flc.manager.create_view(name=view_name)

# Search for newly created View
view_search =[0]
view_flc = FeatureLayerCollection.fromitem(view_search)

service_layer = view_flc.layers[0]

# Populate the update_dict with the geometry and the spatial reference
update_dict = {"viewLayerDefinition":{"filter":
{"rings": view_geom,

# Update the definition to include the Area of Interest

1 Comment
New Contributor II

Works for my case exactly.


About the Author
GIS Consultant at Tensing GIS Consultancy B.V.