I’m trying to build a python script that will update attributes on feature layers on AGOL, end goal would be to have scheduled tasks to update the attributes nightly (without effecting geometry). My attribute update file is a csv with approximately 9000 row and about 6 columns. Every time I run the full csv file I run into a timeout error. I have reduced the file to 3 columns and still occasionally get the timeout error. I’m newer to python so I wouldn’t be surprised if this is a horribly inefficient way to complete this task.
import pandas as pd
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
try:
# Connect to ArcGIS Online
gis = GIS('', 'UN', 'PW', expiration=9999)
logging.info("Successfully connected to ArcGIS Online.")
# Access the feature layer, use the polygon layer service url not feature layer
layer_url = "layer url"
feature_layer = FeatureLayer(layer_url)
logging.info("Successfully accessed the feature layer.")
# Read the CSV file
df = pd.read_csv('updates.csv')
updates_df = df.fillna(value=0)
logging.info("Successfully read the CSV/xlsx file.")
# Define the identifier field used in your CSV and feature layer
csv_id_field = 'apprid' # Identifier in CSV file
layer_id_field = 'apprid' # Corresponding field in feature layer
# Query the feature layer to get all features
query_result = feature_layer.query(where="1=1")#, out_fields="*")
#logging.info(f"Full query result: {query_result}")
logging.info(f"Query successful: {query_result}")
# Check if 'features' key is present in the query result
if not query_result.features:
logging.error("Query result does not contain features. Check the layer URL and query parameters.")
raise KeyError("Query result does not contain features")
features = query_result.features
logging.info(f"Successfully queried the feature layer. Number of features retrieved: {len(features)}")
# Prepare updates based on the CSV file
updates = []
for feature in features:
feature_id = feature.attributes[layer_id_field]
update_row = updates_df[updates_df[csv_id_field] == feature_id]
if not update_row.empty:
updated_attributes = {"apprid": feature.attributes["apprid"]}
for column in updates_df.columns:
if column != csv_id_field:
updated_attributes[column] = update_row[column].values[0]
updates.append({"attributes": updated_attributes})
else:
logging.warning(f"No update found for feature_id: {feature_id}")
# Apply the attribute updates
if updates:
update_result = feature_layer.edit_features(updates=updates)
logging.info(f"Updates Complete")
else:
logging.info("No updates to apply.")
except Exception as e:
logging.error(f"An error occurred: {e}")
Saw something on another chain to add expiration = 9999 to my GIS connection. This didn’t seem to make a difference.
gis = GIS('', 'UN', 'PW', expiration=9999)
eflam93 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.