1

I may just not know the terminology needed to find the proper google results but I am trying to count the number of feature classes and store that for reference later in the script so that if the number of feature classes change then the workflow checks the projection of the newly added feature class.

This is my script with the len function:

# Import arcpy and numpy
import os
import arcpy
import numpy

arcpy.env.workspace = r"C:\Users\Distances.gdb"
outWorkspace = r"C:\Users\Distances.gdb"

try:

    # Use ListFeatureClasses to generate a list of inputs
    for fc in arcpy.ListFeatureClasses():
        fcCount = len(fc)
        # Determine if the input has a defined coordinate system, can't project it if it does not
        dsc = arcpy.Describe(fc)

        if dsc.spatialReference.Name != 'NAD_1983_StatePlane_California_III_FIPS_0403_Feet':
            # Determine the new output feature class path and name
            outfc = os.path.join(outWorkspace, fc + "_NAD1983")

            # Set output coordinate system
            outCS = arcpy.SpatialReference('NAD 1983 StatePlane California III FIPS 0403 (US Feet)')

            # run project tool
            arcpy.Project_management(fc, outfc, outCS)

            # check messages
            print(arcpy.GetMessages())
        else:

            print(fc)

except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

except Exception as ex:
    print(ex.args[0])

Storing and checking for feature class names may be a more fine grained detail worth considering but un-found in my google searching.

  • 2
    Given that lists, sets, and dictionaries are so fundamental to what makes Python pythonic, it's curious that your searching on lookups using Python didn't turn up the in operator. Lists could be used in two passes to find removed and added objects, sets could be used to find differences, and dictionaries could be used to detect row count change (or any other property). – Vince Feb 14 at 1:02
2

It is unclear what you want to use the Counts for. But try creating the list first, count, then iterate:

fclist = arcpy.ListFeatureClasses() 

l = len(fclist)
for fc in fclist:
   ...

When you do len(fc) you count each list element, not all of them

Or if you want to count each iteration, for example to use when naming outputs you can do:

fclist = arcpy.ListFeatureClasses()
count = 0
for fc in fclist: 
    count+=1
    ...
  • *Sorry I don't know how to add the code breakout in this 'comment' field. Let me see if I can wrap my brain around this. If I have 3 feature classes in a .gdb named {cat,dog,pig}, would this save cat, dog, and pig as {0,1,2}? What I would like to do is be able to check my .gdb and see if the file later on becomes {cat,dog,pig,fish} and check to see if 'fish' is the right projection. Would this then be something like: fcCheck="" for fc in fclist count+1 if fcCheck != 3 Check for projection – Therow Feb 13 at 21:09
  • I dont know exactly what you are trying to do. Im just saying that the second example could be used if you want to name the outputs by Count, for example cat_1, dog_2, pig_3 etc. – BERA Feb 14 at 6:55
  • What I would like to do is run the python script and determine if everything in the .gdb is projected -- then store the 'names' or the 'count of features' for the next time the script is run so that if something changes the script uses the projection section again to make sure all projections are the same. So, for instance, I add a new feature class to the .gdb and I run this script it would detect a change and then see if the new feature class needs projecting. – Therow Feb 14 at 17:07
0

You can use len() and Fiona's listlayers to do this. For example:

import fiona

count = len(fiona.listlayers('/path/to/your/geodatabase.gdb'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.