I need to be able to progammatically create Hosted Feaure Layer Views of a number of services which we collaborate to ArcGIS Online. This is to provide a community view of data we hold at Council level. I have found an excellent Python script - https://community.esri.com/t5/gis-life-blog/create-view-from-hosted-feature-layer-and-define/ba-p/89... - which I have managed to hack to use our own data. This works perfectly when the source item consists of a single layer. However I am completely failing to make it work when the source layer for the view is a sublayer.
For example I want to create a community view of the Places theme of our Local Plan Policy areas - https://ren.maps.arcgis.com/home/item.html?id=805b7050bcfd4154a6a15f533eae7a52
https://services2.arcgis.com/fq3IIWzrdf1xBoPI/arcgis/rest/services/LDP_ADOPTED_2021/FeatureServer/6
using our Community Council boundaries - ItemID 0382e3a4a2e944858426ddde271211f0
e.g. places = gis.content.get('0382e3a4a2e944858426ddde271211f0').layers[1].query()
I just don't seem to be able to find a way to make this work. Any help gratefully received.
Solved! Go to Solution.
Hi @SeanCulpan
You need to use the view_layers parameter for the creat_view() method and supply a list of feature layers to add to the view, in this case there is one layer to add.
## create the view, the view will contain one layer
new_view = flc.manager.create_view(
name = "NAME_OF_NEW_VIEW",
## select the layer based on its id
view_layers = [flc.layers[6]]
)
Here's the full script that works for me.
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
## access agol
agol = GIS("home")
## access the feature service item that contains the layers
fs_item = agol.content.get("FS_ITEM_ID")
## create a FeatureLayerCololection object from the feature service item
flc = FeatureLayerCollection.fromitem(fs_item)
## get the feature that represents the polygon to apply as a geometry filter
aoi_record = agol.content.get("ANOTHER_FS_ITEM_ID").layers[0].query(where="COUNTY='GALWAY'")
## get the srs of the aoi
srs = aoi_record.spatial_reference
## get the geometry of the aoi polygon
view_geom = aoi_record.features[0].geometry.get('rings')
## create the view, the view will contain one layer
new_view = flc.manager.create_view(
name = "NAME_OF_NEW_VIEW",
## select the layer based on its id
view_layers = [flc.layers[6]]
)
## access that layer
lyr = new_view.layers[0]
## create a dictionary to define the geometry filter with the aoi
update_dict = {
"viewLayerDefinition" : {
"filter" : {
"operator" : "esriSpatialRelContains",
"value" : {
"geometryType" : "esriGeometryPolygon",
"geometry" : {
"rings": view_geom,
"spatialReference":srs
}
}
}
}
}
## update the view to apply the geometry filter
print(lyr.manager.update_definition(update_dict))
I hope I got the workflow you were looking for correct.
Hi @SeanCulpan
You need to use the view_layers parameter for the creat_view() method and supply a list of feature layers to add to the view, in this case there is one layer to add.
## create the view, the view will contain one layer
new_view = flc.manager.create_view(
name = "NAME_OF_NEW_VIEW",
## select the layer based on its id
view_layers = [flc.layers[6]]
)
Here's the full script that works for me.
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
## access agol
agol = GIS("home")
## access the feature service item that contains the layers
fs_item = agol.content.get("FS_ITEM_ID")
## create a FeatureLayerCololection object from the feature service item
flc = FeatureLayerCollection.fromitem(fs_item)
## get the feature that represents the polygon to apply as a geometry filter
aoi_record = agol.content.get("ANOTHER_FS_ITEM_ID").layers[0].query(where="COUNTY='GALWAY'")
## get the srs of the aoi
srs = aoi_record.spatial_reference
## get the geometry of the aoi polygon
view_geom = aoi_record.features[0].geometry.get('rings')
## create the view, the view will contain one layer
new_view = flc.manager.create_view(
name = "NAME_OF_NEW_VIEW",
## select the layer based on its id
view_layers = [flc.layers[6]]
)
## access that layer
lyr = new_view.layers[0]
## create a dictionary to define the geometry filter with the aoi
update_dict = {
"viewLayerDefinition" : {
"filter" : {
"operator" : "esriSpatialRelContains",
"value" : {
"geometryType" : "esriGeometryPolygon",
"geometry" : {
"rings": view_geom,
"spatialReference":srs
}
}
}
}
}
## update the view to apply the geometry filter
print(lyr.manager.update_definition(update_dict))
I hope I got the workflow you were looking for correct.
Thank you. I was missing the list of layers in the create_view method. Got my Notebook working as expected now.