Select to view content in your preferred language

How to download feature layer(s) to local machine using notebook

4061
8
05-07-2020 09:44 AM
by Anonymous User
Not applicable

I've been working through this post to try to download/backup my feature hosted layer(s) to gdb onto my local machine from ArcGIS Online using Notebook but it is not working. I can easily create a backup in a user's/my contents online but it does not work when I try and download the files to my desktop or similar. 

My code is below (excluding personal info) - it doesn't error it just doesn't really do anything?! I'm very very new to this so appreciate its a bit clunky. Any tips greatly appreciated!

#import modules
from arcgis.gis import GIS
from arcgis.features import manage_data
import time, os, fnmatch, shutil
from pathlib import Path

gis = GIS(username="Username", password="Password")

item_id = 'itemid'

data_item = gis.content.get(item_id)
data_item
data_item.title

fgdb_title = data_item.title
t = time.localtime()
timestamp = time.strftime('%d%b%Y_%H%M',t)
result = data_item.export(fgdb_title + ' ' + timestamp, "File Geodatabase")
result

data_path = Path(r'C:\Users\name\Desktop\backup')
if not data_path.exists():
 data_path.mkdir()
extract_path = data_path.joinpath('export')
result.download(save_path=data_path)
result.download
0 Kudos
8 Replies
by Anonymous User
Not applicable

Hi Flora,

I'm so sorry for the slow response - your question must have slipped through the cracks. In case you still have this question I'll answer.

Your code is good, it's only the filepath that needs changing. Due to the nature of ArcGIS Online Notebooks, you cannot use your local filepath to download directly to your desktop. You will instead need to download your export result to your AGOL Notebook working directory ('/arcgis/home/' accessible via the files tab in the Notebook UI) and can then download the resulting .zip file to your desktop by clicking it in the UI. If you wish to download several feature services you should download them all to the same folder, and then zip that folder using ZipFile. This will save you from having to manually click on each FGDB you export.

Below is an example of what can be done.

If you have anymore questions please feel free to ask.

Josh

by Anonymous User
Not applicable

Hey Josh,

Thanks for this! And no worries about the slow reply - I've only just revisited this. I've developed the code a little more so it reads through my content and exports certain item types. It works for the most part but there are a few that fail with the error exportItemId. Do you know what this means and what I need to do to my code to troubleshoot this? I've attached the last cell in my code below and an example of one of the failed error messages.

for data_item in useful:
    print("\nProcessing: " + data_item.title)
    try:
       result = data_item.export(data_item.title, "File Geodatabase")
       result.download(path)
    except Exception as e:
       print ("Error occurred \n",e)
Processing: Local Routes Error occurred   'exportItemId'
0 Kudos
by Anonymous User
Not applicable

Hi Flora - glad to hear you weren't waiting around! At first glance your code seems fine, it's hard to say what exactly is wrong without knowing what your variables contain. Is `useful` an array of items found via the search function?  What's `path` set to? 

Also if you break it out into separate cells it'll help us debug

DareWater
New Contributor

Josh,

   Is there any way to download files from my online Notbook files area to the browser using code rather than the UI?  It would be far preferable to execute a cell that downloads the results of my geoprocessing rather than try to give an end user instructions for how to click on the "Files" button, and look for a file of a particular name, and so on.

  Any help would be appreciated.

Clay

gis_user_ca
Emerging Contributor

I second this, We would love a way to have our backups on our local drive rather than have them stored in this folder. 

 

 

0 Kudos
IanHamilton1
Emerging Contributor

A little off topic, but I have created some files in the /arcgis/home area but I don’t seem to be able to delete things,

any guidance?

Ian

0 Kudos
by Anonymous User
Not applicable

Hi Ian - Are you trying to delete individual files, or folders? We currently don't support recursive delete in the UI so any deletes to folders containing files will not work. However, notebooks do support UNIX commands. If you wish to delete a directory and its contents you can run the rm command inside a cell with the recursive flag (-r).

!rm -r /arcgis/home/testFolder

If instead you are experiencing issues deleting individual files via the UI, please let me know or contact support.

Josh

RonaldVanderpool
Frequent Contributor

Hey Josh,

I have been through the various posts above and a fairly deep dive into Esri documentation covering working with content in the workspace but failing epically to find why my script will not export results to the arcgis/home in Notebooks.

The script I am using(before I modified it to represent details above) works great from Pro to download all of the AGOL folder feature layer contents to a folder on my desktop, zipping each feature layer individually, and naming them by the y_m_d when the script was run. It works great but I have to set reminders to do this for my systems on a scheduled basis.

My goal it to use the Tasks tool in Notebooks to automate the process so I do not have to worry about it.

Any rate, the script runs in Notebooks, but never places anything in the home folder.

Here is the last part of the script;

And any guidance would be appreciated!!! 

Folder layers export.jpg

0 Kudos