How To: Herunterladen von Feature-Service-Elementen aus ArcGIS Online mithilfe von ArcGIS API for Python
Zusammenfassung
Beim Arbeiten mit Feature-Services in ArcGIS Online ist es nützlich, eine Kopie aller Feature-Services in ArcGIS Online als Sicherungskopie oder als Alternative bei Speicherplatzbeschränkungen zu erstellen. Das einzelne Herunterladen jedes Feature-Service kann recht zeitaufwendig sein, insbesondere bei großen Volumina. Das folgende Python-Skript ermöglicht dem Benutzer das Herunterladen einer Kopie aller Feature-Service-Elemente. Wenn der Prozess abgeschlossen ist, können diese Elemente gelöscht werden.
Vorgehensweise
Das folgende Skript sucht nach Feature-Service-Elementen des Benutzers und exportiert sie als einzelne Datei im gewünschten Format. Wenn die Datei in ArcGIS Online erstellt wurde, kann sie an den Dateipfad, der im Download-Abschnitt des Skripts angegeben ist, heruntergeladen werden.
- Importieren Sie in Python die erforderlichen Bibliotheken.
import arcgis from arcgis.gis import GIS
- Definieren Sie die Anmeldeinformationen.
gis = GIS(None,'username', 'password', verify_cert=False)
- Definieren Sie das Download-Format und den gewünschten Ausgabedateipfad.
def downloadUserItems(owner, downloadFormat): try: # Search items by username items = gis.content.search('owner:{0}'.format(owner)) 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('sample {}'.format(item.type), downloadFormat) result.download(r'file path of where to store the download') # Delete the item after it downloads to save space (OPTIONAL) result.delete() except Exception as e: print(e)
- Laden Sie die generierte Datei herunter.
downloadUserItems('username', downloadFormat='Shapefile')
import arcgis from arcgis.gis import GIS gis = GIS(None,'username', 'password', verify_cert=False) # Download all data from a user def downloadUserItems(owner, downloadFormat): try: # Search items by username items = gis.content.search('owner:{0}'.format(owner)) 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('sample {}'.format(item.type), downloadFormat) result.download(r'file path of where to store the download') # Delete the item after it downloads to save on space result.delete() except Exception as e: print(e) # Function takes in two parameters. Benutzername und Typ des Download-Formats downloadUserItems('username', downloadFormat='Shapefile')
Die Funktion downloadUserItems verwendet zwei Parameter:
- owner[string]: Für den Benutzernamen
- downloadFormat[string]: Das Format für die Download-Ausgabedatei
Für die Ausgabe gibt es mehrere Dateiformate. Einige davon sind: Shapefile (SHP), CSV, KML, FGDB, Feature-Sammlung.
Referenzinformationen
- ArcGIS-Hilfe: ArcGIS API for Python
- ArcGIS-Hilfe: Overview of the ArcGIS API for Python
- ArcGIS-Hilfe: Vorbereiten von Daten für die Offline-Verwendung