1

Goal: I want to create copies of feature services and save them in a FGDB in a network folder on my machine. The overall purpose of the script is to have back-ups of my authoritative feature services from AGOL.

What I've tried: I originally found How To: Download feature service items from ArcGIS Online using ArcGIS API for Python, which has the same idea (it runs without error but produces no results). The following code I've been putting together creates the FGDBs in a directory. If I understand correctly, arcpy.CreateFileGDB_management() will create an empty FGDB. Everything up to this point works the way I want it to. However, I'm confused as to how to get the feature services to be copied to the empty FGDBs. From here how can I use arcpy.management.CopyFeatures() to copy the feature services to the FGDBs?

from arcgis.gis import GIS
import shutil
from pathlib import Path
import os

gis = GIS("agol/url", "username", "password", verify_cert=False)
#iterate over all feature layer items and get the authoritative ones I've created
auth_items = [i for i in gis.content.search(query="owner:" + gis.users.me.username, item_type='Feature Layer', 
             max_items=-1) if i.content_status == 'org_authoritative' or i.content_status == 'public_authoritative']
rootDir = r'path\to\BackUpFiles\FeatureLayers'

#remove old directory
try:
    shutil.rmtree(rootDir)
except FileExistsError as e:
    print(e)
#create new directory
Path(rootDir).mkdir(parents=True, exist_ok=True)

#create FGDBs from feature services
for a in auth_items:
    if a.type == 'Feature Service':
        name = a.title
        t = a.type
        print(f'{name} | {t}')
        arcpy.CreateFileGDB_management(rootDir, name)

To show you that the FGDBs are in fact created, here is a screenshot of them in Pro followed by a screenshot of where they reside in the directory. I'm also confused as to why you see files in the directory, while the FGDB in Pro is empty.

enter image description here

enter image description here

2
  • 2
    CreateFileGDB_management` only creates the container, not the feature classes in the container. An empty geodatabase contains files (the seven hidden "tables" of the geodatabase are implemented in the geodatabase). `
    – Vince
    Commented Jun 7, 2021 at 22:02
  • Thanks, I was sort of assuming that. More to the point, I'm looking for a way to write feature services to those FGDBs.
    – Pfalbaum
    Commented Jun 8, 2021 at 1:58

1 Answer 1

0

I ended up modifying the linked help doc: How To: Download feature service items from ArcGIS Online using ArcGIS API for Python

  • At the time of writing it wasn't working for me. As such, if you have multiple items of the same type they will be overwritten: result = item.export('sample {}'.format(item.type), downloadFormat)

  • Changed to: result = item.export(f'{item.title}', downloadFormat)

  • The "Note:" at the bottom of the page is misleading. It tells you to use FGDB for the parameter here: downloadUserItems('owner', downloadFormat='FGDB')

  • Changed to: downloadUserItems('owner', downloadFormat='File Geodatabase')

  • This searches the item by owner and format: items = gis.content.search('owner:{0}'.format(owner))

  • Changed to: auth_items = [i for i in gis.content.search(query="owner:" + gis.users.me.username, item_type='Feature Layer', max_items=-1) if i.content_status == 'org_authoritative' or i.content_status == 'public_authoritative'] because I'm only interested in the 'authoritative' items.

The following backs up all feature services in your AGOL content marked as 'Authoritative' . It will export them from AGOL to FGDBs and save them to whatever directory you tell it to.

import arcgis
from arcgis.gis import GIS

#enter AGOL sign-in credentials
gis = GIS(None, "username", "password", verify_cert=False)


def downloadUserItems(owner, downloadFormat):
    try:
        # Search 'authoritative' items
        auth_items = [i for i in gis.content.search(query="owner:" + gis.users.me.username, item_type='Feature Layer', 
                     max_items=-1) if i.content_status == 'org_authoritative' or i.content_status == 'public_authoritative']
        # Loop through each item and if equal to Feature service then download it
        for item in auth_items:
            if item.type == 'Feature Service':
                print(item)
                result = item.export(f'{item.title}', downloadFormat)
                #r'file path of where to store the download'
                result.download(r'path\to\BackUpFiles\FeatureLayers')
                # Delete the item after it downloads to save on space
                result.delete()
    except Exception as e:
        print(e)
# Function takes two parameters. Username and the type of download format
# replace 'owner' with your username. replace download format with relevant format.
downloadUserItems('owner', downloadFormat='File Geodatabase')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.