HOW TO
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.
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)
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
Unterstützung durch ArcGIS-Experten anfordern
Beginnen Sie jetzt mit dem Chatten