Create view from Hosted Feature Layer and define Area of Interest

2198
1
03-27-2019 08:06 AM
Egge-Jan_Pollé
MVP Regular Contributor
6 1 2,198

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: https://doc.arcgis.com/en/arcgis-online/manage-data/set-view-definition.htm 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 https://community.esri.com/groups/arcgis-python-api/blog/2019/02/11/using-the-arcgis-api-for-python-... by Earl Medina.

## ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Script: agol_define_areas_of_interest_on_hosted_feature_layer_views.py
## 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" agol_define_areas_of_interest_on_hosted_feature_layer_views.py

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

username, password = provide_credentials()

my_agol = GIS("https://www.arcgis.com", 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('-','_')
    print(prov_name)
    view_name = police_stations.name + "_" + prov_name + "_View"
    print(view_name)
    # 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 = my_agol.content.search(view_name)[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":   
    {"operator":"esriSpatialRelContains","value":
    {"geometryType":"esriGeometryPolygon","geometry":
    {"rings": view_geom,
    "spatialReference":spat_ref}}}}}

    # Update the definition to include the Area of Interest
    service_layer.manager.update_definition(update_dict)

print('Done!')‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
1 Comment
About the Author
GIS Consultant at Avineon-Tensing