Workflow: Create a feature service for an editor app

feature-view-layer

In this workflow, you will learn how to create a feature layer view to return a subset of features from an existing feature layer, and then build an application to access, display and edit the features.

Prerequisites

You need an account for ArcGIS Platform, ArcGIS Online, or ArcGIS Enterprise to create hosted data services. If you need an account, go to Get started.

Steps

Get the data

For this workflow, you will use the Santa Monica Parcels dataset.

  1. In your web browser, go to the Santa Monica Parcels item.

  2. Click the Download button to download the zip file locally. Do not unzip this file.

Create a feature layer

To create a feature layer in a feature service, you need to upload data into ArcGIS. You can use data management tools or scripting APIs.

Data management tools

Import the shapefile using a data management tool.

In your web browser, go to ArcGIS.com and sign in with your ArcGIS Developer account.

  1. In the top navigation bar, click Content.

  2. 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.
  3. Select Add Santa Monica Parcels.zip to publish the file as a hosted feature layer.

  4. In Fields, leave all fields at their default settings and click Next.

  5. In Location settings, leave the default settings and click Next.

  6. Set the following information in the item details pane:

    • Title: Santa Monica Parcels
    • Tags: Santa Monica Parcels.
    • Summary: Parcels in the Santa Monica Mountains.
  7. Click Next to create the new feature layer and feature service.

Scripting APIs

You can also import the shapefile with the ArcGIS API for Python or ArcGIS REST JS. The general steps are as follows:

  1. Import the required libraries.
  2. Provide an access token.
  3. Create and publish a portal item.
  4. Handle the results.
Expand
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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}")

The feature layer will look something like this:

Santa monica parcels feature layer

Set the feature styles

Use data management tools to set the style for a feature layer. Style the parcels based on the usetype field.

Use the Visualization tab in the item page in ArcGIS.com to style the Santa Monica parcels layer.

  1. Go back to the item page page > Visualization.

  2. In the left panel, click the Layers and select the Santa Monica parcels layer. In the right panel, click Styles.

  3. Click + Field, and add usetype.

  4. Under Pick a style, click Style options, update Types (unique symbols)

  5. Click on the symbol next to each of the use type values to update the symbol color.

  6. Set each of the usetype values to the following properties:

    • Fill Color:
      • Residental: #ffde3e
      • Commercial: #c29ed7
      • Industrial: #004c73
      • Government: #fc921f
      • Institutional: #149ece
      • Recreational: #267300
      • Miscellaneous: #b7814a
    style-screenshot
  7. Click the X to exit out of Symbol style.

  8. Click the pencil icon next to symbol style. Set the following properties for all the use types:

    • Fill transparency: 30%
    • Outline color: #ffffff
    • Outline transparency: 65%
    • Outline width: 1
    • Adjust width automatically: false
    style-screenshot
  9. Click the X to exit out of Symbol style. Then click Done twice.

  10. Click Save to save the style in the feature layer item.

The styled layer will look something like this:

Santa monica parcels feature layer

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.

Data management tools

In ArcGIS.com, use the item page to create a view layer.

  1. In the source feature layer item page, click Create View Layer > View layer.
  2. In the Create View Layer dialog, under choose layers, ensure your layer is checked and click Next.
select-layer
  1. In Included layers, click on the layer, to define the fields and filters.
  2. Click Add Filter > + Add expression.
  3. In the expression dialog, in the first dropdown, select usetype. In the second dropdown, select is and in the last dropdown select Commercial.
add-expression
  1. Click the back arrow, under Layer definitions click Fields > Select fields.

  2. Select the following fields: situsfulla, usetype, usedescrip. Uncheck all the remaining fields.

  3. Click Done.

  4. Click the back arrow and click Next to open the Create view tab.

  5. 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
  6. Click Create. Once the view is created you will be redirected to the item page of the new feature layer view.

  7. Click the Data tab. You should see only the four fields you used in the expression.

  8. Click the Visualization tab. Only features with "usetype"='Commercial' should display.

