0

I'm trying to finish a script that select a polygon attribute by location using the arcpy call. Here is my code:

project = arcpy.GetParameter(0)

proj_layer = r'memory/' + str('proj')
arcpy.MakeFeatureLayer_management(project_path, proj_layer)

arcpy.AddMessage("PROJECT_NAME = '{}'".format(project))

arcpy.SelectLayerByAttribute_management(proj_layer, "NEW_SELECTION", "PROJECT_NAME = '{}'".format(project))

The variable project_path is the correct path and I know it is because I used a print statement to verify that. I'm thinking that it may be some issue with the expression because the output is:

PROJECT_NAME = 'spokane_test'
Failed script UUIDScript...
Traceback (most recent call last):
File "C:\Users\jhodg1\Desktop\Construction Dev\UUIDScript2.py", line 58, in <module>
arcpy.SelectLayerByAttribute_management(proj_layer, "NEW_SELECTION", "PROJECT_NAME = '{}'".format(project))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 9727, in 
SelectLayerByAttribute
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 9724, in 
SelectLayerByAttribute
retval = convertArcObjectToPythonObject(gp.SelectLayerByAttribute_management(*gp_fixargs((in_layer_or_view, selection_type, where_clause, invert_where_clause), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).

What happens is that the user is supposed to input the project name as a parameter and then I run the script which will then select the polygon based on its name. 'PROJECT_NAME' is the name of the field that I am querying in the 'project_path' layer and I know it exists and that I'm typing in the right name for the project name because I'm looking at the row so I know. Can anyone tell what I am doing wrong and what's making it print out "invalid expression"?

7
  • The back-end data source can affect SQL syntax, try using a file geodatabase feature class to make sure it isn't an issue with the memory workspace.
    – bixb0012
    Commented Mar 27, 2023 at 16:35
  • 1
    Strange name for layer. What 'memory' role here?
    – FelixIP
    Commented Mar 27, 2023 at 19:03
  • Why are you doing str('proj')? 'proj' is already a string. Commented Mar 27, 2023 at 23:28
  • 1
    What is project_path? Where is it defined? Commented Mar 27, 2023 at 23:29
  • 1
    Probably best to avoid having slash (/) or backslash () in the name of a layer, unless you really want to use group layer hierarchies. Commented Mar 27, 2023 at 23:33

1 Answer 1

0

I suggest you make a python variable called select_clause and set it to the embedded format expression as follows:

select_clause = "PROJECT_NAME = '{}'".format(project)

Then (temporarily print it) use it what was line 58 where the arcpy chokes on the string manipulation inside the parameter usage:

print ('Select Clause = ' + select_clause)
arcpy.SelectLay`erByAttribute_management(proj_layer, "NEW_SELECTION", select_clause)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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