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

233
5
05-07-2020 09:44 AM
Labels (1)
Highlighted
New Contributor

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
Reply
0 Kudos
5 Replies
Highlighted
New Contributor III

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

Highlighted
New Contributor

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'
Reply
0 Kudos
Highlighted
New Contributor III

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

Highlighted
New 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

Reply
0 Kudos
Highlighted
New Contributor III

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