HOW TO

Create and download a file geodatabase replica from a hosted feature service using Python

Last Published: April 25, 2020

Summary

Hosted feature services can be downloaded as replica geodatabases from the REST endpoint of the service when sync is enabled on the service. In some circumstances (such as period data backups), it may be useful to programmatically create replicas of services.

Note:
The procedure described below is for Python 2.x, and works for ArcMap. 
For ArcGIS Pro, using Python 3.x, use the Feature Class to Feature Class tool, as described in the GeoNet blog "Download ArcGIS Online Feature Service or ArcGIS Server Feature/Map Service".

Procedure

To create and download a file geodatabase replica from a hosted feature service with Python, follow the steps below.

  1. Copy and paste the following Python script to a text or Python editor.
    import urllib, urllib2, json, time, os
    
    username = "username"                                             #CHANGE
    password = "password"                                             #CHANGE
    replicaURL = "feature service url/FeatureServer/createReplica"    #CHANGE
    replicaLayers = [0]                                               #CHANGE
    replicaName = "replicaTest"                                       #CHANGE
    
    def sendRequest(request):
        response = urllib2.urlopen(request)
        readResponse = response.read()
        jsonResponse = json.loads(readResponse)
        return jsonResponse
    
    print("Generating token")
    url = "https://arcgis.com/sharing/rest/generateToken"
    data = {'username': username,
            'password': password,
            'referer': "https://www.arcgis.com",
            'f': 'json'}
    request = urllib2.Request(url, urllib.urlencode(data))
    jsonResponse = sendRequest(request)
    token = jsonResponse['token']
    
    print("Creating the replica")
    data = {'f' : 'json',
        'replicaName' : replicaName,
        'layers' : replicaLayers,
        'returnAttachments' : 'true',
        'returnAttachmentsDatabyURL' : 'false',
        'syncModel' : 'none',
        'dataFormat' : 'filegdb',
        'async' : 'true',
        'token': token}
    request = urllib2.Request(replicaURL, urllib.urlencode(data))
    jsonResponse = sendRequest(request)
    print(jsonResponse)
    
    print("Pinging the server")
    responseUrl = jsonResponse['statusUrl']
    url = "{}?f=json&token={}".format(responseUrl, token)
    request = urllib2.Request(url)
    jsonResponse = sendRequest(request)
    while not jsonResponse.get("status") == "Completed":
        time.sleep(5)
        request = urllib2.Request(url)
        jsonResponse = sendRequest(request)
    
    userDownloads = os.environ['USERPROFILE'] + "\\Downloads"
    
    print("Downloading the replica. In case this fails note that the replica URL is: \n")
    jres = jsonResponse['resultUrl']
    url = "{0}?token={1}".format(jres, token)
    print(url)
    f = urllib2.urlopen(url)
    with open(userDownloads + "\\" + os.path.basename(jres), "wb") as local_file:
        local_file.write(f.read())
    print("\n Finished!")
  2. For the first five lines of the script, modify the following:
    • Modify "username" and "password" to match the name and credentials of the administrative account with the hosted feature service.
    • Modify the "feature service url" in "feature service url/FeatureServer/createReplica" with the URL of the hosted feature service to replicate.
    • Modify the [0] with the list of layers and table to include in the replica.
    • Modify "replicaTest" to the users' name for the replica.
  3. Select File > Save.
  4. Navigate to the .py file and double-click the file to run the script.
Note:
The script assumes the replica is created asynchronously. After the create replica request is submitted, the server is pinged every five seconds until the zipped file is fully populated, at which point the zipped file is downloaded in the Downloads folder.

Article ID:000012968

Software:
  • ArcGIS API for Python
  • ArcGIS Online

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic