How can I export a Feature Layer in AGOL to Shapefile, using API for Python?

1305
3
05-07-2018 03:41 AM
SergioInfanzón
New Contributor

In AGOL you can export a layer to Shapefile, but I can't do it with the API for Python. I have only achieved it with some layer, passing through CSV, but it gives me many errors (text fields that pass to numeric, or layers that do not find the delimiter).

Can someone help me?

Thank you very much

0 Kudos
3 Replies
J_R_Matchett
New Contributor III

An arcgis Item has an export method that will save to another format. For example:

from arcgis.gis import GIS
gis = GIS(username='name', password='pass')
item = gis.content.get('ITEM_ID')
item_shp = item.export(title='item_shapefile', export_format='Shapefile')
# item_shp is stored on AGOL, it can be downloaded locally if desired
item_shp.download()
‍‍‍‍‍‍‍‍‍
PaulSweeney3
Occasional Contributor II

HI J.R. Matchett

Can this item be queried to reduce download sizes , say everything created in the last week for example ? 

Paul

0 Kudos
J_R_Matchett
New Contributor III

Yes, you can query the features you want, access the SpatialDataFrame, and export to a shapefile. For example:

layer = item.layers[0]
features = layer.query('created_date >= CURRENT_TIMESTAMP - 7')
features.sdf.spatial.to_featureclass('./output.shp')‍‍‍‍‍‍

The output shapefile will be saved locally in this case.

0 Kudos