Select to view content in your preferred language

[Python] download feature service from AGOL to folder

1937
3
07-19-2021 12:29 PM
by Anonymous User
Not applicable

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
3 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
WillemVan_Riet1
Emerging Contributor

Hi, I posted an error when I tried the code sample from How To: Download feature service items from ArcGIS Online using ArcGIS API for Python (esri.com):

# define the download format and the specified output path

# path = my_test_path ## changed for forum post 

def downloadUserItems(owner, downloadFormat):

    try:

        # Search items by username

#         items = gis.content.search('owner:{0}'.format(owner))

#         print(items)

        # search items by group

        group = gis.groups.search("title:ArcGIS API Test", max_groups=15)[0]

        items = group.content()

        print(items)

        # Loop through each item and if equal to Feature service then download it

        for item in items:

            print(item.type)

           # if item.type in ['Feature Service', 'Vector Tile Service', 'Scene Service']:                

            result = item.export('sample {}'.format(item.title), downloadFormat, wait =True)

            result.download(path)

            # Delete the item after it downloads to save space (OPTIONAL)

           # result.delete()

            print("Exported item: {}".format(result))

    except Exception as e:

        print(e)

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

 And I get this error:

[<Item title:"Storm_Feature" type:Feature Layer Collection owner:dsfgis>]
Feature Service
Could not export item.

 Your help/insights would be appreciated!

0 Kudos