Hello community (cc @David_McRitchie),
I've encountered an issue whereby when deleting unused fields from a hosted feature layer, some of the associated views are becoming corrupted. After some discussions with ESRI support, it was suggested that one way to improve processes to allow us to identify and fix any broken views, is after any field deletions, to run a notebook that does the following:
I have trialled the following code, however it returns no results ("No empty hosted view layers found. Results saved to empty_view_layers.txt"). I know however that there are multiple view layers with less than 10 rows of data but none are being picked up. I'm not quite sure what is going wrong here so any suggestions would be gratefully received.
from arcgis.gis import GIS # import necessary libraries
gis = GIS("home") # connect to your GIS
# Function to find empty hosted view layers
def find_empty_view_layers():
empty_view_layers = []
# Search for hosted feature services
for item in gis.content.search(query="type:Feature Service", max_items=10000):
if "View Layer" in item.typeKeywords: # Check if the item is a view layer
# Check if the view layer is empty
feature_count = item.layers[0].query(return_count_only=True) if item.layers else 0
if feature_count <= 10:
empty_view_layers.append(item.title)
return empty_view_layers
# Retrieve and display the empty view layers
empty_layers = find_empty_view_layers()
if empty_layers:
print("Empty Hosted View Layers:")
for layer in empty_layers:
print(f"- {layer}")
else:
print("No empty hosted view layers found.")
# Optionally, save the results to a text file
with open("empty_view_layers.txt", "w") as file:
for layer in empty_layers:
file.write(layer + "\n")
print("Results saved to empty_view_layers.txt")
Alternatively, a method for outputting all view layers and their associated feature count would also do the job. I have tried this via the following approach:
from arcgis.gis import GIS # import libraries
gis = GIS("home") # connect to your GIS
items = gis.content.search(query='View Service', max_items=10000) # get view layers
for layer in items: # check the output by printing the view layer titles
print(layer.title)
# Loop through the layers and get the feature count
for layer_item in items:
print(f"Layer: {layer_item.title}")
for lyr in layer_item.layers:
feature_count = lyr.query(where="1=1", return_count_only=True)
print(f" Feature Layer: {lyr.properties.name}, Feature Count: {feature_count}")
This 2nd approach works to begin with, but then returns the error below. This appears to be caused by it trying to work against a geocoding view. Therefore, is there a way to limit the layer list to only 'hosted, view' type items, or at least exclude a geocoding view?
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/opt/conda/lib/python3.11/site-packages/arcgis/gis/__init__.py in ?(self, k)
14045 if not self._hydrated and not k.startswith("_"):
14046 self._hydrate()
> 14047 return dict.__getitem__(self, k)
KeyError: 'layers'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
/tmp/ipykernel_142/3627018840.py in ?()
1 # Loop through the layers and get the feature count
2 for layer_item in items:
3 print(f"Layer: {layer_item.title}")
----> 4 for lyr in layer_item.layers:
5 feature_count = lyr.query(where="1=1", return_count_only=True)
6 print(f" Feature Layer: {lyr.properties.name}, Feature Count: {feature_count}")
/opt/conda/lib/python3.11/site-packages/arcgis/gis/__init__.py in ?(self, name)
13999 def __getattribute__(self, name):
14000 if name == "layers":
> 14001 if self["layers"] is None or self["layers"] == []:
14002 try:
14003 with _common_utils._DisableLogger():
14004 self._populate_layers()
/opt/conda/lib/python3.11/site-packages/arcgis/gis/__init__.py in ?(self, k)
14043 return dict.__getitem__(self, k)
14044 except KeyError:
14045 if not self._hydrated and not k.startswith("_"):
14046 self._hydrate()
> 14047 return dict.__getitem__(self, k)
KeyError: 'layers'
Thanks in advance for any assistance on this.