Hi community! Wondering if y'all have any ideas to help speed this up.
(ArcGIS Pro toolbox with my arcpy script embedded, accessing data on a faraway server)
I have a data model (GDB) with 15+ feature datasets and 240+ feature classes. I'm doing is iterating through each feature class in each feature dataset, check if there is data by counting the rows, and if that count is greater than 0 adding that feature class to the map.
It adds non-empty feature classes to the map but takes 20+ minutes for large datasets (where feature classes contain more than 1000+ rows) and 15 minutes for small datasets (<50 rows) when data is run from the server. Less than 5 minutes when the GDB is copied to C: drive, but that's still quite a while.
I've tried using a cursor instead, and I've been unsuccessful looking for methods other than GetCount_management(feature_class).getOutput to figure out if a feature class is empty or not-- I am guessing this slows me down since the iteration itself isn't the slowest part. Most of the slow is when counting the rows and adding them to the map. I've also been looking into multiprocessing but can't seem to wrap my head around that yet.
If this turns out to be a 'your data is too far away / connectivity' issue, which I'm afraid it is, at least I'll have a second opinion on that.
import arcpy
gdb_path = arcpy.GetParameterAsText(0)
arcpy.AddMessage("Connection to GDB successful.")
# Set the path to your ArcGIS Pro project
aprx = arcpy.mp.ArcGISProject("CURRENT")
arcpy.AddMessage("Current project selected.")
# Get the first map in project
map = aprx.listMaps()[0]
arcpy.AddMessage("Getting first map in project.")
arcpy.env.workspace = gdb_path
arcpy.AddMessage("Made GDB your current workspace.")
feature_datasets = arcpy.ListDatasets(feature_type='Feature')
# Iterate through each feature dataset in the geodatabase
for dataset in feature_datasets:
arcpy.env.workspace = f"{gdb_path}/{dataset}"
feature_classes = arcpy.ListFeatureClasses()
arcpy.AddMessage(f"Checked {dataset}")
# Iterate through each feature class in the feature dataset
for feature_class in feature_classes:
# Check if the feature class has any rows
if int(arcpy.GetCount_management(feature_class).getOutput(0)) > 0:
# Add the feature class to the map
data_file = f"{gdb_path}/{dataset}/{feature_class}"
map.addDataFromPath(data_file)
arcpy.AddMessage(f"Added {feature_class}.")