HOW TO
When working with feature services in ArcGIS Online, it is useful to create a copy of all feature services in ArcGIS Online as backup or if there are storage space limitations. Downloading each feature service individually can be time consuming, especially in large volumes. The following Python script allows users to download a copy of all feature service items. The items can be safely deleted once the process is complete.
The following script performs a search of the user's feature service items, and exports the items as a single file in the desired format. Once the file is created in ArcGIS Online, it can be downloaded to the file path specified in the download section of the script.
Note: The item.export() function is available only to users with an organizational subscription. This function is invokable only by the service item owner or an administrator.
import arcgis from arcgis.gis import GIS
gis = GIS(None,'username', 'password', verify_cert=False)
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)
downloadUserItems('username', downloadFormat='Shapefile')
The following is a sample full script:
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.title), 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. Username and the type of download format downloadUserItems('username', downloadFormat='Shapefile')
The downloadUserItems function receives two parameters:
Note: There are multiple file formats for the output and these include, but are not limited to - Shapefile (SHP), CSV, KML, File Geodatabase, Feature Collection. Refer to API Reference for the ArcGIS API for Python, and scroll down to the export method.
Get help from ArcGIS experts
Download the Esri Support App