[Python] download feature service from AGOL to folder

396
2
07-19-2021 12:29 PM
GISUserVa
New Contributor II

Using Python I am would like to take files from agol and download them onto my laptop as shapefile.  Does anyone have code that they could share with me?  I am new to python

Tags (1)
0 Kudos
2 Replies
curtvprice
MVP Esteemed Contributor

Here's an example using the gis module:

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

https://community.esri.com/t5/arcgis-api-for-python-questions/how-can-i-export-a-feature-layer-in-ag... 

HamishMorton
Esri Contributor

Hi,

There is a useful code sample in the following link showing how to download all items owned by a particular user - How To: Download feature service items from ArcGIS Online using ArcGIS API for Python (esri.com):

import arcgis
from arcgis.gis import GIS

gis = GIS(None,'username', 'password', verify_cert=False)

# Download all data from a user
def downloadUserItems(owner, downloadFormat):
    try:
        # Search items by username
        items = gis.content.search('owner:{0}'.format(owner))
        print(items)
        # Loop through each item and if equal to Feature service then download it
        for item in items:
            if item.type == 'Feature Service':
                result = item.export('sample {}'.format(item.title), downloadFormat)
                result.download(r'file path of where to store the download')
                # Delete the item after it downloads to save on space
                result.delete()
    except Exception as e:
        print(e)


# Function takes in two parameters. Username and the type of download format
downloadUserItems('username', downloadFormat='Shapefile')

 

Alternatively, if you have access to arcpy you could use the Feature Class to Feature Class tool - Feature Class To Feature Class (Conversion)—ArcGIS Pro | Documentation

Finally, the following post may also be of use - Solved: Re: Downloading AGOL feature as Excel file and sha... - Esri Community

 

Best,

Hamish

Hamish
0 Kudos