You will learn: how to download data from ArcGIS Online using the ArcGIS API for Python.
You can automate the process of downloading data with the ArcGIS API for Python. In this lab you will learn how to download datasets from ArcGIS Online that were gathered from the Los Angeles GeoHub. They include Trailheads (CSV), Trails (GeoJSON), and Parks and Open Spaces (Shapefile) files.
If you use notebooks.esri.com rather than install the ArcGIS API for Python locally (see info note in Step 1), this lab must be completed immediately before the Import data lab. The files downloaded in this lab will be lost if you exit the session.
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.
Add the following code to import the ArcGIS API for Python and other Python libraries.
from arcgis.gis import * import os from zipfile import ZipFile
Create a variable to hold the item id of the public dataset.
public_data_item_id = 'a04933c045714492bda6886f355416f2'
Create an anonymous connection to ArcGIS Online to download this public data.
anon_gis = GIS()
Access this dataset as an ArcGIS Python API
data_item = anon_gis.content.get(public_data_item_id) data_item
Download it to the notebook server's current location.
data_item.download(save_path = r'./')
ZipFile library you imported earlier to extract the contents of the downloaded dataset.
zf = ZipFile(r'./LA_Hub_Datasets.zip') zf.extractall(path=r'./LA_Hub_datasets')
os module to list the contents of the directory.
file_list = os.listdir(r'./LA_Hub_datasets/') file_list
This should return a list of three datasets:
You've successfully completed the download portion of this lab.