0

I have a goal: take a feature class, select some of the records by attribute, and make a layer for further processing. Should be simple, but I am having multiple problems. Note: I am working in a Python 2.65/ArcGIS 10 environment (may be cause of some of these issues). I start with my gdb

 master_dict = {'Alabama': ['01009', '01013'], ...}
 statePath = "<...>/gdb/master_merge_footprints.gdb"

I want to iterate through the gdb and find specific features based on state names. This part works. I can even print the fields just to test that the feature class exists:

 stateNames = ['Alabama', 'Alaska'...]
 for name in stateNames:
     arcpy.env.workspace = statePath
    featureclasses = arcpy.ListFeatureClasses()
    fip_merge_list = []
    for fc in featureclasses:
        if name in fc:
            print fc
            fipslist  = (master_dict[name]):
            for fip in fipslist:
                 ### Here's where the problem occurs

Here is where the problem occurs. As I mentioned if I've tried several things to make sure that fc exists, including fieldnames = [field.name for field in arcpy.ListFields(fc)]. I am able to print the field names and verify that the field for the where clause also exists. I create a hard output path to an existing gdb just to make sure the process will work. When I run this however, nothing shows up in my gdb when I check in ArcMap. I can manually run a selectByAttributes function in ArcMap directly on this fc as well.

select = arcpy.MakeFeatureLayer_management(fc, statePath + "/%s_temp_lyr_%s"%(name, fip),"FIPS_CODE = '%s'"%(fip))

The process runs and doesn't throw an error, but nothing is showing in my gdb. So I tried just using the SelectLayerByAttributes_management with fc and it throws an error:

select_1 = arcpy.SelectLayerByAttribute_management (fc, "NEW_SELECTION", "FIPS_CODE = '%s'"%(fip))

Traceback (most recent call last): File "", line 1, in select_1 = arcpy.SelectLayerByAttribute_management (fc, "NEW_SELECTION", "FIPS_CODE = '%s'"%(fip)) File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 4259, in SelectLayerByAttribute raise e ExecuteError: Failed to execute. Parameters are not valid. The value cannot be a feature class ERROR 000840: The value is not a Raster Layer. ERROR 000840: The value is not a Mosaic Layer. Failed to execute (SelectLayerByAttribute).

So, I can't make a layer from my feature class, I can't select by attributes, and I am stuck. I have manually verified that the feature class exists (both in Python and ArcMap), I have verified that the field exists, and I have verified that the fip exists. Any advice on what I am doing incorrectly here?

marked as duplicate by PolyGeo arcpy Jun 22 at 23:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Are you really using 10.0? If so, which service pack has been applied? Did you apply the scores of critical patches to SP5? You're not going to have many here which can reproduce your issue, since most ArcGIS users are on somewhat recent releases (e.g., I have access to 10.4.1, 10.5.1, and 10.6.1). Python support was new, and in flux, way back at 10.0, so it's possible that this was a bug back then. – Vince Oct 29 '18 at 16:23
2

A feature layer is not stored on disk, meaning it's not going to show up in your GDB. A feature layer is a data format that mimics a layer in ArcMap's table of contents in many regards. For your code to work reference the layer name instead of the feature class. Just like you cannot perform a selection of a feature class (try a selection on a feature class in ArcCatalog; you can't), you can't make a selection of a feature class using arcpy.

This should work for you:

arcpy.MakeFeatureLayer_management(fc, "%s_temp_lyr_%s"%(name, fip),"FIPS_CODE = '%s'"%(fip))
arcpy.SelectLayerByAttribute_management ("%s_temp_lyr_%s"%(name, fip), "NEW_SELECTION", "FIPS_CODE = '%s'"%(fip))

If you want to save the results of your layer selection use Copy Features.

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