Learn how to use ArcGIS portal and scripting APIs to create a feature layer view.
Feature layer views are created from existing feature services. In this tutorial, you create a feature layer view from an existing feature service by limiting the number of fields available and setting an expression to limit the types of features available. You will also manage the feature layer view settings and display the feature layer using a client-side API.
Prerequisites
You need an account for ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise to create hosted data services. If you need an account, go to Get started.
Steps
Download the data
For this workflow, you will use the Santa Monica Parcels dataset.
-
In your web browser, go to the Santa Monica Parcels item.
-
Click the Download button to download the zip file locally. Do not unzip this file.
Create a feature layer in a feature service
You can upload data into ArcGIS in supported file types such as CSV, XLS, GeoJSON, shapefile, and File Geodatabase. To create a feature service for the parcel data, import the shapefile by either using a data management tool or scripting API.
Follow the steps below to use Data management tools or Scripting APIs:
In your web browser, go to https://location.arcgis.com, and sign in with your ArcGIS Location Platform account. In the ArcGIS Location Platform dashboard, click My portal to go to your portal.
-
In the top navigation bar, click Content.
-
Click New item. To upload the Santa Monica Parcels shapefile, you can either:
- Drag and drop the file.
- Or, click Your device and navigate to the file path.
-
Select Add Santa Monica Parcels.zip to publish the file as a hosted feature layer.
-
Set the following information in the item details pane:
- Title:
Santa Monica Parcels
- Tags:
Santa Monica
Parcels
. - Summary:
Parcels in the Santa Monica Mountains.
- Title:
-
Click Next to create the new feature layer and feature service.
- Import the required libraries.
- Implement authentication.
- Create and publish a portal item.
- Handle the results.
# local path to shapefile zip file
input_file_path = str(
Path(__file__).parent / "Santa_Monica_Public_Parcels.zip"
)
# add the zip file as an item to portal
shp_item = portal.content.add(
{
"title": "Santa Monica Public Parcels",
"description": "Santa Monica public parcels",
"tags": "Santa, Monica, public, parcels",
"type": "Shapefile",
},
input_file_path,
)
# publish the item to create a hosted featurelayer
shp_service = shp_item.publish(None)
print(f"New item id : {shp_service.id, }, url: {shp_service.layers[0].url}")
Create the feature layer view
To create a hosted feature layer view from a feature service, you can use data management tools or scripting APIs. Use a tool or API to create a view layer that contains only commercial parcels.
Follow the steps below to use Data management tools or Scripting APIs:
In ArcGIS portal, use the item page to create a view layer.
- In the source feature layer item page, click Create View Layer > View layer.
- In the Create View Layer dialog, under choose layers, ensure your layer is checked and click Next.
- In Included layers, click on the layer, to define the fields and filters.
- Click Add Filter > + Add expression.
- In the expression dialog, in the first dropdown, select
usetype
. In the second dropdown, selectis
and in the last dropdown selectCommercial.
-
Click the back arrow, under Layer definitions click Fields > Select fields.
-
Select the following fields:
situsfulla
,usetype
,usedescrip
. Uncheck all the remaining fields. -
Click Done.
-
Click the back arrow and click Next to open the Create view tab.
-
In the Create pane, Set the following parameters:
- Title: Santa Monica commercial parcels view
- Tags: view, public, parcels, santa, monica, commercial, tutorial
- Summary: Feature layer view representing commercial parcels in Santa Monica, CA
-
Click Create. Once the view is created you will be redirected to the item page of the new feature layer view.
-
Click the Data tab. You should see only the four fields you used in the expression.
-
Click the Visualization tab. Only features with
"usetype"='
should display.Commercial'
- Import the required libraries.
- Implement authentication.
- Create and publish a portal item.
- Handle the results.
# Create the view layer
view_item = flc.manager.create_view(name="Santa monica commercial parcels (view)")
# reduce the number of fields included in the view
# Get the current field list
view_flds = view_item.layers[0].properties.fields
# Set only the fields we want to visible, also include OID
vis_flds = [
{"name": f"{f.name}", "visible": True}
if f.name in ["apn", "situsfulla", "usetype", "usedescrip"]
or f.type == "esriFieldTypeOID"
else {"name": f"{f.name}", "visible": False}
for f in view_flds
]
layer_def = {"viewDefinitionQuery": "usetype = 'Commercial'", "fields": vis_flds}
view_item.layers[0].manager.update_definition(layer_def)
print(f"Feature layer view created\n\t ItemID : {view_item.id}\n\t URL:{view_item.url}")
You now have a feature layer view hosted in your portal. You can access the view layer with its service URL or ID in your applications. To manage your hosted layer (item) properties and capabilities, visit the Manage a feature layer tutorial.
What's next?
Learn how to use additional tools, APIs, and location services in these tutorials:
Define a new feature layer
Use data management tools to define and create a new empty feature layer in a feature service.
Manage a feature layer
Use a hosted feature layer item to set the properties and settings of a feature layer in a feature service.
Create a vector tile service
Use data management tools to create a new vector tile service from a feature service.