Select to view content in your preferred language

Exporting Hosted Feature Layers to File Geodatabase without attachments

1299
6
02-21-2024 08:39 AM
Dirk_Vandervoort
Regular Contributor

We are working on disaster recovery: we would like a Python API for ArcGIS script to (1) export all Hosted Feature Layers (HFLs) to File Geodatabases (FGDBs), and (2) download the FGDB to the local file system.

Many of the HFLs have layers with attachments. We do not want to include the attachments in the exported FGDB. 

We are working with code that looks like this:

    the_query = "type:Feature Service AND NOT typekeywords:View Service"
    some_content = gis.content.search(query=the_query, max_items=10000, outside_org=False)
    for an_item in some_content:
        the_Title = "{s} GDB".format(s=an_item.name)
        try:
            output_file = an_item.export(title=the_Title, export_format="File Geodatabase", wait=True)
            file_location = output_file.download(r'ArcGISOnlineBackup')
            delete_result =output_file.delete()

 Mostly this does what we want except there are attachments.

My question: Is there a way to perform the export without the attachments? 

I know I can do it with Shapefiles and I know I can do it with ArcPy. Neither is an option.

TIA

6 Replies
Tom_Laue
Regular Contributor

I'm only aware of the arcpy command to not export attachments within Pro exports.
arcpy.env.maintainAttachments = False

Other than writing a script to delete all the attachments after they've been downloading, I'm curious what other's have found as a solution.

0 Kudos
RhettZufelt
MVP Notable Contributor

One way is to create an empty local FGDB with the schema, then just use arcpy append to copy the data to the local FGDB with the 

arcpy.env.maintainAttachments = False setting.

R_

Clubdebambos
Frequent Contributor

Hi @Dirk_Vandervoort 

Can a file geodatabase already exist for your workflow? 

See below, this workflow:

  • gdb already exists
  • get feature layer of interest (you can alter to iterate through many feature services and each layer within)
  • Create Spatial Data Frame (SDF)
  • Export SDF to feature class

Code is commented.

 

 

from arcgis import GIS
from arcgis.features import GeoAccessor

## ouput gdb
out_gdb = r"C:\path\to\your\output.gdb"

## connect to AGOL
agol = GIS("home")

## grab a feature service item
item = agol.content.get("FS_ITEM_ID")

## access a layer - you can iterate through all
fl = item.layers[0]

## get the name of the layer, replace spaces with underscores because we all
## know the feature class naming rules
fl_name = fl.properties.name.replace(" ", "_")

## convert layer to spatial data frame
sdf = GeoAccessor.from_layer(fl)

"""
you could cleanup in here, for example remove unnecessary columns. do a couple
of test runs and see how the sdf exports to a feature class.

if your data is z or m enabled you can also figure that out at from the JSON
to help enable / disable with the export.

See the to_featureclass() method for GeoAccessor for more info
https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#arcgis.features.GeoAccessor.to_featureclass
"""

## export to featureclass
sdf.spatial.to_featureclass(
    location = "{0}\\{1}".format(out_gdb, fl_name)
)

 

 

You should also post as an Idea to make it so that we can use the normal channel of export/download without attachments.

~ learn.finaldraftmapping.com
0 Kudos
EarlMedina
Esri Regular Contributor

The way to do this is to use Create Replica with returnAttachments set to false and dataFormat set to "filegdb" - in the Python API, this will correspond to SyncManager's Create method: arcgis.features.managers module | ArcGIS API for Python.

Of course, in order to do this you will need to have the sync capability enabled (which I assume it is if you are doing offline field collection).

 

You can find sample code that shows how to create a replica here: Sync overview | ArcGIS API for Python

Dirk_Vandervoort
Regular Contributor

That's a good solution, I like it. However - and I should have included this - having sync capabilities is not an option for a variety of reasons.

0 Kudos
SMH-Rio
Frequent Contributor

It would be amazing to have a parameter in Export itself to disable attachments.

In my case, I chose to export as Geopackage. The bad thing is losing the 'automatic' relationship between layers. However, I'd rather rebuild this manually in gdb if necessary than spend hours exporting attachments or creating complex code to delete them.

0 Kudos