1

I need to write a code to read a polygon shapefile that contain three classes of land use (trees, urban, and crops) and I need to select only polygons of Trees.

Here is my code to apply Select by polygon that is not working:

class btn_read_LandUse(object):
    """Implementation for Addins_addin.button_LU (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        LU_layer = pythonaddins.OpenDialog("Please Select the land use file" ,False,r'','Select')
        desc = arcpy.Describe(LU_layer)
        layerName = desc.baseName
        arcpy.SelectLayerByAttribute_management(in_layer_or_view="LayerName", selection_type="NEW_SELECTION", where_clause=""""LU" = 'Crops'""")

The error:

Traceback (most recent call last):
  File "C:\Users\eng.adel\AppData\Local\ESRI\Desktop10.4\AssemblyCache\{FEC573D9-E048-4AEE-A331-8380627D272B}\Addins_addin.py", line 96, in onClick
    arcpy.SelectLayerByAttribute_management(in_layer_or_view="LayerName", selection_type="NEW_SELECTION", where_clause=""""LU" = 'Crops'""")
  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 7182, in SelectLayerByAttribute
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Layer Name or Table View: Dataset LayerName does not exist or is not supported
Failed to execute (SelectLayerByAttribute).

Python error 000732

Any help?

2
  • The code that you have presented appears to be a copy/paste of a class from a Python AddIn rather than a code snippet that can be used to illustrate where you are stuck. Have you tried getting the code throwing the error to work in a small code snippet away from your AddIn?
    – PolyGeo
    Oct 11 '16 at 12:08
  • Could you please, tell me how? nothing appears in "results" tab Oct 11 '16 at 12:26
3

You have not set the LayerName correctly.

At the moment, LayerName is only the name of the dataset. Its it not the full path to the dataset.

You need to identify the actual layer object when using the geoprocessing tool, in this case, its the variable LU_Layer.

In addition, its sometimes better to set "where_clauses" as a variable.

def onClick(self):
        LU_layer = pythonaddins.OpenDialog("Please Select the land use file" ,False,r'','Select')

        where_clause=""""LU" = 'Crops'"""

        arcpy.SelectLayerByAttribute_management(LU_layer, "NEW_SELECTION", where_clause)
2
  • Sorry, but it didnt work arcpy.SelectLayerByAttribute_management(LU_Layer, "NEW_SELECTION", where_clause) NameError: global name 'LU_Layer' is not defined Oct 11 '16 at 12:24
  • The LU_layer was spelt incorrectly. Try the edited version above. I used capital letter for "Layer", rather than "layer".
    – MacroZED
    Oct 11 '16 at 12:26
0

It worked after adding function of "MakeFeatureLayer_management", that will generate a layer similar to LU_layer but it can be used in ""SelectLayerByAttribute_management" function. Thank you MacroZED

class btn_read_LandUse(object):
    """Implementation for Addins_addin.button_LU (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        LU_layer = pythonaddins.OpenDialog("Please Select the land use file" ,False,'','Select')
        global LU_layerB
        LU_layerB=arcpy.MakeFeatureLayer_management(LU_layer,'temp_LU') # both f aaaa, asasad are just temporary names # Thsis tep to create intermediate layer similar to the landuse
        where_clause=""""LU" = 'Crops'"""
        arcpy.SelectLayerByAttribute_management(LU_layerB, "NEW_SELECTION", where_clause)

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.