Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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"
tmp_layer="tmp_layer"

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

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

try:
    #Make a layer from the feature class
    arcpy.MakeFeatureLayer_management(in_feature,tmp_layer)   
    arcpy.SelectLayerByAttribute_management(tmp_layer,"NEW_SELECTION",where_clause)

    # Write the selected features to a new featureclass
    arcpy.CopyFeatures_management(tmp_layer, "Rede_Cbr_MM_2008_WithoutMetro")
except:
    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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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")
arcpy.SelectLayerByAttribute_management(tmp_layer,"NEW_SELECTION",where_clause)
share|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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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