create a feature layer view with ArcGIS API for Python

881
5
08-18-2023 12:31 PM
Labels (1)
davedoesgis
Occasional Contributor III

Environment: Running ArcGIS API for Python version 2.0.1 from Pro 3.0.2.

I am trying to use the ArcGIS API for Python to create a view of an AGOL hosted feature layer and set a definition query. This guide seems to be the most definitive documentation of the process, but it's not working for me. Here is the code I have that follows this process:

from arcgis import GIS
from arcgis.features import FeatureLayerCollection
source_item_id = '<your item id>'
view_def = {
    'viewDefinitionQuery': "State_FIPS IN ('09','23','25','33','44','50')"
}
view_name = 'TEST_create_view_python_01'

gis = GIS(profile='AGOL')
source_item = gis.content.get(source_item_id)
source_flc = FeatureLayerCollection.fromitem(source_item)
new_view = source_flc.manager.create_view(name=view_name)
view_search = gis.content.search(view_name)[0]
view_flc = FeatureLayerCollection.fromitem(view_search)
service_layer = view_flc.layers[0]
service_layer.manager.update_definition(view_def)

 

I copied the definition query from a view I manually created in a browser, so I'm pretty sure that's correct. It runs without errors or warnings. The new view shows up as an AGOL item, but when I add the view to a webmap, I see this error:

dcafdg_0-1692383653706.png

And adding it to a map in Pro gives me this:

dcafdg_1-1692384041246.png

Next, I tried one little tweak. After defining the source_flc variable, I create a list of layers and pass that to the create_view function's view_layers parameter. Otherwise, the code is the same:

view_layers = [source_flc.layers[0]]
new_view = source_flc.manager.create_view(name=view_name, view_layers=view_layers)

 

I can add it to Pro and it renders fine, but the popups are just a regular attribute field display, not my custom popup.I went to edit the popups in my layer and found that the Visualization tab (where you set the popup) is missing!

dcafdg_2-1692384906054.png

 

When I click "Open in Map Viewer", nothing loads.

dcafdg_3-1692385088575.png

If I use the "Add" button, the view shows up. If I click the "+Add" button, it changes to an "X Remove" button, but nothing shows up in the layer list or renders on the map.

dcafdg_4-1692385245887.png

 

I can manually create a working view in a web browser. I am getting errors when I try this with other source layers. So this doesn't seem like a data issue. What am I doing wrong?

5 Replies
Clubdebambos
Occasional Contributor III

Hi @davedoesgis 

This is my template for creating a view. Very similar to your workflow. You don't need to search for the view item.

from arcgis import GIS
from arcgis.features import FeatureLayerCollection

## access AGOL
agol = GIS("home")

## feature service to make a view for
item = agol.content.get("FS_ITEM_ID")

## create a FLC object
flc = FeatureLayerCollection.fromitem(item)

## create the view
view = flc.manager.create_view(name="NAME_OF_VIEW")

## define the definition query
defquery_dict = {
    "viewDefinitionQuery" : "SQL_EXPRESSION"
}

## get the layer to apply the def query to
## if there is more than one layer use the correct index
lyr = view.layers[0]

## update the definition
print(lyr.manager.update_definition(defquery_dict))

Make sure that your definition is correct, if the attributes are text use ' ' around the values in your expression, if there are numerical the omit the ' '.

Another thing to try is top open it up in the classic map viewer. The new viewer still has some bugs to work out and you can report as one if so.

~ learn.finaldraftmapping.com
davedoesgis
Occasional Contributor III

Thanks. I've tried everything I can think of and couldn't get it to work. I included the search for the new view because that was in the sample, but working directly with the resulting Item or gis.content.get had the same result. The view is not creating correctly, so the issue isn't in updating the def.

0 Kudos
davedoesgis
Occasional Contributor III

I have hit max frustration with this issue. If I don't include the view_layers argument, I get errors trying to work with it in a webmap. If I include it, I get a view that doesn't show up in the layer list, so effectively just as broken.

I should also point out that you don't need to search for the new view you created. I just did it that way because that's the way the sample had it. You get the view's Item object returned to you from create_view(). Or if you want to retrieve a fresh copy of it for some reason, the create_view() response has an ID that you can use with gis.content.get(). Regardless, this isn't the issue.

I tried creating a template view and using gis.content.clone_items() going from and to the same AGOL connection. It ran, but nothing was created. I tried both Item.copy() and Item.copy_item(). These worked, but instead of creating a new view, the output was a new feature layer.

I'm giving up.This has absurdly passed the amount of time it would take to do it manually several times, which defeats the whole point of automation. I need 10 views with subsets of a hosted feature layer. My new strategy is to create 10 basic views. The code I put in the original post will update the definition query just fine assuming I'm starting with a working view.

 

 

0 Kudos
JohnYaist1
Esri Contributor

Hello @davedoesgis I'd be frustrated as well incurring these issues. I'm not able to reproduce the scenario you describe when testing with the API at 2.0.1 or the most recent release of 2.2.  Using code similar to yours I was able to create views, update them with a definition query, then render them in both the Map Viewer and ArcGIS Pro with custom popups preserved. There appears nothing incorrect about the code to create the view.

Are you able to create an environment with more recent releases of the api either 2.1 or 2.2 and test to see if you incur the same situation?  

Do you see this same behavior when creating a view based on any other Feature Layer item? 

If you want to investigate more in-depth as to what might be causing what you're observing, you could log an incident with Support who can look into the issue in detail. 

 

0 Kudos
NickSchroeder
New Contributor II

@davedoesgis 

I have the exact same problem, no visualization tab when a view layer is created through the python API. However, a manually created view layer has the visualization tab and the layer definitions are exactly the same except for the name and item id. If there is any solution you found I would love to hear it.

0 Kudos