Select by attributes

1157
6
12-20-2011 08:35 AM
NareshPai
New Contributor
I want to replicate something that I can do within ArcMap and model builder. I have a categorical raster with an attribute table consisting of 10 fields "Rowid", "Value", and "Count". I would like to use the SelectLayerByAttribute_Management tool using arcpy. My code so far is:

# Import arcpy module
import arcpy


# define the location of raster titled rstName
Rst = "C:/..../rstName"

# Select Rowid = 0
arcpy.SelectLayerByAttribute_management(Rst, "NEW_SELECTION", "\"Rowid\" = 0")

The error msg I get is:
* File "C:\...\abc.py", line 16, in <module>
*** arcpy.SelectLayerByAttribute_management(Rst, "NEW_SELECTION", "\"Rowid\" = 0")
* 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.
ERROR 000840: The value is not a Table View.
ERROR 000825: The value is not a layer or table view
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByAttribute).
-------------------------

Can some help me troubleshoot this issue?
Tags (2)
0 Kudos
6 Replies
RichardFairhurst
MVP Honored Contributor
I want to replicate something that I can do within ArcMap and model builder. I have a categorical raster with an attribute table consisting of 10 fields "Rowid", "Value", and "Count". I would like to use the SelectLayerByAttribute_Management tool using arcpy. My code so far is:

# Import arcpy module
import arcpy


# define the location of raster titled rstName
Rst = "C:/..../rstName"

# Select Rowid = 0
arcpy.SelectLayerByAttribute_management(Rst, "NEW_SELECTION", "\"Rowid\" = 0")

The error msg I get is:
* File "C:\...\abc.py", line 16, in <module>
*** arcpy.SelectLayerByAttribute_management(Rst, "NEW_SELECTION", "\"Rowid\" = 0")
* 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.
ERROR 000840: The value is not a Table View.
ERROR 000825: The value is not a layer or table view
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByAttribute).
-------------------------

Can some help me troubleshoot this issue?


SelectLayerByAttribute_management cannot take a raster directly as an input.  You need to create a layer for the raster using CreateLayer_Managment (I believe) and then use that as an input to the SelectLayerByAttribute_management tool.
0 Kudos
RuthEmerick
New Contributor II
I'm doing something similar (not with rasters though) and I've found that I need to use MakeFeatureLayer_management and give it a name in order to use SelectLayerByAttribute_management or SelectLayerByLocation_management on my datasets. Good luck!
0 Kudos
NareshPai
New Contributor
I tried your suggestion:

arcpy.MakeFeatureLayer_management works only for a feature layer (not raster).

My overall goal is to perform an Extract by Mask operation in which the Mask is another raster (with specific attributes selected, sequentially, by an SQL query). Is there an alternative to perform this operation?
0 Kudos
RuthEmerick
New Contributor II
Could you use ExtractByAttributes(in_raster, where_clause) instead of SelectLayerByAttribute_management(in_layer_or_view, {selection_type}, {where_clause})?
0 Kudos
NareshPai
New Contributor
Could you use ExtractByAttributes(in_raster, where_clause) instead of SelectLayerByAttribute_management(in_layer_or_view, {selection_type}, {where_clause})?


Thanks for your reply.  My Mask Raster has about 800 rows in its attribute column which I want to use sequentially for performing Extract by Mask with In Raster. Your solution would work well except that it would create about 800 additional rasters.
0 Kudos
NareshPai
New Contributor
Alright, I have found a solution that would work (for now):

for i in range(0,800):

    arcpy.MakeRasterLayer_management(Mask_Rst,"MaskRst_lyr","Rowid = "+ str(i))

    outExtractByMask = ExtractByMask(InRst,"MaskRst_lyr")

    outExtractByMask.save(OutDir + "\\rst" + str(i))
0 Kudos