<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Spatial Join - Fields in Output in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/spatial-join-fields-in-output/m-p/1213953#M65622</link>
    <description>&lt;P&gt;You can use the optional field_mappings parameter:&amp;nbsp;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/spatial-join.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/spatial-join.htm&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 19 Sep 2022 19:57:40 GMT</pubDate>
    <dc:creator>dgiersz_cuyahoga</dc:creator>
    <dc:date>2022-09-19T19:57:40Z</dc:date>
    <item>
      <title>Spatial Join - Fields in Output</title>
      <link>https://community.esri.com/t5/python-questions/spatial-join-fields-in-output/m-p/1213927#M65621</link>
      <description>&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 19 Sep 2022 19:00:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/spatial-join-fields-in-output/m-p/1213927#M65621</guid>
      <dc:creator>GaryBarden</dc:creator>
      <dc:date>2022-09-19T19:00:07Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial Join - Fields in Output</title>
      <link>https://community.esri.com/t5/python-questions/spatial-join-fields-in-output/m-p/1213953#M65622</link>
      <description>&lt;P&gt;You can use the optional field_mappings parameter:&amp;nbsp;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/spatial-join.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/spatial-join.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Sep 2022 19:57:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/spatial-join-fields-in-output/m-p/1213953#M65622</guid>
      <dc:creator>dgiersz_cuyahoga</dc:creator>
      <dc:date>2022-09-19T19:57:40Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial Join - Fields in Output</title>
      <link>https://community.esri.com/t5/python-questions/spatial-join-fields-in-output/m-p/1213996#M65623</link>
      <description>&lt;P&gt;I made a helper function to help with this type of process.&amp;nbsp; 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.&amp;nbsp; It removes all other fields.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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}")&lt;/LI-CODE&gt;&lt;P&gt;You call it as any other function.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# 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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Sep 2022 21:50:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/spatial-join-fields-in-output/m-p/1213996#M65623</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-09-19T21:50:41Z</dc:date>
    </item>
  </channel>
</rss>

