All,
I'm attempting this loop but on all the unique values within a a field in a feature service, and it works for the first 4 times on the list but on the 4th or 3rd iteration it appears to lose the feature service that its pointed towards. Any clue whats happening here?
source code
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
import pandas
import time
## access agol
agol = GIS("home")
## access the feature service item that contains the layers
fs_item = agol.content.get("ITEMID")
url = " URL "
## create a FeatureLayerCollection object from the feature service item
flc = FeatureLayerCollection.fromitem(fs_item)
queryfields = ["Program"]
grants= flc.layers[0].query()
grantdf = grants.sdf
programs = grantdf['PROGRAM'].unique().tolist()
programs.sort()
print(f"{len(programs)} grant programs: {programs}")
for prog in programs:
starttime = time.perf_counter()
where_clause = "PROGRAM IN ({})".format(','.join(["'{}'".format(prog)]))
fs_item = agol.content.get("ITEMID")
flc = FeatureLayerCollection.fromitem(fs_item)
print (flc)
with arcpy.da.SearchCursor(url, queryfields, where_clause) as cursor:
rows = [row for row in cursor]
print(f" Total Grant in {prog}: {len(rows)}")
new_view = flc.manager.create_view(
name = prog+"_Program",
#select the layer based on its id
view_layers = [flc.layers[0]],
description = f"This layer depicts all awarded grants for the "+prog+f" program.",
tags = prog+", TAG",
snippet = f"PLACEHOLDER",
query = where_clause+" AND (PAYTAMT > 0)"
)
time.sleep(5)
endtime =time.perf_counter()
print(f"Time to process:{(endtime-starttime)}")Error Message
IndexError Traceback (most recent call last)
Cell In[16], line 11
9 rows = [row for row in cursor]
10 print(f" Total Grant in {prog}: {len(rows)}")
---> 11 new_view = flc.manager.create_view(
12 name = prog+"_=Program",
13 #select the layer based on its id
14 view_layers = [flc.layers[0]],
18 query = where_clause+" AND (PAYTAMT > 0)"
19 )
20 time.sleep(5)
21 endtime =time.perf_counter()
File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\features\managers.py:3041, in FeatureLayerCollectionManager.create_view(self, name, spatial_reference, extent, allow_schema_changes, updateable, capabilities, view_layers, view_tables, description, tags, snippet, overwrite, set_item_id, preserve_layer_ids, visible_fields, query, folder)
3038 update_item_data(view_item, item, view_layers)
3040 item = gis.content.get(res["itemId"])
-> 3041 set_visible_fields_and_query(item, visible_fields, query, gis)
3043 return item
File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\arcgis\features\managers.py:3009, in FeatureLayerCollectionManager.create_view.<locals>.set_visible_fields_and_query(item, visible_fields, query, gis)
3007 if visible_fields or query:
3008 values = {}
-> 3009 fields = item.layers[0].properties["fields"]
3010 if visible_fields:
3011 field_names = [f.lower() for f in visible_fields]
IndexError: list index out of range