0

I am trying to select layer by attribute and for that, I am using arcpy.SelectLayerByAttribute_management function . My command is as follows:

arcpy.SelectLayerByAttribute_management(union, 'NEW_SELECTION', '"gridcode" =1 AND "area" >=25000000*0.4') 

Here, union is the shapefile, from which I need to select layers by attribute. My SQL query is "gridcode" =1 AND "area" >=25000000*0.4

I am getting the following error:

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).`

artwork21
  • 35,114
  • 8
  • 66
  • 134
lsr729
  • 323
  • 3
  • 10
  • couldn't you test "area" >= 10000000? – Vince Mar 13 '19 at 11:34
  • yes i just changed it but still it gives error – lsr729 Mar 13 '19 at 11:35
  • 1
    Try creating a feature layer using MakeFeatureLayer, and pass this to SelectLayerByAttributes. – BERA Mar 13 '19 at 12:02
  • We'd need to see more of your script to know. Whatever union is appears to be the problem. It's not a layer that the tool wants. – KHibma Mar 13 '19 at 12:22
  • Bring in the make feature layer and select layer by attribute tools into ModelBuilder, set up everything in there and run it. If it runs successfully in ModelBuilder select Model>Export>To Python Script... You may then evaluate these two statements in python and see what was needed to get it to work. – artwork21 Mar 13 '19 at 12:27
  • @KHibma union is my grid shapefile – lsr729 Mar 13 '19 at 12:37
  • @lsr729 ok - but knowing how shapefile is referenced is important. As mentioned 2x now, using Make Feature Layer will probably "fix" it as your reference is shapefile reference is probably to a path on disk, not as a layer. – KHibma Mar 13 '19 at 12:40
  • 1
    Yes it worked after making a layer first. Thank – lsr729 Mar 13 '19 at 12:47
  • You can do a self-answer here. Add the code you used to "make it work" and accept it as an answer – KHibma Mar 13 '19 at 13:25

1 Answers1

0

You are trying to select on a feature class when it should be a feature layer. Use MakeFeatureLayer to create a layer then pass this to SelectLayerByAttributes:

Creates a feature layer from an input feature class or layer file

(It is also possible to use a where_clause in MakeFeatureLayer and then there is no need for SelectLayerByAttributes.)

BERA
  • 72,339
  • 13
  • 72
  • 161