I am quite new to coding python in arcmap so I would like to get my code reviewed and hopefully improved.
I have two Shapefiles, one contain polygons of cities borders and the other is points representing city center of gravity. There is only one point per city but a polygon can coverage several cities.
My goal is that each point will have the objectid of the polygon containing it.
My code complexity is O(N^2) so its little disturbting and probably can be much better but I still dont understand good enough how to go over feature class and selecting each row at a step without looping and quering.
import arcpy # fcYesuvimPolygon holds the location of the polygon feature class. fcYesuvimPolygon = "h:/acadwork/Ortho/yesuvim_rec_final.shp" # fcYesuvimPoint holds the location of the point feature class. fcYesuvimPoint = "h:/acadwork/Ortho/yesuvim_name_point.shp" # create layer for SHP. arcpy.MakeFeatureLayer_management(fcYesuvimPoint,"fcYesuvimPoint_lyr") arcpy.MakeFeatureLayer_management(fcYesuvimPolygon,"fcYesuvimPolygon_lyr") # Go over the point SHP. with arcpy.da.UpdateCursor(fcYesuvimPoint,["OID@","PolygonID"]) as cursorPoint: for rowPoint in cursorPoint: # query for selecting each row. qry = "FID = " + str(rowPoint) # select by attribute of each row FID arcpy.SelectLayerByAttribute_management("fcYesuvimPoint_lyr", "NEW_SELECTION", qry) # select polygons intersecting each point arcpy.SelectLayerByLocation_management("fcYesuvimPolygon_lyr", "INTERSECT", "fcYesuvimPoint_lyr", "", "NEW_SELECTION") # Go over selected polygon (one for each point) with arcpy.da.SearchCursor("fcYesuvimPolygon_lyr","OBJECTID") as cursorPolygon: for rowPolygon in cursorPolygon: # insert the objectid of polygon to point SHP field names Polygon ID rowPoint = rowPolygon cursorPoint.updateRow(rowPoint)