You will 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 load the Trailheads
feature layer you created in the challenge to the Import Data Tutorial. You can also create a blank online layer with the New Layers online tool.
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 tutorial.
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 use that data you imported from the Import data tutorial in this lab, so make sure you complete that tutorial first.
Go to Esri Juptyter Notebooks 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.
Add the following code to import the ArcGIS API for Python.
from arcgis.gis import GIS
Log into ArcGIS Online by making a GIS connection to ArcGIS Online using your developer account. Replace username
and password
with your own credentials.
gis = GIS("https://www.arcgis.com", "username", "password")
Search for the Trailhead
Feature Layer you created, as a challenge, from CSV data in the Import Data tutorial.
query = 'title: "Trailheads*" AND type: "Feature Service"'
search_results = gis.content.search(query=query, max_items=10)
search_results
Retrieve the Trailhead feature layer collection from the list of results, and display the URL of the item.
csv_data = search_results[0]
csv_data.url
Retrieve the list of layers from the feature layer collection, and assign the Trailshead feature layer to a variable.
feature_layers = csv_data.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.
for field in trailheads_layer.properties['fields']:
print('Name: {:16s}\tType: {}'.format(field['name'], field['actualType']))
Write a callback function for the map widget's on_click
method.
from arcgis import geometry
from arcgis import features
def create_feature(_map, location):
try:
object_id = 1
point = geometry.Point(location)
feature = features.Feature(
geometry=point,
attributes={
'OBJECTID': object_id,
'PARK_NAME': 'My Park',
'TRL_NAME': 'Foobar Trail',
'ELEV_FT': '5000'
}
)
trailheads_layer.edit_features(adds=[feature])
_map.draw(point)
except Exception as e:
print("Couldn't create the feature. {}".format(str(e)))
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.
m = gis.map()
# wait for the map to load, and then add the following in a new cell
m.center = [34.09042, -118.71511] # `[latitude, longitude]`
m.zoom = 11
m.on_click(create_feature)
Clear the map graphics.
m.clear_graphics()
Add the layer to the map to visualize features.
m.add_layer(trailheads_layer)
Your map and layer (feature service) should look something like this.
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.
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://services9.arcgis.com/ZPYuLYCJmpcXcod0/arcgis/rest/services/Trailheads/FeatureServer/0
Try pasting the URL in your browser to explore the data. Try the Query function at the bottom.
In the item page, locate the Share button and set the permissions to everyone to make your data public. Learn more about sharing items.
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.
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.