I have a hosted feature class and a hosted table in the Organization's Contents section initially created from two Google Sheets (through shared CSV URLs), that I need regularly updated whenever new data is added/updated in the original two Google Sheets. For this purpose, I've put together a basic python script that sits on a local machine, with the plan to run it through a Windows Task Scheduler (i.e., user need only update the data in the Google Sheets for changes to reflect across to AGOL). However, the script does not appear to be completing the data overwrite. What am I missing?
# Import libraries
from arcgis.gis import GIS
import urllib.request, csv
from arcgis import features
import pandas as pd
import os
# Define
AGOL_username = "username"
AGOL_password = "password"
Layer_ID = 'long alphanumeric ID'
Table_ID = 'long alphanumeric ID'
Google_layer_csv = "https://docs.google.com/spreadsheets/...1&single=true&output=csv" #used to populate the location feature class
Google_table_csv = "https://docs.google.com/spreadsheets/...2&single=true&output=csv" #used to populate the related table
# Connect to the GIS
gis = GIS("https://www.arcgis.com", AGOL_username, AGOL_password)
print("Logged in as " + str(gis.properties.user.username))
# ## 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.
#item id of the hosted feature layer in AGOL Organization
Engines_featureLayer_item = gis.content.get(Layer_ID)
#item id of the hosted table in AGOL Organization
Engines_table_item = gis.content.get(Table_ID)
#access the manager helper object to overwrite our currently existing hosted feature layer or table
from arcgis.features import FeatureLayerCollection
Engines_flayer_collection = FeatureLayerCollection.fromitem(Engines_featureLayer_item)
Engines_table_collection = FeatureLayerCollection.fromitem(Engines_table_item)
#call the overwrite() method which can be accessed using the manager property
Engines_flayer_collection.manager.overwrite(Google_layer_csv)
Engines_table_collection.manager.overwrite(Google_table_csv)
The overwrite function, in my experience, is fickle. Store the overwrite job in a variable and then print this statement for debugging. It should provide a status as a json dictionary.
Instead of overwrite, can you access the individual feature layer and do a truncate, convert the csv to a data frame, then add data frame to the feature layer?
import pandas as pd
#Wipe out all features in layer
yourfeaturelayer.manager.truncate()
#import your csv data into the data frame
df = pd.read_csv("your_csv_file.csv")
feature_set = FeatureSet.from_dataframe(df)
#Add in features from data frame into feature layer
result = yourfeaturelayer.edit_features(adds=feature_set)