Overview

You will learn: how to use the ArcGIS API for Python to populate an empty feature layer in the cloud.

Feature layers created using the Create Layer tool have a schema, but they aren't yet populated with data. The ArcGIS API for Python can be used to publish edits. Once new features have been added, your apps can access the layer to query, draw or edit them.

In this lab you will draw graphic markers by clicking on a map. You'll use the Point class and Feature class to construct data features to add to the layer. The lab will create points near Los Angeles, California. The data will be stored in your own ArcGIS Online account and can be used in other labs.

Before You Begin

The ArcGIS API for Python DevLabs use Jupyter Notebooks to execute Python code. If you are new to Python, please see the ArcGIS API for Python guide to Using the Jupyter notebook environment.

Complete the Create a new dataset DevLab. You will use the output feature layer to complete this DevLab.

Steps

Add features with the ArcGIS API for Python

  1. Go to the Esri Jupyter Notebook and click New > Python 3 to create a new notebook.

    In each step below, type (or copy and paste) the commands into a new notebook cell and run the code by clicking run cell or pressing shift + Enter.

  2. Add the following code to import the ArcGIS API for Python.

    from arcgis.gis import *
    
  3. Log into ArcGIS Online by making a GIS connection to ArcGIS Online using your developer account. Replace username and password with your own credentials.

    dev_gis = GIS("https://www.arcgis.com", "username", "password")
    
  4. Search for the Griffith Park Access layer created in the Create a new dataset DevLab.

    feature_layer_srch_results = dev_gis.content.search(query='title: "Griffith*" AND type: "Feature Service"')
    feature_layer_srch_results
    
  5. Retrieve the feature layer collection item from the list of results.

    feature_layer_coll_item = feature_layer_srch_results[0]
    
  6. Retrieve the list of layers from the feature layer collection, and assign the feature layer to a variable.

    feature_layers = feature_layer_coll_item.layers
    feature_layer = feature_layers[0]
    
  7. Retrieve a list of all the field names for the feature layer so you can populate the values in the step below.

    for field in feature_layer.properties['fields']:
        print(field['name'])
    
  8. Write a function to serve as the event handler for the map widget's on_click event.

    from arcgis import geometry
    from arcgis import features
    
    def create_feature(map1, g):
       try:
           oid = 1
           pt = geometry.Point(g)
           feat = features.Feature(geometry=pt, attributes={'OBJECTID': oid,
                                                            'name': 'name',
                                                            'type': 'park',
                                                            'surface': 'dirt'})
           flayer.edit_features([feat])
           map1.draw(pt)
       except:
           print("Couldn't draw the point. Try again...")
    
  9. Load a map of Los Angeles in your notebook.

    map1 = gis.map('Los Angeles', 10)
    map1
    
  10. Assign the function you defined in step 7 to the callback parameter of the map widget's on_click() method. Run the cell and begin clicking locations on the map.

    map1.on_click(create_feature)
    
  11. Clear the map graphics.

    map1.clear_graphics()
    
  12. Add the layer to the map to visualize features.

    map1.add_layer(feature_layer)
    

Congratulations, you're done!

Your map and layer (feature service) should look something like this.

Challenge

Explore the Item Details Page.

Go to ArcGIS for Developers > Layers or the Map Viewer to reach the items details page and explore the settings for your layer and the functionality available.

The item details page describes your feature service and allows you to perform a number of operations, including creating a view, publishing or exporting your data.

Find the URL

In the item page, find the Service URL link and identify the service endpoint. This is what applications use to access the data. It should look something like this:

https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Griffith_Park_Access/FeatureServer/0

Try pasting the URL in your browser to explore the data. Try the Query function at the bottom.

Learn more about the ArcGIS REST API and specification

Set permissions

In the item page, locate the Share button and set the permissions to everyone to make your data public. Learn more about sharing items.

Set the styles

In the item page, locate the Visualization button and try styling your layer with different symbols. When you are done, save the symbols. This will allow apps that load the layer to display your new defaults. Learn more about styling layers.

Explore the Table View

In the Map Viewer, the table view is a great way to explore your dataset. You can view fields and rows, and you can also add, remove, sort, filter and calculate field values. You can also upload media files for each record.

Try exploring the table and some of the table Options.