Download Item Data

8990
28
12-28-2016 11:57 AM
LesiMai3
Occasional Contributor

Hi,

I was trying to use the item's download method to download the data associated with a map image layer on Portal. But it doesn't seem to be able to download the data and I got b' ' as the output. I also tried the get_data method, which returns an error message saying "the JSON object must be str, not 'bytes'".

I did a similar workflow on a hosted feature layer on ArcGIS Online. And this time, it was able to download the item. But it only returns the data schema in a dictionary. Is there way to download the actual data behind a hosted feature service? Can the download method work with map image layers?

Or did I miss something here?

Thank you!

Tags (2)
0 Kudos
28 Replies
RohitSingh2
Esri Contributor

Hello,

You can download the data for file based item types. File based items include CSV, Service Definition, Shapefile, File Geodatabase, Powerpoint, etc.

Hosted feature layers and map image layers are not file based items, so you cannot use the item.download() method to download their data. 

For hosted feature layers, you can use one of the following ways to get the data:

1) Use extract_data tool from the features.manage_data module (doc: arcgis.features.manage_data module — arcgis 1.0 documentation). It's easy to use, couple examples:

from arcgis.features.manage_data import extract_data
csv_lyr = gis.content.get('c8bc7d3c3b60415e9845bc00dcd777ed')
outputgdb = extract_data([csv_lyr])
outputgdb.download('C:\\xc')
Out[]: 'C:\\xc\\extract_data_20161025204926.zip'

# you can also specify the format:
outputcsv = extract_data([csv_lyr], data_format='CSV')

2) You can get the related data item for the layer, and download the data item used to publish the layer:

relitems = layer_item.related_items("Service2Data")

relitems[0].download()

Hope this helps,

Rohit

LesiMai3
Occasional Contributor

Hi Rohit,

Thank you for the detailed clarification!

I tried the your sample code on a hosted feature layer but got the following error message --

How can I fix it?

Thank you,

Lesi

0 Kudos
RohitSingh2
Esri Contributor

Hi Lesi,

I'm not sure why extracting data to a file geodatabase isn't working and the error message isn't very informative. 

Please try specifying a different data_format such as Shapefile:

res = extract_data([lyr], data_format='Shapefile')

res.download('C:\\Test')

Thanks,

Rohit

0 Kudos
LesiMai3
Occasional Contributor

Hi Rohit,

You're right! Changing to a data format other than file geodatabase works. Not sure why though...

Thank you for your help!

Lesi

0 Kudos
BlakeGardiner2
New Contributor II

I can also confirm I'm getting the same problem. Won't export as a file geodatabase, but it will as a shapefile. When i try to specify output as FileGeodatabase, i get "Item does not exist or is inaccessible" which is obviously not the case.

0 Kudos
LesiMai3
Occasional Contributor

Hi Rohit,

I have a follow-up question. After I downloaded the data using extract_data, I noticed that file item was added to AGOL. Is that the intended result?

Thank you,

Lesi

0 Kudos
RohitSingh2
Esri Contributor

Hi Lesi,

That is the intended result. You can then get the data downloaded to your computer using:

res.download('C:\\Test')

Thanks,

Rohit

BenjaminBlackshear
New Contributor III

Is there a way to specify which folder within AGOL it is exported to?

0 Kudos
PhilLarkin1
Occasional Contributor III

agolItem = gis.content.get('xcvxcv345345345')
outputGDB = extract_data([agolItem])
downloadFolderPath = '\\\\your\\path\\Python\\Spatial_Data\\Downloads'
pathWithName = outputGDB.download(downloadFolderPath)

0 Kudos