1

我正在尝试按照此处列出的步骤从本地要素类更新 AGOL 上的要素。我不断在 for 循环中获得循环引用,但我不确定它为什么会发生。

请参阅下面我使用的代码。

import arcgis, arcpy, csv, os, time, copy, pandas as pd
from arcgis.gis import GIS
from pandas import DataFrame
from copy import deepcopy
gis = GIS("url", "username","pass")
fc = gis.content.get('ItemID')
flayer = fc.layers[0]
fset=flayer.query()
fields = ('GPS_Time','Visibility','EngineeringSection','Condition')
UpdateLayer  = "C:\\Users\\USer\\Documents\\ArcGIS\\Default.gdb\\Data"
UpdateTable=DataFrame(arcpy.da.FeatureClassToNumPyArray(UpdateLayer , fields, skip_nulls=True))
overlap_rows = pd.merge(left=fset.sdf, right = UpdateTable, how='inner', on='EngineeringSection')
features_for_update = []
all_features = fset.features    
for EngSec in overlap_rows['EngineeringSection']:
    original_feature = [f for f in all_features if     f.attributes['EngineeringSection'] == EngSec][0]
    feature_to_be_updated = deepcopy(original_feature)
    matching_row = UpdateTable.where(UpdateTable['EngineeringSection'] == EngSec).dropna()
    original_feature.attributes['GPS_Time'] = (matching_row['GPS_Time'])
    original_feature.attributes['Visibility'] = int(matching_row['Visibility'])
    original_feature.attributes['Condition'] = str(matching_row['Condition'])
    update_result = flayer.edit_features(updates=[original_feature])
    flayer.edit_features(updates= features_for_update)

这是我收到的错误:

Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\layer.py", line 1249, in edit_features
default=_date_handler)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
ValueError: Circular reference detected
4

2 回答 2

2

下面的行将元组分配为属性值。是你想要的吗?

original_feature.attributes['GPS_Time'] = (matching_row['GPS_Time'])

如果要分配值,只需执行以下操作:

original_feature.attributes['GPS_Time'] = matching_row['GPS_Time']

另外,我认为这一行:

flayer.edit_features(updates= features_for_update)

应该:

flayer.edit_features(updates=[feature_to_be_updated])
于 2019-02-28T19:12:27.527 回答
1

感谢您的帮助,我能够使用这个脚本让它全部运行:我还添加了一些时间来查看它需要多长时间

import arcpy, csv, os, time
import pandas as pd
from arcgis.gis import GIS
from pandas import DataFrame
from copy import deepcopy
start_time = time.time()
gis = GIS("url", "user","pass")
fc = gis.content.get('ContentID')
flayer = fc.layers[0]
fset=flayer.query()
fields = ('GPS_Time','Visibility','EngineeringSection','Condition')
UpdateLayer  = "C:\\Users\\user\\Documents\\ArcGIS\\Default.gdb\\data"
UpdateTable=DataFrame(arcpy.da.FeatureClassToNumPyArray(UpdateLayer , fields, skip_nulls=True))
overlap_rows = pd.merge(left=fset.sdf, right = UpdateTable, how='inner', on='EngineeringSection')
features_for_update = []
all_features = fset.features
for EngSec in overlap_rows['EngineeringSection']:
    original_feature = [f for f in all_features if f.attributes['EngineeringSection'] == EngSec][0]
    feature_to_be_updated = deepcopy(original_feature)
    matching_row = UpdateTable.where(UpdateTable['EngineeringSection'] == EngSec).dropna()
    feature_to_be_updated.attributes['GPS_Time'] = matching_row['GPS_Time'].iloc[0]
    feature_to_be_updated.attributes['Visibility'] = int(matching_row['Visibility'])
    feature_to_be_updated.attributes['Condition'] = str(matching_row['Condition'].iloc[0])
    update_result = flayer.edit_features(updates=[feature_to_be_updated])
    update_result
elapsed_time = time.time() - start_time
totaltime = time.strftime("%H:%M:%S", time.gmtime(elapsed_time))
print("Total processing time: "+ totaltime)
于 2019-03-01T21:20:15.510 回答