3

I would like to publish a table to our ArcGIS Online (AGOL) organization account.

Is it possible to consume hosted features in AGOL to a local db or ArcSDE database connection?

By consume I mean import a hosted ArcGIS Online table into a geodatabase with ArcPy.

By local db, I mean file geodatabase

  • 1
    By what do you mean "consume"? Any data can always be loaded in a myriad of ways, but some of those ways are easier than others. Please edit the question to expound on the desired process. – Vince Apr 23 '15 at 21:15
  • What do you mean by a "local db"? Do you mean a file geodatabase? – PolyGeo Apr 27 '15 at 3:45
  • @Geoffrey West: I got the same error. I think it is caused by an empty dataset. – user93959 Mar 22 '17 at 15:52
3

Give a quick read of this blog. It lays the ground work for what you want to do. You just need to replace FeatureSet with RecordSet.

The following code uses a featureservice, and brings down the items as a table -- you keep saying table, so I assume you're talking items without geometry. If you actually mean features, change the returnGeo field to True, and then flip RecordSet to FeatureSet, and CopyRows to CopyFeatures

import arcpy

FSURL = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Military/FeatureServer/2/"

where = "1=1"
fields = "reinforced, speed, countrycode, staffcomment"
returnGeo = False
token = ''

query = "query?where={0}&outFields={1}&returnGeometry={2}&returnIdsOnly=false&returnCountOnly=false&f=json&token={3}".format(
where, fields, returnGeo, token)

url = FSURL + query
rs = arcpy.RecordSet()
rs.load(url)

arcpy.CopyRows_management(rs, r"c:\temp\f.gdb\fsTable")

Modify the query, where clause and fields to match what you want to copy local from your featureservice.

  • Kevin, will this work with the secured (non-public) AGOL hosted service? You need to provide username and password. I read that you can provide token, but how this token should be generated? – Alex Tereshenkov Apr 27 '15 at 16:40
  • You'll have to obtain a token to work with a secured service. Theres a code example to obtain a token here: github.com/arcpy/AutomaticUpdatesIntoFeatureService/blob/master/… – KHibma Apr 27 '15 at 18:36
  • This is a good start, but based on network connectivity, this script must leverage AGOL, and not ArcGIS Server for a hosted table. The ideal workflow would to have a constituent enter a table each day and a script then consumes this AGOL hosted table into a local file geodatabase or ArcSDE database. – Geoffrey West Apr 28 '15 at 13:46
  • Its the same difference. Replace the URL in the code sample with the one to a hosted feature service on AGOL. – KHibma Apr 28 '15 at 14:34
  • I see, when I try this, this error returns. RuntimeError: RecordSetObject: Cannot open table for Load – Geoffrey West Apr 28 '15 at 16:09
1

@ Alex Tereshenkov

Please see this script which takes into account the token generation for AGOL hosted features.

import arcpy
import json
import urllib
import urllib2

gtUrl = 'https://www.arcgis.com/sharing/rest/generateToken'
gtValues = {'username' : 'yourUserName',
'password' : 'YourPassWord',
'referer' : 'http://www.arcgis.com',
'f' : 'json' }
gtData = urllib.urlencode(gtValues)
gtRequest = urllib2.Request(gtUrl, gtData)
gtResponse = urllib2.urlopen(gtRequest)
gtJson = json.load(gtResponse)
gtToken = gtJson['token']

FSURL = "http://services1.arcgis.com/X1hcdGx5Fxqn4d0j/ArcGIS/rest/services/aService/FeatureServer/0/"
#last back slash is necessary

where = "1=1"
fields = "NUMBERCYLA, NAME"
returnGeo = False
token = gtToken

print token

query = "query?where={0}&outFields={1}&returnGeometry={2}&returnIdsOnly=false&returnCountOnly=false&f=json&token={3}".format(
where, fields, returnGeo, token)


url = FSURL + query

rs = arcpy.RecordSet()
rs.load(url)
arcpy.CopyRows_management(rs, r"c:\rest_test\rest_test.gdb\fsTable")

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.