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
-
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 from arcgis.gis import GIS import getpass gis = GIS( url="https://www.arcgis.com", username="username", password=getpass.getpass("Enter password:") )
-
Search for the
Trailheads
feature layer you created in the Import Data tutorial.Use dark colors for code blocks 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)
-
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 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
-
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 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
-
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 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']}")
-
Write a callback function for the map widget's
o
method. You'll import the features and geometry modules. You will use then_ click geometry
module to create aPoint
geometry object from the location clicked on the map. You'll then use thefeatures
module to create aFeature
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 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)}")
-
Pass the function
create_
as a parameter to the map widget'sfeature o
method. Run the cell and begin clicking locations on the map. When you click at a location on the map,n_ click Point
andFeature
object are constructed. TheFeature
is rendered as a graphic marker on the map. TheFeature
is then added to feature layer through the edit process.Use dark colors for code blocks 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)
-
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 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()
-
Add the layer to the map to visualize features.
Use dark colors for code blocks 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)