I am having trouble updating a point feature layer using the most recent API 2.4.1. I have read more posts and blogs and still have not had any luck making this work. What I am trying to do is update a point layer in AGOL using python. This layer is then consumed by other web maps and eventually a dashboard. I've tried the truncate/append process only to successfully truncate the data but have not found the magic line of code that will make the append work. The workflow involves using a CSV file that is updated daily and then pushing that up to AGOL.
Can anyone shed some light as to what I am doing wrong? I cannot find any detailed information on the new object model for the API that I'd usually use as a roadmap to solve this but the current documentation is quite lacking in examples/details.
Any insights/help would be greatly appreciated. Thanks in advance.
Here is my code:
import agol_utils
from arcgis.layers import Service
import arcpy
from arcpy import env
from arcpy.sa import *
import arcgis
from datetime import datetime as dt
import os, zipfile, glob
import sys
import json
from arcgis.features import FeatureLayer
def TruncateWebLayer(gis=None, target=None):
try:
lyr = arcgis.features.FeatureLayer(target, gis)
lyr.delete_features(where="1=1")
print ("Successfully truncated layer: " + str(target))
except:
print("Failed truncating: " + str(target))
sys.exit()
def main(location, folder_name, data_source, upload_data_location, raster_names, csv_file_name):
try:
#get the current date for file naming etc.
current_dt = str(int(dt.now().timestamp()))
#connect with AGOL
my_gis = agol_utils.connect_to_agol(location)
#make sure I'm logged in and know which version of API I am using
username = my_gis.users.me.username
print(username)
print(arcgis.__version__)
#get the folder the user wants to put the results in on AGOL
content_manager = arcgis.gis.ContentManager(my_gis)
folders_obj = my_gis.content.folders #
item_folder = folders_obj.get(folder=folder_name)
print(f"Your folder is: {item_folder}")
#AFFES anomalies results point layer item id
csv_item_id = "5481ace750b0474d804740e28d2d43a2"
csv_existing_item = my_gis.content.get(csv_item_id)
csv_feature_layer_url = "https://services1.arcgis.com/TJH5KDher0W13Kgo/arcgis/rest/services/AFFES_Anomaly_Results/FeatureServer/0"
csv_feature_layer = FeatureLayer(csv_feature_layer_url)
update_features = r"C:\TEMP\TESTING\INPUT\aware_testing.gdb\percentile_CoordinateTableToPoint"
localJSON = r"C:\TEMP\TESTING\INPUT\csvjson.json"
#remove features
#TruncateWebLayer(my_gis,csv_feature_layer)
csv_feature_layer.delete_features(where="1=1")
#reference the empty layer as FeatureLayer object from the ArcGIS Python API
fl = arcgis.features.FeatureLayer(csv_feature_layer, my_gis)
#create a JSON object from the local features
jSON = arcpy.FeaturesToJSON_conversion(update_features, localJSON)
#create a FeatureSet object from the JSON
fs = arcgis.features.FeatureSet.from_json(open(localJSON).read())
#add/append the local features to the hosted feature layer.
fl.edit_features(adds = fs)
print("Feature layer updated")
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
csv_update_file_name = r'percentile.csv'
input_file_path = os.path.join(r"C:\TEMP\TESTING\INPUT",csv_update_file_name)
print(input_file_path)
main("home","Situational_Awareness",r"c:\temp\input",r"c:\temp\output","bui_interpolation, dc_interpolation, dmc_interpolation", input_file_path)