HOW TO
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".
To create and download a file geodatabase replica from a hosted feature service with Python, follow the steps below.
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!")
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
Get help from ArcGIS experts
Download the Esri Support App