I'm trying to add a filtered view of an ArcServer map service (or feature service, if necessary) to ArcGIS Online by REST service URL using the API for Python. I can create the item in ArcGIS online using the url, but how do I add a filter? When I do this manually, I add the item to my content, go to the item details page, got the Visualization tab, click filter, add the filter, and save. Can this be done programmatically, using the Python API? Any help would be appreciated.
Edit: Perhaps layer definition or definition expression are better terms than filter.
Solved! Go to Solution.
Hi Greg,
The code below shows how you could update the filter for a FeatureService with 1 FeatureLayer in its collection.
It could be expanded to multiple layers within the collection if needed using a loop.
Just modify the Username, Password, and the title of the FeatureService to modify.
Take a look and let me know how it goes.
Thanks,
Jeff
import copy,json
from arcgis.gis import GIS
gis = GIS("https://www.arcgis.com", "<USERNAME>", "<PASSWORD>")
srcResults = gis.content.search("title:ServiceAreas",item_type = "Feature Layer") featCollection = srcResults[0]
#update a featurecollection layer filter featCollD = featCollection.get_data(try_json=True) #make a copy to manipulate the properties featCollDCopy = copy.deepcopy(featCollD) sql = 'ServArNu = 2'#The code below assumes that 1 FeatureLayer was found within the collection #Modify the number or loop on the layers for morefeatCollDCopy['layers'][0]['layerDefinition']['definitionExpression'] = sql#send the update to modify the Feature Layers within the FeatureCollection
featCollection.update(item_properties={'text':json.dumps(featCollDCopy)})
Hi Greg,
The code below shows how you could update the filter for a FeatureService with 1 FeatureLayer in its collection.
It could be expanded to multiple layers within the collection if needed using a loop.
Just modify the Username, Password, and the title of the FeatureService to modify.
Take a look and let me know how it goes.
Thanks,
Jeff
import copy,json
from arcgis.gis import GIS
gis = GIS("https://www.arcgis.com", "<USERNAME>", "<PASSWORD>")
srcResults = gis.content.search("title:ServiceAreas",item_type = "Feature Layer") featCollection = srcResults[0]
#update a featurecollection layer filter featCollD = featCollection.get_data(try_json=True) #make a copy to manipulate the properties featCollDCopy = copy.deepcopy(featCollD) sql = 'ServArNu = 2'#The code below assumes that 1 FeatureLayer was found within the collection #Modify the number or loop on the layers for morefeatCollDCopy['layers'][0]['layerDefinition']['definitionExpression'] = sql#send the update to modify the Feature Layers within the FeatureCollection
featCollection.update(item_properties={'text':json.dumps(featCollDCopy)})
Thanks!
Hello,
thanks for the input, it works great. If that's not too much to ask, maybe you could help me out!
I ran this code to apply a filter to a web feature layer hosted on my web server and it works great. The problem is when i create a view (create_view) made from this parenting layer, the filter is not carried with it. And re-using this code doesn't work as the layer as no properties for some reason. Here's the code :
srcResults = gis.content.search(query='ENERGEREGLOBALE_V2', item_type='Feature Layer') featCollection = srcResults[0] # update a featurecollection layer filter featCollD = featCollection.get_data(try_json=True) # make a copy to manipulate the properties featCollDCopy = copy.deepcopy(featCollD) sql = 'Municipalité IS NULL' # The code below assumes that 1 FeatureLayer was found within the collection # Modify the number or loop on the layers for more featCollDCopy['layers'][0]['layerDefinition']['definitionExpression'] = sql # send the update to modify the Feature Layers within the FeatureCollection featCollection.update(item_properties={'text': json.dumps(featCollDCopy)}) print(featCollection.get_data(try_json=True)) ################################################################# #Generate View flc = arcgis.features.FeatureLayerCollection.fromitem(featCollection) view = flc.manager.create_view(name='TEST', spatial_reference=None, extent=None, allow_schema_changes=False, updateable=True, capabilities='Query, Update, Delete') #view_layers= [flc.layers[0]] print(view)
If you guys have any idea on how to make the filter follow the view, or how to apply a new filter to the view, that would be very appreciated!
Thanks,
R.Muller
Hi Maxime Campeau,
you can update a view layer with a new definition query but I don't think you can create a view layer from another view layer.
For more information about how to update the viewDefinitionQuery property you can refer to the link below about "Using the ArcGIS API for Python to create a view from a Hosted Feature Layer and to define a view definition" by Earl Medina:
How does this work for layers that not Hosted Featurelayers instead they are hosted in User managed Enterprise Geodatabase (e.g ArcSDE)
You can also filter the layer using update definition and the property you're adjusting via admin REST api.
In this case, a view layer required a filter view from the parent host layer:
{
"viewDefinitionQuery": "fieldname = value"
}