HOW TO
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.
from arcgis.gis import GIS from pathlib import Path, PurePath from zipfile import ZipFile
gis = GIS("https://arcgis.com", "Username", "Password") print("Connected.")
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)
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")
Get help from ArcGIS experts
Download the Esri Support App