Overview

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.

Before you begin

The ArcGIS API for Python DevLabs use Jupyter Notebooks to execute Python code. If you are new to Python, please see the ArcGIS API for Python guide to Using the Jupyter notebook environment.

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.

Steps

Download data from ArcGIS Online

  1. 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.

  2. 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
    
  3. Create a variable to hold the item id of the public dataset.

    public_data_item_id = 'a04933c045714492bda6886f355416f2'
    
  4. Create an anonymous connection to ArcGIS Online to download this public data.

    anon_gis = GIS()
    
  5. Access this dataset as an ArcGIS Python API Item object.

    data_item = anon_gis.content.get(public_data_item_id)
    data_item
    
  6. Download it to the notebook server's current location.

    data_item.download(save_path = r'./')
    

Extract the zip file

  1. Use the 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')
    
  2. Use Python 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:

  • Parks and Open Space.zip
  • Trailheads.csv
  • Trails.geojson

Congratulations, you're done!

You've successfully completed the download portion of this lab.

If you used notebooks.esri.com, your datasets are only available temporarily, so proceed to the Python Import data lab and use the same notebook session to complete it.