Filling in a blank field for one dataset based on location AND an attribute of another dataset

1008
1
05-08-2023 01:51 PM
KaylaFlamm
New Contributor III

I am wondering if anyone could give me some pointers or suggestions. I'm not sure if this is doable, and I don't have a ton of experience in using Python or ModelBuilder, but I feel like there has to be some way to make this task a little easier with one of those two...

I have a points dataset and a polygon dataset. These two datasets share two fields. However, only one field is filled in for both layers (let's call this Field 1), and that field does not contain unique values. The other field (let's call this Field 2) that they should share is only filled in for the polygon dataset.

I want to fill Field 2 for the points dataset based on both location (does the point fall within the polygon) AND a specific attribute of the polygons (does Field 1 match for both).

In other words, I want to satisfy the following:

If the point is located within the polygon (spatial join?) AND shares a specific attribute value, I want to fill in a blank field for the points layer to match the value of that same field in the polygon layer. If the above location and attribute criteria are not satisfied, I want to leave that field blank.

0 Kudos
1 Reply
VinceE
by
Occasional Contributor II

I'm not totally sure I follow what you're after, but here's an attempt based on my understanding. I have some points, and polygons both with fields "FIELD1" and "FIELD2". For visual purposes, FIELD1 are colors, and FIELD2s are some numeric value, that as per your instructions (I think) are only filled out in the polygon layer. OIDs are shown below for additional clarity, the white lettering with black halos being the polygon layer.

VinceE_0-1689130913950.png

My approach is perform a Spatial Join, and then perform comparisons between the FIELD1s--if there is a spatial match, and FIELD1 also matches, then FIELD2 should be copied from the polygon to the point. I don't know what your experience with Python is, but you should be able to update the values in the "INPUTS" section, and either run this externally in the IDE of your choice, or simply paste the below directly into the Python window in ArcGIS Pro and hit "enter."

import arcpy

arcpy.env.overwriteOutput = True

# INPUTS----------------------------------------------------------------------#
# All of these "INPUTS" must be updated by the user.
# Leave the leading "r" and the quotations.
input_point = r"THIS IS A FILE PATH TO YOUR POINT FEATURE CLASS"
input_polygon = r"THIS IS A FILE PATH TO YOUR POLYGON FEATURE CLASS"

point_flds = ["FIELD1", "FIELD2"]
polygon_flds = ["FIELD1", "FIELD2"]

output_point = r"THIS IS A FILE PATH TO YOUR OUTPUT POINTS"

# MAIN----------------------------------------------------------------------#
# This block appends "_1" if input fields are the same between feature classes.
# Spatial join output cannot have fields of the same name.
updated_polygon_flds = []
for pnt_fld, poly_fld in zip(point_flds, polygon_flds):
    if pnt_fld == poly_fld:
        updated_polygon_flds.append(f"{poly_fld}_1")
    else:
        updated_polygon_flds.append(poly_fld)

# Join the polygon info to the point geometry. Output will be points.
arcpy.analysis.SpatialJoin(
    target_features=input_point,
    join_features=input_polygon,
    out_feature_class=output_point,
    join_operation="JOIN_ONE_TO_MANY",
    join_type="KEEP_ALL",
    match_option="INTERSECT", )

# Read the output feature class; specifically, field lists from both inputs.
with arcpy.da.UpdateCursor(output_point, point_flds + updated_polygon_flds) as ucurs:
    for pnt_f1, pnt_f2, poly_f1, poly_f2 in ucurs:
        
        # If spatially related, and Field1's are equiavalent, copy over Field2
        #   from the polygon feature class into the point feature class.
        if pnt_f1 == poly_f1:
            ucurs.updateRow([pnt_f1, poly_f2, poly_f1, poly_f2])

Here's the output. To be clear, the first FIELD1 and FIELD2 are from the point, and the second FIELD1 and FIELD 2 are from the polygons. These are aliases of course.

For example, the original point #1 (TARGET_FID = 1 in the output) is RED. It's within a RED polygon, therefore we should calculate FIELD2 in the points to be 3, because that's FIELD2 in the polygon. OID 3 in the below table has a "JOIN_FID" of -1, signifying it's not within a polygon at all. So the point-version of FIELD2 is not calculated, and both FIELD1 and FIELD2 (at the end of the table, from the polygon, are null). TARGET_FID 4 is within a polygon, but the colors don't match--FIELD2 not calculated.

TARGET_FID 5 is represented twice, because it overlaps two polygons. One of the copies of this point matches the underlying polygon, and is calculated. The other doesn't, and is not calculated.

VinceE_1-1689131562125.png

0 Kudos