I am trying to generate a script that will scan a fgdb with feature classes all within feature datasets, and then it will output to a csv with two columns: state and count, and these will represent which state (US states) are in how many FCs.
The tool will parse the FGDB and FDS, then scan the FCs in the first FDS, but then not manage to search each subsequent FDS after the first one. It also does not write anything into the csv except the headers 'state' and 'count.'
pardon the abundance of messages and try/except stuff, I'm not very proficient (just kidding, I started from chatGPT).
import arcpy
import csv
# Set the workspace to the folder containing the feature classes
workspace = arcpy.GetParameterAsText(0)
arcpy.env.workspace = workspace
arcpy.AddMessage("workspace: " + str(workspace))
#feature_classes = arcpy.ListFeatureClasses()
feature_datasets = arcpy.ListDatasets("", "Feature")
arcpy.AddMessage(str(feature_datasets)) #outputs feature datasets properly
state_counts = {}
for fds in feature_datasets:
arcpy.env.workspace = fds
arcpy.AddMessage(str(fds)) #properly scans through each fds
# Print the current feature dataset to verify if it's correctly set
arcpy.AddMessage(f"Processing feature dataset: {fds}")
try:
feature_classes = arcpy.ListFeatureClasses()
# Print the number of feature classes found in the current feature dataset
arcpy.AddMessage(f"Number of feature classes: {len(feature_classes)}")
arcpy.AddMessage(str(feature_classes))
if feature_classes:
try:
for fc in feature_classes:
arcpy.AddMessage(str(fc))
desc = arcpy.Describe(fc)
state_field_name = "stateName" # Update with the actual field name for state attribute
if state_field_name in desc.fields:
#with arcpy.da.SearchCursor(fc, "stateName") as cursor:
#with arcpy.da.SearchCursor(fc, [state_field_name]) as cursor:
with arcpy.da.SearchCursor(fc, '*') as cursor:
field_names = cursor.fields
state_field_index = field_names.index(state_field_name)
for row in cursor:
state = row[state_field_index]
arcpy.AddMessage(str(state))
arcpy.AddMessage(state)
if state and state != "":
if state in state_counts:
state_counts[state] += 1
else:
state_counts[state] = 1
else:
arcpy.AddMessage(f"Empty or None value encountered in {state_field_name} field.")
# Delete the cursor and row objects
del cursor
del row
except Exception as e:
arcpy.AddMessage(f"An error occurred: {str(e)}")
else:
arcpy.AddMessage("no if feature_classes")
except Exception as e:
arcpy.AddMessage(f"An error occurred while retrieving feature classes: {str(e)}")
# fails here after the first feature dataset is scanned
# Define the output CSV file path
output_csv = r"C:\path\output.csv"
# Write the state counts to the CSV file
with open(output_csv, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['State', 'Count'])
for state, count in state_counts.items():
writer.writerow([state, count])
#arcpy.AddMessage(f"State counts saved to {output_csv}")
if len(state_counts) > 0:
arcpy.AddMessage(f"State counts saved to {output_csv}")
else:
arcpy.AddMessage("No valid state values found in the feature classes.")
cleaned up text from View Details window from running as a geoprocessing tool:
workspace: C:\path\data.gdb
[list of feature datasets]
FDS1
Processing feature dataset: FDS1
Number of feature classes: 8
[list of feature classes in FDS]
feature_classes1
2
3...
8
FDS2
Processing feature dataset: FDS2
An error occurred while retrieving feature classes: object of type 'NoneType' has no len()
FDS3
Processing feature dataset: FDS3
An error occurred while retrieving feature classes: object of type 'NoneType' has no len()
CONITNUES HERE OVER EACH FDS...
DOES NOT PRINT ANY MESSAGES ABOUT THE CSV