Select to view content in your preferred language

Downloading AGOL feature as Excel file and shapefile with Python

3851
6
Jump to solution
03-10-2021 11:21 AM
ClintBoaz1
Occasional Contributor

I am trying to download an existing AGOL feature layer to a local server as both an excel file and shapefile using python.  The file is downloading however the shape file is only downloading the .shp file and non of the other files (cpg, dbf, prj, shx) and both the .shp and .xlsx files are producing an unregistered error message in ArcMap.  When I manually download the feature layer as a shapefile in AGOL it gives me a shapefile zipfile with all files that loads into ArcMap without error.  How can I replicate this with python?  I am new to AGOL and python 3.  Here is my code: 

import arcgis
import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True
from arcgis.gis import GIS

###Variables
AGOL_Login = GIS(None, "Username", "Password")
itemToDownload = AGOL_Login.content.get("ItemID")
exportLoc = r"Export Path"
itemExportName_XL = "RMapFieldData"
itemExportName_SHP = "RMap"

###EXCEL
###Export the ArcGIS Online Item
itemToDownload.export(itemExportName_XL, "Excel", None, True)

###Search for and Get the Newly Exported Item
searchForExportedItem = AGOL_Login.content.search(itemExportName_XL)
exportedItemID = searchForExportedItem[0].id
getTheExportedItems = AGOL_Login.content.get(exportedItemID)

###Download the Newly Exported Item
getTheExportedItems.download(exportLoc, "{}.xlsx".format(itemExportName_XL))

###SHAPEFILE
###Export the ArcGIS Online Item
itemToDownload.export(itemExportName_SHP, "Shapefile", None, True)

###Search for and Get the Newly Exported Item
searchForExportedItem = AGOL_Login.content.search(itemExportName_SHP)
exportedItemID = searchForExportedItem[0].id
getTheExportedItems = AGOL_Login.content.get(exportedItemID)

###Download the Newly Exported Item
getTheExportedItems.download(exportLoc, "{}.shp".format(itemExportName_SHP))

 

0 Kudos
1 Solution

Accepted Solutions
MehdiPira1
Esri Contributor

Hi @ClintBoaz1 ,

By putting .shp you are telling python to download just this file and not the other related files (Shapefiles).

The following script should work, you can also specify the save_path and file_name inside the download method:

 

item_1 = gis.content.get('FL_itemID')
item_shp = item_1.export(title='item_shapefile', export_format='Shapefile')
item_shp.download()
# you can specify the file path and name 
# item_shp.download(save_path=r"D:\temp", file_name="item_downloadedFromAGOL.zip")

item_2 = gis.content.get('FL_itemID')
item_excel = item_2.export(title='item_excel', export_format='Excel')
item_excel.download()

 

 

View solution in original post

6 Replies
DavidPike
MVP Frequent Contributor

I would give it a go without specifying the filename as an argument:

###Download the Newly Exported Item
getTheExportedItems.download(exportLoc)

 

0 Kudos
ClintBoaz1
Occasional Contributor

Hey David,

Thanks for the reply.  I just tied taking out the arguments as you suggested however after taking them out the .shp file does not export at all.  Only the excel file exports, still unregistered.   

0 Kudos
DavidPike
MVP Frequent Contributor

ah ok, possibly this might help if it's exported as a hosted feature layer Download Item Data - Esri Community but unfortunately I can't test anything, and I'm straying away from my experience. Perhaps:

from arcgis.features.manage_data import extract_data

shapefile = extract_data([getTheExportedItems])
shapefile.download('C:\\outFolder')

good luck!

0 Kudos
MehdiPira1
Esri Contributor

Hi @ClintBoaz1 ,

By putting .shp you are telling python to download just this file and not the other related files (Shapefiles).

The following script should work, you can also specify the save_path and file_name inside the download method:

 

item_1 = gis.content.get('FL_itemID')
item_shp = item_1.export(title='item_shapefile', export_format='Shapefile')
item_shp.download()
# you can specify the file path and name 
# item_shp.download(save_path=r"D:\temp", file_name="item_downloadedFromAGOL.zip")

item_2 = gis.content.get('FL_itemID')
item_excel = item_2.export(title='item_excel', export_format='Excel')
item_excel.download()

 

 

ClintBoaz1
Occasional Contributor

Hey Mehdi,

This worked!  Thank you.

0 Kudos
MehdiPira1
Esri Contributor

You're welcome @ClintBoaz1 

Glad to hear that!

0 Kudos