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.
SelectLayerByLocation
andCalculateField
, but when the same code is run in arcpy, all fields are converted to 1.fc
in the UpdateCursor to bepts