How do you compare a list of records in a table with a list of feature classes, populating the table with the feature class records counts?

09-26-2019 08:32 AM
Regular Contributor


So I have a relatively simple script that is basically a complete list of feature classes in a database and I was wondering how you would compare that list of feature classes to the list of records in a table.

The other thing is, if there is a match between the list of feature classes and the table, to have the table populate with the record count of the matching feature class. I have a rough script that has both the list of the feature classes in a database and the list from the table, as well as the counts for each feature class. The other issue is that the names aren't the exact same but are similar.

So I am trying to first figure out how to compare the list of feature classes to the list of records in the table. I think once I can get that then the rest should be relatively simple to do.

import arcpy

#set parameters
inputTable = r'G:\MyThings\Test\NewTest3\StormLoad.gdb\Stormload_Table'#arcpy.GetParameterAsText(0)
select_databases = r'G:\MyThings\Test\NewTest4\StormLoad.gdb'#arcpy.GetParamterAsText(1)
#select_fields_toUpdate = arcpy.GetParameterAsText(2)
input_value = '2'#arcpy.GetParameterAsText(3)

arcpy.env.workspace = select_databases

#Get list of field names
field_names = [ for f in arcpy.ListFields(inputTable)]

#Identify feature class and database field name
fcdb_name_field = field_names[1]

#Select the field names to populate
select_field_names = field_names[2:]
datasetList = arcpy.ListDatasets('*','Feature')

#List feature classes
def fcsInDS():
    for dataset in datasetList:
        arcpy.env.workspace = dataset
        fcList = arcpy.ListFeatureClasses()
    return fcList

fcsInDB = arcpy.ListFeatureClasses()

fcs = fcsInDS() + fcsInDB
for fc in fcs:
    fc_ReCount = arcpy.GetCount_management(fc)
    print '{0} has a record count of {1}'.format(fc, fc_ReCount) 

    with arcpy.da.UpdateCursor(inputTable, field_names) as cursor:
        for row in cursor:
            if row[1] in fc:
                row[2] = input_value #

I also noticed that when it loops through the list of combined lists, that it errors out with the feature classes in the database as either non existent or supported. I tried making a couple of adjustments to this script but it returns errors.

I would greatly appreciate any assistance with this.


0 Kudos
3 Replies
MVP Esteemed Contributor

The other issue is that the names aren't the exact same but are similar.

This issue will be the hardest one to overcome, as you have already guessed.  Can you elaborate on how they are similar or different?  Some examples would also be helpful.

0 Kudos
Regular Contributor

They have similar characters, but the names in the table are spelled out for the most part where the feature class names are conjunctions. Here is the list of the feature class names.


0 Kudos
Regular Contributor

I could probably change the names of the feature classes in the table. I thought about working on a script that would pull out only the names of the needed feature classes. The issue I ran into is how to set up the output parameters so that the user could simple select from the list rather than having it scripted. I don't quite know how to set that up, but I am familiar with the set parameter and/or the set parameter as text options. I may look into that further after I manage to get this script up and running.

0 Kudos