<?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: SWITCH_SELECTION in SelectLayerByLocation_management in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/switch-selection-in-selectlayerbylocation/m-p/508858#M39979</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Is there a selection in your ArcMap session? It looks like you once made a selection and then commented it out.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If there is no prior selection, you are running up against a couple of quirks of &lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//0017000000n7000000"&gt;GetCount&lt;/A&gt;​ and &lt;A href="http://resources.arcgis.com/EN/HELP/MAIN/10.2/index.html#//001700000072000000"&gt;SelectLayerByLocation&lt;/A&gt;. 1.) GetCount returns the count of all features if there is no selection. Your variable "prematchcount" returns a count of all features because there is no selection. Later, "postmatchcount" returns a count of all features, because all features are selected. 2.) When you use "SWITCH_SELECTION", the second and third parameters are ignored, so there is no spatial component to the tool whatsoever at that point.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 27 Oct 2015 17:07:00 GMT</pubDate>
    <dc:creator>DarrenWiens2</dc:creator>
    <dc:date>2015-10-27T17:07:00Z</dc:date>
    <item>
      <title>SWITCH_SELECTION in SelectLayerByLocation_management</title>
      <link>https://community.esri.com/t5/python-questions/switch-selection-in-selectlayerbylocation/m-p/508856#M39977</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy,sys
sdeConn = r"Database Connections\\Test.sde"
muniLoc = "Municipalities"
luLoc = "Land_Use"
tempLoc = "tempMuniLuRatio"
lu_lyr = "lu_lyr"

arcpy.env.workspace = sdeConn


try:
&amp;nbsp; print "MakeFeatureLayer_management lu_lyr"
&amp;nbsp; arcpy.MakeFeatureLayer_management(luLoc, lu_lyr) 
&amp;nbsp; prematchcount = int(arcpy.GetCount_management(lu_lyr).getOutput(0)) 
&amp;nbsp; print "MakeFeatureLayer_management muni_lyr"
&amp;nbsp; #arcpy.MakeFeatureLayer_management(muniLoc, "muni_lyr") 
&amp;nbsp; #print "SelectLayerByLocation_management NEW_SELECTION"
&amp;nbsp; #arcpy.SelectLayerByLocation_management(lu_lyr, "COMPLETELY_CONTAINS",muniLoc,"","NEW_SELECTION")
&amp;nbsp; print "SelectLayerByLocation_management SWITCH_SELECTION"
&amp;nbsp; arcpy.SelectLayerByLocation_management(lu_lyr, "COMPLETELY_CONTAINS",muniLoc,"","SWITCH_SELECTION")
&amp;nbsp; postmatchcount = int(arcpy.GetCount_management(lu_lyr).getOutput(0)) 
&amp;nbsp; if prematchcount == postmatchcount:
&amp;nbsp; print "SelectLayerByLocation_management DID NOT WORK"
&amp;nbsp; else:
&amp;nbsp; print "SelectLayerByLocation_management LOOKS GOOD"
&amp;nbsp; if arcpy.Exists(tempLoc):
&amp;nbsp; print "Delete_management "
&amp;nbsp; arcpy.Delete_management(tempLoc)
&amp;nbsp; print "CopyFeatures_management "
&amp;nbsp; arcpy.CopyFeatures_management(lu_lyr,tempLoc)
except Exception:
&amp;nbsp; e = sys.exc_info()[1]
&amp;nbsp; print(e.args[0])&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Result is just same as lu_lyr even after SWICH_SELECTION option.&lt;/P&gt;&lt;P&gt;Where should I take a look to make this work?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jack&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:17:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/switch-selection-in-selectlayerbylocation/m-p/508856#M39977</guid>
      <dc:creator>GyeyoungChoi</dc:creator>
      <dc:date>2021-12-11T22:17:57Z</dc:date>
    </item>
    <item>
      <title>Re: SWITCH_SELECTION in SelectLayerByLocation_management</title>
      <link>https://community.esri.com/t5/python-questions/switch-selection-in-selectlayerbylocation/m-p/508857#M39978</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Gyeyoung.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've been checking the syntax on &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//001700000072000000"&gt;Esri help.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I see that the 4th parameter "search_distance" is optional, but is a numeric parameter. So, what I think it is doing, is assume that you don't give an optional numeric parameter, and it reads the empty string as the 5th parameter. With other words, try this:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;arcpy.SelectLayerByLocation_management(lu_lyr, "COMPLETELY_CONTAINS",muniLoc,0,"SWITCH_SELECTION")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;...giving zero instead of empty strig "". I think it will take it as the numeric 4th parameter, and after it will execute your switch selection.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope it helps. Good luck!&lt;BR /&gt;Luis Pascual&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 27 Oct 2015 15:00:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/switch-selection-in-selectlayerbylocation/m-p/508857#M39978</guid>
      <dc:creator>Luis_ÁngelPascual_Camino</dc:creator>
      <dc:date>2015-10-27T15:00:58Z</dc:date>
    </item>
    <item>
      <title>Re: SWITCH_SELECTION in SelectLayerByLocation_management</title>
      <link>https://community.esri.com/t5/python-questions/switch-selection-in-selectlayerbylocation/m-p/508858#M39979</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Is there a selection in your ArcMap session? It looks like you once made a selection and then commented it out.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If there is no prior selection, you are running up against a couple of quirks of &lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//0017000000n7000000"&gt;GetCount&lt;/A&gt;​ and &lt;A href="http://resources.arcgis.com/EN/HELP/MAIN/10.2/index.html#//001700000072000000"&gt;SelectLayerByLocation&lt;/A&gt;. 1.) GetCount returns the count of all features if there is no selection. Your variable "prematchcount" returns a count of all features because there is no selection. Later, "postmatchcount" returns a count of all features, because all features are selected. 2.) When you use "SWITCH_SELECTION", the second and third parameters are ignored, so there is no spatial component to the tool whatsoever at that point.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 27 Oct 2015 17:07:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/switch-selection-in-selectlayerbylocation/m-p/508858#M39979</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-10-27T17:07:00Z</dc:date>
    </item>
    <item>
      <title>Re: SWITCH_SELECTION in SelectLayerByLocation_management</title>
      <link>https://community.esri.com/t5/python-questions/switch-selection-in-selectlayerbylocation/m-p/508859#M39980</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;And there is a whole bunch of indentations after the ifs that need to be sorted out.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 27 Oct 2015 17:10:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/switch-selection-in-selectlayerbylocation/m-p/508859#M39980</guid>
      <dc:creator>NeilAyres</dc:creator>
      <dc:date>2015-10-27T17:10:27Z</dc:date>
    </item>
    <item>
      <title>Re: SWITCH_SELECTION in SelectLayerByLocation_management</title>
      <link>https://community.esri.com/t5/python-questions/switch-selection-in-selectlayerbylocation/m-p/508860#M39981</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Darren is right on with SWITCH_SELECTION. Maybe you really meant "REMOVE_FROM_SELECTION"?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To avoid confusion, I always use SWITCH_SELECTION in my code with SelectLayerByAttribute (simpler syntax):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14462658441991017" data-renderedposition="92_8_912_16" jivemacro_uid="_14462658441991017"&gt;&lt;P&gt;arcpy.SelectLayerByAttribute(lyr, "SWITCH_SELECTION")&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 31 Oct 2015 04:31:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/switch-selection-in-selectlayerbylocation/m-p/508860#M39981</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2015-10-31T04:31:51Z</dc:date>
    </item>
  </channel>
</rss>

