I'm recording data using the Survey123 app which logs data into a standard ESRI rest service, stored in my AGOL account. I would like to save this data an SDE dataset. Is it possible to do so using python (arcpy or other library)?


This can be easily done with ArcREST.

from arcrest.manageorg import Administration
from arcrest import AGOLTokenSecurityHandler
from arcrest.agol import FeatureService
from arcrest.common.filters import LayerDefinitionFilter

def download_features(fs_url,where_clause,out_path):
    '''downloads a hosted service features into a feature class'''
    agol_securityHandler = AGOLTokenSecurityHandler('user','pwd','http://company.maps.arcgis.com/')
    agol_org_obj = Administration(securityHandler=agol_securityHandler,initialize=True)

    fs = FeatureService(url=fs_url,securityHandler=agol_securityHandler,initialize=True)

    ldf = LayerDefinitionFilter()
    ldf.addFilter(0, where=where_clause)

    queryResults = fs.query(layerDefsFilter=ldf,returnCountOnly=False,returnGeometry=True)
    result = queryResults[0].save(r'in_memory','SampleCities')

This is the function that will do the work. I query the hosted feature service with an SQL where clause. If you need to take all of your features, just use the 1=1 for the where clause. I like writing first to in_memory and then copying from there into the SDE.

Now calling the function:

out_path = r'C:\GIS\Temp\yourdb.sde\yourdb.dbo.SampleCities'
fs_url = r'http://services3.arcgis.com/companyid/arcgis/rest/services/Cities/FeatureServer'
where_clause = "POP1990 > 1000000"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.