Check if a layer exists in ArcGIS Online

776
6
03-23-2021 12:04 PM
DryCreekEng
Occasional Contributor

I am uploading about 40 zip files to AGOL as shapefiles being published as feature layers using a Jupyter Notebook.  Just about all of the zip files are shapefiles but I have discovered a few are actually geodatabases zipped up.  Each time the program finds a gdb, it stops and I get an error.  I have to rename the folder, go into AGOL and delete ALL the layers before it that were just created, then rerun the program until it hits another gdb.  Then I repeat those steps.  I tried using " if arcpy.Exists" but that only checks for existence in the folder on my drive, not on AGOL.  I would ideally like it to print the files that dont work but keep on chugging through.  

list = []
for p in arcpy.ListFiles("*.zip"):
    list.append(p)
    print (p)    
for n in list:
    if "gdb" not in n:
        if "App" not in n:
                print (n)
                data_file_location = workspace + "\\" + n
                layer = { 'title': ('%s') % n[:-4],    'tags': 'data collection',    'type': 'Shapefile'}
                add_layer = gis.content.add(layer, data = data_file_location) 
                pub = add_layer.publish()

 

Tags (3)
0 Kudos
6 Replies
MehdiPira1
Esri Contributor

Hi @DryCreekEng ,

For the file geodatabase you need to specify the type in the item properties.

This is a quick script I put together:

if gdb:
    item_props = {'type':'File Geodatabase'}
elif shapefile:
    item_props = {'type':'Shapefile'}
else: 
    .....
add_item = gis.content.add(item_properties= item_props, data = path to data file)

 

I hope that helps.

Mehdi

DryCreekEng
Occasional Contributor

thank you Mehdi, I had tried to do this but wasn't successful.  Its good to know I can set the data type in the code like that.

0 Kudos
MehdiPira1
Esri Contributor

@DryCreekEng ,

Not a problem. What is the error you're getting?

You'd better clean up the AGOL from the published zipped files first.

0 Kudos
DryCreekEng
Occasional Contributor

I get an error saying that the layer already exists in AGOL, and the code stops running.  My problem is that I run code to upload a bunch of files, and if an error occurs, the code stops.  I fix the problem, but before I can run the code again, I have to delete the files in AGOL that were uploaded prior to the error. Otherwise, I get the error saying the files already exist in AGOL. Its a pain, and I would like to use "OverwriteOutput = True" or something along the lines of "ifExists" but I havent been able to get anything to work. 

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

You can use gis.content.search to check if a service is existing in AGOL or Portal.

have you seen this user guide:

https://developers.arcgis.com/python/guide/working-with-feature-layers-and-features/#Searching-the-G...

 

0 Kudos
DryCreekEng
Occasional Contributor

Thank you @simoxu for the article.  The article shows how you can check for a feature by specifying the feature's title or ID. Do you know how I could use this to recursively search for a feature as part of an if statement, and if True, then skip over the layer or replace it?

0 Kudos