Download feature service as file geodatabase

15152
27
Jump to solution
01-24-2018 03:09 AM
XabierVelasco_Echeverría
Occasional Contributor

Hi,

I apply the following workflow to download a hosted feature service as a file geodatabase:

from arcgis.gis import GIS
gis = GIS("https://www.arcgis.com", USER, PASSWORD)
servicio = gis.content.get('XXXXXXXXXXX')
itemDescargar = servicio.export(nombre, 'File Geodatabase', parameters=None, wait='True')
fgdb = gis.content.get(itemDescargar ['exportItemId'])
fgdb.download(save_path='c:\modelbuilder')

If I execute like this, the downloaded ZIP has 1 KB. If I wait several seconds to download, the downloaded ZIP has the proper size (14MB). That means, servicio.export is still working while I try to download as I am calling from a variable.

If I instead execute servicio.export(nombre, 'File Geodatabase', parameters=None, wait='True') without a variable, the process finishes correctly before the next line of code is executed, but then I do not know what is the value of exportItemId in order to download it. How do I get the results of servicio.export? I mean, how do I get values from the output message? i.e.  {'type': 'file', 'size': 61145088, 'jobId': '359e30e3-0fc7-4925-8e92-354a4ccdd478::XXXXXXXX', 'serviceItemId': 'XXXXXXXX', 'exportFormat': 'fileGeodatabase', 'exportItemId': 'XXXXXXXXX'}

1 Solution

Accepted Solutions
XabierVelasco_Echeverría
Occasional Contributor

I found out a workaround for this bug.

from arcgis.gis import GIS
gis = GIS("https://www.arcgis.com", USER, PASSWORD)
servicio = gis.content.get('XXXXXXXXXXX')
servicio.export(nombre, 'File Geodatabase', parameters=None, wait='True') 
items = gis.content.search(nombre)
fgdb = gis.content.get(items[0].itemid)
fgdb.download(save_path=arcpy.env.scratchFolder)

The documentation for the API is a bit tricky, but if you search content you can get the item ID of the new item (FGDB). Anyway, the export function should not be asyncronous when you use the parameter wait = True. That means, if you call the export function inside a variable, why does it register as downloaded although the download is not finished yet?

View solution in original post

27 Replies
XabierVelasco_Echeverría
Occasional Contributor

For the API developers: 

 

Summing it up, how may I access the dictionary that outputs from an item.export? You can do that with the function results in arcpy, what is the proper way to do it with the arcgis module? I guess it should be super easy, but could not find examples in the documentation

0 Kudos
XabierVelasco_Echeverría
Occasional Contributor

I found out a workaround for this bug.

from arcgis.gis import GIS
gis = GIS("https://www.arcgis.com", USER, PASSWORD)
servicio = gis.content.get('XXXXXXXXXXX')
servicio.export(nombre, 'File Geodatabase', parameters=None, wait='True') 
items = gis.content.search(nombre)
fgdb = gis.content.get(items[0].itemid)
fgdb.download(save_path=arcpy.env.scratchFolder)

The documentation for the API is a bit tricky, but if you search content you can get the item ID of the new item (FGDB). Anyway, the export function should not be asyncronous when you use the parameter wait = True. That means, if you call the export function inside a variable, why does it register as downloaded although the download is not finished yet?

AllanKrygeris1
New Contributor

Thanks for this.

I was getting the "'dict' object has no attribute 'download'" error until I changed the wait parameter from a string:

servicio.export(nombre, 'File Geodatabase', parameters=None, wait='True') 

to a boolean:

servicio.export(nombre, 'File Geodatabase', parameters=None, wait=True) 

0 Kudos
Tom_Laue
New Contributor III

I was also intermittently getting the 1 KB zipped file export.

After a lot of testing, I found a solution that has been working for me.  I used Xabier Velasco Echeverría's  script (above) as a starting point and made a couple of additions:

  1. I added a line when exporting the hosted feature layer to an AGOL File GDB to wait 10 seconds to allow extra time while it searches ArcGIS Online for the item. This has avoided the error that it responds that the File Geodatabase the script created does not exist in ArcGIS Online.
  2. I added a While statement to verify the zipped file downloaded is a valid zipped file (not a 1 KB file). If not, the download will run again and again until a valid zipped file is downloaded.

Attached is my script.

Sveinung_Bertnes_Råheim
New Contributor III

I tested your script. Ran it in the arcgis pro version of python. And it worked first time.
Excellent documentation in the python script for novices like me!

0 Kudos
DarrylKlassen1
Occasional Contributor II

I tried this script - but am getting an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis.py", line 2590, in __getattr__
    raise AttributeError("'%s' object has no attribute '%s'" % (type(self).__name__, name))
AttributeError: 'Item' object has no attribute 'export'

Not sure why the item doesn't have this ability? In the settings in AGOL I have this turned on

Any thoughts?

Thanks

Darryl

0 Kudos
XabierVelasco_Echeverría
Occasional Contributor

Hi Darryl,

What is the size of the geodatabase that you are trying to download?

0 Kudos
DarrylKlassen1
Occasional Contributor II

I don't know the exact size since it is coming from AGOL, but it is a feature service hosted by AGOL with under 100 points, and only about 10 attributes.  So quite small.

0 Kudos
XabierVelasco_Echeverría
Occasional Contributor

Ok, could you please post the code you are executing so that I may check what is going on?

0 Kudos