Create data

Learn how to use the ArcGIS API for Python to add data to an existing feature layer in ArcGIS Online.

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

Prerequisites

The ArcGIS API for Python tutorials use Jupyter Notebooks to execute Python code. If you are new to this environment, please see the guide to install the API and use notebooks locally.

You will access the Trailheads feature layer created in the Import Data tutorial. You can also create a blank online layer using the Developers Dashboard or ArcGIS Online, see Create a new feature layer for detailed steps.

Steps

  1. Log into ArcGIS Online by making a GIS connection to ArcGIS Online using your ArcGIS Online or Developer account. It is not good security practice to leave account credentials in source code shared by others, but it is beyond the scope of this tutorial to go over security best practices. Read about the Python getpass module for a way to avoid display of a password.

    Use dark colors for code blocks
                                                           
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    
    from arcgis.gis import GIS
    import getpass
    
    gis = GIS(
      url="https://www.arcgis.com",
      username="username",
      password=getpass.getpass("Enter password:")
    )
    
    
  2. Search for the Trailheads feature layer you created in the Import Data tutorial.

    Use dark colors for code blocks
                                                           
    Add line.Add line.
    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
    
    from arcgis.gis import GIS
    import getpass
    
    gis = GIS(
      url="https://www.arcgis.com",
      username="username",
      password=getpass.getpass("Enter password:")
    )
    
    query = 'title: Trailheads*" AND type: "Feature Service"'
    search_results = gis.content.search(query=query, max_items=10)
    
    
  3. Retrieve the Trailheads feature layer collection from the list of results, and display the URL of the item. The index for the specific layer may be different in your organization.

    Use dark colors for code blocks
                                                           
    Add line.Add line.
    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
    
    query = 'title: Trailheads*" AND type: "Feature Service"'
    search_results = gis.content.search(query=query, max_items=10)
    
    trailheads_item = search_results[0]
    trailheads_item.url
    
    
  4. Retrieve the list of layers from the feature layer collection, and assign the Trailheads feature layer to a variable.

    Use dark colors for code blocks
                                                           
    Add line.Add line.Add line.
    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
    
    query = 'title: Trailheads*" AND type: "Feature Service"'
    search_results = gis.content.search(query=query, max_items=10)
    
    trailheads_item = search_results[0]
    trailheads_item.url
    
    feature_layers = trailheads_item.layers
    trailheads_layer = feature_layers[0]
    trailheads_layer.properties.name
    
    
  5. Print a list of the field names and types so you can populate the values in the step below.

    Use dark colors for code blocks
                                                           
    Add line.Add line.
    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
    
    feature_layers = trailheads_item.layers
    trailheads_layer = feature_layers[0]
    trailheads_layer.properties.name
    
    for field in trailheads_layer.properties['fields']:
      print(f"Name: {field['name']}, type: {field['type']}")
    
    
  6. Write a callback function for the map widget's on_click method. You'll import the features and geometry modules. You will use the geometry module to create a Point geometry object from the location clicked on the map. You'll then use the features module to create a Feature object from this point to add it to the feature layer. When fields are required in the Create Layer tool, null values are not allowed.

    Use dark colors for code blocks
                                                           
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    
    for field in trailheads_layer.properties['fields']:
      print(f"Name: {field['name']}, type: {field['type']}")
    
    from arcgis import features
    from arcgis import geometry
    
    def create_feature(_map, location):
      try:
        point = geometry.Point(location)
        feature = features.Feature(
          geometry=point,
          attributes={
            'PARK_NAME': 'My Park',
            'TRL_NAME': 'Foobar Trail',
            'ELEV_FT': '5000'
          }
        )
    
        trailheads_layer.edit_features(adds=[feature])
        _map.draw(point)
    
      except Exception as ex:
        print(f"Could not create feature. {str(ex)}")
    
    
  7. Pass the function create_feature as a parameter to the map widget's on_click method. Run the cell and begin clicking locations on the map. When you click at a location on the map, Point and Feature object are constructed. The Feature is rendered as a graphic marker on the map. The Feature is then added to feature layer through the edit process.

    Use dark colors for code blocks
                                                           
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    
    from arcgis import features
    from arcgis import geometry
    
    def create_feature(_map, location):
      try:
        point = geometry.Point(location)
        feature = features.Feature(
          geometry=point,
          attributes={
            'PARK_NAME': 'My Park',
            'TRL_NAME': 'Foobar Trail',
            'ELEV_FT': '5000'
          }
        )
    
        trailheads_layer.edit_features(adds=[feature])
        _map.draw(point)
    
      except Exception as ex:
        print(f"Could not create feature. {str(ex)}")
    
    m = gis.map()
    m
    
    # wait for the map to load, and then add the following:
    m.center = [34.09042, -118.71511]
    m.zoom = 11
    m.on_click(create_feature)
    
    
  8. Clear the map graphics. Clear the graphic markers from the map so that in the subsequent step you can add the feature layer and see the features created by clicking locations.

    Use dark colors for code blocks
                                                           
    Add line.
    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
    
    m = gis.map()
    m
    
    # wait for the map to load, and then add the following:
    m.center = [34.09042, -118.71511]
    m.zoom = 11
    m.on_click(create_feature)
    
    m.clear_graphics()
    
    
  9. Add the layer to the map to visualize features.

    Use dark colors for code blocks
                                                           
    Add line.
    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
    
    m = gis.map()
    m
    
    # wait for the map to load, and then add the following:
    m.center = [34.09042, -118.71511]
    m.zoom = 11
    m.on_click(create_feature)
    
    m.clear_graphics()
    
    m.add_layer(trailheads_layer)

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