-1

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).

1 Answers1

0

It seems to me that what you're trying to do is a spatial join.

Spatial join from Arcmap documentation: Joins attributes from one feature to another based on the spatial relationship.

The documentation will explain far better than I how to use it.

Note 1: If multiple points with differents values are within a single polygon, the polygon will only have the fields of only one point.

Note 2: The spatial join function join all the fields from the join_features to the target_feature. So if you only want one column to be added, you have to hide the other fields. Here is an example how to hide fields (must create a new layer):

input_layer = 'your_layer_name'
desired_fields =  #['field1', 'field2', ...] 
field_info = arcpy.Describe(input_layer).fieldInfo
for i in range(field_info.count):
    if field_info.getfieldname(i) not in desired_fields:
        field_info.setvisible(i, 'HIDDEN')
arcpy.MakeFeatureLayer_management(input_layer, 'new_layer_name', '', '', field_info)
Paulloed
  • 333
  • 1
  • 9