need help overwriting hosted feature layer

1774
2
11-25-2020 07:22 AM
JillRKelly
New Contributor II
According to the example below, you can use FeatureCollection. manager.overwrite to wholesale overwrite a feature layer you have hosted on AGOL -- or perhaps this is only an option for the Enterprisers?  But, the example is built in terms of csv's stored locally on one's own hard drive.  I have a spatial dataframe that I am pushing directly to AGOL without making any local shapefile or anything (sdf.spatial.to_featurelayer).
 
As I am regularly updating the sdf, I'd like to overwrite the hosted feature layer directly and often.  Can that be done?  The docs say, "Only hosted feature layer collection services can be overwritten" and I get about as far as "hosted feature layer . . . " before I don't know what that phrase means. 

Overwrite the feature layer

Let us overwrite the feature layer using the new csv file we just created. To overwrite, we will use the overwrite() method.

In [21]:
from arcgis.features import FeatureLayerCollection
cities_flayer_collection = FeatureLayerCollection.fromitem(cities_item)
In [22]:
#call the overwrite() method which can be accessed using the manager property
cities_flayer_collection.manager.overwrite(os.path.join('data', 'updating_gis_content',
                               'updated_capitals_csv', 'capitals_1.csv'))
Tags (1)
2 Replies
Kara_Shindle
Occasional Contributor III

I export dataframe directly to a CSV that overwrites the file on AGOL every hour.  I couldn't get the dataframe to work and haven't had the time to suss out why but here is my code:

 

 

 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
gis = GIS('<AGOL ORG URL>', usernameAGOL, passy)
# #### 3.1.2 Update Parcel Action List

searchResult = gis.content.search(query="title:Parcel Action List")

pal = searchResult[1]

palTable = gis.content.get('<Insert item number here>')  #This gets the hosted table in AGOL
pal_collection = FeatureLayerCollection.fromitem(palTable)

pal_collection.manager.overwrite(newCSV)

 

 

 

where newCSV is the CSV I exported from my pandas frame and is stored in a folder every hour.  Every hour, it finds the CSV, renames it to "CSV_OLD" in case something goes wrong, and then recreates a new CSV from the data with the same name. 

 

Edited to include gis variable in python

I'm not versed enough to be able to get the token stuff working yet - so I explicitly state the username & password in this script, although you should probably at least create a secrets file if you know how

LoganLaudano
New Contributor

Thank you

0 Kudos