1

I have a point shapefile and a polygon shapefile and want to set the field "Present" to 1 if it intersects with the polygon in ArcPy, but I am having trouble with the SQL syntax for select by location. When I run the following code in arcpy, it converts all rows in the field Present to 1.

pts = "points"
poly = "poly"
fieldname = "Present" arcpy.management.SelectLayerByLocation(pts, "INTERSECT", poly, None, "NEW_SELECTION", "NOT_INVERT")
arcpy.management.CalculateField(pts, fieldname, "1", "PYTHON3", '',"TEXT")

My question is similar to Using select by attributes then calculate field on selection in ArcPy?, which is for select by attribute. I am assuming the answer is close to this code.

pts = "points"
poly = "poly"
fieldname = "Present"

arcpy.management.SelectLayerByLocation(pts, "INTERSECT", poly, None, "NEW_SELECTION", "NOT_INVERT")

sql = """{0} = ''""".format(arcpy.AddFieldDelimiters(pts,fieldname)) ### this is wrong

with arcpy.da.UpdateCursor(fc,fieldname,sql) as cursor:
    for row in cursor:
        row[0] = 1
        cursor.updateRow(row)

I basically want to set row to 1 (default is 0), when the point is selected.

  • What are you trying to do with the SQL? The other question you linked uses the SQL to select by attributes - you are not selecting by attributes, so unless you have further requirements the SQL shouldn't be necessary. Remove it, and the UpdateCursor should run on your selected features – Midavalo Jul 20 at 17:02
  • I am trying to use the SQL to take the selected rows and convert to 1 if selected. This can be done in the ArcGIS GUI using SelectLayerByLocation and CalculateField, but when the same code is run in arcpy, all fields are converted to 1. – user44796 Jul 20 at 17:06
  • Although you'd need to change fc in the UpdateCursor to be pts – Midavalo Jul 20 at 17:06
  • You don't need a SQL query to convert to 1, your UpdateCursor is what converts to 1. If you point your UpdateCursor at your layer then the selection set from the SelectLayerByLocation should be honored and only process the selected features. – Midavalo Jul 20 at 17:09
  • If your Calculate Field is setting every row to 1, then something isn't working right. If the select by location works correctly then Calculate Field and Update Cursor should both process ONLY selected features. But make sure the Calculate Field and/or Update Cursors are processing the LAYER and not the Feature Class – Midavalo Jul 20 at 17:13
3

I think you are misunderstanding the answer in Using select by attributes then calculate field on selection in ArcPy? - This question is using SelectLayerByAttributes() and the answer is replacing the Selection with the SQL query.

You are not using SelectLayerByAttributes, so the SQL query won't do anything for you here.

Both of your examples should work - CalculateField and UpdateCursor will both honor the selection set and process only selected features, but only if you pass the Layer to the tool. If you pass the Feature Class then they will process every feature. See Using UpdateCursor based on current selection of features.

pts = "MyPointsLayer" 
poly = "MyPolygons"
fieldname = "Present" 
arcpy.management.SelectLayerByLocation(pts, "INTERSECT", poly, None, "NEW_SELECTION", "NOT_INVERT")
arcpy.management.CalculateField(pts, fieldname, "1", "PYTHON3", '',"TEXT")
pts = "MyPointsLayer" 
poly = "MyPolygons"
fieldname = "Present"
arcpy.management.SelectLayerByLocation(pts, "INTERSECT", poly, None, "NEW_SELECTION", "NOT_INVERT")
with arcpy.da.UpdateCursor(pts,fieldname) as cursor:
    for row in cursor:
        row[0] = 1
        cursor.updateRow(row)

Notice that both tools are pointed at pts which refers to your layer. In the example in your question you had the UpdateCursor pointing to fc.

| improve this answer | |

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.