English

How To: Download feature service items from ArcGIS Online using ArcGIS API for Python

Summary

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.

Procedure

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.

  1. In Python, import the necessary libraries.
    import arcgis
    from arcgis.gis import GIS
  2. Define the login credentials.
    gis = GIS(None,'username', 'password', verify_cert=False)
  3. Define the download format and the specified output file path. 
    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)
  4. Download the file generated.
    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.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. Username and the type of download format
downloadUserItems('username', downloadFormat='Shapefile')

The downloadUserItems function receives two parameters:

  • owner[string] - for the username
  • downloadFormat[string] - the format for the download output file

There are multiple file formats for the output and these include, but are not limited to - shapefile (SHP), CSV, KML, FGDB, feature collection.

Related Information