Using Python API to Update a Hosted Feature Layer

6148
3
Jump to solution
02-28-2020 05:29 AM
LouieDowney_III
Emerging Contributor

I'm trying to automate the process of taking data from our local gis servers and push a copy into ArcGIS Online to use the OpenData tools/website.

I've uploaded a file geodatabase containing two layers and created hosted feature layer from them, which was fairly simple.

Where I'm struggling is updating the data.  When I go to the ArcGIS online website and go to hosted feature layer and select "Update Data" and give it the new file geodatabase with the updated data, it works like a charm.

From what I've found, this seems like it should work, and it returns successfully, but the data is never updated when I go and look at it.

# overwrite the initial data
updateData = '<Path2MyData>'
fgdb = gis.content.get('<IDofMyHostedLayer')
if fgdb.update({}, updateData):
    print("Success?")
else:
    print("No success?")

What am I missing here?  I just want to overwrite the existing data with the new files.

Thanks,

1 Solution

Accepted Solutions
WilliamCraft
MVP Regular Contributor

I think you're missing the 'publish' step.  Here is some code that should work for completely overwriting the data any time you need.  The example is for a CSV but the same concept applies for file geodatabases.  Just replace the item ID, the URL, username/password, and the data source reference.  

import os, sys, arcpy
from arcgis.gis import GIS

source = GIS("http://yourorganization.maps.arcgis.com", "username", "password", verify_cert=False)
csv = source.content.get('a4e4587122354eb2847d461263e3ccb7')
csv.update({}, r'C:\TEMP\data.csv')
csv.publish(overwrite=True)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

3 Replies
WilliamCraft
MVP Regular Contributor

I think you're missing the 'publish' step.  Here is some code that should work for completely overwriting the data any time you need.  The example is for a CSV but the same concept applies for file geodatabases.  Just replace the item ID, the URL, username/password, and the data source reference.  

import os, sys, arcpy
from arcgis.gis import GIS

source = GIS("http://yourorganization.maps.arcgis.com", "username", "password", verify_cert=False)
csv = source.content.get('a4e4587122354eb2847d461263e3ccb7')
csv.update({}, r'C:\TEMP\data.csv')
csv.publish(overwrite=True)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
FyneAde-Amadi
Emerging Contributor

Hi, will this code work if the geodatabase is compressed or there's another layer to add? Thanks.

0 Kudos
LouieDowney_III
Emerging Contributor

Thanks, that's what I was missing.

I had to change my ItemID to be the file geodatabase I'd uploaded instead of the hosted feature layer, and add the filetype to the publish properties, but seems to have it.

fgdb.publish(overwrite=True,file_type='fileGeodatabase')

On an unrelated note, I've been struggling to use the Python API documentation.  I'm used to the format from the JavaScript API, where each class has it's own page, and all the properties are listed in a nice clean table at the top that then links to the details of that function.

The python api documentation is one massive page and all the properties of a class, while listed, have all the details of the other properties before you get to them.  So if you go on there and look at the Item class, you just have to keep scrolling until you find publish, and if you just search "Publish", you get over 60 results to try and weed through.  All the info is there, just not nearly as easy to sift through.

And I can't seem to find anywhere in there that updating the source data requires you to republish the feature layer for the changes to show up, so I don' t know how I'm supposed to know that.