HOW TO

Herunterladen von in ArcGIS Online gehosteten Feature-Layern als ZIP-Dateien und Extrahieren der Dateien mithilfe der ArcGIS API for Python

Last Published: July 21, 2022

Zusammenfassung

Das Herunterladen und Extrahieren von ZIP-Dateien ist zeitaufwendig, wenn viele ZIP-Dateien einzeln heruntergeladen und extrahiert werden müssen. Diese Aufgabe kann mit ArcGIS API for Python automatisiert werden. Der Workflow in diesem Artikel enthält ein Python-Beispielskript zum Herunterladen von in ArcGIS Online gehosteten Feature-Layer-Elementen als ZIP-Dateien und Extrahieren der Dateien mit einer eigenständigen Python-Datei (.py). Die Python-Datei kann auch in Jupyter Notebook ausgeführt werden.

Vorgehensweise

  1. Importieren Sie die erforderlichen Module einschließlich des Moduls "zipfile".
from arcgis.gis import GIS from pathlib import Path, PurePath from zipfile import ZipFile
  1. Geben Sie die zu verwendenden ArcGIS Online-Anmeldeinformationen an.
gis = GIS("https://arcgis.com", "Username", "Password") print("Connected.")
  1. Definieren Sie eine Funktion für die Suche nach allen gehosteten Feature-Layern eines bestimmten Besitzers, laden Sie diese als einzelne ZIP-Dateien herunter, und extrahieren Sie die ZIP-Dateien in neue Ordner für alle gehosteten Feature-Layer.
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. Rufen Sie die in Schritt 3 erstellte Funktion auf.
Hinweis: Der Parameter "downloadFormat" ist auf "Shapefile" festgelegt, da ArcGIS Online Shapefiles als ZIP-Dateien herunterlädt.
downloadUserItems('Owner_Name', downloadFormat='Shapefile') print("All items downloaded")
Im folgenden Code-Block finden Sie das vollständige Skript.
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")

Artikel-ID: 000027904

Holen Sie sich Unterstützung mit KI

Lösen Sie Ihr Problem schnell mit dem Esri Support AI Chatbot.

Beginnen Sie jetzt mit dem Chatten

Zugehörige Informationen

Weitere Informationen zu diesem Thema erkunden

Unterstützung durch ArcGIS-Experten anfordern

An den technischen Support wenden

Beginnen Sie jetzt mit dem Chatten

Zu Download-Optionen wechseln