1

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.

10X advance.

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[0])
        # 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[1] = rowPolygon[0]
                cursorPoint.updateRow(rowPoint)

closed as too broad by PolyGeo Jan 8 '18 at 21:08

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Code/logic looks good, couple suggestions (not code related) 1. if the data is large (many-many features) you may want to import the two layers into a geodatabase and reference the code to those layers (to take advantage of spatial indexes and that geoprocessing large data is faster when the data is in a database) 2. if data is large copy data/datebase to local computer directory and run process locally instead of over a network drive. – artwork21 Oct 17 '17 at 16:46
  • 1
    Simple intersect of two layers or spatial join does that. Output to temp feature class point and bring results back using join. – FelixIP Oct 17 '17 at 18:56
  • 1
    For code reviews there is the Code Review Stack Exchange. – PolyGeo Oct 17 '17 at 19:08

Browse other questions tagged or ask your own question.