You will learn: how to download data from ArcGIS Online using the ArcGIS API for Python.
In this tutorial you will download and import data taken from the Los Angeles GeoHub using the ArcGIS API for Python. The data sets include a Trailheads (CSV), Trails (GeoJSON), and a Parks and Open Space (Shapefile) file.
The data will be stored locally on your machine, or temporarily in an online Jupyter notebook at notebooks.esri.com.
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.
GIS class to create a connection to ArcGIS Online.
from arcgis.gis import GIS # Python Standard Library Modules from pathlib import Path from zipfile import ZipFile
Store the ID of the public data item we want to download.
public_data_item_id = 'a04933c045714492bda6886f355416f2'
Because the data is public, we can use an anonymous connection to ArcGIS Online to download the data.
gis = GIS()
data_item = anon_gis.content.get(public_data_item_id) # `ContentManager.get` will return `None` if there is no Item with ID `a04933c045714492bda6886f355416f2` data_item
LA_Hub_datasets.zip to the notebook server's current location.
# configure where we should save our data, and where the ZIP file is located data_path = Path('./data') if not data_path.exists(): data_path.mkdir() zip_path = data_path.joinpath('LA_Hub_Datasets.zip') extract_path = data_path.joinpath('LA_Hub_datasets') data_item.download(save_path=data_path)
ZipFile to extract the contents of the dataset.
zip_file = ZipFile(zip_path) zip_file.extractall(path=extract_path)
Next we can call
glob() on the
extract_path to make a list of the contents of our data directory.
list(file.name for file in extract_path.glob('*'))
You will see a list with the following three datasets:
Parks and Open Space.zip
You've successfully completed the download portion of this tutorial.