3

I'm currently trying to iterate through the features of a feature class (using an update cursor, because the goal is to ultimately update the field) and buffer each individual segment. I eventually want to use the buffer in each iteration with different tools, but I can't get the initial Select by Attribute to work. Here's my code:

fc = "Original_Clip"
fcfield = "SEGMENTID"
strt_lyr = arcpy.MakeFeatureLayer_management(fc, "Street_lyr")
with arcpy.da.UpdateCursor(fc, fcfield) as cursor:
 for row in cursor:
 var_segment = row[0]
 print "Buffering segment " + str(var_segment)
 arcpy.SelectLayerByAttribute_management(strt_lyr, "NEW_SELECTION", '{} = \'{}\''.format(var_segment))
 arcpy.Buffer_analysis(strt_lyr, str(var_segment) + "Buff", "20 Feet") 

When I run this code, an error is thrown with the SelectLayerByAttributefunction, saying my where clause is invalid?

Runtime error Traceback (most recent call last): File "", line 15, in File "d:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6688, in SelectLayerByAttribute raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute). I'm assuming I just have the SQL syntax wrong (I'm querying a file geodatabase btw), but maybe what I'm trying to do just isn't possible.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Oct 3, 2016 at 20:04
7
  • 1
    You can read "Shape@" in your feature class, and use geometry method to construct the buffer Commented Oct 3, 2016 at 20:08
  • 2
    Your expression has two placeholders, but you're only passing one in the format method. What is the other variable supposed to be? Commented Oct 3, 2016 at 20:25
  • Good catch, it's supposed to be .format(fcfield, var_segment). Commented Oct 3, 2016 at 20:35
  • @AndyFisher does that solve the problem? Commented Oct 3, 2016 at 20:53
  • @Midavalo, it did not. Commented Oct 3, 2016 at 20:56

1 Answer 1

1

I've had problems like this with where clauses in SelectLayerByAttribute when I try to build the clause right in the function parameter list. I work around it by building the where clause in a string variable and using the variable in the function parameters instead.

answered Oct 3, 2016 at 20:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.