Export ArcGIS Online Layer to Feature Class Without Attachments using the ArcGIS API for Python

Today’s blog post is inspired by a question posed on Esri Communities. The question asks how to export hosted feature layers using only the ArcGIS API for Python, and specifically states not to use ArcPy. The usual method to extract data from ArcGIS Online is to export an item (a feature service) to a file geodatabase, that sits as a file geodatabase content item in ArcGIS Online, and then export that file geodatabase item to disk. The problem with this workflow is that there are no options to omit attachments, so if attachments are enabled for a layer and present, then they are going to be extracted each time.

In the workflow below, we will use the ArcGIS API for Python to convert a feature layer to a a feature class in a file geodatabase. The only caveat here, is that the file geodatabase must already exists. 

We will use the GeoAccessor module available with the ArcGIS API for Python. The GeoAccessor module enables spatial capabilities on a pandas dataframe and we can create a GeoAccessor object from a hosted feature layer, and then use the spatial dataframe (sdf) returned and call the GeoAccessor to_featureclass() method to export the data without any attachments.

Check out the code below and follow the comments.

					from arcgis import GIS
from arcgis.features import GeoAccessor

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

## if these exist in the fields we will remove them from the sdf
## you can more in here based on experience
remove_cols = ["FID", "OBJECTID", "OID"]

## 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)

## check if the columns to drop exists and get a list of those that exist
cols_to_drop = [col for col in remove_cols if col in sdf.columns]

## drop those columns
sdf = sdf.drop(columns=cols_to_drop)

## get rid of unwanted Shape_ fields
shape_cols = sdf.filter(like='Shape_').columns
sdf = sdf.drop(columns=shape_cols)

See the to_feature_class() method for GeoAccessor for more info

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

Let’s see it in action. Below we have a feature layer containing the County Boundaries in Ireland. If you notice KILDARE has 3 attachments.

After running the script we can check the output in ArcGIS Pro. Our feature layer has been exported from ArcGIS Online to a feature class with no attachments.

Leave a Comment

Your email address will not be published. Required fields are marked *