I have a spatial join where I'm joining points in polygons. What's the easiest way to have the attributes in the output feature only retain one attribute from the Target Feature and one or more from the Join Feature?
Thanks
Solved! Go to Solution.
I made a helper function to help with this type of process. It includes all of the fields in the target featureclass, and a takes a list of fields that you want brought over from the joining featureclass. It removes all other fields.
def create_fieldmap(targetFc=None, joinFc=None, keepFields=None):
"""
Function to assist in making joins by mapping all the fields in the target fc and
including a list of fields that is wanted within the join dataset
class.fieldMapping('target fc', 'fc to join', ['field1', 'field2'])
:param targetFc: Featureclass that the fields from the joinFC will be added to
:param joinFc: Featureclass that contains the fields you want the other featureclass to have
:param keepFields: List of fields that you want to keep from joinFc
"""
try:
# List of fields to keep during Join operation
fldMap = arcpy.FieldMappings()
# Creating field maps for the two files
fldMap.addTable(targetFc)
fldMap.addTable(joinFc)
# Adds the fields from the targetFC to the keepers or keeps all targetFC fields if keepfields is None
keepFields.extend(i.name for i in arcpy.ListFields(targetFc)) if keepFields else [i.name for i in arcpy.ListFields(targetFc)]
# Removing unwanted fields. You can add additional checks here like required fields.
for field in fldMap.fields:
if field.name not in keepFields:
fldMap.removeFieldMap(fldMap.findFieldMapIndex(field.name))
return fldMap
except RuntimeError as e:
raise Exception(f"{sys._getframe().f_code.co_name} failed.\n{e}")
You call it as any other function.
# list of fields to keep
keepFields = [f.name for f in arcpy.ListFields(asr) if f.name != 'PALNB']
# Create the fieldmap, joining address fields to tmpLyr
fldMap = create_fieldmap(tmpLyr, address, keepFields)
# use fldMap in function
arcpy.SpatialJoin_analysis(targetFeatures, joinFeatures, outfc, "#", "#", fldMap)
You can use the optional field_mappings parameter: https://pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/spatial-join.htm
I made a helper function to help with this type of process. It includes all of the fields in the target featureclass, and a takes a list of fields that you want brought over from the joining featureclass. It removes all other fields.
def create_fieldmap(targetFc=None, joinFc=None, keepFields=None):
"""
Function to assist in making joins by mapping all the fields in the target fc and
including a list of fields that is wanted within the join dataset
class.fieldMapping('target fc', 'fc to join', ['field1', 'field2'])
:param targetFc: Featureclass that the fields from the joinFC will be added to
:param joinFc: Featureclass that contains the fields you want the other featureclass to have
:param keepFields: List of fields that you want to keep from joinFc
"""
try:
# List of fields to keep during Join operation
fldMap = arcpy.FieldMappings()
# Creating field maps for the two files
fldMap.addTable(targetFc)
fldMap.addTable(joinFc)
# Adds the fields from the targetFC to the keepers or keeps all targetFC fields if keepfields is None
keepFields.extend(i.name for i in arcpy.ListFields(targetFc)) if keepFields else [i.name for i in arcpy.ListFields(targetFc)]
# Removing unwanted fields. You can add additional checks here like required fields.
for field in fldMap.fields:
if field.name not in keepFields:
fldMap.removeFieldMap(fldMap.findFieldMapIndex(field.name))
return fldMap
except RuntimeError as e:
raise Exception(f"{sys._getframe().f_code.co_name} failed.\n{e}")
You call it as any other function.
# list of fields to keep
keepFields = [f.name for f in arcpy.ListFields(asr) if f.name != 'PALNB']
# Create the fieldmap, joining address fields to tmpLyr
fldMap = create_fieldmap(tmpLyr, address, keepFields)
# use fldMap in function
arcpy.SpatialJoin_analysis(targetFeatures, joinFeatures, outfc, "#", "#", fldMap)