I need assistance with my script. Can anyone point me in the right direction?
I'm attempting to overwrite a feature layer with data from a CSV file, both of which are hosted on my portal on ArcGIS Online.
My code is displayed below.
When I run the code to perform the overwriting process, I get the following error message:
Exception: Error while analyzing File Geodatabase 'Hospitals_0.csv' Unable to extract 'file geodatabase' Invalid File Geodatabase, missing gdbtable or gdbindexes file.
(Error Code: 406)
# import libraries
from arcgis.gis import GIS
from arcgis import features
import pandas as pd
import os
import datetime as dt
import shutil
from arcgis.features import FeatureLayerCollection
gis = GIS("home")
""""Align Current Working Directory for Notebook and Scheduled Tasks
By default, Notebook is '/arcgis' and Scheduler is '/home/arcgis/data/overwrite"""
os.chdir( "/arcgis") # match scheduler to notebook
data_path = "/arcgis/home/data/overwrite"
if not os.path.exists( data_path):
# create work folder
os.mkdir( data_path)
# confirm path exists
if os.path.isdir(data_path):
print("Directory already exists")
else:
print("Creating directory for you")
# Item Added From Toolbar
# Title: Python Testing for Hospitals | Type: Feature Service | Owner: bayparkds
primary_overwrite = gis.content.get("778f98bd191a4bc6921885fae2b5109e")
primary_overwrite
# assign path variables for data
# MCH_Hospitals.csv contains the initial incomplete dataset which is published as a feature layer
csv_file = 'Hospitals_0.csv'
# assign variable to current timestamp to make unique file to add to portal
now = int(dt.datetime.now().timestamp())
# read the csv data directly
my_csv = os.path.join(data_path, csv_file)
# read the initial csv
hospitals_df_1 = pd.read_csv(my_csv)
hospitals_df_1.head()
# add the csv into content
item_prop = {'title':'Python Testing for Hospitals' + str(now)}
csv_item = gis.content.add(item_properties=item_prop, data=my_csv)
csv_item
# update the item csv name
item_prop = {'title':'Python Testing for Hospitals'}
csv_item.update(item_properties = item_prop,
thumbnail=os.path.join(data_path, csv_file))
csv_item
from arcgis.features import FeatureLayerCollection
primaryoverwrite_flayer_collection = FeatureLayerCollection.fromitem(primary_overwrite)
# call the overwrite() method which can be accessed using the manager property
primaryoverwrite_flayer_collection.manager.overwrite(os.path.join(data_path, 'Hospitals_0.csv'))
Looks like the File Item used to publish the Service was originally a File GeoDatabase, not a CSV file. In the Overwrite process, the data used for the overwrite is uploaded to the Service's associated File Item. Then, the file is analyzed for compatibility. Your first message reports that it is trying to compare the upload to a File GeoDatabase. You can only Overwrite using the original File item type used to publish the Service.
You can leverage a CSV file with the Append function, to update or insert (upsert) matching/new rows. Or, leverage the Truncate REST call on the Layers to clean out all rows and then use Append to add or insert new rows.
Hope this helps...