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-to-create-a-view-from-a-hosted-feature-layer-and-to-define-a-view-fefinition by Earl Medina.
<SPAN class="comment token">## ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</SPAN>
<SPAN class="comment token">## Script: agol_define_areas_of_interest_on_hosted_feature_layer_views.py</SPAN>
<SPAN class="comment token">## Goal: to define an area of interest on multiple hosted feature layer views using input geometry</SPAN>
<SPAN class="comment token">## Author: Egge-Jan Polle - Tensing GIS Consultancy</SPAN>
<SPAN class="comment token">## Date: March 27, 2019</SPAN>
<SPAN class="comment token">## ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</SPAN>
<SPAN class="comment token">#</SPAN>
<SPAN class="comment token"># This script should be run within a specific ArcGIS/Python environment using the batch file below</SPAN>
<SPAN class="comment token"># (This batch file comes with the installation of ArcGIS Pro)</SPAN>
<SPAN class="comment token"># "C:\Program Files\ArcGIS\Pro\bin\Python\scripts\propy.bat" agol_define_areas_of_interest_on_hosted_feature_layer_views.py</SPAN>
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>features <SPAN class="keyword token">import</SPAN> FeatureLayerCollection
<SPAN class="keyword token">from</SPAN> provide_credentials <SPAN class="keyword token">import</SPAN> provide_credentials
username<SPAN class="punctuation token">,</SPAN> password <SPAN class="operator token">=</SPAN> provide_credentials<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
my_agol <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"https://www.arcgis.com"</SPAN><SPAN class="punctuation token">,</SPAN> username<SPAN class="punctuation token">,</SPAN> password<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># The Hosted Feature Layer containing a countrywide dataset </SPAN>
police_stations <SPAN class="operator token">=</SPAN> my_agol<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'<itemID>'</SPAN><SPAN class="punctuation token">)</SPAN>
police_stations_flc <SPAN class="operator token">=</SPAN> FeatureLayerCollection<SPAN class="punctuation token">.</SPAN>fromitem<SPAN class="punctuation token">(</SPAN>police_stations<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># The Hosted Feature Layer containing the regional division</SPAN>
provinces <SPAN class="operator token">=</SPAN> my_agol<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'<itemID>'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>query<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Get the Spatial Reference</SPAN>
spat_ref <SPAN class="operator token">=</SPAN> provinces<SPAN class="punctuation token">.</SPAN>spatial_reference
<SPAN class="comment token"># Loop through the regional division to create the views</SPAN>
<SPAN class="keyword token">for</SPAN> index<SPAN class="punctuation token">,</SPAN> province <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>provinces<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
prov_name <SPAN class="operator token">=</SPAN> provinces<SPAN class="punctuation token">.</SPAN>features<SPAN class="punctuation token">[</SPAN>index<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>attributes<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Provincienaam'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>upper<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'-'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'_'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>prov_name<SPAN class="punctuation token">)</SPAN>
view_name <SPAN class="operator token">=</SPAN> police_stations<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">+</SPAN> <SPAN class="string token">"_"</SPAN> <SPAN class="operator token">+</SPAN> prov_name <SPAN class="operator token">+</SPAN> <SPAN class="string token">"_View"</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>view_name<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Get the geometry for the regions</SPAN>
view_geom <SPAN class="operator token">=</SPAN> provinces<SPAN class="punctuation token">.</SPAN>features<SPAN class="punctuation token">[</SPAN>index<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>geometry<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'rings'</SPAN><SPAN class="punctuation token">)</SPAN>
new_view <SPAN class="operator token">=</SPAN> police_stations_flc<SPAN class="punctuation token">.</SPAN>manager<SPAN class="punctuation token">.</SPAN>create_view<SPAN class="punctuation token">(</SPAN>name<SPAN class="operator token">=</SPAN>view_name<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Search for newly created View</SPAN>
view_search <SPAN class="operator token">=</SPAN> my_agol<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN>view_name<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
view_flc <SPAN class="operator token">=</SPAN> FeatureLayerCollection<SPAN class="punctuation token">.</SPAN>fromitem<SPAN class="punctuation token">(</SPAN>view_search<SPAN class="punctuation token">)</SPAN>
service_layer <SPAN class="operator token">=</SPAN> view_flc<SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># Populate the update_dict with the geometry and the spatial reference</SPAN>
update_dict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"viewLayerDefinition"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">"filter"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">{</SPAN><SPAN class="string token">"operator"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="string token">"esriSpatialRelContains"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"value"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">{</SPAN><SPAN class="string token">"geometryType"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="string token">"esriGeometryPolygon"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"geometry"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">{</SPAN><SPAN class="string token">"rings"</SPAN><SPAN class="punctuation token">:</SPAN> view_geom<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"spatialReference"</SPAN><SPAN class="punctuation token">:</SPAN>spat_ref<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">}</SPAN>
<SPAN class="comment token"># Update the definition to include the Area of Interest</SPAN>
service_layer<SPAN class="punctuation token">.</SPAN>manager<SPAN class="punctuation token">.</SPAN>update_definition<SPAN class="punctuation token">(</SPAN>update_dict<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Done!'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>