HOW TO

Download ArcGIS Online hosted feature layers as zip files and extract the files using ArcGIS API for Python

Last Published: July 21, 2022

Summary

Downloading and extracting .zip files is time-consuming if many .zip files must be individually downloaded and extracted. This task can be automated using ArcGIS API for Python. The workflow in this article provides a sample Python script to download ArcGIS Online hosted feature layer items as .zip files and extract them using a stand-alone Python file (.py). The Python file also runs in Jupyter Notebook.

Procedure

  1. Import the necessary modules including the zipfile module.
from arcgis.gis import GIS
from pathlib import Path, PurePath
from zipfile import ZipFile
  1. Specify the ArcGIS Online credentials to use.
gis = GIS("https://arcgis.com", "Username", "Password")
print("Connected.")
  1. Define a function to search for all the hosted feature layers owned by a specific owner, download them as individual .zip files, and extract the .zip files to new folders for all the hosted feature layers.
def downloadUserItems(owner, downloadFormat):
    try:
        # Search items by username
        items = gis.content.search(query='owner:Owner_Name', item_type='Feature Service')
        print(items)
        # Loop through each item and if equal to Feature Service then download it
        for item in items:
            if item.type == 'Feature Service': #Feature Service is selected, since the source for Feature Layer (Hosted) type item is Feature Service
                result = item.export(item.title, downloadFormat)
                #Set the download path for the ZIP files
                data_path = Path(r'<Download_Folder_Path')
                result.download(save_path=data_path)
                #Create a new ZIP file path, new folder with the same name, and extract the ZIP files items into respective folders
                zip_path = data_path.joinpath(result.title + '.zip')
                extract_path = data_path.joinpath(result.title)
                if not extract_path.exists():
                    extract_path.mkdir()
                with ZipFile(zip_path, 'r') as zip_ref:
                    zip_ref.extractall(extract_path)
                # Delete the item after it downloads to save on space - Optional
                result.delete()
    except Exception as e:
        print(e)
  1. Call the function created in Step 3.
Note:
The downloadFormat parameter is set to 'Shapefile' as ArcGIS Online downloads shapefiles as .zip files.
downloadUserItems('Owner_Name', downloadFormat='Shapefile') 
print("All items downloaded")
The code block below demonstrates the full script.
import arcgis
from arcgis.gis import GIS
from pathlib import Path, PurePath
from zipfile import ZipFile

gis = GIS("https://arcgis.com", "Username", "Password")
print("Connected")

# Download all data from a user
def downloadUserItems(owner, downloadFormat):
    try:
        # Search items by username
        items = gis.content.search(query='owner:Owner123', item_type='Feature Service')
        print(items)
        # Loop through each item and if equal to Feature Service then download it
        for item in items:
            if item.type == 'Feature Service':
                result = item.export(item.title, downloadFormat)
                data_path = Path(r'C:\Users\USER\Desktop\Download Folder')
                result.download(save_path=data_path)
                zip_path = data_path.joinpath(result.title + '.zip')
                extract_path = data_path.joinpath(result.title)
                if not extract_path.exists():
                    extract_path.mkdir()
                with ZipFile(zip_path, 'r') as zip_ref:
                    zip_ref.extractall(extract_path)
                # Delete the item after it downloads to save on space - Optional
                result.delete()
    except Exception as e:
        print(e)


# Function takes in two parameters. Username, and the type of download format, in this case, Shapefile.
downloadUserItems('Owner123', downloadFormat='Shapefile')
print("All items downloaded")

Article ID: 000027904

Software:
  • ArcGIS Online
  • ArcGIS API for Python

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options