Skip to main content
added 2 characters in body
Source Link
PolyGeo
  • 65.5k
  • 29
  • 115
  • 352

I have achieved similar in the past with the below code. I have commented but let me know if anything needs clarification. Basically it uses a combination of the AGOLArcGIS Python API and ArcPy to access the feature services and export layers to feature classes in a gdb.

from arcgis import GIS
import arcpy

## set to True if you want any attachments to also be exported
## a lot faster to ignore any attachments if they are not needed as output
arcpy.env.maintainAttachments = False

## access AGOL
conn = GIS("home")

## you could change this to a list of feature service ids and iterate to
## export them to gdb
## get the item ID from the Feature Service page on AGOL, it is in the URL
## bar at the top of the browser 
fs_id = "INSERT ITEM ID"

## gdb path to export to
out_gdb = r"INSERT GDB PATH"

## get the fs content, here is where you could loop through multiple feature
## service ids and export
item = conn.content.get(fs_id)

## use item.tables if interested in non-spatial tables
for lyr in item.layers:
    ## you might need to use the string replace function and replace any
    ## spaces with underscores as fc naming wont allow spaces and the
    ## feature class to feature class tool will fail.
    fc_name = lyr.properties.name
    ## at this point you could use an if statement if you are only interested 
    ## in layers with a certain name
    print(fc_name)
    ## get direct url to the layer within the feature service
    fc = lyr.url
    ## export to gdb
    arcpy.FeatureClassToFeatureClass_conversion(fc, out_gdb, fc_name)

I have achieved similar in the past with the below code. I have commented but let me know if anything needs clarification. Basically it uses a combination of the AGOL Python API and ArcPy to access the feature services and export layers to feature classes in a gdb.

from arcgis import GIS
import arcpy

## set to True if you want any attachments to also be exported
## a lot faster to ignore any attachments if they are not needed as output
arcpy.env.maintainAttachments = False

## access AGOL
conn = GIS("home")

## you could change this to a list of feature service ids and iterate to
## export them to gdb
## get the item ID from the Feature Service page on AGOL, it is in the URL
## bar at the top of the browser 
fs_id = "INSERT ITEM ID"

## gdb path to export to
out_gdb = r"INSERT GDB PATH"

## get the fs content, here is where you could loop through multiple feature
## service ids and export
item = conn.content.get(fs_id)

## use item.tables if interested in non-spatial tables
for lyr in item.layers:
    ## you might need to use the string replace function and replace any
    ## spaces with underscores as fc naming wont allow spaces and the
    ## feature class to feature class tool will fail.
    fc_name = lyr.properties.name
    ## at this point you could use an if statement if you are only interested 
    ## in layers with a certain name
    print(fc_name)
    ## get direct url to the layer within the feature service
    fc = lyr.url
    ## export to gdb
    arcpy.FeatureClassToFeatureClass_conversion(fc, out_gdb, fc_name)

I have achieved similar in the past with the below code. I have commented but let me know if anything needs clarification. Basically it uses a combination of the ArcGIS Python API and ArcPy to access the feature services and export layers to feature classes in a gdb.

from arcgis import GIS
import arcpy

## set to True if you want any attachments to also be exported
## a lot faster to ignore any attachments if they are not needed as output
arcpy.env.maintainAttachments = False

## access AGOL
conn = GIS("home")

## you could change this to a list of feature service ids and iterate to
## export them to gdb
## get the item ID from the Feature Service page on AGOL, it is in the URL
## bar at the top of the browser 
fs_id = "INSERT ITEM ID"

## gdb path to export to
out_gdb = r"INSERT GDB PATH"

## get the fs content, here is where you could loop through multiple feature
## service ids and export
item = conn.content.get(fs_id)

## use item.tables if interested in non-spatial tables
for lyr in item.layers:
    ## you might need to use the string replace function and replace any
    ## spaces with underscores as fc naming wont allow spaces and the
    ## feature class to feature class tool will fail.
    fc_name = lyr.properties.name
    ## at this point you could use an if statement if you are only interested 
    ## in layers with a certain name
    print(fc_name)
    ## get direct url to the layer within the feature service
    fc = lyr.url
    ## export to gdb
    arcpy.FeatureClassToFeatureClass_conversion(fc, out_gdb, fc_name)
Source Link
Clubdebambos
  • 1.7k
  • 1
  • 12
  • 20

I have achieved similar in the past with the below code. I have commented but let me know if anything needs clarification. Basically it uses a combination of the AGOL Python API and ArcPy to access the feature services and export layers to feature classes in a gdb.

from arcgis import GIS
import arcpy

## set to True if you want any attachments to also be exported
## a lot faster to ignore any attachments if they are not needed as output
arcpy.env.maintainAttachments = False

## access AGOL
conn = GIS("home")

## you could change this to a list of feature service ids and iterate to
## export them to gdb
## get the item ID from the Feature Service page on AGOL, it is in the URL
## bar at the top of the browser 
fs_id = "INSERT ITEM ID"

## gdb path to export to
out_gdb = r"INSERT GDB PATH"

## get the fs content, here is where you could loop through multiple feature
## service ids and export
item = conn.content.get(fs_id)

## use item.tables if interested in non-spatial tables
for lyr in item.layers:
    ## you might need to use the string replace function and replace any
    ## spaces with underscores as fc naming wont allow spaces and the
    ## feature class to feature class tool will fail.
    fc_name = lyr.properties.name
    ## at this point you could use an if statement if you are only interested 
    ## in layers with a certain name
    print(fc_name)
    ## get direct url to the layer within the feature service
    fc = lyr.url
    ## export to gdb
    arcpy.FeatureClassToFeatureClass_conversion(fc, out_gdb, fc_name)