I have two shapefiles, one is points and the other polygons and they share a common attribute field Field_Name
that is only populated in the points shapefile. I want to create a Python/ArcPy script that will accomplish the following:
- Create a unique list from the values in the populated field in the points shapefile.
- Iterate through the unique list, for each value:
- Select the points features by attribute based on name in the unique list.
- Select polygon features by location using previously selected points that are contained within the polygons.
- Populate the null fields in the polygon shapefile with the values from the selected points features (same as the value from unique list).
This is what I have so far, anyone know how I can get it to work?
import arcpy
arcpy.env.workspace = r"C:\Users\*****\Desktop\GIS_test\shapefiles"
Point = r'C:\Users\*****\Desktop\GIS_test\shapefiles\Point.shp'
field = 'Field_Name'
Polygons = r'C:\Users\*****\Desktop\GIS_test\shapefiles\Polygons.shp'
# Use SearchCursor to return a unique set of values in the specified field and create set.
values = [row[0] for row in arcpy.da.SearchCursor(Point, field)]
uniqueValues = set(values)
# Convert the set to a list.
unique_list = (list(uniqueValues))
#Loop through list of unique values.
for x in unique_list:
#SelectLayerByAttribute(in_layer_or_view, {selection_type}, {where_clause}, {invert_where_clause})
arcpy.SelectLayerByAttribute_management(Point, 'NEW_SELECTION', [Field_Name] = '{0}'.format(x))
#SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance},
#{selection_type},{invert_spatial_relationship})
arcpy.SelectLayerByLocation_management(Polygons, 'CONTAINS', Point)
#Update Field_Name attribute value in Polygon attribute table with name in unique list from Point.shp
with arcpy.da.UpdateCursor(Polygons, 'Field_Name') as cursor:
for row in cursor:
row[0] = x
cursor.updateRow(row)
The script is executing as far as line 18 (first select statement).