尝试通过ArcGIS Online更新GIS要素时出现循环参考错误

时间:2019-02-28 18:59:15

标签: python pandas arcgis arcpy

我正在尝试按照here列出的步骤从本地要素类更新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

2 个答案:

答案 0 :(得分:1)

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

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])

答案 1 :(得分:0)

感谢您的帮助,我能够使用此脚本将其全部运行: 我还添加了一些时间来查看花费了多长时间

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)