<?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 - join features to only one target in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/spatial-join-join-features-to-only-one-target/m-p/1562405#M90535</link>
    <description>&lt;P&gt;You can create a script tool for this.&lt;BR /&gt;You can rename the variable names if you like, in my case I was joining crashes to roads.&lt;BR /&gt;&lt;BR /&gt;Here are the parameters:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JamesPoeschel_0-1732566274853.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/120410i08819CB672AF30D9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JamesPoeschel_0-1732566274853.png" alt="JamesPoeschel_0-1732566274853.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;And here is the execution code:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os

# Define inputs and outputs
crash_points = arcpy.GetParameterAsText(0)  # Input crash points
road_lines = arcpy.GetParameterAsText(1)    # Input road lines
output_feature_class = arcpy.GetParameterAsText(2)  # Output feature class
search_radius = arcpy.GetParameterAsText(3)  # Search radius (e.g., "500 Meters")
near_table = os.path.join("in_memory", "near_table")  # Temporary Near Table

try:
    # Step 1: Generate Near Table
    arcpy.AddMessage("Generating Near Table...")
    arcpy.GenerateNearTable_analysis(
        in_features=crash_points,
        near_features=road_lines,
        out_table=near_table,
        search_radius=search_radius,
        location="NO_LOCATION",
        angle="NO_ANGLE",
        closest="CLOSEST"  # Only find the single closest feature
    )

    # Step 2: Summarize crashes per road
    arcpy.AddMessage("Summarizing crashes per road...")
    summary_table = os.path.join("in_memory", "summary_table")
    arcpy.Statistics_analysis(
        in_table=near_table,
        out_table=summary_table,
        statistics_fields=[["IN_FID", "COUNT"]],
        case_field="NEAR_FID"
    )

    # Step 3: Add crash counts (Join_Count) to roads
    arcpy.AddMessage("Joining crash counts to roads...")
    arcpy.MakeFeatureLayer_management(road_lines, "road_lines_lyr")
    arcpy.AddJoin_management(
        in_layer_or_view="road_lines_lyr",
        in_field="OBJECTID",
        join_table=summary_table,
        join_field="NEAR_FID"
    )

    # Export joined features with crash counts
    arcpy.CopyFeatures_management("road_lines_lyr", output_feature_class)

    arcpy.AddMessage(f"Spatial join completed. Output saved to {output_feature_class}")

