Extracting from an ArcGIS Online Feature Service to a File Geodatabase

Table of Contents

Introduction

In this blog post we will assess the options for extracting layers and tables from an ArcGIS Online Feature Service to Feature Classes and Tables in a File Geodatabase. We will use ArcPy, the ArcGIS API for Python, and a combination of both.

Here are the options we will assess:

  • Export Layer to Feature Class using ArcPy
  • Export all Layers and Tables using ArcGIS API for Python & ArcPy
  • Download Feature Service as a File Geodatabase using the ArcGIS API for Python
  • Export a Layer to Feature Class using the ArcGIS API for Python
  • Export all Layers and Tables using the ArcGIS API for Python

Please note: we do not asses maintaining attachments or relationships in these workflows.

Export Layer to Feature Class with ArcPy

In this example we will use ArcPy to export a layer from ArcGIS Online. We need to know the url to the layer (or table) that we wish to export. We can find the URL from the Layer page within the Feature Service in ArcGIS Online as shown below. Simply copy the URL and paste into the upcoming script as the variable fl_url.

The workflow below uses the ArcPy ExportFeatures tool from the Conversion toolbox. When using an ArcGIS Online feature layer, the required in_features parameter for the ExportFeatures tool must be the feature layer’s URL. If you want to export a table you can replace the ExportFeatures tool with the ExportTable tool, also from the Conversion toolbox.

				
					import arcpy
 
## output feature class name
out_fc = "exported_layer"
 
## url to the feature layer
## this could also be to a table
fl_url = "https://services-eu1.arcgis.com/*******/arcgis/rest/services/FS_Name/FeatureServer/0"
 
## file geodatabase path
## NOTE you can use arcpy to create a geodatabase rather than having an existing
gdb = r"C:\Path\to\Documents\fgdb.gdb"
 
## path for saving output feature class
out_fc_path = "{0}\\{1}".format(gdb, out_fc)
 
## create feature class from feature layer
## you can also use FeatureClassToFeatureClass in older versions of ArcGIS Pro
## if exporting a table use arcpy.conversion.ExportTable()
arcpy.conversion.ExportFeatures(fl_url, out_fc_path)
				
			

Export all Layers and Tables using ArcGIS API for Python & ArcPy

We will build upon the previous example and export all layers and tables from a Feature Service in ArcGIS Online. In the workflow below we use the Feature Layer / Table URL property to access the URL for the datasets and use the Feature Layer / Table properties property to access the name of each Layer and Table which will be each exported datasets name in the File Geodatabase. You will need the Feature Service Item ID which you can get from the Feature Service URL or from the homepage of the Feature Service and over on the right-hand-side as per below.

				
					import arcpy
from arcgis.gis import GIS
 
## access AGOL
agol = GIS("home")
 
fs = agol.content.get("FeatureService_ID")
 
## file geodatabase path
## NOTE you can use arcpy to create a geodatabase rather than having an existing
gdb = r"C:\Path\to\Documents\fgdb.gdb"
 
for lyr in fs.layers:
    ## url to the feature layer
    fl_url = lyr.url
 
    ## output feature class name
    out_fc = lyr.properties.name
 
    ## path for saving output feature class
    out_fc_path = "{0}\\{1}".format(gdb, out_fc)
 
    ## create feature class from feature layer
    arcpy.conversion.ExportFeatures(fl_url, out_fc_path)
 
for tbl in fs.tables:
    ## url to the feature table
    tbl_url = tbl.url
 
    ## output table name
    out_tbl = tbl.properties.name
 
    ## path for saving output table
    out_tbl_path = "{0}\\{1}".format(gdb, out_tbl)
 
    ## create table from feature table
    arcpy.conversion.ExportTable(tbl_url, out_tbl_path)
				
			

Download Feature Service as a File Geodatabase using the ArcGIS API for Python

In this example we will purely use the ArcGIS API for Python to download a Feature Service as a File Geodatabase. We first get the Feature Service as an Item object and use the Item object export() method to export to a File Geodatabase content item in ArcGIS Online. We then use the File Geodatabase content item, which is also an Item object and call the download() method to download a zipped folder containing our File Geodatabase to the nominated folder. You could further evolve this script to extract the zipped File Geodatabase and copy its contents into another File Geodatabase of choice using ArcPy.

				
					from arcgis.gis import GIS
 
## what the file geodatabase will be called in ArcGIS Online
export_title = "fs_export"
 
## the export format
export_format = "File Geodatabase"
 
## where to save the download
save_path = r"C:\path\to\folder"
 
## access agol
agol = GIS("home")
 
## access the feature service
item = agol.content.get("FS_ITEM_ID")
 
## when we call the export() method from an Item object we are returned
## the Item object from the export
export = item.export(
    title = export_title,
    export_format = export_format
)
 
## use the download() method from the export Item object to download the
## file geodatabase in zipped folder
export.download(
    save_path = save_path
)
				
			

Export a Layer to Feature Class using the ArcGIS API for Python

In this example we will use an alternate method to export a Feature Layer / Table from ArcGIS Online using the ArcGIS API for Python. With this workflow we are using the Feature Layer object query method to return a Feature Set object containing all records, you could of course use the query() method to access a subset of records and export that subset. This workflow has a limitation, if there are no records in a Layer or Table, an empty dataset will not be exported. If you require a dataset to always be exported whether it contains data or not then I suggest using the first or second options from the list of export options in this blog post.

  • Export Layer to Feature Class using ArcPy
  • Export all Layers and Tables using ArcGIS API for Python & ArcPy
				
					from arcgis.gis import GIS
 
## required inputs
out_fc = "fs_export_name"
gdb = r"C:\Path\to\agol.gdb"
 
## access agol
agol = GIS("home")
 
## access the feature service
item = agol.content.get("FS_ITEM_ID")
 
## access feature layer of choice, here we are accessing the first layer
fl = item.layers[0]
 
## use query to return a FeatureSet object
export = fl.query()
 
## save the feature set as a feature class in a gdb
export.save(gdb, out_fc)
				
			

Export all Layers and Tables using the ArcGIS API for Python

In this example we build upon the previous example by iterating through the Feature Layers and Tables within a Feature Service. We will use the name of each Layer/Table to name our output Feature Classes and Tables.

				
					from arcgis.gis import GIS
 
## access AGOL
agol = GIS("home")
 
fs = agol.content.get("FS_ITEM_ID")
 
## file geodatabase path
## NOTE you can use arcpy to create a geodatabase rather than having an existing
gdb = r"C:\Path\to\agol.gdb"
 
for lyr in fs.layers:
    ## output feature class name
    out_fc = lyr.properties.name
 
    ## use query to return a feature set object
    export = lyr.query()
 
    ## save the feature set as a feature class in a gdb
    export.save(gdb, out_fc)
 
for tbl in fs.tables:
    ## output table name
    out_tbl = tbl.properties.name
 
    ## use query to return a feature set object
    export = tbl.query()
 
    ## save the feature set as a table in a gdb
    export.save(gdb, out_tbl)
				
			

Something missing?

Let me know in the comments if none of these satisfy your workflow and I can look at updating the list of options.

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

Leave a Comment

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