Is it possible to use an existent layer to do a selection by attributes without having to use MakeFeatureLayer_management first? Because I already have a layer with the same name as the feature class "Rede_Cbr_MM_2008_proj".

Only this code works for me:

import arcpy
from arcpy import env

# Set workspace
env.workspace = "S:/workspace/MSc_git/ArcGis/estagio_db.gdb"
workspace = "S:/workspace/MSc_git/ArcGis/Layers"
in_feature= "Rede_Cbr_MM_2008_proj"

# # Set overwrite option
# arcpy.env.overwriteOutput = True

#MakeFeatureLayer variables
where_clause='TSYSSET NOT LIKE \'%Metro%\''

    #Make a layer from the feature class

    # Write the selected features to a new featureclass
    arcpy.CopyFeatures_management(tmp_layer, "Rede_Cbr_MM_2008_WithoutMetro")
    print arcpy.GetMessages()

But if I try to use my layer using only SelectLayerByAttribute_management I get this error:

Executing: SelectLayerByAttribute S:/workspace/MSc_git/ArcGis/Layers/Rede_Cbr_MM_2008_proj.lyr NEW_SELECTION "TSYSSET NOT LIKE '%Metro%'"
Start Time: Sat Jul 20 21:49:55 2013
Failed to execute. Parameters are not valid.
ERROR 000825: The value is not a layer or table view
ERROR 000840: The value is not a Raster Layer.
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByAttribute).

Isn't the SelectLayerByAttribute supposed to take a layer as 1st parameter? I don't know I do I have to do this kind of "layer on the fly" (tmp_layer) and then select.


Layer != Layer File

You need to create a Layer object from the layer file first - this should work:

tmp_layer = arcpy.mapping.Layer("S:/workspace/MSc_git/ArcGis/Layers/Rede_Cbr_MM_2008_proj.lyr")
|improve this answer|||||
  • Uhm, ok, I really didn't realize that by reading the arcgis help. But we always have to create a in memory layer, either from a feature class or from an existent layer file, right? – neverMind Jul 20 '13 at 22:02
  • 1
    Another way is to get a Layer object returned from those that exist within a map document (.mxd), a data frame within a map document, or layers within a layer (.lyr) file by using arcpy.mapping.ListLayers. – PolyGeo Jul 20 '13 at 22:45

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.