English

How To: Create a view from a hosted feature layer and define the view using ArcGIS API for Python

Summary

A view is created from a hosted feature layer to filter desired features or display fields in a map. The view is added as a feature layer to the Content tab and can be shared. Refer to Portal for ArcGIS: Create hosted feature layer views for detailed steps.

The view definition is defined at the service level, and the viewDefinitionQuery property is updated. Navigate to Portal REST API Directory > the layer ID > JSON to define and update the view. The property is displayed as shown in the image below:

Property is displayed
Note:
Changing the value of viewDefinitionQuery also updates the related definitionQuery property.

Alternatively, views are also created using ArcGIS API for Python. Creating views using ArcGIS API for Python eliminates the need to open the Portal for ArcGIS application.

Procedure

The following steps describe how to create a view and view definition using ArcGIS API for Python:

  1. Import the necessary modules.
from arcgis import GIS
from arcgis.features import FeatureLayerCollection
gis = GIS("https://www.arcgis.com", "username","password")
  1. Search for the source hosted feature layer.
source_search = gis.content.search("FEATURE_LAYER")[0] 
source_flc = FeatureLayerCollection.fromitem(source_search)
  1. Create the view.
new_view = source_flc.manager.create_view(name="VIEW_NAME")
  1. Search for the newly created view.
view_search = gis.content.search("VIEW_NAME")[0]
view_flc = FeatureLayerCollection.fromitem(view_search)
  1. Set a parameter to represent the viewDefinitionQuery property.
service_layer = view_flc.layers[0]
  1. Define the desired SQL query to use as the filter.
update_dict = {"viewDefinitionQuery" : "TEST_QUERY"}
  1. Update the definition to include the view definition query.
service_layer.manager.update_definition(update_dict)

The following shows the full script.

from arcgis import GIS
from arcgis.features import FeatureLayerCollection
gis = GIS("https://www.arcgis.com", "username","password")

source_search = gis.content.search("world_earthquakes")[0]
source_flc = FeatureLayerCollection.fromitem(source_search)

new_view = source_flc.manager.create_view(name="worldEQView")

view_search = gis.content.search("worldEQView")[0]
view_flc = FeatureLayerCollection.fromitem(view_search)

service_layer = view_flc.layers[0]

update_dict = {"viewDefinitionQuery" : "year_ < 1988"}

service_layer.manager.update_definition(update_dict)

To verify, check the Set View Definition settings.

Define features

Related Information