Skip To Content ArcGIS for Developers Sign In Dashboard

Overview

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.

Before you begin

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.

Steps

Create a new notebook

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

  1. Import the 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
    
  2. Store the ID of the public data item we want to download.

    public_data_item_id = 'a04933c045714492bda6886f355416f2'
    
  3. Because the data is public, we can use an anonymous connection to ArcGIS Online to download the data.

    anon_gis = GIS()
    
  4. The content property for gis is an instance of a ContentManager that is used to manage content on ArcGIS Online. Below, get() makes an ArcGIS REST API request to retrieve an Item object data_item.

    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
    
  5. Download 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)
    

Extract the zip file

  1. Use ZipFile to extract the contents of the dataset.

    zip_file = ZipFile(zip_path)
    zip_file.extractall(path=extract_path)
    
  2. 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
  • Trailheads.csv
  • Trails.geojson

Congratulations, you're done!

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

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