Select to view content in your preferred language

Python to download feature layer by name

311
3
Jump to solution
08-14-2024 12:58 AM
ForrestStreeter
Emerging Contributor

I have found some python that will loop through a list and download all feature layers from ArcGIS online. Found here.

How would I manipulate this script to only search and download a specified feature layer (prompted by input)?

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)

Thanks

0 Kudos
1 Solution

Accepted Solutions
RichardHowe
Frequent Contributor

Apolgies, I'm missing a aclosing apsotrohe. Last line shoudl read:

items = gis.content.search(query=FileName +', type:"Feature Service"') 

View solution in original post

0 Kudos
3 Replies
RichardHowe
Frequent Contributor

I'm assuming your putting a toolbox front end on this. So your input parameter would be a string.

 

So you define that as a variable (assuming it;s first input parameter e.g.:

FileName = arcpy.GetParameterAsText(0)

 

Then you change the search line to:

items = gis.content.search(query=FileName +', type:"Feature Service")
0 Kudos
RichardHowe
Frequent Contributor

Apolgies, I'm missing a aclosing apsotrohe. Last line shoudl read:

items = gis.content.search(query=FileName +', type:"Feature Service"') 
0 Kudos
ForrestStreeter
Emerging Contributor

Much appreciated. Thank you