我正在尝试构建一个 python 脚本,该脚本将更新 AGOL 上要素图层的属性,最终目标是安排任务以每晚更新属性(不影响几何图形)。我的属性更新文件是一个 csv,大约有 9000 行和大约 6 列。每次运行完整的 csv 文件时,我都会遇到超时错误。我已将文件减少到 3 列,但仍然偶尔会出现超时错误。我对 python 比较陌生,所以如果这是完成此任务的一种非常低效的方法,我不会感到惊讶。
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}")
在另一条链上看到一些内容,将过期 = 9999 添加到我的 GIS 连接中。这似乎没有什么区别。
gis = GIS('', 'UN', 'PW', expiration=9999)
几个问题和建议:
是否需要循环遍历所有行及其每一列? 9000 个特征需要处理很多。考虑将批次分解为 100 行,而不是按列分解。
是否可以只更新已更新的行?这可能需要一些连接并找出两个数据集之间的差异。也许这会给你一个想法: https://community.esri.com/t5/arcgis-api-for-python-questions/python-api-how-to-update-feature-layer-attributes/td-p/867767
是否可以使用更新的 CSV 覆盖要素图层?有点暴力的方法,但考虑到您的数据集大小,这可能是最简单的方法: https://developers.arcgis.com/python/samples/overwriting-feature-layers/#overwrite-the-feature-layer