Scripting APIs

Expand
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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}")

The layer will look something like this:

feature-view-layer

Manage service settings

Use data management tools to enable authenticated users to update the attributes of features in this view by configuring the edit settings.

In ArcGIS.com, use the Settings tab to configure the service edit settings.

  1. In the item settings pane, scroll down to the Editing section.
  2. Click the Enable editing check box.
  3. Under the setting What kind of editing is allowed?, uncheck the Add and Delete privilege.
    • This will only allow editors to update existing feautres.
  4. Under the Update privilege, click Attributes only.
    • In our case, we only want editors to manage the feature attributes. They will be unable to remove or create new features.
  5. Click Save.

Your edit settings should look like this:

edit settings

To focus the edit workflow on a single field, use the item page Data tab, to only enable editing to the 'situsfulla' field.

  1. Go back to the item settings page, and click the Data tab.
  2. In the Data page, click Fields, to open the field list.
  3. Click the usetype field, under settings clik Edit next to Editable.
  4. Uncheck Allow attribute editing. Click Save.
  5. Click the usedescrip field, under settings clik Edit next to Editable.
  6. Uncheck Allow attribute editing. Click Save.
  7. Go back to the Data tab.

The fields with editing disabled show a lock next to the field name:

edit field settings

Find the service URL and item ID

To access a hosted layer view from an application, you need to be able to identify its ID and URL. If a layer view is public, you use the URL or item ID to access it directly with your web browser or any application. If the layer is private, you need to provide an access token.

If you are using ArcGIS Maps SDKs:

  1. In the item page, click View to access the feature service. Locate the Service ItemId, which will look something like this:

If you are using open source libraries:

  1. In the item page, scroll down to the bottom of the page to find the Service URL. For example:
    • Service URL: https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_commercial_parcels_view/FeatureServer

Get an access token

By default, the sharing level of an item is set to Owner and requires an access token, such as an API key to access it in a client-side application. If you have an ArcGIS Developer account, you can scope an API key to access private items. You cannot scope an API key to access private items if you have an ArcGIS Online or ArcGIS Enterprise account. Instead, to access private items, you need to generate a token from an OAuth 2.0 workflow.

  1. Sign in to the developer dashboard.

  2. Click the API keys tab > Edit API key.

  3. In the Overview, find the Maps, layers, and data box. If you have an ArcGIS Developer account, you will be able to click Set content item scopes.

  4. Select the item you want to scope > Add items.

  5. Copy the API key.

Display features

  1. In the layer item page, find the item ID or the service URL and layer index.

  2. In the code, set the item ID or the service URL.

  3. Add the layer to the map.

ArcGIS Maps SDK for JavaScriptArcGIS Maps SDK for JavaScriptEsri LeafletMapLibre GL JSOpenLayers
Expand
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
        const viewLayer = new FeatureLayer({
          portalItem: {
            id: "a364900459734862ba4c235fa446c8f9",
          },
        });

Your app will look something like this:

A feature layer view displaying commercial parcels in Santa Monica, CA

Edit features

You can add, update, or delete features by using an ArcGIS Maps SDK or open source library.

  1. In the layer item page, find the item ID or the service URL and layer index.
  2. Verify editing is enabled in the item page.
  3. In the code, set the item ID or the service URL.
  4. Add an editor widget or client-side class to edit features.
  5. Add the layer to the map.
ArcGIS Maps SDK for JavaScriptArcGIS Maps SDK for JavaScriptArcGIS REST JS
Expand
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
        // Editor widget
        const editor = new Editor({
          view: view,
        });
        // Add widget to the view
        view.ui.add(editor, "top-right");
Edit a feature layer view with the ArcGIS Maps SDK for Javascript Editor widget

What's next?

Learn how to use additional tools, APIs, and location services in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.