Download Feature Service as FGDB Question

942
5
09-30-2021 03:31 PM
Labels (2)
by Anonymous User
Not applicable

Good Evening (east coast US..)

I am developing a script which downloads feature services as FGDBs, unzips them, and then appends them to a SQL database for archiving purposes. My only issue is the FGDBs from the feature service download have randomly generated names... (i.e. 14347fdgydfdygvyutgfvycv-789u98h9bibh-gerrt34554y654-r9384hrt98h98h) which doesn't really do me any good. 

Is there a way around this? The only thing I could think of is to download each hosted feature service to its own directory.. unzip the data, rename the geodatabase. I have tried to find a pythonic way to rename the file after it is extracted from the .zip, but have not been successful. 

As the feature services in question all pertain to workforce, the schemas and FC names are the same across all feature services. So I have no way of knowing which GDB is the Parks and Rec... Utilities, Public Works... etc. 

I could amend the script to download feature services from AGOL based on a series of item ID's rather than a tag search... but I feel like this is unnecessary and not the right way to go about this. Am I missing something here? I would like a way to have the Geodatabase from the export have the same name as the Feature Service it was generated from. Any help is tremendously appreciated!

0 Kudos
5 Replies
LongDinh
Occasional Contributor II

Hi @Anonymous User,

Is there a way around this?

Always! It's a bit difficult to fix without understanding what you have done so far? Are you able to share snippets of your scripts?

As the feature services in question all pertain to workforce, the schemas and FC names are the same across all feature services. So I have no way of knowing which GDB is the Parks and Rec... Utilities, Public Works... etc.

Should the feature service url be present in your loop so that you can link and rename the gdb? Alternatively, you could download download the JSON properties of the service and append the gdb name that you download as a property as you loop through your urls.

Perhaps you can download the itemID metadata (e.g. AGOL name/description/summary) and use it for file naming purposes?

I could amend the script to download feature services from AGOL based on a series of item ID's rather than a tag search...

Is the purpose of your script to do something repeatable or are you trying to extract data based on tag searches?

by Anonymous User
Not applicable
gis= GIS("https://sarasota.maps.arcgis.com", username, password)
WorkforceDB= "C:\Database Connections\Workforce as DBO.sde"
#setworkspace
#arcpy.env.workspace

#download workforce items from AGO
folder_path = ('C:\WorkforceBackup')
num_items = int(('100'))
query_string = "tags: SQL Backup"
items = gis.content.search(query=query_string, max_items=num_items, sort_field='modifed', sort_order='desc')
print(str(len(items)) + " items will be backed up to " + folder_path +". See the list below:")
items

def download_as_fgdb(items, folder_path):
    for item in items:
        try:
            if 'View Service' in item.typeKeywords:
                print(item.title + " is view, not downloading")
            else: 
                print("Downloading " + item.title)
                version = time.strftime("%d_%b_%Y")
                result = item.export(item.title + "_" + version, "File Geodatabase")
                result.download(folder_path)
                result.delete()
                print("Successfully downloaded " + item.title)
        except:
            print("An error occurred downloading " + item.title)
            
    print("The function has completed")

download_as_fgdb(items, folder_path)

 

I am wanting to repeat this as a scheduled task, basically archiving the workforce hosted feature layers into a SQL Enterprise Geodatabase. The script works, but the default geodatabase names are too complex and I would like to just name them based on the hosted feature service they originated from. Thanks for the reply! 

0 Kudos
JakeSkinner
Esri Esteemed Contributor

@Anonymous User instead of downloading the feature service as a File Geodatabase, you can use ArcGIS Pro's Append tool and append the feature service directly to your Enterprise Geodatabase feature class.

by Anonymous User
Not applicable

Will appending directly from the Hosted FS preserve GlobalIDs? I wasn't even aware that the append tool could accept a feature service as input. Learned something!

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Yes, there is an Environments setting to do this:

JakeSkinner_0-1633345186738.png

 

0 Kudos