<?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: Calling arcpy spatial join on a layer with a selection resets the layer selection in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1348993#M69229</link>
    <description>&lt;P&gt;setSelectionSet&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/table-class.htm" target="_blank"&gt;Table—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This method provides an easy way to manage a table's selection. To clear the selection, use the NEW selection method with an empty list or don't set any parameters. Python lists are used for the oidList, but sets get returned from the getSelectionSet method on the Table object.&lt;/P&gt;&lt;P&gt;So you can confirm "selection" is &amp;gt; 0 (?) and you seem to have set it to a list appropriately (as per the help)&lt;/P&gt;</description>
    <pubDate>Mon, 13 Nov 2023 23:28:37 GMT</pubDate>
    <dc:creator>DanPatterson</dc:creator>
    <dc:date>2023-11-13T23:28:37Z</dc:date>
    <item>
      <title>Calling arcpy spatial join on a layer with a selection resets the layer selection</title>
      <link>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1346431#M69169</link>
      <description>&lt;P&gt;Hi, I am the process of converting a geoprocessing script from ArcMap &amp;amp; Python 2 to Pro &amp;amp; Python 3 and I've run into an annoying issue with the spatial join tool.&amp;nbsp; My script takes a feature layer as input and then acts on the selection in that layer.&amp;nbsp; This worked fine in ArcMap but in Pro arcpy's spatial join is clearing the layer selection after it runs.&amp;nbsp; I am using ArcGIS Pro 3.1.&amp;nbsp; Here is an example script that demonstrates the problem:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy


def get_selection_count(in_layer):
    """return the number of selected features in the layer"""
    selection = in_layer.getSelectionSet()
    if selection:
        return len(selection)
    return 0


# envs
arcpy.env.overwriteOutput = True
arcpy.env.workspace = arcpy.env.scratchGDB

# create test feature class
fc = arcpy.management.CreateFeatureclass(arcpy.env.scratchGDB, "test_point_fc", "POINT")[0]
points = [arcpy.Point(100, 100), arcpy.Point(200, 200)]
with arcpy.da.InsertCursor(fc, ["SHAPE@"]) as crsr:
    for point in points:
        crsr.insertRow((point,))

# make a layer
layer = arcpy.management.MakeFeatureLayer(fc, "test_layer")[0]

# make a selection
layer = arcpy.management.SelectLayerByAttribute(layer, "NEW_SELECTION", "1=1")[0]


print(f"There are {get_selection_count(layer)} features selected.")

arcpy.SpatialJoin_analysis(layer, layer, "out_sj")

