I am trying to automate a process to populate a field in a point feature class with the name of district (a polygon layer) that it falls inside using python. Ideally this script would only populate the points that have NULL district names. I'm thinking this may be done programmatically using the select by attribute, select by location, and calculate field functions? If someone could point me in the right direction, that would be much appreciated!
Yeah that's essentially what i'm trying to do except I don't want to create a new feature class. I want the result of the spatial join to populate the existing point feature class.
I think Doug's suggestion is the only option. Here is a code sample that you may use:
arcpy.env.overwriteOutput = True
wks = os.path.join(os.getcwd(), "UpdateOneFC.gdb") # or hard code the workspace
point_fc = os.path.join(wks, "cities")
poly_fc = os.path.join(wks, "districts")
out_fc = r"memory\\spj_out" # in_memory if on 10x
# create a FieldMappings object
field_mapping = arcpy.FieldMappings()
# create a fieldmap object using Join Feature's District field.
fm = arcpy.FieldMap()
# update the variable field properties
out_fld = fm.outputField # get District field from the output field
out_fld_name = "District_trns" # new name of the polygon field
fm.outputField = out_fld
# add back the updated field map
# transfer Polygon NAMEs to point fc
result = arcpy.analysis.SpatialJoin(point_fc, poly_fc, out_fc, "JOIN_ONE_TO_ONE", "KEEP_ALL", field_mapping, "WITHIN")
out_fc = result.getOutput(0)
# check result for testing purpose
for fld in arcpy.ListFields(out_fc, "*"):
This will create a new feature class - I don't see any other tool can do so in-place. If you want the new field to your Target Feature Class then you use Join Field to to transfer the field back.