0

Using ArcGIS 10.4.1 and Arcpy I am trying to select a shapefile by it attributes and create new feature class based on unique values like

import arcpy
from arcpy import env

def unique_values(table , field):
    with arcpy.da.SearchCursor(table, [field]) as cursor:
        return sorted({row[0] for row in cursor})

uniques = unique_values(r'D:/GIS/Roads.shp' , 'fclass')
for unique in uniques:
    arcpy.SelectLayerByAttribute_management (r'D:/GIS/Roads.shp', "NEW_SELECTION", " 'fclass' = '"+unique+"' ")
    print (unique)

but I am getting this error message

enter image description here

Message File Name Line Position Traceback
11
SelectLayerByAttribute C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\management.py 7182
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).

4

The tool Select Layer by Attribute requires a layer, not a feature class. A shapefile is a feature class.

You either need to use a layer from the Table of Contents in ArcMap, or create a layer using Make Feature Layer tool.

for unique in uniques:
    arcpy.MakeFeatureLayer_management(r'D:/GIS/Roads.shp', 'myLayer')
    arcpy.SelectLayerByAttribute_management ('myLayer', "NEW_SELECTION", " 'fclass' = '"+unique+"' ")
    print (unique)

Take a look at the documentation for Select Layer by Attribute

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.