Esri / arcgis-python-api

Documentation and samples for ArcGIS API for Python
https://developers.arcgis.com/python/
Apache License 2.0
1.84k stars 1.09k forks source link

Adding a Feature Layer item to the map widget does not honor any filters set on the item #1192

Closed crackernutter closed 10 months ago

crackernutter commented 2 years ago

Describe the bug When adding a Portal item that is a feature layer to a map, the map does not reflect any filter that has been set on the Portal item

To Reproduce Steps to reproduce the behavior:

from arcgis.gis import GIS, Item
gis = GIS(url, username, password)
m = gis.map()
hostedfeatureservice = Item(gis, "468c13da509947aab4bacb8a60378ce9")
m.add_layer(hostedfeatureservice)

error: No error message

Screenshots If applicable, add screenshots to help explain your problem.

This is a screenshot from the map widget - see all features are displayed AllFeatures This is a screenshot from the item in Portal - see the filter I have set only displays a subset of the features VisualizationTabInPortal

Expected behavior I would expect that when adding a feature layer to a map in the Python API map widget, that any filter set on that feature layer would be honored.

Platform (please complete the following information):

Additional context Add any other context about the problem here, attachments etc.

achapkowski commented 2 years ago

@crackernutter you can set a filter on a featurelayer, then add that feature layer to the map.

crackernutter commented 2 years ago

@achapkowski True, but I would think that the map widget would respect the filter already set on the Portal item. This is how the behavior works in the MapViewer, as well as consuming a Portal item through the 4.x API. If you set a filter on the Portal item in the visualization tab (and click save layer afterwards), you can load the layer in the MapViewer or in a JS App and that filter persists. But somehow loading the layer into the Jupyter map widget does not honor the filter.

achapkowski commented 2 years ago

We will look into this functionality in a future release. I'll add this to our backlog.
Thank you for the suggestion.

achapkowski commented 2 years ago

In v2.0.0 if I have a webmap with a filter on a layer, it gets added:

image

With the webmap definition of:

image

How did you create your filter? Can you post a completely reproducible sample?

crackernutter commented 2 years ago

I will do my best: Step 1. Create a feature layer - I downloaded a CSV from usgs and uploaded it to create a feature layer. Step 2. Click on the feature layer item Step 3. Click on the visualization tab (upper right hand side of feature layer info page) VisualizationTab Step 4. Set a filter on the feature layer. For earthquakes, I set it to be "mag is at least 5.5" Step 5. Click "Save Layer" - upper right hand side Step 6. Click on overview tab - upper right hand side Step 7. Click "Open in Map Viewer" Step 8. Notice how the layer that is added to the MapViewer honors the filter you just set - you do not need to reset the filter. Step 9. Save the map.

You should be able to access to two items I created.
The feature layer item id is 38bef233c6f0401988b5b3a0a175a133 The web map item id is 69803525074c4e0ba392c99751f0c30d

Now onto the Python

from arcgis.gis import GIS
gis = GIS()
m = gis.map()
m
filteredfeaturelayer.get_data()['layers'][0]['layerDefinition']

Notice the filter is evident in the json: {'definitionExpression': 'mag >= 5.5', 'defaultVisibility': True}

m.add_layer(filteredfeaturelayer)

Notice all the data is loaded on the map viewer - it does NOT respect the filter. Now lets get the map

m.remove_layers() ##first clear the map
mapwithfilteredfeaturelayer = gis.content.get("69803525074c4e0ba392c99751f0c30d")
mapwithfilteredfeaturelayer.get_data()['operationalLayers']

Notice operational layers shows it has the feature layer added to the map

m.add_layer(mapwithfilteredfeaturelayer)

The above actually generates an error: No 'layers' in Item: will not be added to map This seems to be a second bug as well.

There are lots of valid and useful workflows for setting filters on the feature layer item individually, not on the layer once it's been added to a map. Suppose you want to create multiple maps with the same feature layer. You will want to create your feature layer, and set the symbology, filter, popups, etc. from the feature layer "visualization" tab of the item page, save the feature layer, and add it to your multiple maps. That way you don't have to set the filter, popups, symbology on each map after the fact. It is all controlled through the single layer. If you change the filter on that single layer (again, via the visualization tab) and resave the layer, every map with the layer added should reflect your changes. A lot of folks use this pattern for their assets, and it works across most of the platform (map viewer, js api). But not the python api seemingly.

Hope this helps clear things up.
Let me know if you have further questions.