Using a AGOL HFS in Python directly?

1881
11
Jump to solution
04-17-2018 01:28 PM
DougBrowning
MVP Esteemed Contributor

In the past I have had some luck giving a AGOL HFS directly to a built in geoprocessing tool or script.  I usually created a layer file and fed that in.  BUT layer files refuse to save the tables - and most of what I want to use in tables.  In fact tables are always hard to get in ArcMap.  Try it - (Add a HFS with tables to ArcMap - save as layer file - open layer file - no tables! Or go to Add Data button - AGOL HFS - no tables! Or File - ArcOnline - Add - no tables!  Finally in Catalog go down to My Hosted Services - add the HFS - and actual tables!  But then save this out as a lyr file and tables are gone.).

What I would like to do is change my input source in Python from this

sourceDB = r"\\adgroup\blah\blah\data.gdb"

to something like

sourceDB = "direct link to HFS in AGOL"

Most of the script is Search Cursors so it would need to work with that.  This may be over hoping.

Any ideas on this? 

Backup plan is to use the python api to export/download a GDB (I already have this code) then unzip and feed to script.  But it would be nice to just go direct.

Another option may be ArcPro using v3?

Thanks for any ideas.

0 Kudos
11 Replies
JonathanQuinn
Esri Notable Contributor

What does your script look like? This works for me:

import json, arcpy
fs = arcpy.FeatureSet()
fs.load("http://sampleserver6.arcgisonline.com/arcgis/rest/services/ServiceRequest/FeatureServer/0/query?wher...")
fsJSON = json.loads(fs.JSON)
with arcpy.da.SearchCursor(fs,"*") as cursor:
    for row in cursor:
        print(row)
        break

rs = arcpy.RecordSet()
rs.load("http://sampleserver6.arcgisonline.com/arcgis/rest/services/ServiceRequest/FeatureServer/1/query?wher...")
rsJSON = json.loads(rs.JSON)
with arcpy.da.SearchCursor(rs,"*") as cursor:
    for row in cursor:
        print(row)
        break‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The second layer in the ServiceRequest service is a table. The responses are:

(1118183, None, None, None, None, None, None, None, u'Unassigned', u'{F522A006-6BD4-432F-B55B-F52B6C48D8FC}', None, None, (None, None))
(310006, u'1', u'Obstructing stop sign', datetime.datetime(2018, 4, 5, 17, 15), 4, u'{1D161AE8-F54F-45B4-8EFC-4FCFEB42C76B}')‍‍

The breaks are in there to just get the first row.

0 Kudos
DougBrowning
MVP Esteemed Contributor

You code with your URLs works just fine for me. 

If I use only your code and paste in my URL for a FC it works (with a added manually token).

If I change only the layer number from a FC layer to a table I get the Cannot load to table error again.

If I take the table URL that fails in python and paste it into the browser it works just fine.

If I take the result from the browser, add it as text to Python, then try a load from text I get the Error again!

I asked and I can maybe share it with you if you give the name to share with.

Weird.

Thanks

0 Kudos