print(f"There are {get_selection_count(layer)} features selected.")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit:&lt;/P&gt;&lt;P&gt;The output would be&lt;/P&gt;&lt;LI-CODE lang="python"&gt;There are 2 features selected.
There are 0 features selected.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Is this a bug?&amp;nbsp; Am I missing something?&amp;nbsp; Thanks.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2023 01:35:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1346431#M69169</guid>
      <dc:creator>justin-mpls</dc:creator>
      <dc:date>2023-11-07T01:35:28Z</dc:date>
    </item>
    <item>
      <title>Re: Calling arcpy spatial join on a layer with a selection resets the layer selection</title>
      <link>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1346455#M69170</link>
      <description>&lt;P&gt;What were the print results?&lt;/P&gt;&lt;P&gt;Does the other join work?&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;albeit, it is permanent&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 23:58:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1346455#M69170</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-11-06T23:58:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calling arcpy spatial join on a layer with a selection resets the layer selection</title>
      <link>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1346482#M69173</link>
      <description>&lt;P&gt;I've edited the post to include the output.&amp;nbsp; First, all 2 features are selected, then 0.&amp;nbsp; I haven't tried the other tool yet.&amp;nbsp; I would like to understand if this one is malfunctioning or not.&amp;nbsp; Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2023 01:38:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1346482#M69173</guid>
      <dc:creator>justin-mpls</dc:creator>
      <dc:date>2023-11-07T01:38:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calling arcpy spatial join on a layer with a selection resets the layer selection</title>
      <link>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1346486#M69174</link>
      <description>&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/select-layer-by-attribute.htm" target="_blank"&gt;Select Layer By Attribute (Data Management)—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;from the python section, there is no assignment of the select layer by attributes to another variable name (line 27), perhaps that is causing the issues downstream&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2023 02:21:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1346486#M69174</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-11-07T02:21:26Z</dc:date>
    </item>
    <item>
      <title>Re: Calling arcpy spatial join on a layer with a selection resets the layer selection</title>
      <link>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1347742#M69200</link>
      <description>&lt;P&gt;I can't replicate your problem, how are you running this script?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 16:04:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1347742#M69200</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2023-11-09T16:04:58Z</dc:date>
    </item>
    <item>
      <title>Re: Calling arcpy spatial join on a layer with a selection resets the layer selection</title>
      <link>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1347755#M69201</link>
      <description>&lt;P&gt;I'm running it as a standalone script, executed in VS Code through a cloned conda env from ArcGIS Pro 3.1.3, so Python 3.9.16 and arcpy version 3.1.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 16:23:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1347755#M69201</guid>
      <dc:creator>justin-mpls</dc:creator>
      <dc:date>2023-11-09T16:23:48Z</dc:date>
    </item>
    <item>
      <title>Re: Calling arcpy spatial join on a layer with a selection resets the layer selection</title>
      <link>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1347822#M69207</link>
      <description>&lt;P&gt;did you address the layer assignment thing on line 32, I am not sure that the script knows what layer is what&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 17:13:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1347822#M69207</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-11-09T17:13:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calling arcpy spatial join on a layer with a selection resets the layer selection</title>
      <link>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1348955#M69228</link>
      <description>&lt;P&gt;If I don't rebind layer to the result from SelectLayerByAttribute, then the call to getSelectionSet() will return no selection.&amp;nbsp; To switch gears a bit, let me tell you a bit more about my actual project.&lt;BR /&gt;&lt;BR /&gt;In my real python toolbox script, I take a feature layer as input and rely on the layer selection to process only certain features.&amp;nbsp; I will try to provide the important pieces of code so you can see how it works and where it fails.&lt;BR /&gt;&lt;BR /&gt;Starting out, I use a function to define the parameters:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def make_param(disp_name, p_name, data_type, default_val=None, p_type="Required", p_dir="Input"):
    param = arcpy.Parameter(
        displayName=disp_name,
        name=p_name,
        datatype=data_type,
        parameterType=p_type,
        direction=p_dir,
    )
    param.value = default_val
    return param&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;Then, in the toolbox init, the layer parameter is defined like so:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;self.parameters = [
            make_param("AOI Polygon Layer", "in_lyr", "GPFeatureLayer")
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the tool's execute method, the argument is assigned to a local variable like so:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;in_lyr = parameters[0].value&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;Shortly thereafter, I check for a selection and raise an exception if there is none:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# check for selection on AOI layer
        selection = in_lyr.getSelectionSet()
        if not selection:
            arcpy.AddError("You must select AOI features to evaluate!")
            raise NoSelectionError("There was no selection in the AOI layer")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then, I open a search cursor on the layer and create a dictionary that will be used to store the results from other tools:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.SearchCursor(in_lyr, ["AOI_ID"]) as cursor:
            result = {row[0]: {"AOI_ID": row[0]} for row in cursor}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At this point, I run spatial join because later steps depend on its output.&amp;nbsp; You will notice at the end that I've now included a line to restore the selection back to its original state after this tool is run.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;        # spatial join to check for overlaps in the AOI polygons if zonal stats to be used
        # creating the spatial joined feature class now, otherwise it might be done repeatedly
        if option_sl_score or option_gi_scores or option_canopy_cover:
            arcpy.AddMessage("Checking for overlap in input AOIs ...")
            arcpy.analysis.SpatialJoin(
                in_lyr,
                in_lyr,
                "out_spatial_join",
                "JOIN_ONE_TO_MANY",
                match_option="WITHIN_A_DISTANCE",
                search_radius=-0.1,
            )
            # spatial join clears layer selection, so we restore it
            in_lyr.setSelectionSet(list(selection))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If line 14 above is omitted, the selection in the layer will be cleared and the code breaks downstream because I've only started gathering results for a selection of features.&amp;nbsp; For example:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;            arcpy.TabulateIntersection_analysis(
                in_zone_features=in_lyr,
                zone_fields="AOI_ID",
                in_class_features=wmo_bdry,
                out_table="wmo_tab",
                class_fields="NAME_TXT",
            )

            wmo_flds = ["AOI_ID", "NAME_TXT", "PERCENTAGE"]
            wmo_d = {"3": "MCWD", "6": "MWMO", "7": "BCWMC", "8": "SCWMC"}

            # store results in result dict

            with arcpy.da.SearchCursor("wmo_tab", wmo_flds) as cursor:
                for row in cursor:
                    aoi_id, wmo_id, perc = row

                    wmo = wmo_d.get(wmo_id, "OTHER")
                    pct_fld = f"PCT_{wmo}"

                    result[aoi_id][pct_fld] = round(perc, 2)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the selection is not restored, tabulate intersection will operate on all features and the tool will fail with a KeyError exception because the aoi_id does not exist in the result dictionary.&amp;nbsp; Of course, this problem can be resolved with some tweaks to the script, but my reason for posting is to either figure out if I am using the layer object from the parameter inappropriately or to determine that this is indeed a bug in arcpy.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2023 21:58:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1348955#M69228</guid>
      <dc:creator>justin-mpls</dc:creator>
      <dc:date>2023-11-13T21:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: Calling arcpy spatial join on a layer with a selection resets the layer selection</title>
      <link>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1348993#M69229</link>
      <description>&lt;P&gt;setSelectionSet&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/table-class.htm" target="_blank"&gt;Table—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This method provides an easy way to manage a table's selection. To clear the selection, use the NEW selection method with an empty list or don't set any parameters. Python lists are used for the oidList, but sets get returned from the getSelectionSet method on the Table object.&lt;/P&gt;&lt;P&gt;So you can confirm "selection" is &amp;gt; 0 (?) and you seem to have set it to a list appropriately (as per the help)&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2023 23:28:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calling-arcpy-spatial-join-on-a-layer-with-a/m-p/1348993#M69229</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-11-13T23:28:37Z</dc:date>
    </item>
  </channel>
</rss>

