<?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: SelectLayerByAttribute empty selection in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/selectlayerbyattribute-empty-selection/m-p/543939#M42390</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can use getcount to test if there is a selection. You can reverse if you have no selection more often than not, or if you only want to be doing something if there is no selection.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
selectcount= int(arcpy.GetCount_management(layer).getOutput(0))
if selectcount != 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; do something if a selection
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; do something else if no selection&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 23:32:37 GMT</pubDate>
    <dc:creator>MathewCoyle</dc:creator>
    <dc:date>2021-12-11T23:32:37Z</dc:date>
    <item>
      <title>SelectLayerByAttribute empty selection</title>
      <link>https://community.esri.com/t5/python-questions/selectlayerbyattribute-empty-selection/m-p/543938#M42389</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;How can I use the information from arcpy.SelectLayerByAttribute_management, when I have an empty selection? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If empty:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; do something&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;do somethingelse&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Apr 2012 11:37:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selectlayerbyattribute-empty-selection/m-p/543938#M42389</guid>
      <dc:creator>RA1</dc:creator>
      <dc:date>2012-04-04T11:37:51Z</dc:date>
    </item>
    <item>
      <title>Re: SelectLayerByAttribute empty selection</title>
      <link>https://community.esri.com/t5/python-questions/selectlayerbyattribute-empty-selection/m-p/543939#M42390</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can use getcount to test if there is a selection. You can reverse if you have no selection more often than not, or if you only want to be doing something if there is no selection.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
selectcount= int(arcpy.GetCount_management(layer).getOutput(0))
if selectcount != 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; do something if a selection
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; do something else if no selection&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:32:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selectlayerbyattribute-empty-selection/m-p/543939#M42390</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T23:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: SelectLayerByAttribute empty selection</title>
      <link>https://community.esri.com/t5/python-questions/selectlayerbyattribute-empty-selection/m-p/543940#M42391</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have never been able to make this work as intended; running the script on a layer which has no selection just returns the total number of rows in the feature class. Below is a python function to count the number of rows in a selection using a SearchCursor:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def count_selection(lyr):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Returns a count of selected features from an input feature layer."""
&amp;nbsp;&amp;nbsp;&amp;nbsp; count = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(lyr)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; return count
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Example: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
lyr = "Buildings_lyr"

if count_selection(lyr) == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr, "is empty"
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Not as neat as having a working inbuilt function; maybe someone can show me I am using the arcpy function incorrectly.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:32:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selectlayerbyattribute-empty-selection/m-p/543940#M42391</guid>
      <dc:creator>JamesNunn</dc:creator>
      <dc:date>2021-12-11T23:32:40Z</dc:date>
    </item>
    <item>
      <title>Re: SelectLayerByAttribute empty selection</title>
      <link>https://community.esri.com/t5/python-questions/selectlayerbyattribute-empty-selection/m-p/543941#M42392</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can use Describe on layer and FIDSet property. It should looks like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;desc = arcpy.Describe(lyr)
if len(desc.FIDSet) &amp;gt; 0: # layer has selection
&amp;nbsp; doSomething
else:
&amp;nbsp; doSomethingElse
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Arek&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:32:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selectlayerbyattribute-empty-selection/m-p/543941#M42392</guid>
      <dc:creator>ArkadiuszMatoszka</dc:creator>
      <dc:date>2021-12-11T23:32:42Z</dc:date>
    </item>
  </channel>
</rss>

