Selecting points within polygon and updating attributes using ArcPy?

5446
9
11-22-2019 03:15 AM
Vakhtang_Zubiashvili
Frequent Contributor

HI Guys,

I have two layers, polygon (in code "Poly" and "Points") and point, i want to upload polygon layer attribute to point using intersect (like spatial join). E.g in polygon i have A and B features, i wont to update  their values in point layer features attributes which under polygons. When i run my code it runs, but uploads only one value from polygon layer, when i have two values fro different features.

Here is a code help me please:

import arcpy

>>> cur_pol = arcpy.da.SearchCursor("Poly","name")

>>> for row_pol in cur_pol:

...     arcpy.SelectLayerByLocation_management("Points","INTERSECT","Poly","","NEW_SELECTION")

...    

>>> for row in arcpy.da.SearchCursor("Points","my_field"):

...     cursor = arcpy.UpdateCursor("Points")

...     for row in cursor:

...         row.SeniSvna = row_pol[0]

...         cursor.updateRow(row)

...        

0 Kudos
9 Replies
JoshuaBixby
MVP Esteemed Contributor

Several things to comment on regarding the code itself.

  • You are mixing original ArcPy cursors (arcpy.UpdateCursor) and ArcPy Data Access cursors (arcpy.da.SearchCursor).  Although technically it may work if coded properly, mixing the two different styles of cursors is bound to create confusion at some point in terms of maintaining code.  I suggest you use all ArcPy DA cursors.
  • Creating a cursor within an existing cursor loop leads to terrible performance.  If your outer cursor/loop only has a few records, it might be OK, but the script will take forever if the outer cursor/loop has more than a trivial number of records.
  • In the second half of the code with the nested cursors, you are using the same variable ("row") to loop through each cursor.  At a minimum, that will lead to confusion into maintaining the code.
  • In the second half of the code with nested cursors, I am not even sure what your search cursor is accomplishing.
0 Kudos
Vakhtang_Zubiashvili
Frequent Contributor

Hi Joshua,

I did it like this, but still updates only sing one value from polygon:

with arcpy.da.SearchCursor("Poly","name") as cursor:
... for row_lsd in cursor:
... arcpy.SelectLayerByLocation_management("Points","INTERSECT","Poly","","NEW_SELECTION")
... with arcpy.da.UpdateCursor("Points",["name"])as cursor_a:
... for row_a in cursor_a:
... row_a[0]=row_lsd[0]
... cursor_a.updateRow(row_lsd)

0 Kudos
deleted-user-NvcfpBOWaKwr
Deactivated User

Whats fields in the polygon layer and in the points layer are you using? Also what is the field mapping? For example What fields in the polygon layer update which fields in the points layer? If this doesn't make sense please let me know and I can explain it another way.

0 Kudos
Vakhtang_Zubiashvili
Frequent Contributor

Dear jeremy,

Field (from which i want to move value into points layer)  in polygon Layer('Poly'  --- string)  i have is called "NAMES" and in points Layer ('Points' --- ) is "NewName". 

I try also this code:

Import arcpy

with arcpy.da.SearchCursor("Poly","NAMES") as cursor:
... for row_lsd in cursor:
... arcpy.SelectLayerByLocation_management("Points","INTERSECT","Poly","","NEW_SELECTION")
... with arcpy.da.UpdateCursor("Points",["NewName"]) as cursor_a:
... for row_a in cursor_a:
... row_a[0]=row_lsd[0]
... cursor_a.updateRow(row_lsd)

It makes update, but updates only one value from Polygon ('Poly'), E.G. i have two names - John and Mike, it update only Mike, even if points features is under John.

0 Kudos
Vakhtang_Zubiashvili
Frequent Contributor

Thanks Guys, i did it, thank you for your help

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If you have found responses helpful, you should mark them Helpful.

0 Kudos
GISAdmin29
New Contributor

Dear Vakhtang_Zubiashvili,

Kindly share the updated code where it is updating the John and Mike both in the "Points" layer.

Thank you

 

 
0 Kudos
SibghatUllah1
Regular Contributor

GISAdmin29
New Contributor

Thank you so much...!!! 🙂

0 Kudos