except Exception as e:
    arcpy.AddError(f"An error occurred: {e}")
    raise&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;In my case, I only needed the join count per target feature. You may need to edit it if you need more information.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 25 Nov 2024 20:26:51 GMT</pubDate>
    <dc:creator>JamesPoeschel</dc:creator>
    <dc:date>2024-11-25T20:26:51Z</dc:date>
    <item>
      <title>Spatial Join - join features to only one target</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/spatial-join-join-features-to-only-one-target/m-p/1235808#M62773</link>
      <description>&lt;P&gt;I would like to make a &lt;STRONG&gt;spatial join&lt;/STRONG&gt; and use every join feature only once.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Current Input&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Layer1 =&amp;nbsp;Lines of road segments --&amp;gt; Target Features&lt;/LI&gt;&lt;LI&gt;Layer2 = Point data of accidents --&amp;gt; Join Features&lt;/LI&gt;&lt;LI&gt;Join Operation --&amp;gt;Join one to one&lt;/LI&gt;&lt;LI&gt;Keep all target features --&amp;gt; check&lt;/LI&gt;&lt;LI&gt;Match Option --&amp;gt; within a distance (25 Meters because the points are not perfectly accurate)&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;STRONG&gt;Wish&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Spatial join should match every accident to one and only one road. Output should have a Join count with the number of accidents that happened on each road segment. Each accident should therefore be counted on the closest road segment.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Problem&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The only thing that doesn't work until now is, that the &lt;U&gt;accidents can be assigned to multiple road segments this way&lt;/U&gt;. How can I prevent this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2022 10:01:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/spatial-join-join-features-to-only-one-target/m-p/1235808#M62773</guid>
      <dc:creator>faoiltiama</dc:creator>
      <dc:date>2022-11-29T10:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial Join - join features to only one target</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/spatial-join-join-features-to-only-one-target/m-p/1235813#M62775</link>
      <description>&lt;P&gt;Are you using&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/add-spatial-join.htm" target="_blank"&gt;Add Spatial Join (Data Management)—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;which supports one-to-one&lt;/P&gt;&lt;P&gt;or&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/spatial-join.htm#GUID-F9111853-D7D3-42C5-A53F-4B035A8548C0" target="_blank"&gt;Spatial Join (Analysis)—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;which supports one-to-many&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2022 10:27:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/spatial-join-join-features-to-only-one-target/m-p/1235813#M62775</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-11-29T10:27:17Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial Join - join features to only one target</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/spatial-join-join-features-to-only-one-target/m-p/1235874#M62788</link>
      <description>&lt;P&gt;I'm using the latter, Spatial Join (Analysis)&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2022 13:59:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/spatial-join-join-features-to-only-one-target/m-p/1235874#M62788</guid>
      <dc:creator>faoiltiama</dc:creator>
      <dc:date>2022-11-29T13:59:47Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial Join - join features to only one target</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/spatial-join-join-features-to-only-one-target/m-p/1562405#M90535</link>
      <description>&lt;P&gt;You can create a script tool for this.&lt;BR /&gt;You can rename the variable names if you like, in my case I was joining crashes to roads.&lt;BR /&gt;&lt;BR /&gt;Here are the parameters:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JamesPoeschel_0-1732566274853.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/120410i08819CB672AF30D9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JamesPoeschel_0-1732566274853.png" alt="JamesPoeschel_0-1732566274853.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;And here is the execution code:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os

# Define inputs and outputs
crash_points = arcpy.GetParameterAsText(0)  # Input crash points
road_lines = arcpy.GetParameterAsText(1)    # Input road lines
output_feature_class = arcpy.GetParameterAsText(2)  # Output feature class
search_radius = arcpy.GetParameterAsText(3)  # Search radius (e.g., "500 Meters")
near_table = os.path.join("in_memory", "near_table")  # Temporary Near Table

try:
    # Step 1: Generate Near Table
    arcpy.AddMessage("Generating Near Table...")
    arcpy.GenerateNearTable_analysis(
        in_features=crash_points,
        near_features=road_lines,
        out_table=near_table,
        search_radius=search_radius,
        location="NO_LOCATION",
        angle="NO_ANGLE",
        closest="CLOSEST"  # Only find the single closest feature
    )

    # Step 2: Summarize crashes per road
    arcpy.AddMessage("Summarizing crashes per road...")
    summary_table = os.path.join("in_memory", "summary_table")
    arcpy.Statistics_analysis(
        in_table=near_table,
        out_table=summary_table,
        statistics_fields=[["IN_FID", "COUNT"]],
        case_field="NEAR_FID"
    )

    # Step 3: Add crash counts (Join_Count) to roads
    arcpy.AddMessage("Joining crash counts to roads...")
    arcpy.MakeFeatureLayer_management(road_lines, "road_lines_lyr")
    arcpy.AddJoin_management(
        in_layer_or_view="road_lines_lyr",
        in_field="OBJECTID",
        join_table=summary_table,
        join_field="NEAR_FID"
    )

    # Export joined features with crash counts
    arcpy.CopyFeatures_management("road_lines_lyr", output_feature_class)

    arcpy.AddMessage(f"Spatial join completed. Output saved to {output_feature_class}")

except Exception as e:
    arcpy.AddError(f"An error occurred: {e}")
    raise&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;In my case, I only needed the join count per target feature. You may need to edit it if you need more information.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2024 20:26:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/spatial-join-join-features-to-only-one-target/m-p/1562405#M90535</guid>
      <dc:creator>JamesPoeschel</dc:creator>
      <dc:date>2024-11-25T20:26:51Z</dc:date>
    </item>
  </channel>
</rss>

