I have a script that runs daily and updates a csv with new records and then overwrites two identical feature layers, but one is on my company's AGOL site and the other is on the ESRI Portal site. This process has run for a while and has almost always run fine with the occasional time where the feature layer would corrupt on the portal and need to be replaced.
Ever since my company upgraded to Enterprise/Portal 10.9.1 the Portal feature layer seems to corrupt basically every time the feature layer is overwritten the csv. I will show a reference to the code I run below that performs this overwrite:
gisUser = "user"
gisPass = "pass"
gis = GIS("https://gis.COMPANY.com/portal/", gisUser, gisPass)
data_path = "C:\\PATH TO CSV"
csv_file = "CSVNAME.csv"
fullCSV = os.path.join(data_path, csv_file)
item = gis.content.get("STRING FOR ITEM ID")
the_flc = FeatureLayerCollectionManager.fromitem(item)
the_flc.overwrite(fullCSV)
print(item.share(org=True))
Is there any reason that I am running into this corruption problem more frequently now after this most recent upgrade to 10.9.1?
SOLUTION:
Here is the correct code to append new features to the portal if you don't want to scroll
# Sign in to ArcGIS with the credentials given and the portal url
gisUser = "USER"
gisPass = "PASS"
target = GIS("PORTAL URL", gisUser, gisPass)
# making data frame from csv file
data = pd.read_csv(CSV PATH)
# change the date columns in the DataFrame which are currently in String format to datetime64
data["DateColumn"] = pd.to_datetime(data["DateColumn"])
data["DateColumn2"] = pd.to_datetime(data["DateColumn2"])
# pull the feature layer to append to
lyr = target.content.get("ITEM ID").layers[0]
# GeoAccessor class adds a spatial namespace that performs spatial operations on the given Pandas DataFrame
# "Longitude" and "Latitude" are the exact names of my spatial columns in my csv
sdf = GeoAccessor.from_xy(data, "Longitude", "Latitude")
# convert column names from csv to match lower case format on the ESRI portal
cols = {
"Column1": "column1",
"Column2": "column2",
"Column3": "column3",
}
# rename the column in the DataFrame, this will not change the base csv
sdf.rename(columns=cols, inplace=True)
sdf.columns.to_list()
# truncate all records from the feature layer
lyr.manager.truncate()
# apply new records to layer in 200-feature chunks
i = 0
while i < len(sdf):
fs = sdf.loc[i : i + 199].spatial.to_featureset()
updt = lyr.edit_features(adds=fs)
msg = updt["addResults"][0]
# print(f"Rows {i:4} - {i+199:4} : {msg['success']}")
if "error" in msg:
print(f"Rows {i:4} - {i+199:4} : {msg['success']}")
print(msg["error"]["description"])
i += 200